Skip to content

Commit 0c2c1f7

Browse files
authored
fix: clear session token env var if present for non-session credentials (#65)
1 parent 2d01f93 commit 0c2c1f7

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Add the following step to your workflow:
3131
```
3232
3333
For example, you can use this action with the AWS CLI available in [GitHub's hosted virtual environments](https://help.github.com/en/actions/reference/software-installed-on-github-hosted-runners).
34+
You can also run this action multiple times to use different AWS accounts, regions, or IAM roles in the same GitHub Actions workflow job.
3435
3536
```yaml
3637
jobs:
@@ -42,16 +43,27 @@ jobs:
4243
- name: Checkout
4344
uses: actions/checkout@v2
4445

45-
- name: Configure AWS credentials
46+
- name: Configure AWS credentials from Test account
4647
uses: aws-actions/configure-aws-credentials@v1
4748
with:
48-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
49-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
50-
aws-region: us-east-2
49+
aws-access-key-id: ${{ secrets.TEST_AWS_ACCESS_KEY_ID }}
50+
aws-secret-access-key: ${{ secrets.TEST_AWS_SECRET_ACCESS_KEY }}
51+
aws-region: us-east-1
52+
53+
- name: Copy files to the test website with the AWS CLI
54+
run: |
55+
aws s3 sync . s3://my-s3-test-website-bucket
56+
57+
- name: Configure AWS credentials from Production account
58+
uses: aws-actions/configure-aws-credentials@v1
59+
with:
60+
aws-access-key-id: ${{ secrets.PROD_AWS_ACCESS_KEY_ID }}
61+
aws-secret-access-key: ${{ secrets.PROD_AWS_SECRET_ACCESS_KEY }}
62+
aws-region: us-west-2
5163

52-
- name: Copy files to S3 with the AWS CLI
64+
- name: Copy files to the production website with the AWS CLI
5365
run: |
54-
aws s3 sync . s3://my-s3-website-bucket
66+
aws s3 sync . s3://my-s3-prod-website-bucket
5567
```
5668
5769
See [action.yml](action.yml) for the full documentation for this action's inputs and outputs.

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ function exportCredentials(params){
107107
if (sessionToken) {
108108
core.exportVariable('AWS_SESSION_TOKEN', sessionToken);
109109
core.setSecret(sessionToken);
110+
} else if (process.env.AWS_SESSION_TOKEN) {
111+
// clear session token from previous credentials action
112+
core.exportVariable('AWS_SESSION_TOKEN', '');
110113
}
111114
}
112115

index.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ describe('Configure AWS Credentials', () => {
154154
expect(core.setSecret).toHaveBeenCalledWith(FAKE_ACCOUNT_ID);
155155
});
156156

157+
test('session token is cleared if necessary', async () => {
158+
const mockInputs = {...CREDS_INPUTS, 'aws-region': 'eu-west-1'};
159+
core.getInput = jest
160+
.fn()
161+
.mockImplementation(mockGetInput(mockInputs));
162+
process.env.AWS_SESSION_TOKEN = 'helloworld';
163+
164+
await run();
165+
expect(mockStsAssumeRole).toHaveBeenCalledTimes(0);
166+
expect(core.exportVariable).toHaveBeenCalledTimes(5);
167+
expect(core.setSecret).toHaveBeenCalledTimes(3);
168+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_ACCESS_KEY_ID', FAKE_ACCESS_KEY_ID);
169+
expect(core.setSecret).toHaveBeenCalledWith(FAKE_ACCESS_KEY_ID);
170+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SECRET_ACCESS_KEY', FAKE_SECRET_ACCESS_KEY);
171+
expect(core.setSecret).toHaveBeenCalledWith(FAKE_SECRET_ACCESS_KEY);
172+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_SESSION_TOKEN', '');
173+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_DEFAULT_REGION', 'eu-west-1');
174+
expect(core.exportVariable).toHaveBeenCalledWith('AWS_REGION', 'eu-west-1');
175+
expect(core.setOutput).toHaveBeenCalledWith('aws-account-id', FAKE_ACCOUNT_ID);
176+
expect(core.setSecret).toHaveBeenCalledWith(FAKE_ACCOUNT_ID);
177+
});
178+
157179
test('validates region name', async () => {
158180
process.env.SHOW_STACK_TRACE = 'false';
159181

0 commit comments

Comments
 (0)