@@ -72,6 +72,7 @@ jest.mock("@aws-sdk/client-sso", () => {
72
72
// This var must be hoisted.
73
73
// eslint-disable-next-line no-var
74
74
var stsSpy : jest . Spied < any > | any | undefined = undefined ;
75
+ const assumeRoleArns : string [ ] = [ ] ;
75
76
76
77
jest . mock ( "@aws-sdk/client-sts" , ( ) => {
77
78
const actual = jest . requireActual ( "@aws-sdk/client-sts" ) ;
@@ -80,6 +81,7 @@ jest.mock("@aws-sdk/client-sts", () => {
80
81
81
82
stsSpy = jest . spyOn ( actual . STSClient . prototype , "send" ) . mockImplementation ( async function ( this : any , command : any ) {
82
83
if ( command . constructor . name === "AssumeRoleCommand" ) {
84
+ assumeRoleArns . push ( command . input . RoleArn ) ;
83
85
return {
84
86
Credentials : {
85
87
AccessKeyId : "STS_AR_ACCESS_KEY_ID" ,
@@ -91,6 +93,7 @@ jest.mock("@aws-sdk/client-sts", () => {
91
93
} ;
92
94
}
93
95
if ( command . constructor . name === "AssumeRoleWithWebIdentityCommand" ) {
96
+ assumeRoleArns . push ( command . input . RoleArn ) ;
94
97
return {
95
98
Credentials : {
96
99
AccessKeyId : "STS_ARWI_ACCESS_KEY_ID" ,
@@ -177,6 +180,22 @@ describe("credential-provider-node integration test", () => {
177
180
let sts : STS = null as any ;
178
181
let processSnapshot : typeof process . env = null as any ;
179
182
183
+ const sink = {
184
+ data : [ ] as string [ ] ,
185
+ debug ( log : string ) {
186
+ this . data . push ( log ) ;
187
+ } ,
188
+ info ( log : string ) {
189
+ this . data . push ( log ) ;
190
+ } ,
191
+ warn ( log : string ) {
192
+ this . data . push ( log ) ;
193
+ } ,
194
+ error ( log : string ) {
195
+ this . data . push ( log ) ;
196
+ } ,
197
+ } ;
198
+
180
199
const RESERVED_ENVIRONMENT_VARIABLES = {
181
200
AWS_DEFAULT_REGION : 1 ,
182
201
AWS_REGION : 1 ,
@@ -257,6 +276,8 @@ describe("credential-provider-node integration test", () => {
257
276
output : "json" ,
258
277
} ,
259
278
} ;
279
+ assumeRoleArns . length = 0 ;
280
+ sink . data . length = 0 ;
260
281
} ) ;
261
282
262
283
afterAll ( async ( ) => {
@@ -511,7 +532,7 @@ describe("credential-provider-node integration test", () => {
511
532
} ) ;
512
533
} ) ;
513
534
514
- it ( "should be able to combine a source_profile having credential_source with an origin profile having role_arn and source_profile" , async ( ) => {
535
+ it ( "should be able to combine a source_profile having only credential_source with an origin profile having role_arn and source_profile" , async ( ) => {
515
536
process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI = "http://169.254.170.23" ;
516
537
process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN = "container-authorization" ;
517
538
iniProfileData . default . source_profile = "credential_source_profile" ;
@@ -529,6 +550,138 @@ describe("credential-provider-node integration test", () => {
529
550
clientConfig : {
530
551
region : "us-west-2" ,
531
552
} ,
553
+ logger : sink ,
554
+ } ) ,
555
+ } ) ;
556
+ await sts . getCallerIdentity ( { } ) ;
557
+ const credentials = await sts . config . credentials ( ) ;
558
+ expect ( credentials ) . toEqual ( {
559
+ accessKeyId : "STS_AR_ACCESS_KEY_ID" ,
560
+ secretAccessKey : "STS_AR_SECRET_ACCESS_KEY" ,
561
+ sessionToken : "STS_AR_SESSION_TOKEN" ,
562
+ expiration : new Date ( "3000-01-01T00:00:00.000Z" ) ,
563
+ credentialScope : "us-stsar-1__us-west-2" ,
564
+ } ) ;
565
+ expect ( spy ) . toHaveBeenCalledWith (
566
+ expect . objectContaining ( {
567
+ awsContainerCredentialsFullUri : process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI ,
568
+ awsContainerAuthorizationToken : process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN ,
569
+ } )
570
+ ) ;
571
+ expect ( assumeRoleArns ) . toEqual ( [ "ROLE_ARN" ] ) ;
572
+ spy . mockClear ( ) ;
573
+ } ) ;
574
+
575
+ it ( "should be able to combine a source_profile having web_identity_token_file and role_arn with an origin profile having role_arn and source_profile" , async ( ) => {
576
+ iniProfileData . default . source_profile = "credential_source_profile" ;
577
+ iniProfileData . default . role_arn = "ROLE_ARN_2" ;
578
+
579
+ iniProfileData . credential_source_profile = {
580
+ web_identity_token_file : "token-filepath" ,
581
+ role_arn : "ROLE_ARN_1" ,
582
+ } ;
583
+
584
+ sts = new STS ( {
585
+ region : "us-west-2" ,
586
+ requestHandler : mockRequestHandler ,
587
+ credentials : defaultProvider ( {
588
+ awsContainerCredentialsFullUri : process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI ,
589
+ awsContainerAuthorizationToken : process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN ,
590
+ clientConfig : {
591
+ region : "us-west-2" ,
592
+ } ,
593
+ logger : sink ,
594
+ } ) ,
595
+ } ) ;
596
+ await sts . getCallerIdentity ( { } ) ;
597
+ const credentials = await sts . config . credentials ( ) ;
598
+ expect ( credentials ) . toEqual ( {
599
+ accessKeyId : "STS_AR_ACCESS_KEY_ID" ,
600
+ secretAccessKey : "STS_AR_SECRET_ACCESS_KEY" ,
601
+ sessionToken : "STS_AR_SESSION_TOKEN" ,
602
+ expiration : new Date ( "3000-01-01T00:00:00.000Z" ) ,
603
+ credentialScope : "us-stsar-1__us-west-2" ,
604
+ } ) ;
605
+ expect ( assumeRoleArns ) . toEqual ( [ "ROLE_ARN_1" , "ROLE_ARN_2" ] ) ;
606
+ } ) ;
607
+
608
+ it ( "should complete chained role_arn credentials" , async ( ) => {
609
+ process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI = "http://169.254.170.23" ;
610
+ process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN = "container-authorization" ;
611
+
612
+ iniProfileData . default . source_profile = "credential_source_profile_1" ;
613
+ iniProfileData . default . role_arn = "ROLE_ARN_3" ;
614
+
615
+ iniProfileData . credential_source_profile_1 = {
616
+ source_profile : "credential_source_profile_2" ,
617
+ role_arn : "ROLE_ARN_2" ,
618
+ } ;
619
+
620
+ iniProfileData . credential_source_profile_2 = {
621
+ credential_source : "EcsContainer" ,
622
+ role_arn : "ROLE_ARN_1" ,
623
+ } ;
624
+
625
+ const spy = jest . spyOn ( credentialProviderHttp , "fromHttp" ) ;
626
+ sts = new STS ( {
627
+ region : "us-west-2" ,
628
+ requestHandler : mockRequestHandler ,
629
+ credentials : defaultProvider ( {
630
+ awsContainerCredentialsFullUri : process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI ,
631
+ awsContainerAuthorizationToken : process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN ,
632
+ clientConfig : {
633
+ region : "us-west-2" ,
634
+ } ,
635
+ logger : sink ,
636
+ } ) ,
637
+ } ) ;
638
+ await sts . getCallerIdentity ( { } ) ;
639
+ const credentials = await sts . config . credentials ( ) ;
640
+ expect ( credentials ) . toEqual ( {
641
+ accessKeyId : "STS_AR_ACCESS_KEY_ID" ,
642
+ secretAccessKey : "STS_AR_SECRET_ACCESS_KEY" ,
643
+ sessionToken : "STS_AR_SESSION_TOKEN" ,
644
+ expiration : new Date ( "3000-01-01T00:00:00.000Z" ) ,
645
+ credentialScope : "us-stsar-1__us-west-2" ,
646
+ } ) ;
647
+ expect ( spy ) . toHaveBeenCalledWith (
648
+ expect . objectContaining ( {
649
+ awsContainerCredentialsFullUri : process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI ,
650
+ awsContainerAuthorizationToken : process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN ,
651
+ } )
652
+ ) ;
653
+ expect ( assumeRoleArns ) . toEqual ( [ "ROLE_ARN_1" , "ROLE_ARN_2" , "ROLE_ARN_3" ] ) ;
654
+ spy . mockClear ( ) ;
655
+ } ) ;
656
+
657
+ it ( "should complete chained role_arn credentials with optional role_arn in credential_source step" , async ( ) => {
658
+ process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI = "http://169.254.170.23" ;
659
+ process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN = "container-authorization" ;
660
+
661
+ iniProfileData . default . source_profile = "credential_source_profile_1" ;
662
+ iniProfileData . default . role_arn = "ROLE_ARN_3" ;
663
+
664
+ iniProfileData . credential_source_profile_1 = {
665
+ source_profile : "credential_source_profile_2" ,
666
+ role_arn : "ROLE_ARN_2" ,
667
+ } ;
668
+
669
+ iniProfileData . credential_source_profile_2 = {
670
+ credential_source : "EcsContainer" ,
671
+ // This scenario tests the option of having no role_arn in this step of the chain.
672
+ } ;
673
+
674
+ const spy = jest . spyOn ( credentialProviderHttp , "fromHttp" ) ;
675
+ sts = new STS ( {
676
+ region : "us-west-2" ,
677
+ requestHandler : mockRequestHandler ,
678
+ credentials : defaultProvider ( {
679
+ awsContainerCredentialsFullUri : process . env . AWS_CONTAINER_CREDENTIALS_FULL_URI ,
680
+ awsContainerAuthorizationToken : process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN ,
681
+ clientConfig : {
682
+ region : "us-west-2" ,
683
+ } ,
684
+ logger : sink ,
532
685
} ) ,
533
686
} ) ;
534
687
await sts . getCallerIdentity ( { } ) ;
@@ -546,6 +699,7 @@ describe("credential-provider-node integration test", () => {
546
699
awsContainerAuthorizationToken : process . env . AWS_CONTAINER_AUTHORIZATION_TOKEN ,
547
700
} )
548
701
) ;
702
+ expect ( assumeRoleArns ) . toEqual ( [ "ROLE_ARN_2" , "ROLE_ARN_3" ] ) ;
549
703
spy . mockClear ( ) ;
550
704
} ) ;
551
705
} ) ;
0 commit comments