@@ -21,6 +21,14 @@ jest.mock("@aws-sdk/shared-ini-file-loader", () => ({
21
21
} ) ) ;
22
22
import { loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader" ;
23
23
24
+ jest . mock ( "@aws-sdk/credential-provider-sso" , ( ) => {
25
+ const ssoProvider = jest . fn ( ) ;
26
+ return {
27
+ fromSSO : jest . fn ( ) . mockReturnValue ( ssoProvider ) ,
28
+ } ;
29
+ } ) ;
30
+ import { fromSSO , FromSSOInit } from "@aws-sdk/credential-provider-sso" ;
31
+
24
32
jest . mock ( "@aws-sdk/credential-provider-ini" , ( ) => {
25
33
const iniProvider = jest . fn ( ) ;
26
34
return {
@@ -81,11 +89,13 @@ beforeEach(() => {
81
89
} ) ;
82
90
83
91
( fromEnv ( ) as any ) . mockClear ( ) ;
92
+ ( fromSSO ( ) as any ) . mockClear ( ) ;
84
93
( fromIni ( ) as any ) . mockClear ( ) ;
85
94
( fromProcess ( ) as any ) . mockClear ( ) ;
86
95
( fromContainerMetadata ( ) as any ) . mockClear ( ) ;
87
96
( fromInstanceMetadata ( ) as any ) . mockClear ( ) ;
88
97
( fromEnv as any ) . mockClear ( ) ;
98
+ ( fromSSO as any ) . mockClear ( ) ;
89
99
( fromIni as any ) . mockClear ( ) ;
90
100
( fromProcess as any ) . mockClear ( ) ;
91
101
( fromContainerMetadata as any ) . mockClear ( ) ;
@@ -120,17 +130,37 @@ describe("defaultProvider", () => {
120
130
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
121
131
} ) ;
122
132
133
+ it ( "should stop after the SSO provider if credentials have been found" , async ( ) => {
134
+ const creds = {
135
+ accessKeyId : "foo" ,
136
+ secretAccessKey : "bar" ,
137
+ } ;
138
+
139
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
140
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
141
+
142
+ expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
143
+ expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
144
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
145
+ expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
146
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
147
+ expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
148
+ expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
149
+ } ) ;
150
+
123
151
it ( "should stop after the ini provider if credentials have been found" , async ( ) => {
124
152
const creds = {
125
153
accessKeyId : "foo" ,
126
154
secretAccessKey : "bar" ,
127
155
} ;
128
156
129
157
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
158
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
130
159
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
131
160
132
161
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
133
162
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
163
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
134
164
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
135
165
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
136
166
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -144,11 +174,13 @@ describe("defaultProvider", () => {
144
174
} ;
145
175
146
176
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
177
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
147
178
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
148
179
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
149
180
150
181
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
151
182
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
183
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
152
184
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
153
185
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
154
186
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -161,12 +193,14 @@ describe("defaultProvider", () => {
161
193
secretAccessKey : "bar" ,
162
194
} ;
163
195
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
196
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
164
197
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
165
198
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
166
199
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
167
200
168
201
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
169
202
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
203
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
170
204
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
171
205
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
172
206
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
@@ -180,6 +214,7 @@ describe("defaultProvider", () => {
180
214
} ;
181
215
182
216
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
217
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
183
218
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
184
219
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
185
220
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
@@ -198,6 +233,7 @@ describe("defaultProvider", () => {
198
233
} ;
199
234
200
235
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
236
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
201
237
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
202
238
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
203
239
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
@@ -207,6 +243,7 @@ describe("defaultProvider", () => {
207
243
208
244
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
209
245
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
246
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
210
247
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
211
248
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
212
249
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
@@ -220,16 +257,41 @@ describe("defaultProvider", () => {
220
257
} ;
221
258
222
259
( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
260
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nope!" ) ) ) ;
223
261
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
224
262
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
225
263
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
226
264
227
265
await expect ( defaultProvider ( ) ( ) ) . resolves ;
228
266
expect ( ( loadSharedConfigFiles as any ) . mock . calls . length ) . toBe ( 1 ) ;
229
267
expect ( ( fromIni as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
268
+ expect ( ( fromSSO as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
230
269
expect ( ( fromProcess as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
231
270
} ) ;
232
271
272
+ it ( "should pass configuration on to the SSO provider" , async ( ) => {
273
+ const ssoConfig : FromSSOInit = {
274
+ profile : "foo" ,
275
+ filepath : "/home/user/.secrets/credentials.ini" ,
276
+ configFilepath : "/home/user/.secrets/credentials.ini" ,
277
+ } ;
278
+
279
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
280
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) =>
281
+ Promise . resolve ( {
282
+ accessKeyId : "foo" ,
283
+ secretAccessKey : "bar" ,
284
+ } )
285
+ ) ;
286
+
287
+ ( fromSSO as any ) . mockClear ( ) ;
288
+
289
+ await expect ( defaultProvider ( ssoConfig ) ( ) ) . resolves ;
290
+
291
+ expect ( ( fromSSO as any ) . mock . calls . length ) . toBe ( 1 ) ;
292
+ expect ( ( fromSSO as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ...ssoConfig , loadedConfig } ) ;
293
+ } ) ;
294
+
233
295
it ( "should pass configuration on to the ini provider" , async ( ) => {
234
296
const iniConfig : FromIniInit = {
235
297
profile : "foo" ,
@@ -387,60 +449,86 @@ describe("defaultProvider", () => {
387
449
388
450
// CF https://github.com/boto/botocore/blob/1.8.32/botocore/credentials.py#L104
389
451
describe ( "explicit profiles" , ( ) => {
390
- it ( "should only consult the ini provider if a profile has been specified " , async ( ) => {
452
+ it ( "should only consult SSO provider if profile has been set " , async ( ) => {
391
453
const creds = {
392
454
accessKeyId : "foo" ,
393
455
secretAccessKey : "bar" ,
394
456
} ;
395
457
396
- ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
397
- ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
398
- ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
399
- ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
458
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
459
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( Promise . resolve ( creds ) ) ) ;
460
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
461
+ ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
462
+ ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
400
463
401
464
expect ( await defaultProvider ( { profile : "foo" } ) ( ) ) . toEqual ( creds ) ;
402
465
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
403
- expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
466
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
467
+ expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
404
468
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
405
469
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
406
470
} ) ;
407
471
408
- it ( "should only consult the ini provider if the profile environment variable has been set" , async ( ) => {
472
+ it ( "should only consult SSO provider if the profile environment variable has been set" , async ( ) => {
409
473
const creds = {
410
474
accessKeyId : "foo" ,
411
475
secretAccessKey : "bar" ,
412
476
} ;
413
477
414
- ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
415
- ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
416
- ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
417
- ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
418
- ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
478
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
479
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
480
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
481
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
482
+ ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
483
+ ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
419
484
420
485
process . env [ ENV_PROFILE ] = "foo" ;
421
486
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
422
487
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
423
- expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
488
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
489
+ expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
424
490
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
425
491
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
426
492
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
427
493
} ) ;
428
494
495
+ it ( "should consult ini provider if no credentials is not found in SSO provider" , async ( ) => {
496
+ const creds = {
497
+ accessKeyId : "foo" ,
498
+ secretAccessKey : "bar" ,
499
+ } ;
500
+
501
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
502
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
503
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( Promise . resolve ( creds ) ) ) ;
504
+ ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
505
+ ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
506
+
507
+ expect ( await defaultProvider ( { profile : "foo" } ) ( ) ) . toEqual ( creds ) ;
508
+ expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
509
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
510
+ expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
511
+ expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
512
+ expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
513
+ } ) ;
514
+
429
515
it ( "should consult the process provider if no credentials are found in the ini provider" , async ( ) => {
430
516
const creds = {
431
517
accessKeyId : "foo" ,
432
518
secretAccessKey : "bar" ,
433
519
} ;
434
520
435
- ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
436
- ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
521
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
522
+ ( fromSSO ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
523
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
437
524
( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
438
- ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
439
- ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new Error ( "PANIC" ) ) ) ;
525
+ ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
526
+ ( fromContainerMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "PANIC" ) ) ) ;
440
527
441
528
process . env [ ENV_PROFILE ] = "foo" ;
442
529
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
443
530
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
531
+ expect ( ( fromSSO ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
444
532
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
445
533
expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
446
534
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
0 commit comments