-
Notifications
You must be signed in to change notification settings - Fork 252
CLDSRV-759: bucketGetRateLimit #5997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: improvement/CLDSRV-766/bucket_rate_limiting
Are you sure you want to change the base?
Changes from 1 commit
c353bab
de2ed07
074e87e
94e3fae
422cd7b
c6dc6d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| const nodeFetch = require('node-fetch'); | ||
| const AWS = require('aws-sdk'); | ||
| const xml2js = require('xml2js'); | ||
| const { getCredentials } = require('../support/credentials'); | ||
|
|
||
| const { config } = require('../../../../../lib/Config'); | ||
|
|
||
| const skipIfRateLimitDisabled = config.rateLimiting.enabled ? describe : describe.skip; | ||
|
|
||
| async function sendRateLimitRequest(method, host, path, body = '') { | ||
| const service = 's3'; | ||
| const endpoint = new AWS.Endpoint(host); | ||
|
|
||
| const request = new AWS.HttpRequest(endpoint); | ||
| request.method = method.toUpperCase(); | ||
| request.path = path; | ||
| request.body = body; | ||
| request.headers.Host = host; | ||
| request.headers['X-Amz-Date'] = new Date().toISOString().replace(/[:\-]|\.\d{3}/g, ''); | ||
| const sha256hash = AWS.util.crypto.sha256(request.body || '', 'hex'); | ||
| request.headers['X-Amz-Content-SHA256'] = sha256hash; | ||
| request.region = 'us-east-1'; | ||
|
|
||
| const signer = new AWS.Signers.V4(request, service); | ||
| const credentials = getCredentials('lisa'); | ||
| const awsCredentials = new AWS.Credentials( | ||
| credentials.accessKeyId, | ||
| credentials.secretAccessKey | ||
| ); | ||
| signer.addAuthorization(awsCredentials, new Date()); | ||
|
|
||
| const url = `http://${host}${path}`; | ||
| const options = { | ||
| method: request.method, | ||
| headers: request.headers, | ||
| }; | ||
|
|
||
| if (method !== 'GET' && method !== 'DELETE') { | ||
| options.body = request.body; | ||
| } | ||
|
|
||
| const response = await nodeFetch(url, options); | ||
| const text = await response.text(); | ||
|
|
||
| // Check if response is successful | ||
| if (!response.ok) { | ||
| // Try to parse as XML error first (S3 errors are typically XML) | ||
| let xmlResult; | ||
| try { | ||
| xmlResult = await xml2js.parseStringPromise(text); | ||
| } catch { | ||
| // XML parsing failed, will try JSON below | ||
| } | ||
|
|
||
| if (xmlResult && xmlResult.Error) { | ||
| throw xmlResult; | ||
| } | ||
|
|
||
| // If XML parsing failed or no Error in XML, try JSON | ||
| try { | ||
| const json = JSON.parse(text); | ||
| if (json.error) { | ||
| throw json; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Also, do you want to throw an error in 100% of cases where |
||
| } | ||
| } catch { | ||
| // If both fail, throw the original error | ||
| throw new Error(`Request failed with status ${response.status}: ${text}`); | ||
| } | ||
| } | ||
|
|
||
| if (!text.trim()) { | ||
| return null; | ||
| } | ||
|
|
||
| return JSON.parse(text); | ||
| } | ||
|
|
||
| module.exports = { | ||
| sendRateLimitRequest, | ||
| skipIfRateLimitDisabled, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems a bit odd to attach a body for all other types of requests (i.e. to have a negative check), I'd rather check
if (method === 'PUT' || method === 'POST')instead.