Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support AWS_DEFAULT_REGION for determining AWS region #1149

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/auth/awsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {RefreshOptions} from './oauth2client';
export interface AwsClientOptions extends BaseExternalAccountClientOptions {
credential_source: {
environment_id: string;
// Region can also be determined from the AWS_REGION environment variable.
// Region can also be determined from the AWS_REGION or AWS_DEFAULT_REGION
// environment variables.
region_url?: string;
// The url field is used to determine the AWS security credentials.
// This is optional since these credentials can be retrieved from the
Expand Down Expand Up @@ -78,7 +79,7 @@ export class AwsClient extends BaseExternalAccountClient {
super(options, additionalOptions);
this.environmentId = options.credential_source.environment_id;
// This is only required if the AWS region is not available in the
// AWS_REGION environment variable
// AWS_REGION or AWS_DEFAULT_REGION environment variables.
this.regionUrl = options.credential_source.region_url;
// This is only required if AWS security credentials are not available in
// environment variables.
Expand Down Expand Up @@ -200,8 +201,10 @@ export class AwsClient extends BaseExternalAccountClient {
* @return A promise that resolves with the current AWS region.
*/
private async getAwsRegion(): Promise<string> {
if (process.env['AWS_REGION']) {
return process.env['AWS_REGION'];
// Priority order for region determination:
// AWS_REGION > AWS_DEFAULT_REGION > metadata server.
if (process.env['AWS_REGION'] || process.env['AWS_DEFAULT_REGION']) {
return (process.env['AWS_REGION'] || process.env['AWS_DEFAULT_REGION'])!;
}
if (!this.regionUrl) {
throw new Error(
Expand Down
35 changes: 33 additions & 2 deletions test/test.awsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,21 @@ describe('AwsClient', () => {
let envAwsSecretAccessKey: string | undefined;
let envAwsSessionToken: string | undefined;
let envAwsRegion: string | undefined;
let envAwsDefaultRegion: string | undefined;

beforeEach(() => {
// Store external state.
envAwsAccessKeyId = process.env.AWS_ACCESS_KEY_ID;
envAwsSecretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
envAwsSessionToken = process.env.AWS_SESSION_TOKEN;
envAwsAccessKeyId = process.env.AWS_REGION;
envAwsRegion = process.env.AWS_REGION;
envAwsDefaultRegion = process.env.AWS_DEFAULT_REGION;
// Reset environment variables.
delete process.env.AWS_ACCESS_KEY_ID;
delete process.env.AWS_SECRET_ACCESS_KEY;
delete process.env.AWS_SESSION_TOKEN;
delete process.env.AWS_REGION;
delete process.env.AWS_DEFAULT_REGION;
});

afterEach(() => {
Expand All @@ -563,6 +566,11 @@ describe('AwsClient', () => {
} else {
delete process.env.AWS_REGION;
}
if (envAwsDefaultRegion) {
process.env.AWS_DEFAULT_REGION = envAwsDefaultRegion;
} else {
delete process.env.AWS_DEFAULT_REGION;
}
});

describe('retrieveSubjectToken()', () => {
Expand Down Expand Up @@ -614,10 +622,33 @@ describe('AwsClient', () => {
scope.done();
});

it('should resolve when AWS region is set as environment variable', async () => {
it('should resolve when AWS_REGION is set as environment variable', async () => {
process.env.AWS_ACCESS_KEY_ID = accessKeyId;
process.env.AWS_SECRET_ACCESS_KEY = secretAccessKey;
process.env.AWS_REGION = awsRegion;

const client = new AwsClient(awsOptions);
const subjectToken = await client.retrieveSubjectToken();

assert.deepEqual(subjectToken, expectedSubjectTokenNoToken);
});

it('should resolve when AWS_DEFAULT_REGION is set as environment variable', async () => {
process.env.AWS_ACCESS_KEY_ID = accessKeyId;
process.env.AWS_SECRET_ACCESS_KEY = secretAccessKey;
process.env.AWS_DEFAULT_REGION = awsRegion;

const client = new AwsClient(awsOptions);
const subjectToken = await client.retrieveSubjectToken();

assert.deepEqual(subjectToken, expectedSubjectTokenNoToken);
});

it('should prioritize AWS_REGION over AWS_DEFAULT_REGION environment variable', async () => {
process.env.AWS_ACCESS_KEY_ID = accessKeyId;
process.env.AWS_SECRET_ACCESS_KEY = secretAccessKey;
process.env.AWS_REGION = awsRegion;
process.env.AWS_DEFAULT_REGION = 'fail-if-used';

const client = new AwsClient(awsOptions);
const subjectToken = await client.retrieveSubjectToken();
Expand Down