@@ -110,8 +110,8 @@ const getInstanceMetadataProvider = (init: RemoteProviderInit = {}) => {
110
110
} ;
111
111
112
112
return async ( ) => {
113
- const endpoint = await getInstanceMetadataEndpoint ( ) ;
114
113
await throwIfImdsTurnedOff ( profile , logger ) ;
114
+ const endpoint = await getInstanceMetadataEndpoint ( ) ;
115
115
if ( disableFetchToken ) {
116
116
logger ?. debug ( "AWS SDK Instance Metadata" , "using v1 fallback (no token fetch)" ) ;
117
117
return getCredentials ( maxRetries , { ...endpoint , timeout } ) ;
@@ -149,17 +149,11 @@ export const getImdsProfile = async (
149
149
options : RequestOptions ,
150
150
maxRetries : number ,
151
151
init : RemoteProviderInit = { } ,
152
- profile ?: string ,
153
- resetCache ?: boolean
152
+ profile ?: string
154
153
) : Promise < string > => {
155
154
let apiVersion : "unknown" | "extended" | "legacy" = "unknown" ;
156
155
let resolvedProfile : string | null = null ;
157
156
158
- // If resetCache is true, clear the cached profile name
159
- if ( resetCache ) {
160
- resolvedProfile = null ;
161
- }
162
-
163
157
return retry < string > ( async ( ) => {
164
158
// First check if a profile name is configured
165
159
const configuredName = await getConfiguredProfileName ( init , profile ) ;
@@ -201,8 +195,8 @@ export const getMetadataToken = async (options: RequestOptions) =>
201
195
} ) ;
202
196
203
197
/**
204
- * @internal
205
198
* Checks if IMDS credential fetching is disabled through configuration
199
+ * @internal
206
200
*/
207
201
export const throwIfImdsTurnedOff = async ( profile ?: string , logger ?: any ) : Promise < void > => {
208
202
// Load configuration in priority order
@@ -236,8 +230,8 @@ export const throwIfImdsTurnedOff = async (profile?: string, logger?: any): Prom
236
230
} ;
237
231
238
232
/**
239
- * @internal
240
233
* Gets configured profile name from various sources
234
+ * @internal
241
235
*/
242
236
export const getConfiguredProfileName = async ( init : RemoteProviderInit , profile ?: string ) : Promise < string | null > => {
243
237
// Load configuration in priority order
@@ -264,8 +258,8 @@ export const getConfiguredProfileName = async (init: RemoteProviderInit, profile
264
258
} ;
265
259
266
260
/**
267
- * @internal
268
261
* Gets credentials from profile
262
+ * @internal
269
263
*/
270
264
const getCredentialsFromProfile = async ( profile : string , options : RequestOptions , init : RemoteProviderInit ) => {
271
265
// Try extended API first
@@ -280,7 +274,7 @@ const getCredentialsFromProfile = async (profile: string, options: RequestOption
280
274
if ( legacyError . statusCode === 404 && init . ec2InstanceProfileName === undefined ) {
281
275
// If legacy API also returns 404 and we're using a cached profile name,
282
276
// 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 ) ;
284
278
return getCredentialsFromProfile ( newProfileName , options , init ) ;
285
279
}
286
280
throw legacyError ;
@@ -291,16 +285,21 @@ const getCredentialsFromProfile = async (profile: string, options: RequestOption
291
285
} ;
292
286
293
287
/**
294
- * @internal
295
288
* Gets credentials from specified IMDS path
289
+ * @internal
296
290
*/
297
291
async function getCredentialsFromPath ( path : string , options : RequestOptions ) {
298
292
const response = await httpRequest ( {
299
293
...options ,
300
294
path,
301
295
} ) ;
302
296
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
+ }
304
303
305
304
// Validate response
306
305
if ( ! isImdsCredentials ( credentialsResponse ) ) {
0 commit comments