Skip to content

Commit

Permalink
feat: get AWS region from environment variable (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanderdeijl authored Oct 8, 2020
1 parent 317b1d2 commit 8f657ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/auth/awsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ 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'];
}
const opts: GaxiosOptions = {
url: this.regionUrl,
method: 'GET',
Expand Down
19 changes: 19 additions & 0 deletions test/test.awsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,16 +504,19 @@ describe('AwsClient', () => {
let envAwsAccessKeyId: string | undefined;
let envAwsSecretAccessKey: string | undefined;
let envAwsSessionToken: string | undefined;
let envAwsRegion: 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;
// 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;
});

afterEach(() => {
Expand All @@ -533,6 +536,11 @@ describe('AwsClient', () => {
} else {
delete process.env.AWS_SESSION_TOKEN;
}
if (envAwsRegion) {
process.env.AWS_REGION = envAwsRegion;
} else {
delete process.env.AWS_REGION;
}
});

describe('retrieveSubjectToken()', () => {
Expand Down Expand Up @@ -583,6 +591,17 @@ describe('AwsClient', () => {
});
scope.done();
});

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);
});
});

describe('getAccessToken()', () => {
Expand Down

0 comments on commit 8f657ce

Please sign in to comment.