Skip to content

Commit da518c1

Browse files
committed
Add support for masking the AWS account ID from logs
1 parent 3aa1c0e commit da518c1

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ inputs:
1313
aws-region:
1414
description: 'AWS Region, e.g. us-east-2'
1515
required: true
16+
mask-aws-account-id:
17+
description: "Whether to set the AWS account ID for these credentials as a secret value, so that it is masked in logs. Valid values are 'true' and 'false'. Defaults to true"
18+
required: false
1619
outputs:
1720
aws-account-id:
1821
description: 'The AWS account ID for the provided credentials'

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ async function run() {
88
const secretAccessKey = core.getInput('aws-secret-access-key', { required: true });
99
const region = core.getInput('aws-region', { required: true });
1010
const sessionToken = core.getInput('aws-session-token', { required: false });
11+
const maskAccountId = core.getInput('mask-aws-account-id', { required: false });
1112

1213
// Configure the AWS CLI and AWS SDKs using environment variables
1314

@@ -35,6 +36,9 @@ async function run() {
3536
const identity = await sts.getCallerIdentity().promise();
3637
const accountId = identity.Account;
3738
core.setOutput('aws-account-id', accountId);
39+
if (!maskAccountId || maskAccountId.toLowerCase() == 'true') {
40+
core.setSecret(accountId);
41+
}
3842
}
3943
catch (error) {
4044
core.setFailed(error.message);

index.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe('Configure AWS Credentials', () => {
2323
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
2424
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
2525
.mockReturnValueOnce('us-east-2') // aws-default-region
26-
.mockReturnValueOnce('MY-AWS-SESSION-TOKEN'); // aws-session-token
26+
.mockReturnValueOnce('MY-AWS-SESSION-TOKEN') // aws-session-token
27+
.mockReturnValueOnce('TRUE'); // mask-aws-account-id
2728

2829
mockStsCallerIdentity.mockImplementation(() => {
2930
return {
@@ -43,6 +44,7 @@ describe('Configure AWS Credentials', () => {
4344
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'us-east-2');
4445
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'us-east-2');
4546
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
47+
expect(core.setSecret).toHaveBeenCalledWith('123456789012');
4648
});
4749

4850
test('session token is optional', async () => {
@@ -59,6 +61,26 @@ describe('Configure AWS Credentials', () => {
5961
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'eu-west-1');
6062
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'eu-west-1');
6163
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
64+
expect(core.setSecret).toHaveBeenCalledWith('123456789012');
65+
});
66+
67+
test('can opt out of masking account ID', async () => {
68+
core.getInput = jest
69+
.fn()
70+
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
71+
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
72+
.mockReturnValueOnce('us-east-1') // aws-default-region
73+
.mockReturnValueOnce('') // aws-session-token
74+
.mockReturnValueOnce('false'); // mask-aws-account-id
75+
76+
await run();
77+
expect(core.exportVariable).toHaveBeenCalledTimes(4);
78+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', 'MY-AWS-ACCESS-KEY-ID');
79+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', 'MY-AWS-SECRET-ACCESS-KEY');
80+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'us-east-1');
81+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'us-east-1');
82+
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
83+
expect(core.setSecret).toHaveBeenCalledTimes(0);
6284
});
6385

6486
test('error is caught by core.setFailed', async () => {

0 commit comments

Comments
 (0)