@@ -110,8 +110,8 @@ const getInstanceMetadataProvider = (init: RemoteProviderInit = {}) => {
110110 } ;
111111
112112 return async ( ) => {
113- const endpoint = await getInstanceMetadataEndpoint ( ) ;
114113 await throwIfImdsTurnedOff ( profile , logger ) ;
114+ const endpoint = await getInstanceMetadataEndpoint ( ) ;
115115 if ( disableFetchToken ) {
116116 logger ?. debug ( "AWS SDK Instance Metadata" , "using v1 fallback (no token fetch)" ) ;
117117 return getCredentials ( maxRetries , { ...endpoint , timeout } ) ;
@@ -149,17 +149,11 @@ export const getImdsProfile = async (
149149 options : RequestOptions ,
150150 maxRetries : number ,
151151 init : RemoteProviderInit = { } ,
152- profile ?: string ,
153- resetCache ?: boolean
152+ profile ?: string
154153) : Promise < string > => {
155154 let apiVersion : "unknown" | "extended" | "legacy" = "unknown" ;
156155 let resolvedProfile : string | null = null ;
157156
158- // If resetCache is true, clear the cached profile name
159- if ( resetCache ) {
160- resolvedProfile = null ;
161- }
162-
163157 return retry < string > ( async ( ) => {
164158 // First check if a profile name is configured
165159 const configuredName = await getConfiguredProfileName ( init , profile ) ;
@@ -201,8 +195,8 @@ export const getMetadataToken = async (options: RequestOptions) =>
201195 } ) ;
202196
203197/**
204- * @internal
205198 * Checks if IMDS credential fetching is disabled through configuration
199+ * @internal
206200 */
207201export const throwIfImdsTurnedOff = async ( profile ?: string , logger ?: any ) : Promise < void > => {
208202 // Load configuration in priority order
@@ -236,8 +230,8 @@ export const throwIfImdsTurnedOff = async (profile?: string, logger?: any): Prom
236230} ;
237231
238232/**
239- * @internal
240233 * Gets configured profile name from various sources
234+ * @internal
241235 */
242236export const getConfiguredProfileName = async ( init : RemoteProviderInit , profile ?: string ) : Promise < string | null > => {
243237 // Load configuration in priority order
@@ -264,8 +258,8 @@ export const getConfiguredProfileName = async (init: RemoteProviderInit, profile
264258} ;
265259
266260/**
267- * @internal
268261 * Gets credentials from profile
262+ * @internal
269263 */
270264const getCredentialsFromProfile = async ( profile : string , options : RequestOptions , init : RemoteProviderInit ) => {
271265 // Try extended API first
@@ -280,7 +274,7 @@ const getCredentialsFromProfile = async (profile: string, options: RequestOption
280274 if ( legacyError . statusCode === 404 && init . ec2InstanceProfileName === undefined ) {
281275 // If legacy API also returns 404 and we're using a cached profile name,
282276 // the profile might have changed - clear cache and retry
283- const newProfileName = await getImdsProfile ( options , init . maxRetries ?? 3 , init , profile , true ) ;
277+ const newProfileName = await getImdsProfile ( options , init . maxRetries ?? 3 , init , profile ) ;
284278 return getCredentialsFromProfile ( newProfileName , options , init ) ;
285279 }
286280 throw legacyError ;
@@ -291,16 +285,21 @@ const getCredentialsFromProfile = async (profile: string, options: RequestOption
291285} ;
292286
293287/**
294- * @internal
295288 * Gets credentials from specified IMDS path
289+ * @internal
296290 */
297291async function getCredentialsFromPath ( path : string , options : RequestOptions ) {
298292 const response = await httpRequest ( {
299293 ...options ,
300294 path,
301295 } ) ;
302296
303- const credentialsResponse = JSON . parse ( response . toString ( ) ) ;
297+ let credentialsResponse ;
298+ try {
299+ credentialsResponse = JSON . parse ( response . toString ( ) ) ;
300+ } catch ( error ) {
301+ throw new CredentialsProviderError ( "Failed to parse JSON from instance metadata service." ) ;
302+ }
304303
305304 // Validate response
306305 if ( ! isImdsCredentials ( credentialsResponse ) ) {
0 commit comments