11import { 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
66describe ( "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 ( / 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) ;
130122 } ) ;
131-
132123} ) ;
0 commit comments