@@ -3,8 +3,7 @@ import { CredentialsProviderError } from "@smithy/property-provider";
3
3
import { afterEach , beforeEach , describe , expect , test as it , vi } from "vitest" ;
4
4
5
5
import { InstanceMetadataV1FallbackError } from "./error/InstanceMetadataV1FallbackError" ;
6
- import { checkIfImdsDisabled , fromInstanceMetadata } from "./fromInstanceMetadata" ;
7
- import * as fromInstanceMetadataModule from "./fromInstanceMetadata" ;
6
+ import { fromInstanceMetadata , getConfiguredProfileName , getImdsProfile , throwIfImdsTurnedOff } from "./fromInstanceMetadata" ;
8
7
import { httpRequest } from "./remoteProvider/httpRequest" ;
9
8
import { fromImdsCredentials , isImdsCredentials } from "./remoteProvider/ImdsCredentials" ;
10
9
import { providerConfigFromInit } from "./remoteProvider/RemoteProviderInit" ;
@@ -67,7 +66,7 @@ describe("fromInstanceMetadata", () => {
67
66
vi . mocked ( staticStabilityProvider ) . mockImplementation ( ( input ) => input ) ;
68
67
vi . mocked ( getInstanceMetadataEndpoint ) . mockResolvedValue ( { hostname } as any ) ;
69
68
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( false ) ) ;
70
- vi . spyOn ( fromInstanceMetadataModule , "checkIfImdsDisabled " ) . mockResolvedValue ( undefined ) ;
69
+ vi . spyOn ( { throwIfImdsTurnedOff } , "throwIfImdsTurnedOff " ) . mockResolvedValue ( undefined ) ;
71
70
( isImdsCredentials as unknown as any ) . mockReturnValue ( true ) ;
72
71
vi . mocked ( providerConfigFromInit ) . mockReturnValue ( {
73
72
timeout : mockTimeout ,
@@ -80,8 +79,16 @@ describe("fromInstanceMetadata", () => {
80
79
} ) ;
81
80
82
81
it ( "returns no credentials when AWS_EC2_METADATA_DISABLED=true" , async ( ) => {
83
- vi . mocked ( loadConfig ) . mockReturnValueOnce ( ( ) => Promise . resolve ( true ) ) ;
84
- vi . mocked ( fromInstanceMetadataModule . checkIfImdsDisabled ) . mockRejectedValueOnce (
82
+ vi . mocked ( loadConfig ) . mockImplementation ( ( { environmentVariableSelector, configFileSelector } ) => {
83
+ if ( environmentVariableSelector && environmentVariableSelector ( { AWS_EC2_METADATA_DISABLED : "true" } ) ) {
84
+ return ( ) => Promise . resolve ( true ) ;
85
+ }
86
+ if ( configFileSelector && configFileSelector ( { imds_disabled : "true" } ) ) {
87
+ return ( ) => Promise . resolve ( true ) ;
88
+ }
89
+ return ( ) => Promise . resolve ( false ) ;
90
+ } ) ;
91
+ vi . spyOn ( { throwIfImdsTurnedOff} , "throwIfImdsTurnedOff" ) . mockRejectedValueOnce (
85
92
new CredentialsProviderError ( "IMDS credential fetching is disabled" )
86
93
) ;
87
94
const provider = fromInstanceMetadata ( { } ) ;
@@ -100,10 +107,10 @@ describe("fromInstanceMetadata", () => {
100
107
vi . mocked ( retry ) . mockImplementation ( ( fn : any ) => fn ( ) ) ;
101
108
vi . mocked ( fromImdsCredentials ) . mockReturnValue ( mockCreds ) ;
102
109
103
- const result = await fromInstanceMetadata ( { ec2InstanceProfileName : profileName } ) ( ) ;
110
+ const credentials = await fromInstanceMetadata ( { ec2InstanceProfileName : profileName } ) ( ) ;
104
111
105
- expect ( result ) . toEqual ( mockCreds ) ;
106
- expect ( result . accountId ) . toBe ( mockCreds . accountId ) ;
112
+ expect ( credentials ) . toEqual ( mockCreds ) ;
113
+ expect ( credentials . accountId ) . toBe ( mockCreds . accountId ) ;
107
114
108
115
expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
109
116
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , mockTokenRequestOptions ) ;
@@ -124,10 +131,10 @@ describe("fromInstanceMetadata", () => {
124
131
125
132
const provider = fromInstanceMetadata ( { } ) ;
126
133
127
- const result = await provider ( ) ;
134
+ const credentials = await provider ( ) ;
128
135
129
- expect ( result ) . toEqual ( mockCreds ) ;
130
- expect ( result . accountId ) . toBe ( mockCreds . accountId ) ;
136
+ expect ( credentials ) . toEqual ( mockCreds ) ;
137
+ expect ( credentials . accountId ) . toBe ( mockCreds . accountId ) ;
131
138
132
139
expect ( httpRequest ) . toHaveBeenCalledTimes ( 3 ) ;
133
140
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , mockTokenRequestOptions ) ;
@@ -165,7 +172,7 @@ describe("fromInstanceMetadata", () => {
165
172
166
173
vi . mocked ( retry ) . mockImplementation ( ( fn : any ) => fn ( ) ) ;
167
174
vi . mocked ( fromImdsCredentials ) . mockReturnValue ( mockCreds ) ;
168
- vi . mocked ( checkIfImdsDisabled ) . mockReturnValueOnce ( Promise . resolve ( ) ) ;
175
+ vi . spyOn ( { throwIfImdsTurnedOff } , "throwIfImdsTurnedOff" ) . mockResolvedValue ( ) ;
169
176
170
177
await expect ( fromInstanceMetadata ( ) ( ) ) . resolves . toEqual ( mockCreds ) ;
171
178
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 3 , {
@@ -282,7 +289,7 @@ describe("fromInstanceMetadata", () => {
282
289
expect ( vi . mocked ( staticStabilityProvider ) ) . toBeCalledTimes ( 1 ) ;
283
290
} ) ;
284
291
285
- describe ( "getImdsProfileHelper " , ( ) => {
292
+ describe ( "getImdsProfile " , ( ) => {
286
293
beforeEach ( ( ) => {
287
294
vi . mocked ( httpRequest ) . mockClear ( ) ;
288
295
vi . mocked ( loadConfig ) . mockClear ( ) ;
@@ -291,29 +298,27 @@ describe("fromInstanceMetadata", () => {
291
298
292
299
it ( "uses ec2InstanceProfileName from init if provided" , async ( ) => {
293
300
const profileName = "profile-from-init" ;
294
- const options = { hostname } as any ;
301
+ const options = { hostname } ;
295
302
296
- // Only use vi.spyOn for imported functions
297
- vi . spyOn ( fromInstanceMetadataModule , "getConfiguredProfileName" ) . mockResolvedValueOnce ( profileName ) ;
303
+ vi . spyOn ( { getConfiguredProfileName} , "getConfiguredProfileName" ) . mockResolvedValueOnce ( profileName ) ;
298
304
299
- const result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , {
305
+ const credentials = await getImdsProfile ( options , mockMaxRetries , {
300
306
ec2InstanceProfileName : profileName ,
301
307
} ) ;
302
308
303
- expect ( result ) . toBe ( profileName ) ;
309
+ expect ( credentials ) . toBe ( profileName ) ;
304
310
expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
305
311
} ) ;
306
312
307
313
it ( "uses environment variable if ec2InstanceProfileName not provided" , async ( ) => {
308
314
const envProfileName = "profile-from-env" ;
309
315
const options = { hostname } as any ;
310
316
311
- // Mock loadConfig to simulate env variable present
312
317
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( envProfileName ) ) ;
313
318
314
- const result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , { } ) ;
319
+ const credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
315
320
316
- expect ( result ) . toBe ( envProfileName ) ;
321
+ expect ( credentials ) . toBe ( envProfileName ) ;
317
322
expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
318
323
} ) ;
319
324
@@ -322,21 +327,19 @@ describe("fromInstanceMetadata", () => {
322
327
const legacyProfileName = "profile-from-legacy" ;
323
328
const options = { hostname } as any ;
324
329
325
- // 1. Simulate config file present: should return configProfileName, no IMDS call
326
330
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( configProfileName ) ) ;
327
331
328
- let result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , { } ) ;
329
- expect ( result ) . toBe ( configProfileName ) ;
332
+ let credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
333
+ expect ( credentials ) . toBe ( configProfileName ) ;
330
334
expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
331
335
332
- // 2. Simulate config file missing: should call IMDS (extended fails, legacy succeeds)
333
336
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( null ) ) ;
334
337
vi . mocked ( httpRequest )
335
338
. mockRejectedValueOnce ( Object . assign ( new Error ( ) , { statusCode : 404 } ) )
336
339
. mockResolvedValueOnce ( legacyProfileName as any ) ;
337
340
338
- result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , { } ) ;
339
- expect ( result ) . toBe ( legacyProfileName ) ;
341
+ credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
342
+ expect ( credentials ) . toBe ( legacyProfileName ) ;
340
343
expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
341
344
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , {
342
345
...options ,
0 commit comments