Skip to content

Commit

Permalink
Merge pull request #116 from Financial-Times/CPREL-856-allow-passing-…
Browse files Browse the repository at this point in the history
…in-aws-creds

Allow passing in AWS creds when initialising
  • Loading branch information
alexmuller authored Nov 10, 2023
2 parents a18fea9 + 318cedd commit ec97052
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/dynamos.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ function dynamo (table, region, opts) {
httpOptions.connectTimeout = opts.connectTimeout
}

let accessKeyId = process.env.URLMGMTAPI_AWS_ACCESS_KEY || process.env.AWS_ACCESS_KEY;
let secretAccessKey = process.env.URLMGMTAPI_AWS_SECRET_KEY || process.env.AWS_SECRET_ACCESS_KEY;

if (opts.auth) {
if (!opts.auth.accessKeyId) {
throw new Error('accessKeyId not set on auth object');
}

if (!opts.auth.secretAccessKey) {
throw new Error('secretAccessKey not set on auth object');
}

accessKeyId = opts.auth.accessKeyId;
secretAccessKey = opts.auth.secretAccessKey;
}

return {
table,
instance: module.exports._createDynamoDBInstance({
region: region,
accessKeyId: process.env.URLMGMTAPI_AWS_ACCESS_KEY || process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.URLMGMTAPI_AWS_SECRET_KEY || process.env.AWS_SECRET_ACCESS_KEY,
accessKeyId,
secretAccessKey,
httpOptions
})
};
Expand Down
22 changes: 22 additions & 0 deletions test/dynamos.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,26 @@ describe('#dynamos', () => {
expect(httpOptions.agent instanceof Agent).to.be.true
});

describe('auth', () => {
it('should allow passing access key and secret', () => {
dynamos.init({
auth: {
accessKeyId: 'akia-test',
secretAccessKey: 'secret-secret'
}
});
expect(dynamos._createDynamoDBInstance.args[0][0].accessKeyId).to.equal('akia-test');
expect(dynamos._createDynamoDBInstance.args[0][0].secretAccessKey).to.equal('secret-secret');
});

it('throws an error if you do not pass in the key or secret', () => {
expect(function(){
dynamos.init({
auth: {
accessKeyId: 'akia-test'
}
});
}).to.throw('secretAccessKey not set on auth object');
});
});
});

0 comments on commit ec97052

Please sign in to comment.