@@ -3,8 +3,7 @@ import { CredentialsProviderError } from "@smithy/property-provider";
33import { afterEach , beforeEach , describe , expect , test as it , vi } from "vitest" ;
44
55import { InstanceMetadataV1FallbackError } from "./error/InstanceMetadataV1FallbackError" ;
6- import { checkIfImdsDisabled , fromInstanceMetadata } from "./fromInstanceMetadata" ;
7- import * as fromInstanceMetadataModule from "./fromInstanceMetadata" ;
6+ import { fromInstanceMetadata , getConfiguredProfileName , getImdsProfile , throwIfImdsTurnedOff } from "./fromInstanceMetadata" ;
87import { httpRequest } from "./remoteProvider/httpRequest" ;
98import { fromImdsCredentials , isImdsCredentials } from "./remoteProvider/ImdsCredentials" ;
109import { providerConfigFromInit } from "./remoteProvider/RemoteProviderInit" ;
@@ -67,7 +66,7 @@ describe("fromInstanceMetadata", () => {
6766 vi . mocked ( staticStabilityProvider ) . mockImplementation ( ( input ) => input ) ;
6867 vi . mocked ( getInstanceMetadataEndpoint ) . mockResolvedValue ( { hostname } as any ) ;
6968 vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( false ) ) ;
70- vi . spyOn ( fromInstanceMetadataModule , "checkIfImdsDisabled " ) . mockResolvedValue ( undefined ) ;
69+ vi . spyOn ( { throwIfImdsTurnedOff } , "throwIfImdsTurnedOff " ) . mockResolvedValue ( undefined ) ;
7170 ( isImdsCredentials as unknown as any ) . mockReturnValue ( true ) ;
7271 vi . mocked ( providerConfigFromInit ) . mockReturnValue ( {
7372 timeout : mockTimeout ,
@@ -80,8 +79,16 @@ describe("fromInstanceMetadata", () => {
8079 } ) ;
8180
8281 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 (
8592 new CredentialsProviderError ( "IMDS credential fetching is disabled" )
8693 ) ;
8794 const provider = fromInstanceMetadata ( { } ) ;
@@ -100,10 +107,10 @@ describe("fromInstanceMetadata", () => {
100107 vi . mocked ( retry ) . mockImplementation ( ( fn : any ) => fn ( ) ) ;
101108 vi . mocked ( fromImdsCredentials ) . mockReturnValue ( mockCreds ) ;
102109
103- const result = await fromInstanceMetadata ( { ec2InstanceProfileName : profileName } ) ( ) ;
110+ const credentials = await fromInstanceMetadata ( { ec2InstanceProfileName : profileName } ) ( ) ;
104111
105- expect ( result ) . toEqual ( mockCreds ) ;
106- expect ( result . accountId ) . toBe ( mockCreds . accountId ) ;
112+ expect ( credentials ) . toEqual ( mockCreds ) ;
113+ expect ( credentials . accountId ) . toBe ( mockCreds . accountId ) ;
107114
108115 expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
109116 expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , mockTokenRequestOptions ) ;
@@ -124,10 +131,10 @@ describe("fromInstanceMetadata", () => {
124131
125132 const provider = fromInstanceMetadata ( { } ) ;
126133
127- const result = await provider ( ) ;
134+ const credentials = await provider ( ) ;
128135
129- expect ( result ) . toEqual ( mockCreds ) ;
130- expect ( result . accountId ) . toBe ( mockCreds . accountId ) ;
136+ expect ( credentials ) . toEqual ( mockCreds ) ;
137+ expect ( credentials . accountId ) . toBe ( mockCreds . accountId ) ;
131138
132139 expect ( httpRequest ) . toHaveBeenCalledTimes ( 3 ) ;
133140 expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , mockTokenRequestOptions ) ;
@@ -165,7 +172,7 @@ describe("fromInstanceMetadata", () => {
165172
166173 vi . mocked ( retry ) . mockImplementation ( ( fn : any ) => fn ( ) ) ;
167174 vi . mocked ( fromImdsCredentials ) . mockReturnValue ( mockCreds ) ;
168- vi . mocked ( checkIfImdsDisabled ) . mockReturnValueOnce ( Promise . resolve ( ) ) ;
175+ vi . spyOn ( { throwIfImdsTurnedOff } , "throwIfImdsTurnedOff" ) . mockResolvedValue ( ) ;
169176
170177 await expect ( fromInstanceMetadata ( ) ( ) ) . resolves . toEqual ( mockCreds ) ;
171178 expect ( httpRequest ) . toHaveBeenNthCalledWith ( 3 , {
@@ -282,7 +289,7 @@ describe("fromInstanceMetadata", () => {
282289 expect ( vi . mocked ( staticStabilityProvider ) ) . toBeCalledTimes ( 1 ) ;
283290 } ) ;
284291
285- describe ( "getImdsProfileHelper " , ( ) => {
292+ describe ( "getImdsProfile " , ( ) => {
286293 beforeEach ( ( ) => {
287294 vi . mocked ( httpRequest ) . mockClear ( ) ;
288295 vi . mocked ( loadConfig ) . mockClear ( ) ;
@@ -291,29 +298,27 @@ describe("fromInstanceMetadata", () => {
291298
292299 it ( "uses ec2InstanceProfileName from init if provided" , async ( ) => {
293300 const profileName = "profile-from-init" ;
294- const options = { hostname } as any ;
301+ const options = { hostname } ;
295302
296- // Only use vi.spyOn for imported functions
297- vi . spyOn ( fromInstanceMetadataModule , "getConfiguredProfileName" ) . mockResolvedValueOnce ( profileName ) ;
303+ vi . spyOn ( { getConfiguredProfileName} , "getConfiguredProfileName" ) . mockResolvedValueOnce ( profileName ) ;
298304
299- const result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , {
305+ const credentials = await getImdsProfile ( options , mockMaxRetries , {
300306 ec2InstanceProfileName : profileName ,
301307 } ) ;
302308
303- expect ( result ) . toBe ( profileName ) ;
309+ expect ( credentials ) . toBe ( profileName ) ;
304310 expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
305311 } ) ;
306312
307313 it ( "uses environment variable if ec2InstanceProfileName not provided" , async ( ) => {
308314 const envProfileName = "profile-from-env" ;
309315 const options = { hostname } as any ;
310316
311- // Mock loadConfig to simulate env variable present
312317 vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( envProfileName ) ) ;
313318
314- const result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , { } ) ;
319+ const credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
315320
316- expect ( result ) . toBe ( envProfileName ) ;
321+ expect ( credentials ) . toBe ( envProfileName ) ;
317322 expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
318323 } ) ;
319324
@@ -322,21 +327,19 @@ describe("fromInstanceMetadata", () => {
322327 const legacyProfileName = "profile-from-legacy" ;
323328 const options = { hostname } as any ;
324329
325- // 1. Simulate config file present: should return configProfileName, no IMDS call
326330 vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( configProfileName ) ) ;
327331
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 ) ;
330334 expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
331335
332- // 2. Simulate config file missing: should call IMDS (extended fails, legacy succeeds)
333336 vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( null ) ) ;
334337 vi . mocked ( httpRequest )
335338 . mockRejectedValueOnce ( Object . assign ( new Error ( ) , { statusCode : 404 } ) )
336339 . mockResolvedValueOnce ( legacyProfileName as any ) ;
337340
338- result = await fromInstanceMetadataModule . getImdsProfileHelper ( options , mockMaxRetries , { } ) ;
339- expect ( result ) . toBe ( legacyProfileName ) ;
341+ credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
342+ expect ( credentials ) . toBe ( legacyProfileName ) ;
340343 expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
341344 expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , {
342345 ...options ,
0 commit comments