-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-factory.js): added rate-limiting facility
used express-rate-limit package, currently fetching configurations from production.js file feat #139
- Loading branch information
Showing
9 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
module.exports = { | ||
passportFile: '/etc/gluu/conf/passport-config.json', | ||
saltFile: '/etc/gluu/conf/salt', | ||
timerInterval: 60000 | ||
timerInterval: 60000, | ||
rateLimitWindowMs: 24 * 60 * 60 * 1000, // 24 hrs in milliseconds | ||
rateLimitMaxRequestAllow: 1000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const rateLimit = require('express-rate-limit') | ||
const config = require('config') | ||
|
||
/** | ||
* Timeframe in miliseconds for which requests are checked/remembered | ||
* default is 24 hrs in miliseconds | ||
*/ | ||
const windowMs = ( | ||
config.has('rateLimitWindowMs') && config.get('rateLimitWindowMs') | ||
) || 24 * 60 * 60 * 1000 | ||
|
||
/** | ||
* Max number of connections during windowMs milliseconds before sending a 429 response. | ||
* Default is 1000 number of requests in 24 hrs | ||
*/ | ||
const max = ( | ||
config.has('rateLimitMaxRequestAllow') && config.get('rateLimitMaxRequestAllow') | ||
) || 1000 | ||
|
||
const rateLimiter = rateLimit({ | ||
windowMs, | ||
max, | ||
message: `You have exceeded the ${max} requests in ${windowMs} milliseconds limit!`, | ||
headers: true | ||
}) | ||
|
||
module.exports = { | ||
rateLimiter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Feature: Rate limiting | ||
|
||
Application should control the number of request comes and | ||
special against the DOC(denial-of-service) attack | ||
# Issue: https://github.com/GluuFederation/gluu-passport/issues/139 | ||
|
||
Below test cases are using test.js config. Current rate limit is 45. | ||
In first step, we requested only 3 times to successfully reach limit i.e. 45 | ||
because we already requested 42 time in endpoint-metrics-steps.js test cases | ||
|
||
Scenario: Application should limit the request | ||
Given requesting application 3 times | ||
When requesting more than 45 | ||
Then should return request limit exceeded with http status 429 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { Given, When, Then } = require('cucumber') | ||
const got = require('got') | ||
const chai = require('chai') | ||
const assert = chai.assert | ||
|
||
Given('requesting application {int} times', async (max) => { | ||
for (let i = 0; i < max; i++) { | ||
const response = await got('http://127.0.0.1:8090/passport/health-check', { retry: 0 }) | ||
assert.equal(response.statusCode, 200, | ||
'response.statusCode is NOT 200') | ||
} | ||
}) | ||
|
||
When('requesting more than 45', async () => { | ||
try { | ||
await got('http://127.0.0.1:8090/passport/health-check', { retry: 0 }) | ||
} catch (err) { | ||
assert.equal(err.message, 'Response code 429 (Too Many Requests)', 'response is not 429') | ||
} | ||
}) | ||
|
||
Then('should return request limit exceeded with http status 429', async () => { | ||
try { | ||
await got('http://127.0.0.1:8090/passport/health-check', { retry: 0 }) | ||
} catch (err) { | ||
assert.equal(err.message, 'Response code 429 (Too Many Requests)', 'response is not 429') | ||
} | ||
}) |