11import { S3 } from "@aws-sdk/client-s3" ;
22import { ParsedIniData , RuntimeConfigAwsCredentialIdentityProvider } from "@aws-sdk/types" ;
3- import { AttributedAwsCredentialIdentity } from "@aws-sdk/types/src" ;
43import { NodeHttpHandler } from "@smithy/node-http-handler" ;
54import { HttpResponse } from "@smithy/protocol-http" ;
65import { externalDataInterceptor } from "@smithy/shared-ini-file-loader" ;
@@ -11,12 +10,13 @@ import { homedir } from "node:os";
1110import { join } from "node:path" ;
1211import { PassThrough } from "node:stream" ;
1312import { afterAll , afterEach , beforeAll , beforeEach , describe , expect , test as it } from "vitest" ;
13+ import { fromSSO } from "@aws-sdk/credential-providers" ;
1414
1515describe ( "placeholder for testing lib" , ( ) => {
1616 it ( "" , ( ) => { } ) ;
1717} ) ;
1818
19- const assumeRoleArns : string [ ] = [ ] ;
19+ export const assumeRoleArns : string [ ] = [ ] ;
2020let iniProfileData : ParsedIniData = null as any ;
2121
2222export type CredentialTestParameters = {
@@ -38,13 +38,30 @@ export type CredentialTestParameters = {
3838 * Credential provider tester.
3939 */
4040export class CTest < P extends ( init ?: any ) => RuntimeConfigAwsCredentialIdentityProvider > {
41- private lastCredentials : AttributedAwsCredentialIdentity | undefined ;
42-
43- public constructor (
44- public credentialProvider : P ,
45- public providerParams : ( testParams : CredentialTestParameters ) => Parameters < P > [ 0 ] ,
46- public profileCredentials ?: boolean
47- ) {
41+ private readonly credentialProvider : P ;
42+ private readonly providerParams : ( testParams : CredentialTestParameters ) => Parameters < P > [ 0 ] ;
43+ private readonly profileCredentials : boolean ;
44+ private readonly filter : ( testParams : CredentialTestParameters ) => boolean ;
45+ private readonly fallbackRegion : string ;
46+
47+ public constructor ( {
48+ credentialProvider,
49+ providerParams,
50+ profileCredentials,
51+ filter,
52+ fallbackRegion,
53+ } : {
54+ credentialProvider : P ;
55+ providerParams : ( testParams : CredentialTestParameters ) => Parameters < P > [ 0 ] ;
56+ profileCredentials ?: boolean ;
57+ filter ?: ( testParams : CredentialTestParameters ) => boolean ;
58+ fallbackRegion ?: string ;
59+ } ) {
60+ this . credentialProvider = credentialProvider ;
61+ this . providerParams = providerParams ;
62+ this . profileCredentials = ! ! profileCredentials ;
63+ this . filter = filter ?? ( ( ) => true ) ;
64+ this . fallbackRegion = fallbackRegion ?? "unresolved" ;
4865 this . init ( ) ;
4966 }
5067
@@ -57,8 +74,11 @@ export class CTest<P extends (init?: any) => RuntimeConfigAwsCredentialIdentityP
5774 } ;
5875 }
5976 return {
77+ // used by fromIni
6078 profile,
6179 clientConfig : {
80+ // used by e.g. fromTemporaryCredentials that don't have top level profile selection
81+ profile,
6282 region : providerRegion ? "provider-region" : undefined ,
6383 } ,
6484 } ;
@@ -157,6 +177,9 @@ export class CTest<P extends (init?: any) => RuntimeConfigAwsCredentialIdentityP
157177 public testRegion ( ) {
158178 for ( const withCaller of [ true , false ] ) {
159179 for ( const callerClientRegion of [ true , false ] ) {
180+ if ( callerClientRegion && ! withCaller ) {
181+ continue ;
182+ }
160183 for ( const envRegion of [ true , false ] ) {
161184 for ( const profileRegion of [ true , false ] ) {
162185 for ( const providerRegion of [ true , false ] ) {
@@ -174,14 +197,33 @@ export class CTest<P extends (init?: any) => RuntimeConfigAwsCredentialIdentityP
174197 profile,
175198 } ;
176199
200+ if ( ! this . filter ( params ) ) {
201+ continue ;
202+ }
203+
177204 it ( `${ serializeParams ( params ) } ` , async ( ) => {
178- const region = await this . resolveStsRegion ( params ) ;
205+ const region = await this . findCredentialSourceRegion ( params ) . catch ( ( e ) => {
206+ return "failed" ;
207+ } ) ;
208+ const regionRequired = this . fallbackRegion === "unresolved" || withCaller ;
209+ const providerParams = this . providerParams ( params ) ;
210+ const isSso = this . credentialProvider === fromSSO || providerParams . ssoStartUrl ;
211+ const hasRegion = providerRegion || profileRegion || callerClientRegion || envRegion ;
212+
213+ if ( regionRequired && ! hasRegion ) {
214+ expect ( region ) . toBe ( "failed" ) ;
215+ }
179216
180217 if ( providerRegion ) {
181218 expect ( region ) . toBe ( "provider-region" ) ;
182219 return ;
183220 }
184221
222+ if ( isSso ) {
223+ expect ( region ) . toBe ( providerParams . ssoRegion ) ;
224+ return ;
225+ }
226+
185227 const usesProfileCredentials = this . profileCredentials ;
186228
187229 if ( usesProfileCredentials && profileRegion ) {
@@ -204,7 +246,7 @@ export class CTest<P extends (init?: any) => RuntimeConfigAwsCredentialIdentityP
204246 return ;
205247 }
206248
207- expect ( region ) . toBe ( "us-east-1" ) ;
249+ expect ( region ) . toBe ( this . fallbackRegion ) ;
208250 } ) ;
209251 }
210252 }
@@ -214,7 +256,7 @@ export class CTest<P extends (init?: any) => RuntimeConfigAwsCredentialIdentityP
214256 }
215257 }
216258
217- private async resolveStsRegion ( testParams : CredentialTestParameters ) {
259+ private async findCredentialSourceRegion ( testParams : CredentialTestParameters ) {
218260 const { withCaller, envRegion, profile, profileRegion, callerClientRegion, providerRegion } = testParams ;
219261
220262 if ( envRegion ) {
@@ -285,13 +327,13 @@ export class CTest<P extends (init?: any) => RuntimeConfigAwsCredentialIdentityP
285327
286328 await s3 . listBuckets ( { } ) ;
287329 const credentials = await s3 . config . credentials ( ) ;
288- return credentials . sessionToken ! . replace ( "STS_AR_SESSION_TOKEN_" , "" ) ;
330+ return credentials . sessionToken ! . replace ( / ( . * ? ) S E S S I O N _ T O K E N _ / , "" ) ;
289331 }
290332
291333 const provider = this . credentialProvider ( this . providerParams ( testParams ) ) ;
292334
293335 const credentials = await provider ( ) ;
294- return credentials . sessionToken ! . replace ( "STS_AR_SESSION_TOKEN_" , "" ) ;
336+ return credentials . sessionToken ! . replace ( / ( . * ? ) S E S S I O N _ T O K E N _ / , "" ) ;
295337 }
296338}
297339
0 commit comments