Skip to content
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

Bf/zenko 436 pick read location fix #1264

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/api/apiUtils/object/checkReadLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* checkReadLocation - verify that a bucket's default read location exists
* a for specified read data locator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: for a

* @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;
19 changes: 4 additions & 15 deletions lib/api/objectGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
67 changes: 67 additions & 0 deletions tests/unit/utils/checkReadLocation.js
Original file line number Diff line number Diff line change
@@ -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);
});
});