Skip to content

Commit ae61133

Browse files
committed
run formatter
1 parent 92178fe commit ae61133

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed
Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { CredentialsProviderError } from "@smithy/property-provider";
2-
import { afterEach, beforeEach, describe, expect, test as it} from "vitest";
2+
import { afterEach, beforeEach, describe, expect, test as it } from "vitest";
33

4-
import { fromInstanceMetadata,getMetadataToken } from "./fromInstanceMetadata";
4+
import { fromInstanceMetadata, getMetadataToken } from "./fromInstanceMetadata";
55

66
describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
77
const originalEnv = { ...process.env };
88
let imdsAvailable = false;
99

1010
beforeEach(async () => {
1111
process.env = { ...originalEnv };
12-
12+
1313
// Check IMDS availability
1414
try {
1515
const testProvider = fromInstanceMetadata({ timeout: 1000, maxRetries: 0 });
@@ -25,17 +25,16 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
2525
});
2626

2727
it("should fetch metadata token successfully", async () => {
28-
2928
if (!imdsAvailable) {
3029
return;
3130
}
3231
const options = {
33-
path: "/latest/api/token",
32+
path: "/latest/api/token",
3433
method: "PUT",
35-
timeout: 1000,
34+
timeout: 1000,
3635
headers: {
37-
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
38-
},
36+
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
37+
},
3938
};
4039
const token = await getMetadataToken(options);
4140
expect(token).toBeDefined();
@@ -44,32 +43,30 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
4443
});
4544

4645
it("retrieves credentials with account ID on allowlisted instances only)", async () => {
46+
if (!imdsAvailable) return;
4747

48-
if (!imdsAvailable)
49-
return;
50-
5148
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
5249
const credentials = await provider();
5350

54-
expect(credentials).toHaveProperty("accessKeyId");
55-
expect(credentials).toHaveProperty("secretAccessKey");
56-
expect(typeof credentials.accessKeyId).toBe("string");
57-
expect(typeof credentials.secretAccessKey).toBe("string");
51+
expect(credentials).toHaveProperty("accessKeyId");
52+
expect(credentials).toHaveProperty("secretAccessKey");
53+
expect(typeof credentials.accessKeyId).toBe("string");
54+
expect(typeof credentials.secretAccessKey).toBe("string");
5855

59-
if (!credentials.accountId) {
60-
console.log("Skipping account ID test not an allowlisted instance");
61-
return;
62-
}
56+
if (!credentials.accountId) {
57+
console.log("Skipping account ID test not an allowlisted instance");
58+
return;
59+
}
6360

64-
expect(credentials.accountId).toBeDefined();
65-
expect(typeof credentials.accountId).toBe("string");
61+
expect(credentials.accountId).toBeDefined();
62+
expect(typeof credentials.accountId).toBe("string");
6663

67-
console.log("IMDSv2 Credentials with Account ID:", {
68-
accessKeyId: credentials.accessKeyId,
69-
sessionToken: credentials.sessionToken?.slice(0, 10) + "...",
70-
accountId: credentials.accountId,
64+
console.log("IMDSv2 Credentials with Account ID:", {
65+
accessKeyId: credentials.accessKeyId,
66+
sessionToken: credentials.sessionToken?.slice(0, 10) + "...",
67+
accountId: credentials.accountId,
68+
});
7169
});
72-
});
7370

7471
it("IMDS access disabled via AWS_EC2_METADATA_DISABLED", async () => {
7572
process.env.AWS_EC2_METADATA_DISABLED = "true";
@@ -88,13 +85,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
8885
});
8986

9087
it("Uses configured profile name from env", async () => {
88+
if (!imdsAvailable) return;
9189

92-
if (!imdsAvailable)
93-
return;
94-
9590
process.env.AWS_EC2_INSTANCE_PROFILE_NAME = "foo-profile";
9691
const provider = fromInstanceMetadata({ timeout: 1000 });
97-
92+
9893
try {
9994
const credentials = await provider();
10095
expect(credentials).toHaveProperty("accessKeyId");
@@ -106,9 +101,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
106101
});
107102

108103
it("Multiple calls return stable results", async () => {
109-
110-
if (!imdsAvailable)
111-
return;
104+
if (!imdsAvailable) return;
112105

113106
const provider = fromInstanceMetadata({ timeout: 1000 });
114107
const creds1 = await provider();
@@ -121,12 +114,10 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
121114
console.log("Stable credentials returned across calls.");
122115
});
123116

124-
it("should timeout as expected when a request exceeds the specified duration", async () => {
125-
if (!imdsAvailable)
126-
return;
117+
it("should timeout as expected when a request exceeds the specified duration", async () => {
118+
if (!imdsAvailable) return;
127119
const provider = fromInstanceMetadata({ timeout: 1 });
128120

129121
await expect(provider()).rejects.toThrow(/timeout|timed out|TimeoutError/i);
130122
});
131-
132123
});

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { CredentialsProviderError } from "@smithy/property-provider";
33
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
44

55
import { InstanceMetadataV1FallbackError } from "./error/InstanceMetadataV1FallbackError";
6-
import { fromInstanceMetadata,getConfiguredProfileName, getImdsProfile, throwIfImdsTurnedOff } from "./fromInstanceMetadata";
6+
import {
7+
fromInstanceMetadata,
8+
getConfiguredProfileName,
9+
getImdsProfile,
10+
throwIfImdsTurnedOff,
11+
} from "./fromInstanceMetadata";
712
import { httpRequest } from "./remoteProvider/httpRequest";
813
import { fromImdsCredentials, isImdsCredentials } from "./remoteProvider/ImdsCredentials";
914
import { providerConfigFromInit } from "./remoteProvider/RemoteProviderInit";
@@ -66,7 +71,7 @@ describe("fromInstanceMetadata", () => {
6671
vi.mocked(staticStabilityProvider).mockImplementation((input) => input);
6772
vi.mocked(getInstanceMetadataEndpoint).mockResolvedValue({ hostname } as any);
6873
vi.mocked(loadConfig).mockReturnValue(() => Promise.resolve(false));
69-
vi.spyOn({throwIfImdsTurnedOff}, "throwIfImdsTurnedOff").mockResolvedValue(undefined);
74+
vi.spyOn({ throwIfImdsTurnedOff }, "throwIfImdsTurnedOff").mockResolvedValue(undefined);
7075
(isImdsCredentials as unknown as any).mockReturnValue(true);
7176
vi.mocked(providerConfigFromInit).mockReturnValue({
7277
timeout: mockTimeout,
@@ -79,16 +84,16 @@ describe("fromInstanceMetadata", () => {
7984
});
8085

8186
it("returns no credentials when AWS_EC2_METADATA_DISABLED=true", async () => {
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(
87+
vi.mocked(loadConfig).mockImplementation(({ environmentVariableSelector, configFileSelector }) => {
88+
if (environmentVariableSelector && environmentVariableSelector({ AWS_EC2_METADATA_DISABLED: "true" })) {
89+
return () => Promise.resolve(true);
90+
}
91+
if (configFileSelector && configFileSelector({ imds_disabled: "true" })) {
92+
return () => Promise.resolve(true);
93+
}
94+
return () => Promise.resolve(false);
95+
});
96+
vi.spyOn({ throwIfImdsTurnedOff }, "throwIfImdsTurnedOff").mockRejectedValueOnce(
9297
new CredentialsProviderError("IMDS credential fetching is disabled")
9398
);
9499
const provider = fromInstanceMetadata({});
@@ -172,7 +177,7 @@ describe("fromInstanceMetadata", () => {
172177

173178
vi.mocked(retry).mockImplementation((fn: any) => fn());
174179
vi.mocked(fromImdsCredentials).mockReturnValue(mockCreds);
175-
vi.spyOn({throwIfImdsTurnedOff}, "throwIfImdsTurnedOff").mockResolvedValue();
180+
vi.spyOn({ throwIfImdsTurnedOff }, "throwIfImdsTurnedOff").mockResolvedValue();
176181

177182
await expect(fromInstanceMetadata()()).resolves.toEqual(mockCreds);
178183
expect(httpRequest).toHaveBeenNthCalledWith(3, {
@@ -298,9 +303,9 @@ describe("fromInstanceMetadata", () => {
298303

299304
it("uses ec2InstanceProfileName from init if provided", async () => {
300305
const profileName = "profile-from-init";
301-
const options = { hostname } ;
306+
const options = { hostname };
302307

303-
vi.spyOn({getConfiguredProfileName}, "getConfiguredProfileName").mockResolvedValueOnce(profileName);
308+
vi.spyOn({ getConfiguredProfileName }, "getConfiguredProfileName").mockResolvedValueOnce(profileName);
304309

305310
const credentials = await getImdsProfile(options, mockMaxRetries, {
306311
ec2InstanceProfileName: profileName,

0 commit comments

Comments
 (0)