11import { STS } from "@aws-sdk/client-sts" ;
22import { HttpRequest , HttpResponse } from "@smithy/protocol-http" ;
3- import { SourceProfileInit } from "@smithy/shared-ini-file-loader" ;
3+ import { externalDataInterceptor } from "@smithy/shared-ini-file-loader" ;
44import type { NodeHttpHandlerOptions , ParsedIniData } from "@smithy/types" ;
5+ import { homedir } from "node:os" ;
6+ import { join } from "node:path" ;
57import { PassThrough } from "node:stream" ;
6- import { afterEach , beforeEach , describe , expect , test as it , vi } from "vitest" ;
8+ import { afterEach , beforeEach , describe , expect , test as it } from "vitest" ;
79
810import { fromIni } from "./fromIni" ;
911
1012let iniProfileData : ParsedIniData = null as any ;
11- vi . mock ( "@smithy/shared-ini-file-loader" , async ( ) => {
12- const actual : any = await vi . importActual ( "@smithy/shared-ini-file-loader" ) ;
13- const pkg = {
14- ...actual ,
15- async loadSsoSessionData ( ) {
16- return Object . entries ( iniProfileData )
17- . filter ( ( [ key ] ) => key . startsWith ( "sso-session." ) )
18- . reduce (
19- ( acc , [ key , value ] ) => ( {
20- ...acc ,
21- [ key . split ( "sso-session." ) [ 1 ] ] : value ,
22- } ) ,
23- { }
24- ) ;
25- } ,
26- async parseKnownFiles ( init : SourceProfileInit ) : Promise < ParsedIniData > {
27- return iniProfileData ;
28- } ,
29- async getSSOTokenFromFile ( ) {
30- return {
31- accessToken : "mock_sso_token" ,
32- expiresAt : "3000-01-01T00:00:00.000Z" ,
33- } ;
34- } ,
35- } ;
36- return {
37- ...pkg ,
38- default : pkg ,
39- } ;
40- } ) ;
13+
14+ function setIniProfileData ( data : ParsedIniData ) {
15+ iniProfileData = data ;
16+ let buffer = "" ;
17+ for ( const profile in data ) {
18+ if ( profile . startsWith ( "sso-session." ) ) {
19+ buffer += `[sso-session ${ profile . split ( "sso-session." ) [ 1 ] } ]\n` ;
20+ } else {
21+ buffer += `[profile ${ profile } ]\n` ;
22+ }
23+ for ( const [ k , v ] of Object . entries ( data [ profile ] ) ) {
24+ buffer += `${ k } = ${ v } \n` ;
25+ }
26+ buffer += "\n" ;
27+ }
28+ const dir = join ( homedir ( ) , ".aws" ) ;
29+ externalDataInterceptor . interceptFile ( join ( dir , "config" ) , buffer ) ;
30+ }
4131
4232class MockNodeHttpHandler {
4333 static create ( instanceOrOptions ?: any ) {
@@ -136,22 +126,20 @@ describe("fromIni region search order", () => {
136126 process . env . AWS_PROFILE = "default" ;
137127 iniProfileData = {
138128 default : {
139- region : "us-west-2" ,
140129 output : "json" ,
130+ region : "us-stsar-1" ,
131+ role_arn : "ROLE_ARN" ,
132+ role_session_name : "ROLE_SESSION_NAME" ,
133+ external_id : "EXTERNAL_ID" ,
134+ source_profile : "assume" ,
135+ } ,
136+ assume : {
137+ region : "us-stsar-1" ,
138+ aws_access_key_id : "ASSUME_STATIC_ACCESS_KEY" ,
139+ aws_secret_access_key : "ASSUME_STATIC_SECRET_KEY" ,
141140 } ,
142141 } ;
143- iniProfileData . assume = {
144- region : "us-stsar-1" ,
145- aws_access_key_id : "ASSUME_STATIC_ACCESS_KEY" ,
146- aws_secret_access_key : "ASSUME_STATIC_SECRET_KEY" ,
147- } ;
148- Object . assign ( iniProfileData . default , {
149- region : "us-stsar-1" ,
150- role_arn : "ROLE_ARN" ,
151- role_session_name : "ROLE_SESSION_NAME" ,
152- external_id : "EXTERNAL_ID" ,
153- source_profile : "assume" ,
154- } ) ;
142+ setIniProfileData ( iniProfileData ) ;
155143 } ) ;
156144
157145 afterEach ( ( ) => {
@@ -201,6 +189,7 @@ describe("fromIni region search order", () => {
201189
202190 it ( "should use 3rd priority for the caller client" , async ( ) => {
203191 delete iniProfileData . default . region ;
192+ setIniProfileData ( iniProfileData ) ;
204193
205194 const sts = new STS ( {
206195 requestHandler : new MockNodeHttpHandler ( ) ,
@@ -221,8 +210,78 @@ describe("fromIni region search order", () => {
221210 } ) ;
222211 } ) ;
223212
224- it ( "should use 4th priority for the default partition's default region" , async ( ) => {
225- delete iniProfileData . default . region ;
213+ it ( "should use 4th priority for the config file region" , async ( ) => {
214+ const credentialsData = await fromIni ( {
215+ clientConfig : {
216+ requestHandler : new MockNodeHttpHandler ( ) ,
217+ } ,
218+ } ) ( ) ;
219+
220+ const sts = new STS ( {
221+ requestHandler : new MockNodeHttpHandler ( ) ,
222+ credentials : credentialsData ,
223+ } ) ;
224+
225+ await sts . getCallerIdentity ( { } ) ;
226+ const credentials = await sts . config . credentials ( ) ;
227+ expect ( credentials ) . toMatchObject ( {
228+ accessKeyId : "STS_AR_ACCESS_KEY_ID" ,
229+ secretAccessKey : "STS_AR_SECRET_ACCESS_KEY" ,
230+ sessionToken : "STS_AR_SESSION_TOKEN_us-stsar-1" ,
231+ } ) ;
232+ } ) ;
233+
234+ it ( "should use 5th priority for the AWS_REGION value" , async ( ) => {
235+ process . env . AWS_REGION = "ap-northeast-1" ;
236+ iniProfileData = {
237+ default : {
238+ role_arn : "ROLE_ARN" ,
239+ role_session_name : "ROLE_SESSION_NAME" ,
240+ external_id : "EXTERNAL_ID" ,
241+ source_profile : "assume" ,
242+ } ,
243+ assume : {
244+ aws_access_key_id : "ASSUME_STATIC_ACCESS_KEY" ,
245+ aws_secret_access_key : "ASSUME_STATIC_SECRET_KEY" ,
246+ } ,
247+ } ;
248+ setIniProfileData ( iniProfileData ) ;
249+
250+ const credentialsData = await fromIni ( {
251+ clientConfig : {
252+ requestHandler : new MockNodeHttpHandler ( ) ,
253+ } ,
254+ } ) ( ) ;
255+
256+ const sts = new STS ( {
257+ requestHandler : new MockNodeHttpHandler ( ) ,
258+ credentials : credentialsData ,
259+ } ) ;
260+
261+ await sts . getCallerIdentity ( { } ) ;
262+ const credentials = await sts . config . credentials ( ) ;
263+ expect ( credentials ) . toMatchObject ( {
264+ accessKeyId : "STS_AR_ACCESS_KEY_ID" ,
265+ secretAccessKey : "STS_AR_SECRET_ACCESS_KEY" ,
266+ sessionToken : "STS_AR_SESSION_TOKEN_ap-northeast-1" ,
267+ } ) ;
268+ } ) ;
269+
270+ it ( "should use 6th priority for the default partition's default region" , async ( ) => {
271+ delete process . env . AWS_REGION ;
272+ iniProfileData = {
273+ default : {
274+ role_arn : "ROLE_ARN" ,
275+ role_session_name : "ROLE_SESSION_NAME" ,
276+ external_id : "EXTERNAL_ID" ,
277+ source_profile : "assume" ,
278+ } ,
279+ assume : {
280+ aws_access_key_id : "ASSUME_STATIC_ACCESS_KEY" ,
281+ aws_secret_access_key : "ASSUME_STATIC_SECRET_KEY" ,
282+ } ,
283+ } ;
284+ setIniProfileData ( iniProfileData ) ;
226285
227286 const credentialsData = await fromIni ( {
228287 clientConfig : {
0 commit comments