Haufe-Lexware / wicked.haufe.io

An API Management system based on Mashape Kong
http://wicked.haufe.io
Other
124 stars 37 forks source link

Unable to add resposnse ratelimiting plugin. #144

Closed himansingh closed 5 years ago

himansingh commented 5 years ago

Hi, I am trying to add the response-rate-limiting plugin to a plan using Kickstarter by following configuring kong plugins documentation.

Here is the relevant part of configuration from plans.json - { "id": "free", "name": "Free Plan", "desc": "This is a free plan.", "config": { "plugins": [ { "name": "response-ratelimiting", "config": { "limits": { "analyze": { "minute": 1, "month": 4 } }, "header_name": "Count-Analyze", "block_on_first_violation": true } } ] } }

I called API using following curl command - curl -kX GET https://api.company.com/get/response-headers?Count-Analyze=counting -H 'X-ApiKey: APIKEY' https://api.company.com/get/response-headers?Count-Analyze=counting is using https://httpbin.org/response-headers as test backend.

This neither limits the API calls nor makes any entry in response-ratelimiting_metrics table in postgres db. To ensure that plugin was enabled I added some logs in kong's response-rate-limiting code. Can anyone tell me where am I going wrong ?

Any help would be very much appreciated. Thanks!

DonMartin76 commented 5 years ago

I haven't used the response rate limiting plugin so far, but reading the documentation, it's meant that the backend service must respond with a number to increase the rate counter with. The response-header endpoint looks like a good match to test this, but you should probably call it more like this:

curl -H 'Count-Analyze: 1'  -H 'X-ApiKey: APIKEY' https://api.company.com/get/response-headers

The response-header endpoint will reply with the same headers as the ones which were passed in, and this will mean that Kong, when proxying back the request from the backend, should increase the counters according to the response headers.

Shout out if you can't make it work, and I will also try it out.

himansingh commented 5 years ago

Hey @DonMartin76 , Actually, the backend service i.e., response-headers endpoint was not responding with correct headers. So I used another custom backend service that sends Count-Analyze= 1 header in the response. Here is the curl command:- curl -v -kX GET https://api.company.com/mocky/v2/5c18ced12f00004e00af11ae -H 'X-ApiKey: ae572b94e89abf971bc6efa68186efa3b69fd68' I still couldn't get it to work.

DonMartin76 commented 5 years ago

OK, the documentation of Kong is a little tricky here. The response header you must (in your specific example) return is:

Count-Analyze: analyze=1

Set 1 to whatever you need it to be. If you have multiple rate limiters, you can comma-separate them.

And to do this with httpbin's response-header endpoint, you can do it like this:

$ curl -H 'X-ApiKey: (...)' https://api.company.com/get/response-headers?count-analyze=analyze=1
{
  "Content-Length": "101", 
  "Content-Type": "application/json", 
  "count-analyze": "analyze=1"
}
$ curl -H 'X-ApiKey: (...)' https://api.company.com/get/response-headers?count-analyze=analyze=1
{"message":"API rate limit exceeded for 'analyze'"}

So it works, it's just the docs which are a little tricky to read.

himansingh commented 5 years ago

Thanks, this works. I just missed it in the documentation.

DonMartin76 commented 5 years ago

Can I close this?

himansingh commented 5 years ago

Yes, you can.