Skip to content

Commit d19cafc

Browse files
authored
feat: Add post-job action cleanup of credentials and region env vars (#101)
1 parent 1fa7cca commit d19cafc

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

Diff for: action.yml

+1
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ outputs:
5252
runs:
5353
using: 'node12'
5454
main: 'dist/index.js'
55+
post: 'dist/cleanup/index.js'

Diff for: cleanup.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const core = require('@actions/core');
2+
3+
/**
4+
* When the GitHub Actions job is done, clean up any environment variables that
5+
* may have been set by the configure-aws-credentials steps in the job.
6+
*
7+
* Environment variables are not intended to be shared across different jobs in
8+
* the same GitHub Actions workflow: GitHub Actions documentation states that
9+
* each job runs in a fresh instance. However, doing our own cleanup will
10+
* give us additional assurance that these environment variables are not shared
11+
* with any other jobs.
12+
*/
13+
14+
async function cleanup() {
15+
try {
16+
// The GitHub Actions toolkit does not have an option to completely unset
17+
// environment variables, so we overwrite the current value with an empty
18+
// string. The AWS CLI and AWS SDKs will behave correctly: they treat an
19+
// empty string value as if the environment variable does not exist.
20+
core.exportVariable('AWS_ACCESS_KEY_ID', '');
21+
core.exportVariable('AWS_SECRET_ACCESS_KEY', '');
22+
core.exportVariable('AWS_SESSION_TOKEN', '');
23+
core.exportVariable('AWS_DEFAULT_REGION', '');
24+
core.exportVariable('AWS_REGION', '');
25+
}
26+
catch (error) {
27+
core.setFailed(error.message);
28+
}
29+
}
30+
31+
module.exports = cleanup;
32+
33+
/* istanbul ignore next */
34+
if (require.main === module) {
35+
cleanup();
36+
}

Diff for: cleanup.test.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const core = require('@actions/core');
2+
const cleanup = require('./cleanup.js');
3+
4+
jest.mock('@actions/core');
5+
6+
const FAKE_ACCESS_KEY_ID = 'MY-AWS-ACCESS-KEY-ID';
7+
const FAKE_SECRET_ACCESS_KEY = 'MY-AWS-SECRET-ACCESS-KEY';
8+
const FAKE_SESSION_TOKEN = 'MY-AWS-SESSION-TOKEN';
9+
const FAKE_REGION = 'fake-region-1';
10+
const ACTION_ENVIRONMENT_VARIABLES = {
11+
AWS_ACCESS_KEY_ID: FAKE_ACCESS_KEY_ID,
12+
AWS_SECRET_ACCESS_KEY: FAKE_SECRET_ACCESS_KEY,
13+
AWS_SESSION_TOKEN: FAKE_SESSION_TOKEN,
14+
AWS_DEFAULT_REGION: FAKE_REGION,
15+
AWS_REGION: FAKE_REGION,
16+
};
17+
18+
describe('Configure AWS Credentials', () => {
19+
const OLD_ENV = process.env;
20+
21+
beforeEach(() => {
22+
jest.resetModules();
23+
process.env = {...OLD_ENV, ...ACTION_ENVIRONMENT_VARIABLES};
24+
});
25+
26+
afterEach(() => {
27+
process.env = OLD_ENV;
28+
});
29+
30+
test('replaces AWS credential and region env vars with empty strings', async () => {
31+
await cleanup();
32+
expect(core.setFailed).toHaveBeenCalledTimes(0);
33+
expect(core.exportVariable).toHaveBeenCalledTimes(5);
34+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', '');
35+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', '');
36+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SESSION_TOKEN', '');
37+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', '');
38+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', '');
39+
});
40+
41+
test('error is caught and fails the action', async () => {
42+
core.exportVariable.mockReset();
43+
core.exportVariable.mockImplementation(() => {
44+
throw new Error();
45+
});
46+
47+
await cleanup();
48+
49+
expect(core.setFailed).toBeCalled();
50+
});
51+
});

Diff for: index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const core = require('@actions/core');
22
const assert = require('assert');
33
const aws = require('aws-sdk');
4-
const run = require('.');
4+
const run = require('./index.js');
55

66
jest.mock('@actions/core');
77

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "eslint **.js",
8-
"package": "ncc build index.js -o dist",
9-
"test": "eslint **.js && jest --coverage"
8+
"package": "ncc build index.js -o dist && ncc build cleanup.js -o dist/cleanup",
9+
"test": "eslint **.js && jest --coverage --verbose"
1010
},
1111
"repository": {
1212
"type": "git",

0 commit comments

Comments
 (0)