Skip to content

Commit c551568

Browse files
smilkurikuhe
authored andcommitted
resolved comments
1 parent 7a7f21f commit c551568

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

packages/credential-provider-imds/src/fromInstanceMetadata.e2e.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
2424
process.env = { ...originalEnv };
2525
});
2626

27-
it("should fetch metadata token successfully", async () => {
28-
if (!imdsAvailable) {
29-
return;
30-
}
27+
const test = imdsAvailable ? it : it.skip;
28+
test("should fetch metadata token successfully", async () => {
3129
const options = {
3230
path: "/latest/api/token",
3331
method: "PUT",
@@ -42,7 +40,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
4240
expect(token.length).toBeGreaterThan(0);
4341
});
4442

45-
it("retrieves credentials with account ID on allowlisted instances only)", async () => {
43+
it("retrieves credentials successfully", async () => {
4644
if (!imdsAvailable) return;
4745

4846
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
@@ -52,10 +50,16 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
5250
expect(credentials).toHaveProperty("secretAccessKey");
5351
expect(typeof credentials.accessKeyId).toBe("string");
5452
expect(typeof credentials.secretAccessKey).toBe("string");
53+
});
54+
55+
it("retrieves credentials with account ID on allowlisted instances", async () => {
56+
if (!imdsAvailable) return;
57+
58+
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
59+
const credentials = await provider();
5560

5661
if (!credentials.accountId) {
57-
console.log("Skipping account ID test not an allowlisted instance");
58-
return;
62+
it.skip("account ID test skipped - not an allowlisted instance", () => {});
5963
}
6064

6165
expect(credentials.accountId).toBeDefined();

packages/credential-provider-imds/src/fromInstanceMetadata.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe("fromInstanceMetadata", () => {
266266
.mockResolvedValueOnce("." as any);
267267
vi.mocked(retry).mockImplementation((fn: any) => fn());
268268

269-
await expect(fromInstanceMetadata()()).rejects.toThrow("Unexpected token");
269+
await expect(fromInstanceMetadata()()).rejects.toThrow("Failed to parse JSON from instance metadata service.");
270270
expect(retry).toHaveBeenCalledTimes(2);
271271
expect(httpRequest).toHaveBeenCalledTimes(3);
272272
expect(fromImdsCredentials).not.toHaveBeenCalled();

packages/credential-provider-imds/src/fromInstanceMetadata.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
207201
export 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
*/
242236
export 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
*/
270264
const 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
*/
297291
async 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

Comments
 (0)