Skip to content

Commit 76997ec

Browse files
fix: action fails when intending to use existing credentials (#796)
* fix: action fails when intending to use existing credentials * fix: action fails when intending to use existing credentials * fix: action fails when intending to use existing credentials * fix: action fails when intending to use existing credentials * fix: action fails when intending to use existing credentials * fix: action fails when intending to use existing credentials --------- Co-authored-by: Tom Keller <1083460+kellertk@users.noreply.github.com>
1 parent a962633 commit 76997ec

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

.github/workflows/tests-integ.yml

+22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ jobs:
4848
role-to-assume: ${{ secrets.SECRETS_AWS_ROLE_TO_ASSUME }}
4949
role-session-name: IntegAccessKeysAssumeRole
5050
role-external-id: ${{ secrets.SECRETS_AWS_ROLE_EXTERNAL_ID }}
51+
integ-access-keys-env:
52+
strategy:
53+
fail-fast: false
54+
matrix:
55+
os: [[self-hosted, linux-fargate], windows-latest, ubuntu-latest, macos-latest]
56+
node: [14, 16, 18]
57+
name: Run access key from env integ tests
58+
runs-on: ${{ matrix.os }}
59+
timeout-minutes: 30
60+
steps:
61+
- name: "Checkout repository"
62+
uses: actions/checkout@v3
63+
- name: Integ test for access keys
64+
uses: ./
65+
env:
66+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
67+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
68+
with:
69+
aws-region: us-west-2
70+
role-to-assume: ${{ secrets.SECRETS_AWS_ROLE_TO_ASSUME }}
71+
role-session-name: IntegAccessKeysAssumeRole
72+
role-external-id: ${{ secrets.SECRETS_AWS_ROLE_EXTERNAL_ID }}
5173
integ-iam-user:
5274
strategy:
5375
fail-fast: false

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ We recommend using [GitHub's OIDC provider](https://docs.github.com/en/actions/d
161161

162162
The following table describes which method is used based on which values are supplied to the Action:
163163

164-
| **Identity Used** | `aws-access-key-id` | `role-to-assume` | `web-identity-token-file` | `role-chaining` |
165-
| --------------------------------------------------------------- | ------------------- | ---------------- | ------------------------- | - |
166-
| [✅ Recommended] Assume Role directly using GitHub OIDC provider | | ✔ | | |
167-
| IAM User | ✔ | | | |
168-
| Assume Role using IAM User credentials | ✔ | ✔ | | |
169-
| Assume Role using WebIdentity Token File credentials | | ✔ | ✔ | |
170-
| Assume Role using existing credentials | | ✔ | | ✔ |
164+
| **Identity Used** | `aws-access-key-id` | `role-to-assume` | `web-identity-token-file` | `role-chaining` | `id-token` permission
165+
| --------------------------------------------------------------- | ------------------- | ---------------- | ------------------------- | - | - |
166+
| [✅ Recommended] Assume Role directly using GitHub OIDC provider | | ✔ | | | ✔ |
167+
| IAM User | ✔ | | | | |
168+
| Assume Role using IAM User credentials | ✔ | ✔ | | | |
169+
| Assume Role using WebIdentity Token File credentials | | ✔ | ✔ | | |
170+
| Assume Role using existing credentials | | ✔ | | ✔ | |
171+
172+
*Note: `role-chaining` is not necessary to use existing credentials in every use case. If you're getting a "Credentials loaded by the SDK do not match" error, try enabling this prop.
171173

172174
### Credential Lifetime
173175
The default session duration is **1 hour**.

dist/index.js

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export async function run() {
7878
!roleChaining
7979
) {
8080
core.info(
81-
'It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission?'
81+
'It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission? ' +
82+
'If you are not trying to authenticate with OIDC and the action is working successfully, you can ignore this message.'
8283
);
8384
}
8485
return (
@@ -127,11 +128,15 @@ export async function run() {
127128
// the source credentials to already be masked as secrets
128129
// in any error messages.
129130
exportCredentials({ AccessKeyId, SecretAccessKey, SessionToken });
130-
} else if (!webIdentityTokenFile && !roleChaining) {
131+
} else if (
132+
!webIdentityTokenFile &&
133+
!roleChaining &&
134+
!(process.env['AWS_ACCESS_KEY_ID'] && process.env['AWS_SECRET_ACCESS_KEY'])
135+
) {
131136
throw new Error('Could not determine how to assume credentials. Please check your inputs and try again.');
132137
}
133138

134-
if (AccessKeyId || roleChaining) {
139+
if (AccessKeyId || roleChaining || (process.env['AWS_ACCESS_KEY_ID'] && process.env['AWS_SECRET_ACCESS_KEY'])) {
135140
// Validate that the SDK can actually pick up credentials.
136141
// This validates cases where this action is using existing environment credentials,
137142
// and cases where the user intended to provide input credentials but the secrets inputs resolved to empty strings.

test/index.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,33 @@ describe('Configure AWS Credentials', () => {
519519
await run();
520520

521521
expect(core.info).toHaveBeenCalledWith(
522-
'It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission?'
522+
'It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission?' +
523+
' If you are not trying to authenticate with OIDC and the action is working successfully, you can ignore this message.'
523524
);
524525
expect(core.setFailed).toHaveBeenCalledWith(
525526
'Could not determine how to assume credentials. Please check your inputs and try again.'
526527
);
527528
});
528529

530+
test('Assume role with existing credentials if nothing else set', async () => {
531+
process.env['AWS_ACCESS_KEY_ID'] = FAKE_ACCESS_KEY_ID;
532+
process.env['AWS_SECRET_ACCESS_KEY'] = FAKE_SECRET_ACCESS_KEY;
533+
jest.spyOn(core, 'getInput').mockImplementation(
534+
mockGetInput({
535+
'role-to-assume': ROLE_ARN,
536+
'aws-region': FAKE_REGION,
537+
})
538+
);
539+
540+
await run();
541+
542+
expect(core.info).toHaveBeenCalledWith(
543+
'It looks like you might be trying to authenticate with OIDC. Did you mean to set the `id-token` permission?' +
544+
' If you are not trying to authenticate with OIDC and the action is working successfully, you can ignore this message.'
545+
);
546+
expect(mockedSTS.commandCalls(AssumeRoleCommand).length).toEqual(1);
547+
});
548+
529549
test('role assumption fails after maximum trials using OIDC provider', async () => {
530550
process.env['GITHUB_ACTIONS'] = 'true';
531551
process.env['ACTIONS_ID_TOKEN_REQUEST_TOKEN'] = 'test-token';

0 commit comments

Comments
 (0)