Skip to content

Commit c69d132

Browse files
committed
add support for passing an AWS.Credentials object via options.amazon.credentials
1 parent 559fcba commit c69d132

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ gulp.task('deploy', function() {
3838

3939
The code above would work as follows
4040
* Take the files sepcified by `gulp.src` and zip them on a file named `{ version }-{ timestamp }.zip` (i.e: `1.0.0-2016.04.08_13.26.32.zip`)
41-
* If amazon credentials (`accessKeyId`, `secretAccessKey`) are provided in the `amazon` object, set them on the `AWS.config.credentials`. If not provided, the default values from AWS CLI configuration will be used.
41+
* AWS credentials may be provided either in Form of a `accessKeyId` and `secretAccessKey` or a [`credentials` object](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html). If no credentials are provided, the default values from AWS CLI configuration will be used.
4242
* Try to upload the zipped file to the bucket specified by `amazon.bucket`. If it fails because the bucket doesn't exist, try to create the bucket and then try to upload the zipped file again
4343
* Uploads the ziped files to the bucket on the path `{{ name }}/{{ filename }}` (i.e: `my-application/1.0.0-2016.04.08_13.26.32.zip`)
4444
* Creates a new version on the Application specified by `applicationName` with VersionLabel `{ version }-{ timestamp }` (i.e: `1.0.0-2016.04.08_13.26.32`)

src/plugin.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync } from 'fs'
1+
import { readFileSync, existsSync } from 'fs'
22
import { join } from 'path'
33
import { omit, isEqual } from 'lodash'
44
import { log as gulpLog, colors, PluginError } from 'gulp-util'
@@ -9,6 +9,43 @@ import AWS from 'aws-sdk'
99
import pad from 'left-pad'
1010
import { S3File, Bean } from './aws'
1111

12+
const credentialProviders = [
13+
{
14+
Ctor: AWS.Credentials,
15+
fields: [
16+
['accessKeyId', 'secretAccessKey']
17+
]
18+
},
19+
{
20+
Ctor: AWS.SAMLCredentials,
21+
fields: [
22+
[ 'RoleArn', 'PrincipalArn', 'SAMLAssertion' ]
23+
]
24+
},
25+
{
26+
Ctor: AWS.CognitoIdentityCredentials,
27+
fields: [
28+
[ 'IdentityPoolId' ],
29+
[ 'IdentityId' ]
30+
]
31+
},
32+
{
33+
// we can only detect these if a custom profile is specified
34+
// but that is fine because shared ini file credentials using the default profile
35+
// are used by the AWS SDK when no credentials are specified
36+
Ctor: AWS.SharedIniFileCredentials,
37+
fields: [
38+
[ 'profile' ]
39+
]
40+
},
41+
{
42+
Ctor: AWS.TemporaryCredentials,
43+
fields: [
44+
[ 'SerialNumber', 'TokenCode' ],
45+
[ 'RoleArn' ]
46+
]
47+
}
48+
]
1249
const IS_TEST = process.env['NODE_ENV'] === 'test'
1350
const log = IS_TEST ? () => {} : gulpLog
1451

@@ -191,8 +228,42 @@ export function buildOptions(opts) {
191228
if (!options.amazon)
192229
throw new PluginError(PLUGIN_NAME, 'No amazon config provided')
193230

194-
// if keys are provided, create new credentials, otherwise defaults will be used
195-
if (options.amazon.accessKeyId && options.amazon.secretAccessKey) {
231+
if (options.amazon.credentials) {
232+
const creds = options.amazon.credentials
233+
const credsType = typeof(creds)
234+
235+
if (credsType === 'string') {
236+
// if the credentials are of type string, assume the user is specifying
237+
// an environment variable name prefix
238+
AWS.config.credentials = existsSync(creds) ? new AWS.EnvironmentCredentials(creds)
239+
: new AWS.FileSystemCredentials(creds)
240+
} else if (credsType !== 'object') {
241+
// otherwise the credentials must be an object
242+
throw new PluginError(PLUGIN_NAME, `Amazon credentials must be an object, got a '${typeof(creds)}'.`)
243+
} else if (creds.constructor.name === 'Credentials' ||
244+
typeof(creds.constructor.__super__) === 'function' &&
245+
creds.constructor.__super__.name === 'Credentials') {
246+
// support pre-build objects of or inheriting the AWS.Credentials class
247+
AWS.config.credentials = creds
248+
} else {
249+
// otherwise try to find a matching provider for the supplied credentials object
250+
const provider = credentialProviders.find(prov =>
251+
prov.fields.find(fields =>
252+
fields.every(field => creds[field] !== undefined)
253+
)
254+
)
255+
if (provider === undefined)
256+
throw new PluginError(PLUGIN_NAME, `Could not find a matching AWS credentials provider for the supplied credentials object.`)
257+
258+
try {
259+
AWS.config.credentials = new provider.Ctor(creds)
260+
} catch(err) {
261+
throw new PluginError(PLUGIN_NAME, `An error occured while trying to construct AWS.${provider.Ctor.name} from supplied credentials object: ${err}`)
262+
}
263+
}
264+
} else if (options.amazon.accessKeyId && options.amazon.secretAccessKey) {
265+
// legacy support for the access key id and secret access key
266+
// passed in directly via the options.amazon object
196267
AWS.config.credentials = new AWS.Credentials({
197268
accessKeyId: opts.amazon.accessKeyId,
198269
secretAccessKey: opts.amazon.secretAccessKey

test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,18 @@ describe('Gulp plugin', () => {
578578
AWS.Credentials.called.should.be.false()
579579
should(AWS.config.credentials).be.null()
580580
})
581+
582+
it('updates AWS.config.credentials with a Credentials object', () => {
583+
spy(AWS, 'Credentials')
584+
const credentials = new AWS.Credentials()
585+
buildOptions({
586+
amazon: {
587+
credentials: credentials
588+
}
589+
})
590+
AWS.Credentials.calledOnce.should.be.true()
591+
AWS.config.credentials.should.be.equal(credentials)
592+
})
581593
})
582594

583595
describe('gulpEbDeploy', () => {

0 commit comments

Comments
 (0)