* bucketPutRateLimit - create or update a bucket policy * @param {AuthInfo} authInfo - Instance of AuthInfo class with requester's info * @param {object} request - http request object * @param {object} log - Werelogs logger * @param {function} callback - callback to server * @return {undefined}
(authInfo, request, log, callback)
| 56 | * @return {undefined} |
| 57 | */ |
| 58 | function bucketPutRateLimit(authInfo, request, log, callback) { |
| 59 | log.debug('processing request', { method: 'bucketPutRateLimit' }); |
| 60 | |
| 61 | if (!isRateLimitServiceUser(authInfo)) { |
| 62 | return callback(errors.AccessDenied); |
| 63 | } |
| 64 | |
| 65 | const { bucketName } = request; |
| 66 | const metadataValParams = { |
| 67 | authInfo, |
| 68 | bucketName, |
| 69 | requestType: request.apiMethods || 'bucketPutRateLimit', |
| 70 | request, |
| 71 | }; |
| 72 | |
| 73 | return async.waterfall([ |
| 74 | next => parseRequestBody(request.post, next), |
| 75 | (requestBody, next) => validateRateLimitConfig(requestBody, next), |
| 76 | (limitConfig, next) => standardMetadataValidateBucket(metadataValParams, request.actionImplicitDenies, log, |
| 77 | (err, bucket) => { |
| 78 | if (err) { |
| 79 | return next(err, bucket); |
| 80 | } |
| 81 | return next(null, bucket, limitConfig); |
| 82 | }), |
| 83 | (bucket, limitConfig, next) => { |
| 84 | bucket.setRateLimitConfiguration(limitConfig); |
| 85 | metadata.updateBucket(bucket.getName(), bucket, log, |
| 86 | err => next(err, bucket)); |
| 87 | }, |
| 88 | ], (err, bucket) => { |
| 89 | const corsHeaders = collectCorsHeaders(request.headers.origin, |
| 90 | request.method, bucket); |
| 91 | if (err) { |
| 92 | log.trace('error processing request', |
| 93 | { error: err, method: 'bucketPutRateLimit' }); |
| 94 | return callback(err, corsHeaders); |
| 95 | } |
| 96 | // Invalidate cache so new limit takes effect immediately |
| 97 | cache.deleteCachedConfig(cache.namespace.bucket, bucketName); |
| 98 | log.debug('invalidated rate limit cache for bucket', { bucketName }); |
| 99 | // TODO: implement Utapi metric support |
| 100 | return callback(null, corsHeaders); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | module.exports = bucketPutRateLimit; |
no test coverage detected