diff --git a/lib/api/apiUtils/object/checkReadLocation.js b/lib/api/apiUtils/object/checkReadLocation.js new file mode 100644 index 0000000000..960d3f0c6e --- /dev/null +++ b/lib/api/apiUtils/object/checkReadLocation.js @@ -0,0 +1,27 @@ +/** + * checkReadLocation - verify that a bucket's default read location exists + * a for specified read data locator + * @param {Config} config - Config object + * @param {string} locationName - location constraint + * @param {string} objectKey - object key + * @param {string} bucketName - bucket name + * @return {Object | null} return object containing location information + * if location exists; otherwise, null + */ +function checkReadLocation(config, locationName, objectKey, bucketName) { + const readLocation = config.getLocationConstraint(locationName); + if (readLocation) { + const bucketMatch = readLocation.details && + readLocation.details.bucketMatch; + const backendKey = bucketMatch ? objectKey : + `${bucketName}/${objectKey}`; + return { + location: locationName, + key: backendKey, + locationType: readLocation.type, + }; + } + return null; +} + +module.exports = checkReadLocation; diff --git a/lib/api/objectGet.js b/lib/api/objectGet.js index e9c214ea6b..80f6a9c9db 100644 --- a/lib/api/objectGet.js +++ b/lib/api/objectGet.js @@ -13,24 +13,13 @@ const locationHeaderCheck = require('./apiUtils/object/locationHeaderCheck'); const getReplicationBackendDataLocator = require('./apiUtils/object/getReplicationBackendDataLocator'); +const checkReadLocation = require('./apiUtils/object/checkReadLocation'); + const { metadataValidateBucketAndObj } = require('../metadata/metadataUtils'); const { config } = require('../Config'); -const { locationConstraints } = config; const validateHeaders = s3middleware.validateConditionalHeaders; -function _retrieveDefaultRead(locationName, objectKey, bucketName) { - const readLocation = locationConstraints[locationName]; - const bucketMatch = readLocation.details.bucketMatch; - const backendKey = bucketMatch ? objectKey : - `${bucketName}/${objectKey}`; - return { - location: locationName, - key: backendKey, - locationType: readLocation.type, - }; -} - /** * GET Object - Get an object * @param {AuthInfo} authInfo - Instance of AuthInfo class with requester's info @@ -145,11 +134,11 @@ function objectGet(authInfo, request, returnTagCount, log, callback) { const defReadLocation = bucket.getReadLocationConstraint(); const defWriteLocation = bucket.getLocationConstraint(); - const defReadDataLocator = _retrieveDefaultRead( - defReadLocation, objectKey, bucketName); let targetLocation = locCheckResult || null; if (objMD.replicationInfo.backends.length > 0 && defReadLocation !== defWriteLocation) { + const defReadDataLocator = checkReadLocation(config, + defReadLocation, objectKey, bucketName); targetLocation = targetLocation || defReadDataLocator || null; } diff --git a/tests/unit/utils/checkReadLocation.js b/tests/unit/utils/checkReadLocation.js new file mode 100644 index 0000000000..7db7b23281 --- /dev/null +++ b/tests/unit/utils/checkReadLocation.js @@ -0,0 +1,67 @@ +const assert = require('assert'); + +const { ConfigObject } = require('../../../lib/Config'); +const checkReadLocation = + require('../../../lib/api/apiUtils/object/checkReadLocation'); + +const locationConstraints = { // eslint-disable-line quote-props + bucketmatch: { + type: 'aws_s3', + legacyAwsBehavior: true, + details: { + bucketMatch: true, + }, + }, + nobucketmatch: { + type: 'aws_s3', + legacyAwsBehavior: true, + details: { + bucketMatch: false, + }, + }, + 'us-east-1': { + type: 'file', + legacyAwsBehavior: true, + details: {}, + }, +}; + +const bucket = 'testBucket'; +const key = 'objectKey'; + +describe('Testing checkReadLocation', () => { + let config; + + before(() => { + config = new ConfigObject(); + config.setLocationConstraints(locationConstraints); + }); + + it('should return null if location does not exist', () => { + const testResult = checkReadLocation( + config, 'nonexistloc', key, bucket); + assert.deepStrictEqual(testResult, null); + }); + + it('should return correct results for bucketMatch true location', () => { + const testResult = checkReadLocation( + config, 'bucketmatch', key, bucket); + const expectedResult = { + location: 'bucketmatch', + key, + locationType: 'aws_s3', + }; + assert.deepStrictEqual(testResult, expectedResult); + }); + + it('should return correct results for bucketMatch false location', () => { + const testResult = checkReadLocation( + config, 'nobucketmatch', key, bucket); + const expectedResult = { + location: 'nobucketmatch', + key: `${bucket}/${key}`, + locationType: 'aws_s3', + }; + assert.deepStrictEqual(testResult, expectedResult); + }); +});