Skip to content

Commit 3aa1c0e

Browse files
committed
Various fixes
Remove file-based env vars Add support for session credentials Add account ID as an output Remove testing actions workflow
1 parent 8ec2a2c commit 3aa1c0e

File tree

11 files changed

+33220
-890
lines changed

11 files changed

+33220
-890
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
## "Configure AWS Credentials" Action For GitHub Actions
22

3-
Configure AWS credential environment variables for use in other GitHub Actions.
4-
5-
<a href="https://github.com/aws/configure-aws-credentials-for-github-actions"><img alt="GitHub Actions status" src="https://github.com/aws/configure-aws-credentials-for-github-actions/workflows/test-local/badge.svg"></a>
6-
7-
This action adds configuration required by [the AWS CLI](https://aws.amazon.com/cli/) for use in subsequent actions.
3+
Configure AWS credential and region environment variables for use in other GitHub Actions. The environment variables will be detected by both the AWS SDKs and the AWS CLI to determine the credentials and region to use for AWS API calls.
84

95
## Usage
106

@@ -16,10 +12,18 @@ Add the following step to your workflow:
1612
with:
1713
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
1814
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
19-
aws-default-region: us-east-2
20-
aws-default-output: json
15+
aws-region: us-east-2
2116
```
2217
18+
## Credentials
19+
20+
We recommend following [Amazon IAM best practices](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html) for the AWS credentials used in GitHub Actions workflows, including:
21+
* Do not store credentials in your repository's code. You may use [GitHub Actions secrets](https://help.github.com/en/github/automating-your-workflow-with-github-actions/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables) to store credentials and redact credentials from GitHub Actions workflow logs.
22+
* [Create an individual IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#create-iam-users) with an access key for use in GitHub Actions workflows, preferably one per repository. Do not use the AWS account root user access key.
23+
* [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege) to the credentials used in GitHub Actions workflows. Grant only the permissions required to perform the actions in your GitHub Actions workflows.
24+
* [Rotate the credentials](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#rotate-credentials) used in GitHub Actions workflows regularly.
25+
* [Monitor the activity](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#keep-a-log) of the credentials used in GitHub Actions workflows.
26+
2327
## License Summary
2428
2529
This code is made available under the MIT license.

action.js

Whitespace-only changes.

action.yml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
name: 'Setup AWS'
2-
description: 'Setup an AWS CLI compatible environment'
1+
name: '"Configure AWS Credentials" Action For GitHub Actions'
2+
description: 'Configure AWS credential and region environment variables for use with the AWS CLI and AWS SDKs'
33
inputs:
44
aws-access-key-id:
5-
description: 'Your AWS access key id credential'
5+
description: 'AWS Access Key ID'
66
required: true
77
aws-secret-access-key:
8-
description: 'Your AWS secret access key credential'
8+
description: 'AWS Secret Access Key'
99
required: true
10-
aws-default-region:
11-
description: 'Default AWS region, e.g. us-east-2'
12-
required: true
13-
aws-default-output:
14-
description: 'Default output format, e.g. json'
10+
aws-session-token:
11+
description: 'AWS Session Token'
1512
required: false
16-
default: json
13+
aws-region:
14+
description: 'AWS Region, e.g. us-east-2'
15+
required: true
16+
outputs:
17+
aws-account-id:
18+
description: 'The AWS account ID for the provided credentials'
1719
runs:
1820
using: 'node12'
1921
main: 'dist/index.js'

dist/index.js

Lines changed: 32980 additions & 739 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
const path = require('path');
21
const core = require('@actions/core');
3-
const io = require('@actions/io');
2+
const aws = require('aws-sdk');
43

54
async function run() {
65
try {
76
// Get inputs
87
const accessKeyId = core.getInput('aws-access-key-id', { required: true });
98
const secretAccessKey = core.getInput('aws-secret-access-key', { required: true });
10-
const defaultRegion = core.getInput('aws-default-region', { required: true });
11-
const outputFormat = core.getInput('aws-default-output', { required: false });
12-
const awsHome = path.join(process.env.RUNNER_TEMP, '.aws');
9+
const region = core.getInput('aws-region', { required: true });
10+
const sessionToken = core.getInput('aws-session-token', { required: false });
1311

14-
// Ensure awsHome is a directory that exists
15-
await io.mkdirP(awsHome);
16-
17-
// Configure the AWS CLI using environment variables
12+
// Configure the AWS CLI and AWS SDKs using environment variables
1813

1914
// AWS_ACCESS_KEY_ID:
2015
// Specifies an AWS access key associated with an IAM user or role
@@ -24,21 +19,22 @@ async function run() {
2419
// Specifies the secret key associated with the access key. This is essentially the "password" for the access key.
2520
core.exportVariable('AWS_SECRET_ACCESS_KEY', secretAccessKey);
2621

27-
// AWS_DEFAULT_REGION:
28-
// Specifies the AWS Region to send requests to
29-
core.exportVariable('AWS_DEFAULT_REGION', defaultRegion);
30-
31-
// AWS_DEFAULT_OUTPUT:
32-
// Specifies the output format to use
33-
core.exportVariable('AWS_DEFAULT_OUTPUT', outputFormat);
22+
// AWS_SESSION_TOKEN:
23+
// Specifies the session token value that is required if you are using temporary security credentials.
24+
if (sessionToken) {
25+
core.exportVariable('AWS_SESSION_TOKEN', sessionToken);
26+
}
3427

35-
// AWS_CONFIG_FILE:
36-
// Specifies the location of the file that the AWS CLI uses to store configuration profiles.
37-
core.exportVariable('AWS_CONFIG_FILE', path.join(awsHome, 'config'));
38-
39-
// AWS_SHARED_CREDENTIALS_FILE:
40-
// Specifies the location of the file that the AWS CLI uses to store access keys.
41-
core.exportVariable('AWS_SHARED_CREDENTIALS_FILE', path.join(awsHome, 'credentials'));
28+
// AWS_DEFAULT_REGION and AWS_REGION:
29+
// Specifies the AWS Region to send requests to
30+
core.exportVariable('AWS_DEFAULT_REGION', region);
31+
core.exportVariable('AWS_REGION', region);
32+
33+
// Get the AWS account ID
34+
const sts = new aws.STS();
35+
const identity = await sts.getCallerIdentity().promise();
36+
const accountId = identity.Account;
37+
core.setOutput('aws-account-id', accountId);
4238
}
4339
catch (error) {
4440
core.setFailed(error.message);

index.test.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
const core = require('@actions/core');
2-
const io = require('@actions/io');
32

43
const run = require('.');
54

65
jest.mock('@actions/core');
7-
jest.mock('@actions/io');
86

9-
describe('Setup AWS', () => {
7+
const mockStsCallerIdentity = jest.fn();
8+
jest.mock('aws-sdk', () => {
9+
return {
10+
STS: jest.fn(() => ({
11+
getCallerIdentity: mockStsCallerIdentity
12+
}))
13+
};
14+
});
15+
16+
describe('Configure AWS Credentials', () => {
1017

1118
beforeEach(() => {
1219
jest.clearAllMocks();
@@ -16,38 +23,48 @@ describe('Setup AWS', () => {
1623
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
1724
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
1825
.mockReturnValueOnce('us-east-2') // aws-default-region
19-
.mockReturnValueOnce('json'); // aws-default-output
26+
.mockReturnValueOnce('MY-AWS-SESSION-TOKEN'); // aws-session-token
27+
28+
mockStsCallerIdentity.mockImplementation(() => {
29+
return {
30+
promise() {
31+
return Promise.resolve({ Account: '123456789012' });
32+
}
33+
};
34+
});
2035
});
2136

2237
test('exports env vars', async () => {
2338
await run();
24-
expect(core.exportVariable).toHaveBeenCalledTimes(6);
39+
expect(core.exportVariable).toHaveBeenCalledTimes(5);
2540
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', 'MY-AWS-ACCESS-KEY-ID');
2641
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', 'MY-AWS-SECRET-ACCESS-KEY');
42+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SESSION_TOKEN', 'MY-AWS-SESSION-TOKEN');
2743
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'us-east-2');
28-
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_OUTPUT', 'json');
29-
expect(core.exportVariable).toHaveBeenCalledWith('AWS_CONFIG_FILE', '/runner/home/.aws/config');
30-
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SHARED_CREDENTIALS_FILE', '/runner/home/.aws/credentials');
44+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'us-east-2');
45+
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
3146
});
3247

33-
test('aws can be configured for a different region', async () => {
48+
test('session token is optional', async () => {
3449
core.getInput = jest
3550
.fn()
3651
.mockReturnValueOnce('MY-AWS-ACCESS-KEY-ID') // aws-access-key-id
3752
.mockReturnValueOnce('MY-AWS-SECRET-ACCESS-KEY') // aws-secret-access-key
38-
.mockReturnValueOnce('eu-west-1') // aws-default-region
39-
.mockReturnValueOnce('json'); // aws-default-output
53+
.mockReturnValueOnce('eu-west-1'); // aws-default-region
4054

4155
await run();
56+
expect(core.exportVariable).toHaveBeenCalledTimes(4);
57+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', 'MY-AWS-ACCESS-KEY-ID');
58+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', 'MY-AWS-SECRET-ACCESS-KEY');
4259
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'eu-west-1');
60+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'eu-west-1');
61+
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', '123456789012');
4362
});
4463

4564
test('error is caught by core.setFailed', async () => {
46-
io.mkdirP = jest
47-
.fn()
48-
.mockImplementation(() => {
49-
throw new Error();
50-
});
65+
mockStsCallerIdentity.mockImplementation(() => {
66+
throw new Error();
67+
});
5168

5269
await run();
5370

jest.config.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

jest.setup-env.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)