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,100 @@ 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+ } ;
375+ } ) ;
376+
377+ it ( "should allow region configuration from config file" , async ( ) => {
378+ const provider = fromTemporaryCredentialsNode ( {
379+ params : {
380+ RoleArn,
381+ RoleSessionName,
382+ } ,
383+ masterCredentials,
384+ } ) ;
385+ await provider ( ) ;
386+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
387+ region : "us-west-2" ,
388+ } ) ;
389+ } ) ;
390+
391+ it ( "should allow region configuration from config file non-default profile" , async ( ) => {
392+ // SDK does not use AWS_DEFAULT_PROFILE.
393+ process . env . AWS_PROFILE = "xyz" ;
394+ const provider = fromTemporaryCredentialsNode ( {
395+ params : {
396+ RoleArn,
397+ RoleSessionName,
398+ } ,
399+ masterCredentials,
400+ } ) ;
401+ await provider ( ) ;
402+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
403+ region : "us-west-1" ,
404+ } ) ;
405+ } ) ;
406+
407+ it ( "should allow region configuration from env" , async ( ) => {
408+ // SDK does not use AWS_DEFAULT_REGION.
409+ process . env . AWS_REGION = "ap-southeast-7" ;
410+ const provider = fromTemporaryCredentialsNode ( {
411+ params : {
412+ RoleArn,
413+ RoleSessionName,
414+ } ,
415+ masterCredentials,
416+ } ) ;
417+ await provider ( ) ;
418+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
419+ region : "ap-southeast-7" ,
420+ } ) ;
421+ } ) ;
422+
423+ it ( "should allow region configuration from env overriding region in profile" , async ( ) => {
424+ process . env . AWS_PROFILE = "xyz" ;
425+ process . env . AWS_REGION = "eu-west-1" ;
426+ const provider = fromTemporaryCredentialsNode ( {
427+ params : {
428+ RoleArn,
429+ RoleSessionName,
430+ } ,
431+ masterCredentials,
432+ } ) ;
433+ await provider ( ) ;
434+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
435+ region : "eu-west-1" ,
436+ } ) ;
437+ } ) ;
438+
439+ it ( "should allow region configuration from env overriding region in profile where profile in code overrides env profile" , async ( ) => {
440+ process . env . AWS_PROFILE = "xyz" ;
441+ const provider = fromTemporaryCredentialsNode ( {
442+ params : {
443+ RoleArn,
444+ RoleSessionName,
445+ } ,
446+ masterCredentials,
447+ clientConfig : {
448+ profile : "abc" ,
449+ } ,
450+ } ) ;
451+ await provider ( ) ;
452+ expect ( vi . mocked ( STSClient as any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
453+ region : "eu-central-1" ,
454+ } ) ;
455+ } ) ;
456+ } ) ;
329457} ) ;
0 commit comments