@@ -12,6 +12,14 @@ jest.mock("@aws-sdk/credential-provider-sso", () => {
12
12
} ) ;
13
13
import { fromSSO , FromSSOInit } from "@aws-sdk/credential-provider-sso" ;
14
14
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
+
15
23
jest . mock ( "@aws-sdk/credential-provider-ini" , ( ) => {
16
24
const iniProvider = jest . fn ( ) ;
17
25
return {
@@ -129,6 +137,29 @@ describe("defaultProvider", () => {
129
137
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
130
138
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
131
139
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 ) ;
132
163
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
133
164
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
134
165
} ) ;
@@ -141,12 +172,14 @@ describe("defaultProvider", () => {
141
172
142
173
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
143
174
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
175
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
144
176
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
145
177
146
178
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
147
179
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
148
180
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
149
181
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
182
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
150
183
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
151
184
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
152
185
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -168,6 +201,7 @@ describe("defaultProvider", () => {
168
201
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
169
202
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
170
203
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
204
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
171
205
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
172
206
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
173
207
} ) ;
@@ -180,13 +214,15 @@ describe("defaultProvider", () => {
180
214
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
181
215
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
182
216
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
217
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
183
218
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
184
219
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
185
220
186
221
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
187
222
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
188
223
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
189
224
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
225
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
190
226
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
191
227
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
192
228
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
@@ -201,6 +237,7 @@ describe("defaultProvider", () => {
201
237
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
202
238
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
203
239
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
240
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
204
241
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
205
242
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
206
243
@@ -220,6 +257,7 @@ describe("defaultProvider", () => {
220
257
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
221
258
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
222
259
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
260
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
223
261
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
224
262
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
225
263
( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
@@ -230,6 +268,7 @@ describe("defaultProvider", () => {
230
268
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
231
269
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
232
270
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
271
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
233
272
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
234
273
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
235
274
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -244,13 +283,15 @@ describe("defaultProvider", () => {
244
283
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
245
284
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
246
285
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
286
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
247
287
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
248
288
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
249
289
250
290
await expect ( defaultProvider ( ) ( ) ) . resolves ;
251
291
expect ( ( loadSharedConfigFiles as any ) . mock . calls . length ) . toBe ( 1 ) ;
252
292
expect ( ( fromIni as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
253
293
expect ( ( fromSSO as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
294
+ expect ( ( fromTokenFile as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
254
295
expect ( ( fromProcess as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
255
296
} ) ;
256
297
@@ -277,6 +318,29 @@ describe("defaultProvider", () => {
277
318
expect ( ( fromSSO as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ...ssoConfig , loadedConfig } ) ;
278
319
} ) ;
279
320
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
+
280
344
it ( "should pass configuration on to the ini provider" , async ( ) => {
281
345
const iniConfig : FromIniInit = {
282
346
profile : "foo" ,
@@ -443,13 +507,15 @@ describe("defaultProvider", () => {
443
507
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
444
508
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( Promise . resolve ( creds ) ) ) ;
445
509
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
510
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
446
511
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
447
512
( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
448
513
449
514
expect ( await defaultProvider ( { profile : "foo" } ) ( ) ) . toEqual ( creds ) ;
450
515
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
451
516
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
452
517
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
518
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
453
519
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
454
520
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
455
521
} ) ;
@@ -463,6 +529,7 @@ describe("defaultProvider", () => {
463
529
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
464
530
( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
465
531
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
532
+ ( fromTokenFile ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
466
533
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
467
534
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
468
535
( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
@@ -472,6 +539,7 @@ describe("defaultProvider", () => {
472
539
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
473
540
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
474
541
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
542
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
475
543
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
476
544
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
477
545
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -493,6 +561,7 @@ describe("defaultProvider", () => {
493
561
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
494
562
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
495
563
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
564
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
496
565
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
497
566
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
498
567
} ) ;
@@ -516,6 +585,7 @@ describe("defaultProvider", () => {
516
585
expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
517
586
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
518
587
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
588
+ expect ( ( fromTokenFile ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
519
589
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
520
590
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
521
591
} ) ;
0 commit comments