From fbbf3856572651519d3cce38635ca41a2e5aecc8 Mon Sep 17 00:00:00 2001 From: Tom Keller <1083460+kellertk@users.noreply.github.com> Date: Thu, 24 Aug 2023 13:05:29 -0700 Subject: [PATCH] feat: add config option for special character handling (#795) --- README.md | 9 +++++ action.yml | 3 ++ src/assumeRole.ts | 5 +-- src/helpers.ts | 9 +++-- src/index.ts | 62 ++++++++++++++++++++----------- test/index.test.ts | 91 +++++++++++++++++++++++----------------------- 6 files changed, 105 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index d6266a294..8423a1c82 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,15 @@ Your account ID is not masked by default in workflow logs since it's not conside #### Unset current credentials Sometimes, existing credentials in your runner can get in the way of the intended outcome, and the recommended solution is to include another step in your workflow which unsets the environment variables set by this action. Now if you set the `unset-current-credentials` input to `true`, the workaround is made eaiser +#### Special characters in AWS_SECRET_ACCESS_KEY +Some edge cases are unable to properly parse an `AWS_SECRET_ACCESS_KEY` if it +contains special characters. For more information, please see the +[AWS CLI documentation](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-troubleshooting.html#tshoot-signature-does-not-match). +If you set the `special-characters-workaround` option, this action will +continually retry fetching credentials until we get one that does not have +special characters. This option overrides the `disable-retry` and +`retry-max-attempts` options. + ## OIDC We recommend using [GitHub's OIDC provider](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) to get short-lived AWS credentials needed for your actions. When using OIDC, this action will create a JWT unique to the workflow run, and it will use this JWT to assume the role. For this action to create the JWT, it is required for your workflow to have the `id-token: write` permission: diff --git a/action.yml b/action.yml index f4cff8099..b6ef5ddfd 100644 --- a/action.yml +++ b/action.yml @@ -70,6 +70,9 @@ inputs: retry-max-attempts: description: The maximum number of attempts it will attempt to retry the assume role call. By default it will retry 12 times required: false + special-characters-workaround: + description: Some environments do not support special characters in AWS_SECRET_ACCESS_KEY. This option will retry fetching credentials until the secret access key does not contain special characters. This option overrides disable-retry and retry-max-attempts. This option is disabled by default + required: false outputs: aws-account-id: description: The AWS account ID for the provided credentials diff --git a/src/assumeRole.ts b/src/assumeRole.ts index e756ace7f..4e6bd7d41 100644 --- a/src/assumeRole.ts +++ b/src/assumeRole.ts @@ -5,7 +5,7 @@ import * as core from '@actions/core'; import type { AssumeRoleCommandInput, STSClient, Tag } from '@aws-sdk/client-sts'; import { AssumeRoleCommand, AssumeRoleWithWebIdentityCommand } from '@aws-sdk/client-sts'; import type { CredentialsClient } from './CredentialsClient'; -import { errorMessage, isDefined, sanitizeGitHubVariables, verifyKeys } from './helpers'; +import { errorMessage, isDefined, sanitizeGitHubVariables } from './helpers'; async function assumeRoleWithOIDC(params: AssumeRoleCommandInput, client: STSClient, webIdentityToken: string) { delete params.Tags; @@ -17,7 +17,6 @@ async function assumeRoleWithOIDC(params: AssumeRoleCommandInput, client: STSCli WebIdentityToken: webIdentityToken, }) ); - verifyKeys(creds.Credentials); return creds; } catch (error) { throw new Error(`Could not assume role with OIDC: ${errorMessage(error)}`); @@ -49,7 +48,6 @@ async function assumeRoleWithWebIdentityTokenFile( WebIdentityToken: webIdentityToken, }) ); - verifyKeys(creds.Credentials); return creds; } catch (error) { throw new Error(`Could not assume role with web identity token file: ${errorMessage(error)}`); @@ -60,7 +58,6 @@ async function assumeRoleWithCredentials(params: AssumeRoleCommandInput, client: core.info('Assuming role with user credentials'); try { const creds = await client.send(new AssumeRoleCommand({ ...params })); - verifyKeys(creds.Credentials); return creds; } catch (error) { throw new Error(`Could not assume role with user credentials: ${errorMessage(error)}`); diff --git a/src/helpers.ts b/src/helpers.ts index 988ef3930..08e19e73f 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -93,18 +93,21 @@ export function reset() { export function verifyKeys(creds: Partial | undefined) { if (!creds) { - return; + return false; } if (creds.AccessKeyId) { if (SPECIAL_CHARS_REGEX.test(creds.AccessKeyId)) { - throw new Error('AccessKeyId contains special characters.'); + core.debug('AccessKeyId contains special characters.'); + return false; } } if (creds.SecretAccessKey) { if (SPECIAL_CHARS_REGEX.test(creds.SecretAccessKey)) { - throw new Error('SecretAccessKey contains special characters.'); + core.debug('SecretAccessKey contains special characters.'); + return false; } } + return true; } // Retries the promise with exponential backoff if the error isRetryable up to maxRetries time. diff --git a/src/index.ts b/src/index.ts index 6c80d3cf6..a3668560c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import * as core from '@actions/core'; +import type { AssumeRoleCommandOutput } from '@aws-sdk/client-sts'; import { assumeRole } from './assumeRole'; import { CredentialsClient } from './CredentialsClient'; import { @@ -8,6 +9,7 @@ import { exportCredentials, exportAccountId, unsetCredentials, + verifyKeys, } from './helpers'; const DEFAULT_ROLE_DURATION = 3600; // One hour (seconds) @@ -43,10 +45,20 @@ export async function run() { const unsetCurrentCredentialsInput = core.getInput('unset-current-credentials', { required: false }) || 'false'; const unsetCurrentCredentials = unsetCurrentCredentialsInput.toLowerCase() === 'true'; const disableRetryInput = core.getInput('disable-retry', { required: false }) || 'false'; - const disableRetry = disableRetryInput.toLowerCase() === 'true'; + let disableRetry = disableRetryInput.toLowerCase() === 'true'; + const specialCharacterWorkaroundInput = + core.getInput('special-characters-workaround', { required: false }) || 'false'; + const specialCharacterWorkaround = specialCharacterWorkaroundInput.toLowerCase() === 'true'; let maxRetries = parseInt(core.getInput('retry-max-attempts', { required: false })) || 12; - if (maxRetries < 1) { - maxRetries = 1; + switch (true) { + case specialCharacterWorkaround: + // 😳 + maxRetries = Number.MAX_SAFE_INTEGER; + disableRetry = false; + break; + case maxRetries < 1: + maxRetries = 1; + break; } for (const managedSessionPolicy of managedSessionPoliciesInput) { managedSessionPolicies.push({ arn: managedSessionPolicy }); @@ -129,25 +141,30 @@ export async function run() { // Get role credentials if configured to do so if (roleToAssume) { - const roleCredentials = await retryAndBackoff( - async () => { - return assumeRole({ - credentialsClient, - sourceAccountId, - roleToAssume, - roleExternalId, - roleDuration, - roleSessionName, - roleSkipSessionTagging, - webIdentityTokenFile, - webIdentityToken, - inlineSessionPolicy, - managedSessionPolicies, - }); - }, - !disableRetry, - maxRetries - ); + let roleCredentials: AssumeRoleCommandOutput; + do { + // eslint-disable-next-line no-await-in-loop + roleCredentials = await retryAndBackoff( + async () => { + return assumeRole({ + credentialsClient, + sourceAccountId, + roleToAssume, + roleExternalId, + roleDuration, + roleSessionName, + roleSkipSessionTagging, + webIdentityTokenFile, + webIdentityToken, + inlineSessionPolicy, + managedSessionPolicies, + }); + }, + !disableRetry, + maxRetries + ); + // eslint-disable-next-line no-unmodified-loop-condition + } while (specialCharacterWorkaround && !verifyKeys(roleCredentials.Credentials)); core.info(`Authenticated as assumedRoleId ${roleCredentials.AssumedRoleUser!.AssumedRoleId!}`); exportCredentials(roleCredentials.Credentials, outputCredentials); // We need to validate the credentials in 2 of our use-cases @@ -173,6 +190,7 @@ export async function run() { } /* c8 ignore start */ +/* istanbul ignore next */ if (require.main === module) { (async () => { await run(); diff --git a/test/index.test.ts b/test/index.test.ts index 4b83bceb3..50a88dd69 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -556,61 +556,62 @@ describe('Configure AWS Credentials', () => { expect(mockedSTS.commandCalls(AssumeRoleWithWebIdentityCommand).length).toEqual(1); }); - test('role assumption fails if access key id contains special characters', async () => { - jest.spyOn(core, 'getInput').mockImplementation(mockGetInput({ ...ASSUME_ROLE_INPUTS })); - - mockedSTS.on(AssumeRoleCommand).resolves({ - Credentials: { - AccessKeyId: 'asdf+', - SecretAccessKey: FAKE_STS_SECRET_ACCESS_KEY, - SessionToken: FAKE_STS_SESSION_TOKEN, - Expiration: new Date(8640000000000000), - }, - }); - - await run(); - - expect(mockedSTS.commandCalls(AssumeRoleCommand).length).toEqual(12); - expect(core.setFailed).toHaveBeenCalledWith( - 'Could not assume role with user credentials: AccessKeyId contains special characters.' - ); - }); - - test('role assumption fails if secret access key contains special characters', async () => { - jest.spyOn(core, 'getInput').mockImplementation(mockGetInput({ ...ASSUME_ROLE_INPUTS })); + test('special character workaround works for AWS_ACCESS_KEY_ID', async () => { + jest + .spyOn(core, 'getInput') + .mockImplementation(mockGetInput({ ...ASSUME_ROLE_INPUTS, 'special-characters-workaround': 'true' })); - mockedSTS.on(AssumeRoleCommand).resolves({ - Credentials: { - AccessKeyId: FAKE_STS_ACCESS_KEY_ID, - SecretAccessKey: 'asdf+', - SessionToken: FAKE_STS_SESSION_TOKEN, - Expiration: new Date(8640000000000000), - }, - }); + mockedSTS + .on(AssumeRoleCommand) + .resolvesOnce({ + Credentials: { + AccessKeyId: FAKE_STS_ACCESS_KEY_ID, + SecretAccessKey: 'asdf+', + SessionToken: FAKE_STS_SESSION_TOKEN, + Expiration: new Date(8640000000000000), + }, + }) + .resolves({ + Credentials: { + AccessKeyId: FAKE_STS_ACCESS_KEY_ID, + SecretAccessKey: 'asdf', + SessionToken: FAKE_STS_SESSION_TOKEN, + Expiration: new Date(8640000000000000), + }, + }); await run(); - expect(mockedSTS.commandCalls(AssumeRoleCommand).length).toEqual(12); - expect(core.setFailed).toHaveBeenCalledWith( - 'Could not assume role with user credentials: SecretAccessKey contains special characters.' - ); + expect(mockedSTS.commandCalls(AssumeRoleCommand).length).toEqual(2); }); - test('role assumption succeeds if keys have no special characters', async () => { - jest.spyOn(core, 'getInput').mockImplementation(mockGetInput({ ...ASSUME_ROLE_INPUTS })); + test('special character workaround works for AWS_SECRET_ACCESS_KEY', async () => { + jest + .spyOn(core, 'getInput') + .mockImplementation(mockGetInput({ ...ASSUME_ROLE_INPUTS, 'special-characters-workaround': 'true' })); - mockedSTS.on(AssumeRoleCommand).resolves({ - Credentials: { - AccessKeyId: FAKE_STS_ACCESS_KEY_ID, - SecretAccessKey: FAKE_STS_SECRET_ACCESS_KEY, - SessionToken: FAKE_STS_SESSION_TOKEN, - Expiration: new Date(8640000000000000), - }, - }); + mockedSTS + .on(AssumeRoleCommand) + .resolvesOnce({ + Credentials: { + AccessKeyId: FAKE_STS_ACCESS_KEY_ID, + SecretAccessKey: 'asdf+', + SessionToken: FAKE_STS_SESSION_TOKEN, + Expiration: new Date(8640000000000000), + }, + }) + .resolves({ + Credentials: { + AccessKeyId: FAKE_STS_ACCESS_KEY_ID, + SecretAccessKey: 'asdf', + SessionToken: FAKE_STS_SESSION_TOKEN, + Expiration: new Date(8640000000000000), + }, + }); await run(); - expect(mockedSTS.commandCalls(AssumeRoleCommand).length).toEqual(1); + expect(mockedSTS.commandCalls(AssumeRoleCommand).length).toEqual(2); }); test('max retries is configurable', async () => {