@@ -12,6 +12,14 @@ jest.mock("@aws-sdk/credential-provider-sso", () => {
1212} ) ;
1313import { fromSSO , FromSSOInit } from "@aws-sdk/credential-provider-sso" ;
1414
15+ jest . mock ( "@aws-sdk/credential-provider-web-identity" , ( ) => {
16+ const webIdentityProvider = jest . fn ( ) ;
17+ return {
18+ fromTokenFile : jest . fn ( ) . mockReturnValue ( webIdentityProvider ) ,
19+ } ;
20+ } ) ;
21+ import { fromTokenFile , FromTokenFileInit } from "@aws-sdk/credential-provider-web-identity" ;
22+
1523jest . mock ( "@aws-sdk/credential-provider-ini" , ( ) => {
1624 const iniProvider = jest . fn ( ) ;
1725 return {
@@ -129,6 +137,29 @@ describe("defaultProvider", () => {
129137 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
130138 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
131139 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
140+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
141+ expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
142+ expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
143+ } ) ;
144+
145+ it ( "should stop after the Web Identity provider if credentials have been found" , async ( ) => {
146+ const creds = {
147+ accessKeyId : "foo" ,
148+ secretAccessKey : "bar" ,
149+ } ;
150+
151+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
152+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
153+ ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
154+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
155+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
156+
157+ expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
158+ expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
159+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
160+ expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
161+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
162+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
132163 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
133164 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
134165 } ) ;
@@ -141,12 +172,14 @@ describe("defaultProvider", () => {
141172
142173 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
143174 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
175+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
144176 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
145177
146178 expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
147179 expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
148180 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
149181 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
182+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
150183 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
151184 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
152185 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -168,6 +201,7 @@ describe("defaultProvider", () => {
168201 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
169202 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
170203 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
204+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
171205 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
172206 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
173207 } ) ;
@@ -180,13 +214,15 @@ describe("defaultProvider", () => {
180214 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
181215 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
182216 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
217+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
183218 ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
184219 ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
185220
186221 expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
187222 expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
188223 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
189224 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
225+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
190226 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
191227 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
192228 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
@@ -201,6 +237,7 @@ describe("defaultProvider", () => {
201237 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
202238 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
203239 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
240+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
204241 ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
205242 ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
206243
@@ -220,6 +257,7 @@ describe("defaultProvider", () => {
220257 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
221258 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
222259 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
260+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
223261 ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
224262 ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
225263 ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
@@ -230,6 +268,7 @@ describe("defaultProvider", () => {
230268 expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
231269 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
232270 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
271+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
233272 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
234273 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
235274 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -244,13 +283,15 @@ describe("defaultProvider", () => {
244283 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
245284 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
246285 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
286+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
247287 ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
248288 ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
249289
250290 await expect ( defaultProvider ( ) ( ) ) . resolves ;
251291 expect ( ( loadSharedConfigFiles as any ) . mock . calls . length ) . toBe ( 1 ) ;
252292 expect ( ( fromIni as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
253293 expect ( ( fromSSO as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
294+ expect ( ( fromTokenFile as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
254295 expect ( ( fromProcess as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
255296 } ) ;
256297
@@ -277,6 +318,29 @@ describe("defaultProvider", () => {
277318 expect ( ( fromSSO as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ...ssoConfig , loadedConfig } ) ;
278319 } ) ;
279320
321+ it ( "should pass configuration on to the Web Identity provider" , async ( ) => {
322+ const webIdentityConfig : FromTokenFileInit = {
323+ roleArn : "someRoleArn" ,
324+ webIdentityTokenFile : "/home/user/.secrets/tokenFile" ,
325+ } ;
326+
327+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
328+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
329+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) =>
330+ Promise . resolve ( {
331+ accessKeyId : "foo" ,
332+ secretAccessKey : "bar" ,
333+ } )
334+ ) ;
335+
336+ ( fromTokenFile as any ) . mockClear ( ) ;
337+
338+ await expect ( defaultProvider ( webIdentityConfig ) ( ) ) . resolves ;
339+
340+ expect ( ( fromTokenFile as any ) . mock . calls . length ) . toBe ( 1 ) ;
341+ expect ( ( fromTokenFile as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ...webIdentityConfig , loadedConfig } ) ;
342+ } ) ;
343+
280344 it ( "should pass configuration on to the ini provider" , async ( ) => {
281345 const iniConfig : FromIniInit = {
282346 profile : "foo" ,
@@ -443,13 +507,15 @@ describe("defaultProvider", () => {
443507 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
444508 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( Promise . resolve ( creds ) ) ) ;
445509 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
510+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
446511 ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
447512 ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
448513
449514 expect ( await defaultProvider ( { profile : "foo" } ) ( ) ) . toEqual ( creds ) ;
450515 expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
451516 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
452517 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
518+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
453519 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
454520 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
455521 } ) ;
@@ -463,6 +529,7 @@ describe("defaultProvider", () => {
463529 ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
464530 ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
465531 ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
532+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
466533 ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
467534 ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
468535 ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
@@ -472,6 +539,7 @@ describe("defaultProvider", () => {
472539 expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
473540 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
474541 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
542+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
475543 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
476544 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
477545 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -493,6 +561,7 @@ describe("defaultProvider", () => {
493561 expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
494562 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
495563 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
564+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
496565 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
497566 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
498567 } ) ;
@@ -516,6 +585,7 @@ describe("defaultProvider", () => {
516585 expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
517586 expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
518587 expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
588+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
519589 expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
520590 expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
521591 } ) ;
0 commit comments