@@ -155,7 +155,7 @@ export class AwsClient extends BaseExternalAccountClient {
155
155
// The credential config contains all the URLs by default but clients may be running this
156
156
// where the metadata server is not available and returning the credentials through the environment.
157
157
// Removing this check may break them.
158
- if ( this . shouldUseMetadataServer ( ) && this . imdsV2SessionTokenUrl ) {
158
+ if ( ! this . regionFromEnv && this . imdsV2SessionTokenUrl ) {
159
159
metadataHeaders [ 'x-aws-ec2-metadata-token' ] =
160
160
await this . getImdsV2SessionToken ( ) ;
161
161
}
@@ -167,6 +167,10 @@ export class AwsClient extends BaseExternalAccountClient {
167
167
if ( this . securityCredentialsFromEnv ) {
168
168
return this . securityCredentialsFromEnv ;
169
169
}
170
+ if ( this . imdsV2SessionTokenUrl ) {
171
+ metadataHeaders [ 'x-aws-ec2-metadata-token' ] =
172
+ await this . getImdsV2SessionToken ( ) ;
173
+ }
170
174
// Since the role on a VM can change, we don't need to cache it.
171
175
const roleName = await this . getAwsRoleName ( metadataHeaders ) ;
172
176
// Temporary credentials typically last for several hours.
@@ -316,12 +320,6 @@ export class AwsClient extends BaseExternalAccountClient {
316
320
return response . data ;
317
321
}
318
322
319
- private shouldUseMetadataServer ( ) : boolean {
320
- // The metadata server must be used when either the AWS region or AWS security
321
- // credentials cannot be retrieved through their defined environment variables.
322
- return ! this . regionFromEnv || ! this . securityCredentialsFromEnv ;
323
- }
324
-
325
323
private get regionFromEnv ( ) : string | null {
326
324
// The AWS region can be provided through AWS_REGION or AWS_DEFAULT_REGION.
327
325
// Only one is required.
0 commit comments