|
| 1 | +const async = require('async'); |
| 2 | +const { parseString } = require('xml2js'); |
| 3 | +const { errorInstances, errors } = require('arsenal'); |
| 4 | + |
| 5 | +const collectCorsHeaders = require('../utilities/collectCorsHeaders'); |
| 6 | +const metadata = require('../metadata/wrapper'); |
| 7 | +const { standardMetadataValidateBucket } = require('../metadata/metadataUtils'); |
| 8 | +const { isRateLimitServiceUser } = require('./apiUtils/authorization/serviceUser'); |
| 9 | + |
| 10 | +function parseRequestBody(requestBody, callback) { |
| 11 | + try { |
| 12 | + const jsonData = JSON.parse(requestBody); |
| 13 | + if (typeof jsonData !== 'object') { |
| 14 | + throw new Error('Invalid JSON'); |
| 15 | + } |
| 16 | + return callback(null, jsonData); |
| 17 | + } catch { |
| 18 | + return parseString(requestBody, (xmlError, xmlData) => { |
| 19 | + if (xmlError) { |
| 20 | + return callback(errorInstances.InvalidArgument |
| 21 | + .customizeDescription('Request body must be a JSON object')); |
| 22 | + } |
| 23 | + return callback(null, xmlData); |
| 24 | + }); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function validateRateLimitConfig(config, callback) { |
| 29 | + const limit = parseInt(config.RequestsPerSecond, 10); |
| 30 | + if (Number.isNaN(limit) || !Number.isInteger(limit) || limit <= 0) { |
| 31 | + return callback(errorInstances.InvalidArgument |
| 32 | + .customizeDescription('RequestsPerSecond must be a positive integer')); |
| 33 | + } |
| 34 | + return callback(null, { |
| 35 | + RequestsPerSecond: limit, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * bucketPutRateLimit - create or update a bucket policy |
| 41 | + * @param {AuthInfo} authInfo - Instance of AuthInfo class with requester's info |
| 42 | + * @param {object} request - http request object |
| 43 | + * @param {object} log - Werelogs logger |
| 44 | + * @param {function} callback - callback to server |
| 45 | + * @return {undefined} |
| 46 | + */ |
| 47 | +function bucketPutRateLimit(authInfo, request, log, callback) { |
| 48 | + log.debug('processing request', { method: 'bucketPutRateLimit' }); |
| 49 | + |
| 50 | + if (!isRateLimitServiceUser(authInfo)) { |
| 51 | + return callback(errors.AccessDenied); |
| 52 | + } |
| 53 | + |
| 54 | + const { bucketName } = request; |
| 55 | + const metadataValParams = { |
| 56 | + authInfo, |
| 57 | + bucketName, |
| 58 | + requestType: request.apiMethods || 'bucketPutRateLimit', |
| 59 | + request, |
| 60 | + }; |
| 61 | + |
| 62 | + return async.waterfall([ |
| 63 | + next => parseRequestBody(request.post, next), |
| 64 | + (requestBody, next) => validateRateLimitConfig(requestBody, next), |
| 65 | + (limitConfig, next) => standardMetadataValidateBucket(metadataValParams, request.actionImplicitDenies, log, |
| 66 | + (err, bucket) => { |
| 67 | + if (err) { |
| 68 | + return next(err, bucket); |
| 69 | + } |
| 70 | + return next(null, bucket, limitConfig); |
| 71 | + }), |
| 72 | + (bucket, limitConfig, next) => { |
| 73 | + bucket.setRateLimitConfig(limitConfig); |
| 74 | + metadata.updateBucket(bucket.getName(), bucket, log, |
| 75 | + err => next(err, bucket)); |
| 76 | + }, |
| 77 | + ], (err, bucket) => { |
| 78 | + const corsHeaders = collectCorsHeaders(request.headers.origin, |
| 79 | + request.method, bucket); |
| 80 | + if (err) { |
| 81 | + log.trace('error processing request', |
| 82 | + { error: err, method: 'bucketPutRateLimit' }); |
| 83 | + return callback(err, corsHeaders); |
| 84 | + } |
| 85 | + // TODO: implement Utapi metric support |
| 86 | + return callback(null, corsHeaders); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +module.exports = bucketPutRateLimit; |
0 commit comments