11import { AssumeRoleCommand , STSClient } from "@aws-sdk/nested-clients/sts" ;
2- import { beforeEach , describe , expect , test as it , vi } from "vitest" ;
2+ import { LoadedConfigSelectors } from "@smithy/node-config-provider" ;
3+ import type { ParsedIniData } from "@smithy/types" ;
4+ import { afterEach , beforeEach , describe , expect , test as it , vi } from "vitest" ;
35
46import { fromTemporaryCredentials as fromTemporaryCredentialsNode } from "./fromTemporaryCredentials" ;
57import { fromTemporaryCredentials } from "./fromTemporaryCredentials.base" ;
@@ -25,6 +27,20 @@ vi.mock("@aws-sdk/nested-clients/sts", () => ({
2527 } ) ,
2628} ) ) ;
2729
30+ let iniProfileData : ParsedIniData = null as any ;
31+ vi . mock ( import ( "@smithy/node-config-provider" ) , async ( importOriginal ) => {
32+ return {
33+ ...( await importOriginal ( ) ) ,
34+ loadConfig : ( (
35+ { environmentVariableSelector, configFileSelector, default : defaultValue } : LoadedConfigSelectors < any > ,
36+ { profile = process . env . AWS_PROFILE ?? "default" } : { profile ?: string }
37+ ) => {
38+ return ( ) =>
39+ environmentVariableSelector ( process . env ) ?? configFileSelector ( iniProfileData [ profile ] ?? { } ) ?? defaultValue ( ) ;
40+ } ) as any ,
41+ } ;
42+ } ) ;
43+
2844describe ( "fromTemporaryCredentials" , ( ) => {
2945 const RoleArn = "ROLE_ARN" ;
3046 const RoleSessionName = "ROLE_SESSION_NAME" ;
@@ -33,6 +49,7 @@ describe("fromTemporaryCredentials", () => {
3349 secretAccessKey : "SECRET_ACCESS_KEY" ,
3450 } ;
3551 const region = "US_BAR_1" ;
52+ let processSnapshot : Record < string , any > ;
3653
3754 beforeEach ( ( ) => {
3855 vi . clearAllMocks ( ) ;
@@ -43,6 +60,21 @@ describe("fromTemporaryCredentials", () => {
4360 SessionToken : "SESSION_TOKEN" ,
4461 } ,
4562 } ) ;
63+
64+ processSnapshot = {
65+ ...process . env ,
66+ } ;
67+ process . env = { } ;
68+
69+ iniProfileData = {
70+ default : {
71+ region : "us-west-2" ,
72+ } ,
73+ } ;
74+ } ) ;
75+
76+ afterEach ( ( ) => {
77+ process . env = processSnapshot ;
4678 } ) ;
4779
4880 it ( "should call STS::AssumeRole API with master credentials" , async ( ) => {
@@ -91,7 +123,7 @@ describe("fromTemporaryCredentials", () => {
91123 credentials : masterCredentials ,
92124 logger : void 0 ,
93125 profile : void 0 ,
94- region : "us-east-1" ,
126+ region : "us-west-2" , // profile default
95127 requestHandler : void 0 ,
96128 } ) ;
97129 expect ( mockUsePlugin ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -326,4 +358,121 @@ describe("fromTemporaryCredentials", () => {
326358 expect ( e . tryNextLink ) . toBe ( false ) ;
327359 }
328360 } ) ;
361+
362+ describe ( "env configuration" , ( ) => {
363+ beforeEach ( ( ) => {
364+ iniProfileData = {
365+ default : {
366+ region : "us-west-2" ,
367+ } ,
368+ abc : {
369+ region : "eu-central-1" ,
370+ } ,
371+ xyz : {
372+ region : "us-west-1" ,
373+ } ,
374+ regionless : { } ,
375+ } ;
376+ } ) ;
377+
378+ it ( "should allow region configuration from config file" , async ( ) => {
379+ const provider = fromTemporaryCredentialsNode ( {
380+ params : {
381+ RoleArn,
382+ RoleSessionName,
383+ } ,
384+ masterCredentials,
385+ } ) ;
386+ await provider ( ) ;
387+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
388+ region : "us-west-2" ,
389+ } ) ;
390+ } ) ;
391+
392+ it ( "should allow region configuration from config file non-default profile" , async ( ) => {
393+ // SDK does not use AWS_DEFAULT_PROFILE.
394+ process . env . AWS_PROFILE = "xyz" ;
395+ const provider = fromTemporaryCredentialsNode ( {
396+ params : {
397+ RoleArn,
398+ RoleSessionName,
399+ } ,
400+ masterCredentials,
401+ } ) ;
402+ await provider ( ) ;
403+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
404+ region : "us-west-1" ,
405+ } ) ;
406+ } ) ;
407+
408+ it ( "should allow region configuration from env" , async ( ) => {
409+ // SDK does not use AWS_DEFAULT_REGION.
410+ process . env . AWS_REGION = "ap-southeast-7" ;
411+ const provider = fromTemporaryCredentialsNode ( {
412+ params : {
413+ RoleArn,
414+ RoleSessionName,
415+ } ,
416+ masterCredentials,
417+ } ) ;
418+ await provider ( ) ;
419+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
420+ region : "ap-southeast-7" ,
421+ } ) ;
422+ } ) ;
423+
424+ it ( "should allow region configuration from env overriding region in profile" , async ( ) => {
425+ process . env . AWS_PROFILE = "xyz" ;
426+ process . env . AWS_REGION = "eu-west-1" ;
427+ const provider = fromTemporaryCredentialsNode ( {
428+ params : {
429+ RoleArn,
430+ RoleSessionName,
431+ } ,
432+ masterCredentials,
433+ } ) ;
434+ await provider ( ) ;
435+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
436+ region : "eu-west-1" ,
437+ } ) ;
438+ } ) ;
439+
440+ it ( "should allow region configuration from env overriding region in profile where profile in code overrides env profile" , async ( ) => {
441+ process . env . AWS_PROFILE = "xyz" ;
442+ const provider = fromTemporaryCredentialsNode ( {
443+ params : {
444+ RoleArn,
445+ RoleSessionName,
446+ } ,
447+ masterCredentials,
448+ clientConfig : {
449+ profile : "abc" ,
450+ } ,
451+ } ) ;
452+ await provider ( ) ;
453+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
454+ region : "eu-central-1" ,
455+ } ) ;
456+ } ) ;
457+
458+ it ( "should use us-east-1 if no region is configured anywhere" , async ( ) => {
459+ // no configured region for the provider
460+ // no caller client with region
461+ // no region env
462+ // no region in profile
463+ process . env . AWS_PROFILE = "regionless" ;
464+ process . env . AWS_REGION = undefined ;
465+ const provider = fromTemporaryCredentialsNode ( {
466+ params : {
467+ RoleArn,
468+ RoleSessionName,
469+ } ,
470+ masterCredentials,
471+ } ) ;
472+ await provider ( ) ;
473+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
474+ region : "us-east-1" ,
475+ } ) ;
476+ } ) ;
477+ } ) ;
329478} ) ;
0 commit comments