1
1
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" ;
3
3
4
- import { fromInstanceMetadata , getMetadataToken } from "./fromInstanceMetadata" ;
4
+ import { fromInstanceMetadata , getMetadataToken } from "./fromInstanceMetadata" ;
5
5
6
6
describe ( "fromInstanceMetadata (Live EC2 E2E Tests)" , ( ) => {
7
7
const originalEnv = { ...process . env } ;
8
8
let imdsAvailable = false ;
9
9
10
10
beforeEach ( async ( ) => {
11
11
process . env = { ...originalEnv } ;
12
-
12
+
13
13
// Check IMDS availability
14
14
try {
15
15
const testProvider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 0 } ) ;
@@ -25,17 +25,16 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
25
25
} ) ;
26
26
27
27
it ( "should fetch metadata token successfully" , async ( ) => {
28
-
29
28
if ( ! imdsAvailable ) {
30
29
return ;
31
30
}
32
31
const options = {
33
- path : "/latest/api/token" ,
32
+ path : "/latest/api/token" ,
34
33
method : "PUT" ,
35
- timeout : 1000 ,
34
+ timeout : 1000 ,
36
35
headers : {
37
- "x-aws-ec2-metadata-token-ttl-seconds" : "21600" ,
38
- } ,
36
+ "x-aws-ec2-metadata-token-ttl-seconds" : "21600" ,
37
+ } ,
39
38
} ;
40
39
const token = await getMetadataToken ( options ) ;
41
40
expect ( token ) . toBeDefined ( ) ;
@@ -44,32 +43,30 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
44
43
} ) ;
45
44
46
45
it ( "retrieves credentials with account ID on allowlisted instances only)" , async ( ) => {
46
+ if ( ! imdsAvailable ) return ;
47
47
48
- if ( ! imdsAvailable )
49
- return ;
50
-
51
48
const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
52
49
const credentials = await provider ( ) ;
53
50
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" ) ;
58
55
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
+ }
63
60
64
- expect ( credentials . accountId ) . toBeDefined ( ) ;
65
- expect ( typeof credentials . accountId ) . toBe ( "string" ) ;
61
+ expect ( credentials . accountId ) . toBeDefined ( ) ;
62
+ expect ( typeof credentials . accountId ) . toBe ( "string" ) ;
66
63
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
+ } ) ;
71
69
} ) ;
72
- } ) ;
73
70
74
71
it ( "IMDS access disabled via AWS_EC2_METADATA_DISABLED" , async ( ) => {
75
72
process . env . AWS_EC2_METADATA_DISABLED = "true" ;
@@ -88,13 +85,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
88
85
} ) ;
89
86
90
87
it ( "Uses configured profile name from env" , async ( ) => {
88
+ if ( ! imdsAvailable ) return ;
91
89
92
- if ( ! imdsAvailable )
93
- return ;
94
-
95
90
process . env . AWS_EC2_INSTANCE_PROFILE_NAME = "foo-profile" ;
96
91
const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
97
-
92
+
98
93
try {
99
94
const credentials = await provider ( ) ;
100
95
expect ( credentials ) . toHaveProperty ( "accessKeyId" ) ;
@@ -106,9 +101,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
106
101
} ) ;
107
102
108
103
it ( "Multiple calls return stable results" , async ( ) => {
109
-
110
- if ( ! imdsAvailable )
111
- return ;
104
+ if ( ! imdsAvailable ) return ;
112
105
113
106
const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
114
107
const creds1 = await provider ( ) ;
@@ -121,12 +114,10 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
121
114
console . log ( "Stable credentials returned across calls." ) ;
122
115
} ) ;
123
116
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 ;
127
119
const provider = fromInstanceMetadata ( { timeout : 1 } ) ;
128
120
129
121
await expect ( provider ( ) ) . rejects . toThrow ( / t i m e o u t | t i m e d o u t | T i m e o u t E r r o r / i) ;
130
122
} ) ;
131
-
132
123
} ) ;
0 commit comments