@@ -21,11 +21,24 @@ import {
21
21
fromIni ,
22
22
FromIniInit
23
23
} from "@aws-sdk/credential-provider-ini" ;
24
+
24
25
import {
25
26
ENV_CONFIG_PATH ,
26
27
ENV_CREDENTIALS_PATH
27
28
} from "@aws-sdk/shared-ini-file-loader" ;
28
29
30
+ jest . mock ( "@aws-sdk/credential-provider-process" , ( ) => {
31
+ const processProvider = jest . fn ( ) ;
32
+ return {
33
+ ENV_PROFILE : "AWS_PROFILE" ,
34
+ fromProcess : jest . fn ( ( ) => processProvider )
35
+ } ;
36
+ } ) ;
37
+ import {
38
+ fromProcess ,
39
+ FromProcessInit
40
+ } from "@aws-sdk/credential-provider-process" ;
41
+
29
42
jest . mock ( "@aws-sdk/credential-provider-imds" , ( ) => {
30
43
const containerMdsProvider = jest . fn ( ) ;
31
44
const instanceMdsProvider = jest . fn ( ) ;
@@ -67,10 +80,12 @@ beforeEach(() => {
67
80
68
81
( fromEnv ( ) as any ) . mockClear ( ) ;
69
82
( fromIni ( ) as any ) . mockClear ( ) ;
83
+ ( fromProcess ( ) as any ) . mockClear ( ) ;
70
84
( fromContainerMetadata ( ) as any ) . mockClear ( ) ;
71
85
( fromInstanceMetadata ( ) as any ) . mockClear ( ) ;
72
86
( fromEnv as any ) . mockClear ( ) ;
73
87
( fromIni as any ) . mockClear ( ) ;
88
+ ( fromProcess as any ) . mockClear ( ) ;
74
89
( fromContainerMetadata as any ) . mockClear ( ) ;
75
90
( fromInstanceMetadata as any ) . mockClear ( ) ;
76
91
} ) ;
@@ -97,6 +112,7 @@ describe("defaultProvider", () => {
97
112
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
98
113
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
99
114
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
115
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
100
116
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
101
117
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
102
118
} ) ;
@@ -115,29 +131,55 @@ describe("defaultProvider", () => {
115
131
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
116
132
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
117
133
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
134
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
118
135
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
119
136
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
120
137
} ) ;
121
138
122
- it ( "should continue on to the IMDS provider if no env or ini credentials have been found" , async ( ) => {
139
+ it ( "should stop after the process provider if credentials have been found" , async ( ) => {
123
140
const creds = {
124
141
accessKeyId : "foo" ,
125
142
secretAccessKey : "bar"
126
143
} ;
127
144
145
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) =>
146
+ Promise . reject ( new ProviderError ( "Nothing here!" ) )
147
+ ) ;
148
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) =>
149
+ Promise . reject ( new ProviderError ( "Nothing here!" ) )
150
+ ) ;
151
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
152
+
153
+ expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
154
+ expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
155
+ expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
156
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
157
+ expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
158
+ expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
159
+ } ) ;
160
+
161
+ it ( "should continue on to the IMDS provider if no env, ini or process credentials have been found" , async ( ) => {
162
+ const creds = {
163
+ accessKeyId : "foo" ,
164
+ secretAccessKey : "bar"
165
+ } ;
128
166
( fromEnv ( ) as any ) . mockImplementation ( ( ) =>
129
167
Promise . reject ( new ProviderError ( "Keep moving!" ) )
130
168
) ;
131
169
( fromIni ( ) as any ) . mockImplementation ( ( ) =>
132
170
Promise . reject ( new ProviderError ( "Nothing here!" ) )
133
171
) ;
172
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) =>
173
+ Promise . reject ( new ProviderError ( "Nor here!" ) )
174
+ ) ;
134
175
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) =>
135
176
Promise . resolve ( creds )
136
177
) ;
137
178
138
179
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
139
180
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
140
181
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
182
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
141
183
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
142
184
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
143
185
} ) ;
@@ -154,6 +196,9 @@ describe("defaultProvider", () => {
154
196
( fromIni ( ) as any ) . mockImplementation ( ( ) =>
155
197
Promise . reject ( new ProviderError ( "Nothing here!" ) )
156
198
) ;
199
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) =>
200
+ Promise . reject ( new ProviderError ( "Nor here!" ) )
201
+ ) ;
157
202
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) =>
158
203
Promise . resolve ( creds )
159
204
) ;
@@ -177,6 +222,9 @@ describe("defaultProvider", () => {
177
222
( fromIni ( ) as any ) . mockImplementation ( ( ) =>
178
223
Promise . reject ( new ProviderError ( "Nothing here!" ) )
179
224
) ;
225
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) =>
226
+ Promise . reject ( new ProviderError ( "Nor here!" ) )
227
+ ) ;
180
228
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) =>
181
229
Promise . reject ( new Error ( "PANIC" ) )
182
230
) ;
@@ -189,6 +237,7 @@ describe("defaultProvider", () => {
189
237
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
190
238
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
191
239
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
240
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
192
241
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
193
242
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
194
243
} ) ;
@@ -224,6 +273,33 @@ describe("defaultProvider", () => {
224
273
expect ( ( fromIni as any ) . mock . calls [ 0 ] [ 0 ] ) . toBe ( iniConfig ) ;
225
274
} ) ;
226
275
276
+ it ( "should pass configuration on to the process provider" , async ( ) => {
277
+ const processConfig : FromProcessInit = {
278
+ filepath : "/home/user/.secrets/credentials.ini" ,
279
+ configFilepath : "/home/user/.secrets/credentials.ini"
280
+ } ;
281
+
282
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) =>
283
+ Promise . reject ( new ProviderError ( "Keep moving!" ) )
284
+ ) ;
285
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) =>
286
+ Promise . reject ( new ProviderError ( "Nothing here!" ) )
287
+ ) ;
288
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) =>
289
+ Promise . resolve ( {
290
+ accessKeyId : "foo" ,
291
+ secretAccessKey : "bar"
292
+ } )
293
+ ) ;
294
+
295
+ ( fromProcess as any ) . mockClear ( ) ;
296
+
297
+ await expect ( defaultProvider ( processConfig ) ( ) ) . resolves ;
298
+ expect ( ( fromProcess as any ) . mock . calls . length ) . toBe ( 1 ) ;
299
+ expect ( ( fromProcess as any ) . mock . calls . length ) . toBe ( 1 ) ;
300
+ expect ( ( fromProcess as any ) . mock . calls [ 0 ] [ 0 ] ) . toBe ( processConfig ) ;
301
+ } ) ;
302
+
227
303
it ( "should pass configuration on to the IMDS provider" , async ( ) => {
228
304
const imdsConfig : RemoteProviderInit = {
229
305
timeout : 2000 ,
@@ -236,6 +312,9 @@ describe("defaultProvider", () => {
236
312
( fromIni ( ) as any ) . mockImplementation ( ( ) =>
237
313
Promise . reject ( new ProviderError ( "Nothing here!" ) )
238
314
) ;
315
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) =>
316
+ Promise . reject ( new ProviderError ( "Nor here!" ) )
317
+ ) ;
239
318
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) =>
240
319
Promise . resolve ( {
241
320
accessKeyId : "foo" ,
@@ -370,6 +449,9 @@ describe("defaultProvider", () => {
370
449
Promise . reject ( new Error ( "PANIC" ) )
371
450
) ;
372
451
( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
452
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) =>
453
+ Promise . reject ( new Error ( "PANIC" ) )
454
+ ) ;
373
455
( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) =>
374
456
Promise . reject ( new Error ( "PANIC" ) )
375
457
) ;
@@ -381,6 +463,7 @@ describe("defaultProvider", () => {
381
463
expect ( await defaultProvider ( ) ( ) ) . toEqual ( creds ) ;
382
464
expect ( ( fromEnv ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
383
465
expect ( ( fromIni ( ) as any ) . mock . calls . length ) . toBe ( 1 ) ;
466
+ expect ( ( fromProcess ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
384
467
expect ( ( fromContainerMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
385
468
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
386
469
} ) ;
0 commit comments