1
1
/* eslint require-jsdoc: "off", new-cap: "off", no-invalid-this: "off" */
2
- import { readFileSync } from 'fs'
2
+ import { readFileSync , writeFileSync , unlinkSync } from 'fs'
3
3
import should from 'should'
4
4
import { spy , stub } from 'sinon'
5
5
import AWS from 'aws-sdk'
6
6
import { File } from 'gulp-util'
7
7
import { S3File , Bean } from '../src/aws'
8
8
import * as plugin from '../src/plugin'
9
9
import gulpEbDeploy from '../src'
10
+ import os from 'os'
11
+ import uuidv4 from 'uuid/v4'
12
+ import path from 'path'
10
13
11
14
describe ( 'Gulp plugin' , ( ) => {
12
15
let file
@@ -539,7 +542,26 @@ describe('Gulp plugin', () => {
539
542
}
540
543
AWS . config . credentials = null
541
544
} )
542
- it ( 'updates AWS.config.credentials with the provided values' , ( ) => {
545
+
546
+ it ( 'sets AWS.config with signatureVersion v4 by default' , ( ) => {
547
+ spy ( AWS , 'Credentials' )
548
+ buildOptions ( {
549
+ amazon : { }
550
+ } )
551
+ AWS . config . signatureVersion . should . be . equal ( 'v4' )
552
+ } )
553
+
554
+ it ( 'allows to set a signatureVersion for AWS.config' , ( ) => {
555
+ spy ( AWS , 'Credentials' )
556
+ buildOptions ( {
557
+ amazon : {
558
+ signatureVersion : 'v2'
559
+ }
560
+ } )
561
+ AWS . config . signatureVersion . should . be . equal ( 'v2' )
562
+ } )
563
+
564
+ it ( 'updates AWS.config.credentials with legacy values' , ( ) => {
543
565
spy ( AWS , 'Credentials' )
544
566
buildOptions ( {
545
567
amazon : {
@@ -548,26 +570,127 @@ describe('Gulp plugin', () => {
548
570
}
549
571
} )
550
572
AWS . Credentials . calledOnce . should . be . true ( )
573
+ AWS . config . credentials . should . be . instanceOf ( AWS . Credentials )
551
574
AWS . config . credentials . accessKeyId . should . be . equal ( '__accessKeyId' )
552
575
AWS . config . credentials . secretAccessKey . should . be . equal ( '__secretAccessKey' )
553
576
} )
554
577
555
- it ( 'sets AWS.config with signatureVersion v4 by default' , ( ) => {
556
- spy ( AWS , 'Credentials' )
578
+ it ( 'updates AWS.config.credentials with access key id and secret access key.' , ( ) => {
557
579
buildOptions ( {
558
- amazon : { }
580
+ amazon : {
581
+ credentials : {
582
+ accessKeyId : '__accessKeyId' ,
583
+ secretAccessKey : '__secretAccessKey'
584
+ }
585
+ }
559
586
} )
560
- AWS . config . signatureVersion . should . be . equal ( 'v4' )
587
+ AWS . config . credentials . should . be . instanceOf ( AWS . Credentials )
588
+ AWS . config . credentials . accessKeyId . should . be . equal ( '__accessKeyId' )
589
+ AWS . config . credentials . secretAccessKey . should . be . equal ( '__secretAccessKey' )
561
590
} )
562
591
563
- it ( 'allows to set a signatureVersion for AWS.config' , ( ) => {
564
- spy ( AWS , 'Credentials' )
592
+ it ( 'updates AWS.config.credentials with SAML credentials.' , ( ) => {
565
593
buildOptions ( {
566
594
amazon : {
567
- signatureVersion : 'v2'
595
+ credentials : {
596
+ RoleArn : '__roleArn' ,
597
+ PrincipalArn : '__principalArn' ,
598
+ SAMLAssertion : '__samlAssertion'
599
+ }
568
600
}
569
601
} )
570
- AWS . config . signatureVersion . should . be . equal ( 'v2' )
602
+ AWS . config . credentials . should . be . instanceOf ( AWS . SAMLCredentials )
603
+ AWS . config . credentials . params . RoleArn . should . be . equal ( '__roleArn' )
604
+ AWS . config . credentials . params . PrincipalArn . should . be . equal ( '__principalArn' )
605
+ AWS . config . credentials . params . SAMLAssertion . should . be . equal ( '__samlAssertion' )
606
+ } )
607
+
608
+ it ( 'updates AWS.config.credentials with MFA temporary credentials.' , ( ) => {
609
+ AWS . config . credentials = new AWS . Credentials ( )
610
+ buildOptions ( {
611
+ amazon : {
612
+ credentials : {
613
+ SerialNumber : '__serialNumber' ,
614
+ TokenCode : '__tokenCode'
615
+ }
616
+ }
617
+ } )
618
+ AWS . config . credentials . should . be . instanceOf ( AWS . TemporaryCredentials )
619
+ AWS . config . credentials . params . SerialNumber . should . be . equal ( '__serialNumber' )
620
+ AWS . config . credentials . params . TokenCode . should . be . equal ( '__tokenCode' )
621
+ } )
622
+
623
+ it ( 'updates AWS.config.credentials with IAM role temporary credentials.' , ( ) => {
624
+ AWS . config . credentials = new AWS . Credentials ( )
625
+ buildOptions ( {
626
+ amazon : {
627
+ credentials : {
628
+ RoleArn : '__roleArn'
629
+ }
630
+ }
631
+ } )
632
+ AWS . config . credentials . should . be . instanceOf ( AWS . TemporaryCredentials )
633
+ AWS . config . credentials . params . RoleArn . should . be . equal ( '__roleArn' )
634
+ } )
635
+
636
+ it ( 'updates AWS.config.credentials with Cognito identity ID credentials.' , ( ) => {
637
+ buildOptions ( {
638
+ amazon : {
639
+ credentials : {
640
+ IdentityId : '__indentityId'
641
+ }
642
+ }
643
+ } )
644
+ AWS . config . credentials . should . be . instanceOf ( AWS . CognitoIdentityCredentials )
645
+ AWS . config . credentials . params . IdentityId . should . be . equal ( '__indentityId' )
646
+ } )
647
+
648
+ it ( 'updates AWS.config.credentials with Cognito identity pool ID credentials.' , ( ) => {
649
+ buildOptions ( {
650
+ amazon : {
651
+ credentials : {
652
+ IdentityPoolId : '__indentityPoolId'
653
+ }
654
+ }
655
+ } )
656
+ AWS . config . credentials . should . be . instanceOf ( AWS . CognitoIdentityCredentials )
657
+ AWS . config . credentials . params . IdentityPoolId . should . be . equal ( '__indentityPoolId' )
658
+ } )
659
+
660
+ it ( 'updates AWS.config.credentials with an environment credential prefix.' , ( ) => {
661
+ process . env . __envPrefix_ACCESS_KEY_ID = '__accessKeyId'
662
+ process . env . __envPrefix_SECRET_ACCESS_KEY = '__secretAccessKey'
663
+
664
+ buildOptions ( {
665
+ amazon : {
666
+ credentials : '__envPrefix'
667
+ }
668
+ } )
669
+ AWS . config . credentials . should . be . instanceOf ( AWS . EnvironmentCredentials )
670
+ AWS . config . credentials . accessKeyId . should . be . equal ( '__accessKeyId' )
671
+ AWS . config . credentials . secretAccessKey . should . be . equal ( '__secretAccessKey' )
672
+
673
+ process . env . __envPrefix_ACCESS_KEY_ID = ''
674
+ process . env . __envPrefix_SECRET_ACCESS_KEY = ''
675
+ } )
676
+
677
+ it ( 'updates AWS.config.credentials with credentials loaded from a credential file' , ( ) => {
678
+ const fileName = path . join ( os . tmpdir ( ) , `credentials-${ uuidv4 ( ) } .json` )
679
+ writeFileSync ( fileName , JSON . stringify ( {
680
+ accessKeyId : '__accessKeyId' ,
681
+ secretAccessKey : '__secretAccessKey'
682
+ } ) )
683
+
684
+ buildOptions ( {
685
+ amazon : {
686
+ credentials : fileName
687
+ }
688
+ } )
689
+ unlinkSync ( fileName )
690
+
691
+ AWS . config . credentials . should . be . instanceOf ( AWS . FileSystemCredentials )
692
+ AWS . config . credentials . accessKeyId . should . be . equal ( '__accessKeyId' )
693
+ AWS . config . credentials . secretAccessKey . should . be . equal ( '__secretAccessKey' )
571
694
} )
572
695
573
696
it ( 'does not update AWS.config.credentials if no access parameters were specified' , ( ) => {
@@ -578,6 +701,48 @@ describe('Gulp plugin', () => {
578
701
AWS . Credentials . called . should . be . false ( )
579
702
should ( AWS . config . credentials ) . be . null ( )
580
703
} )
704
+
705
+ it ( 'updates AWS.config.credentials with a Credentials object' , ( ) => {
706
+ spy ( AWS , 'Credentials' )
707
+ const credentials = new AWS . Credentials ( )
708
+ buildOptions ( {
709
+ amazon : {
710
+ credentials : credentials
711
+ }
712
+ } )
713
+ AWS . Credentials . calledOnce . should . be . true ( )
714
+ AWS . config . credentials . should . be . equal ( credentials )
715
+ } )
716
+
717
+ it ( 'throws an error when provided credentials are not a string or object' , ( ) => {
718
+ ( ( ) => buildOptions ( {
719
+ amazon : {
720
+ credentials : 0
721
+ }
722
+ } ) ) . should . throw ( )
723
+ } )
724
+
725
+ it ( 'throws an error when no matching credential provider is found' , ( ) => {
726
+ ( ( ) => buildOptions ( {
727
+ amazon : {
728
+ credentials : {
729
+ unknown : '__unknown'
730
+ }
731
+ }
732
+ } ) ) . should . throw ( )
733
+ } )
734
+
735
+ it ( 'rethrows an error thrown in the an AWS credentials constructor' , ( ) => {
736
+ // temporary credentials missing master credentials
737
+ ( ( ) => buildOptions ( {
738
+ amazon : {
739
+ credentials : {
740
+ SerialNumber : '__serialNumber' ,
741
+ TokenCode : '__tokenCode'
742
+ }
743
+ }
744
+ } ) ) . should . throw ( )
745
+ } )
581
746
} )
582
747
583
748
describe ( 'gulpEbDeploy' , ( ) => {
0 commit comments