Skip to content

Commit fca96dd

Browse files
committed
e2e
1 parent b2e8e7d commit fca96dd

File tree

2 files changed

+55
-57
lines changed

2 files changed

+55
-57
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: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,31 @@ const getInstanceMetadataProvider = (init: RemoteProviderInit = {}) => {
5353
let fallbackBlockedFromProcessEnv = false;
5454

5555
const _ec2MetadataV1Disabled =
56-
ec2MetadataV1Disabled !== undefined
57-
? ec2MetadataV1Disabled
58-
: await loadConfig(
59-
{
60-
environmentVariableSelector: (env) => {
61-
const envValue = env[AWS_EC2_METADATA_V1_DISABLED];
62-
if (envValue === undefined) {
63-
return undefined;
64-
}
65-
fallbackBlockedFromProcessEnv = !!envValue && envValue !== "false";
66-
return fallbackBlockedFromProcessEnv;
67-
},
68-
configFileSelector: (profile) => {
69-
const profileValue = profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED];
70-
if (profileValue === undefined) {
71-
return undefined;
72-
}
73-
fallbackBlockedFromProfile = !!profileValue && profileValue !== "false";
74-
return fallbackBlockedFromProfile;
75-
},
76-
default: false,
77-
},
78-
{
79-
profile,
56+
ec2MetadataV1Disabled ??
57+
(await loadConfig(
58+
{
59+
environmentVariableSelector: (env) => {
60+
const envValue = env[AWS_EC2_METADATA_V1_DISABLED];
61+
if (envValue === undefined) {
62+
return undefined;
8063
}
81-
)();
64+
fallbackBlockedFromProcessEnv = !!envValue && envValue !== "false";
65+
return fallbackBlockedFromProcessEnv;
66+
},
67+
configFileSelector: (profile) => {
68+
const profileValue = profile[PROFILE_AWS_EC2_METADATA_V1_DISABLED];
69+
if (profileValue === undefined) {
70+
return undefined;
71+
}
72+
fallbackBlockedFromProfile = !!profileValue && profileValue !== "false";
73+
return fallbackBlockedFromProfile;
74+
},
75+
default: false,
76+
},
77+
{
78+
profile,
79+
}
80+
)());
8281

8382
if (_ec2MetadataV1Disabled) {
8483
const causes: string[] = [];
@@ -123,7 +122,7 @@ const getInstanceMetadataProvider = (init: RemoteProviderInit = {}) => {
123122
} else {
124123
let token: string;
125124
try {
126-
token = (await getMetadataToken({ ...endpoint, timeout })).toString();
125+
token = await getMetadataToken({ ...endpoint, timeout });
127126
} catch (error) {
128127
if (error?.statusCode === 400) {
129128
throw Object.assign(error, {
@@ -181,15 +180,20 @@ export const getImdsProfile = async (options: RequestOptions, init: RemoteProvid
181180
}, init.maxRetries ?? DEFAULT_MAX_RETRIES);
182181
};
183182

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

194198
/**
195199
* Checks if IMDS credential fetching is disabled through configuration
@@ -261,10 +265,6 @@ export const getEc2InstanceProfileName = async (init: RemoteProviderInit): Promi
261265
/**
262266
* Gets credentials from profile.
263267
*
264-
* @param imdsProfile - todo: how is this different from init.profile?
265-
* @param options
266-
* @param init
267-
*
268268
* @internal
269269
*/
270270
const getCredentialsFromImdsProfile = async (

0 commit comments

Comments
 (0)