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

AWS: Request cache should add region as key to prevent cross-region cache collision #5694

Merged
merged 3 commits into from Jan 15, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+49 −1
Diff settings

Always

Just for now

@@ -223,14 +223,18 @@ class AwsProvider {
* @param {Object} params - Parameters * @param {Object} params - Parameters
* @param {Object} [options] - Options to modify the request behavior * @param {Object} [options] - Options to modify the request behavior
* @prop [options.useCache] - Utilize cache to retrieve results * @prop [options.useCache] - Utilize cache to retrieve results
* @prop [options.region] - Specify when to request to different region
*/ */
request(service, method, params, options) { request(service, method, params, options) {
const that = this; const that = this;
const credentials = _.cloneDeep(that.getCredentials()); const credentials = _.cloneDeep(that.getCredentials());
// Make sure options is an object (honors wrong calls of request) // Make sure options is an object (honors wrong calls of request)
const requestOptions = _.isObject(options) ? options : {}; const requestOptions = _.isObject(options) ? options : {};
const shouldCache = _.get(requestOptions, 'useCache', false); const shouldCache = _.get(requestOptions, 'useCache', false);
const paramsHash = objectHash.sha1(params); const paramsWithRegion = _.merge({}, params, {
region: _.get(options, 'region'),
});
const paramsHash = objectHash.sha1(paramsWithRegion);
const MAX_TRIES = 4; const MAX_TRIES = 4;
const persistentRequest = (f) => new BbPromise((resolve, reject) => { const persistentRequest = (f) => new BbPromise((resolve, reject) => {
const doCall = (numTry) => { const doCall = (numTry) => {
@@ -565,6 +565,50 @@ describe('AwsProvider', () => {
}); });
}); });


it('should request if same service, method and params but different region in option', () => {
const expectedResult = { called: true };
const sendStub = sinon.stub().yields(null, { called: true });
const requestSpy = sinon.spy(awsProvider, 'request');
class FakeCF {
constructor(credentials) {
this.credentials = credentials;
}

describeStacks() {
return {
send: sendStub,
};
}
}
awsProvider.sdk = {
CloudFormation: FakeCF,
};
const executeRequestWithRegion = (region) => awsProvider.request(
'CloudFormation',
'describeStacks',
{ StackName: 'same-stack' },
{
useCache: true,
region,
}
);
const requests = [];
requests.push(BbPromise.try(() => executeRequestWithRegion('us-east-1')));
requests.push(BbPromise.try(() => executeRequestWithRegion('ap-northeast-1')));

return BbPromise.all(requests)
.then(results => {
expect(_.size(results, 2));
_.forEach(results, result => {
expect(result).to.deep.equal(expectedResult);
});
return expect(sendStub.callCount).to.equal(2);
})
.finally(() => {
requestSpy.restore();
});
});

it('should resolve to the same response with mutiple parallel requests', () => { it('should resolve to the same response with mutiple parallel requests', () => {
const expectedResult = { called: true }; const expectedResult = { called: true };
const sendStub = sinon.stub().yields(null, { called: true }); const sendStub = sinon.stub().yields(null, { called: true });
ProTip! Use n and p to navigate between commits in a pull request.