Skip to content

Commit 050fafe

Browse files
committed
Merge pull request #1 from reidab/s3-credential-resolution
Don't require AWS credentials to be set explicitly
2 parents f04cbe8 + 1f1d5d5 commit 050fafe

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ For detailed information on what plugin hooks are and how they work, please refe
5656

5757
For detailed information on how configuration of plugins works, please refer to the [Plugin Documentation][1].
5858

59-
### accessKeyId (`required`)
59+
### accessKeyId
6060

61-
The AWS access key for the user that has the ability to upload to the `bucket`.
61+
The AWS access key for the user that has the ability to upload to the `bucket`. If this is left undefined, the normal [AWS SDK credential resolution](https://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Setting_AWS_Credentials) will take place.
6262

6363
*Default:* `undefined`
6464

65-
### secretAccessKey (`required`)
65+
### secretAccessKey
6666

67-
The AWS secret for the user that has the ability to upload to the `bucket`.
67+
The AWS secret for the user that has the ability to upload to the `bucket`. This must be defined when `accessKeyId` is defined.
6868

6969
*Default:* `undefined`
7070

lib/cloudfront.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ var uuid = require('uuid');
77
module.exports = CoreObject.extend({
88
init: function(options) {
99
this._plugin = options.plugin;
10+
1011
var AWS = require('aws-sdk');
11-
this._client = this._plugin.readConfig('cloudfrontClient') || new AWS.CloudFront({
12-
accessKeyId: this._plugin.readConfig('accessKeyId'),
13-
secretAccessKey: this._plugin.readConfig('secretAccessKey'),
12+
const accessKeyId = this._plugin.readConfig('accessKeyId');
13+
const secretAccessKey = this._plugin.readConfig('secretAccessKey');
14+
var awsOptions = {
1415
region: this._plugin.readConfig('region')
15-
});
16+
};
17+
18+
if (accessKeyId && secretAccessKey) {
19+
awsOptions.accessKeyId = accessKeyId;
20+
awsOptions.secretAccessKey = secretAccessKey;
21+
}
22+
23+
this._client = this._plugin.readConfig('cloudfrontClient') || new AWS.CloudFront(awsOptions);
1624
},
1725

1826
invalidate: function(options) {

tests/unit/lib/cloudfront-nodetest.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
var assert = require('ember-cli/tests/helpers/assert');
22

3+
34
describe('cloudfront', function() {
45
var CloudFront, mockUi, cloudfrontClient, plugin, subject;
56

67
before(function() {
8+
process.env['AWS_ACCESS_KEY_ID'] = 'set_via_env_var';
9+
process.env['AWS_SECRET_ACCESS_KEY'] = 'set_via_env_var';
10+
711
CloudFront = require('../../../lib/cloudfront');
812
});
913

@@ -34,6 +38,55 @@ describe('cloudfront', function() {
3438
};
3539
});
3640

41+
describe('#init', function() {
42+
context('with a custom CloudFront client', function() {
43+
it('uses the custom client', function() {
44+
assert.equal(cloudfrontClient, subject._client);
45+
})
46+
});
47+
48+
context('using the aws-sdk CloudFront client', function() {
49+
beforeEach(function() {
50+
awsClient = {};
51+
plugin.readConfig = function(propertyName) {};
52+
subject = new CloudFront({plugin: plugin});
53+
});
54+
55+
it('uses the AWS client', function() {
56+
var AWS = require('aws-sdk');
57+
assert.ok(subject._client instanceof AWS.CloudFront);
58+
});
59+
60+
context('with credentials in plugin config', function() {
61+
beforeEach(function() {
62+
plugin.readConfig = function(propertyName) {
63+
if(propertyName === 'accessKeyId' || propertyName === 'secretAccessKey') {
64+
return 'set_via_config';
65+
}
66+
};
67+
subject = new CloudFront({plugin: plugin});
68+
});
69+
70+
it('uses the configured credentials', function() {
71+
assert.equal('set_via_config', subject._client.config.credentials.accessKeyId);
72+
assert.equal('set_via_config', subject._client.config.credentials.secretAccessKey);
73+
});
74+
});
75+
76+
context('with no credentials in the plugin config', function() {
77+
beforeEach(function() {
78+
plugin.readConfig = function(propertyName) {};
79+
subject = new CloudFront({plugin: plugin});
80+
});
81+
82+
it('falls back to default AWS credential resolution', function() {
83+
assert.equal('set_via_env_var', subject._client.config.credentials.accessKeyId);
84+
assert.equal('set_via_env_var', subject._client.config.credentials.secretAccessKey);
85+
});
86+
});
87+
});
88+
});
89+
3790
describe('#invalidate', function() {
3891
it('resolves if invalidation succeeds', function() {
3992
var promises = subject.invalidate(validOptions);

0 commit comments

Comments
 (0)