Skip to content

Commit e5b72e5

Browse files
committed
e2e
1 parent b2e8e7d commit e5b72e5

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { afterEach, beforeEach, describe, expect, test as it } from "vitest";
22

33
import { fromInstanceMetadata, getMetadataToken } from "./fromInstanceMetadata";
4+
import { getInstanceMetadataEndpoint } from "./utils/getInstanceMetadataEndpoint";
45

56
describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
67
const originalEnv = { ...process.env };
78
let imdsAvailable = false;
89

910
beforeEach(async () => {
10-
process.env = { ...originalEnv, AWS_EC2_INSTANCE_PROFILE_NAME: "foo-profile" };
11+
process.env = { ...originalEnv };
1112

1213
// Check IMDS availability
1314
try {
14-
const testProvider = fromInstanceMetadata({ timeout: 1000, maxRetries: 0 });
15+
const testProvider = fromInstanceMetadata({ timeout: 9000 });
1516
await testProvider();
1617
imdsAvailable = true;
1718
} catch (err) {
@@ -28,15 +29,8 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
2829
return context.skip();
2930
}
3031

31-
const options = {
32-
path: "/latest/api/token",
33-
method: "PUT",
34-
timeout: 1000,
35-
headers: {
36-
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
37-
},
38-
};
39-
const token = await getMetadataToken(options);
32+
const endpoint = await getInstanceMetadataEndpoint();
33+
const token = await getMetadataToken(endpoint);
4034
expect(token).toBeDefined();
4135
expect(typeof token).toBe("string");
4236
expect(token.length).toBeGreaterThan(0);
@@ -47,7 +41,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
4741
return context.skip();
4842
}
4943

50-
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
44+
const provider = fromInstanceMetadata();
5145
const credentials = await provider();
5246

5347
expect(credentials).toHaveProperty("accessKeyId");
@@ -61,7 +55,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
6155
return context.skip();
6256
}
6357

64-
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
58+
const provider = fromInstanceMetadata();
6559
const credentials = await provider();
6660

6761
if (!credentials.accountId) {
@@ -75,15 +69,15 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
7569
it("IMDS access disabled via AWS_EC2_METADATA_DISABLED", async () => {
7670
process.env.AWS_EC2_METADATA_DISABLED = "true";
7771

78-
const provider = fromInstanceMetadata({ timeout: 1000 });
72+
const provider = fromInstanceMetadata();
7973

8074
await expect(provider()).rejects.toThrow("IMDS credential fetching is disabled");
8175
});
8276

8377
it("Empty configured profile name should throw error", async () => {
8478
process.env.AWS_EC2_INSTANCE_PROFILE_NAME = " ";
8579

86-
const provider = fromInstanceMetadata({ timeout: 1000 });
80+
const provider = fromInstanceMetadata();
8781

8882
await expect(provider()).rejects.toThrow();
8983
});
@@ -93,7 +87,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
9387
return context.skip();
9488
}
9589

96-
const provider = fromInstanceMetadata({ timeout: 1000 });
90+
const provider = fromInstanceMetadata();
9791

9892
try {
9993
const credentials = await provider();
@@ -108,7 +102,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
108102
return context.skip();
109103
}
110104

111-
const provider = fromInstanceMetadata({ timeout: 1000 });
105+
const provider = fromInstanceMetadata();
112106
const creds1 = await provider();
113107
const creds2 = await provider();
114108

@@ -117,7 +111,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
117111
expect(creds1.accessKeyId).toBe(creds2.accessKeyId);
118112
});
119113

120-
it("should timeout as expected when a request exceeds the specified duration", async (context) => {
114+
/**
115+
* The IMDS may respond too quickly to test this,
116+
* even with 1ms timeout.
117+
*/
118+
it.skip("should timeout as expected when a request exceeds the specified duration", async (context) => {
121119
if (!imdsAvailable) {
122120
return context.skip();
123121
}

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const getInstanceMetadataProvider = (init: RemoteProviderInit = {}) => {
123123
} else {
124124
let token: string;
125125
try {
126-
token = (await getMetadataToken({ ...endpoint, timeout })).toString();
126+
token = (await getMetadataToken({ ...endpoint, timeout }));
127127
} catch (error) {
128128
if (error?.statusCode === 400) {
129129
throw Object.assign(error, {
@@ -181,15 +181,20 @@ export const getImdsProfile = async (options: RequestOptions, init: RemoteProvid
181181
}, init.maxRetries ?? DEFAULT_MAX_RETRIES);
182182
};
183183

184-
export const getMetadataToken = async (options: RequestOptions) =>
185-
httpRequest({
186-
...options,
187-
path: IMDS_TOKEN_PATH,
188-
method: "PUT",
189-
headers: {
190-
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
191-
},
192-
});
184+
/**
185+
* @internal
186+
*/
187+
export const getMetadataToken = async (options: RequestOptions): Promise<string> =>
188+
(
189+
await httpRequest({
190+
...options,
191+
path: IMDS_TOKEN_PATH,
192+
method: "PUT",
193+
headers: {
194+
"x-aws-ec2-metadata-token-ttl-seconds": "21600",
195+
},
196+
})
197+
).toString();
193198

194199
/**
195200
* Checks if IMDS credential fetching is disabled through configuration
@@ -261,10 +266,6 @@ export const getEc2InstanceProfileName = async (init: RemoteProviderInit): Promi
261266
/**
262267
* Gets credentials from profile.
263268
*
264-
* @param imdsProfile - todo: how is this different from init.profile?
265-
* @param options
266-
* @param init
267-
*
268269
* @internal
269270
*/
270271
const getCredentialsFromImdsProfile = async (

0 commit comments

Comments
 (0)