From a49adeb12f0e7c2f0ae16fe2ff2e2087d6177799 Mon Sep 17 00:00:00 2001 From: Jackson Welsh Date: Sat, 17 Dec 2022 17:53:54 -0600 Subject: [PATCH] feat: allow-list option for aws account id Users may set the `allowed-account-ids` input variable to define a comma-separated list of accounts that may be authenticated against. Resolves #432 --- README.md | 191 +++++++++++++++++++++++++++++--------------------- action.yml | 35 +++++---- index.js | 25 ++++++- index.test.js | 31 ++++++++ 4 files changed, 185 insertions(+), 97 deletions(-) diff --git a/README.md b/README.md index 471a9a8c6..55b3afb78 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,26 @@ ## "Configure AWS Credentials" Action For GitHub Actions -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. +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. ## NOTICE: node12 deprecation warning + GitHub actions has recently started throwing warning messages regarding the deprecation of Node 12. If you would like to stop seeing this warning, configure your action to use `aws-actions/configure-aws-credentials@v1-node16`. Both the `v1` branch and the `v1-node16` branch will receive the same updates moving forward. See [this issue](https://github.com/aws-actions/configure-aws-credentials/issues/489) for more information on this topic. **Table of Contents** +- ["Configure AWS Credentials" Action For GitHub Actions](#configure-aws-credentials-action-for-github-actions) +- [NOTICE: node12 deprecation warning](#notice-node12-deprecation-warning) - [Usage](#usage) - [Credentials](#credentials) - [Assuming a Role](#assuming-a-role) - + [Session tagging](#session-tagging) - + [Sample IAM Role Permissions](#sample-iam-role-cloudformation-template) + - [Examples](#examples) + - [Sample IAM Role CloudFormation Template](#sample-iam-role-cloudformation-template) + - [Session tagging](#session-tagging) - [Self-Hosted Runners](#self-hosted-runners) - + [Proxy Configuration](#proxy-configuration) + - [Proxy Configuration](#proxy-configuration) + - [Use with the AWS CLI](#use-with-the-aws-cli) - [License Summary](#license-summary) - [Security Disclosures](#security-disclosures) @@ -26,11 +31,11 @@ GitHub actions has recently started throwing warning messages regarding the depr Add the following step to your workflow: ```yaml - - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v1 - with: - role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role - aws-region: us-east-2 +- name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role + aws-region: us-east-2 ``` 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). @@ -46,28 +51,28 @@ jobs: id-token: write contents: read steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Configure AWS credentials from Test account - uses: aws-actions/configure-aws-credentials@v1 - with: - role-to-assume: arn:aws:iam::111111111111:role/my-github-actions-role-test - aws-region: us-east-1 - - - name: Copy files to the test website with the AWS CLI - run: | - aws s3 sync . s3://my-s3-test-website-bucket - - - name: Configure AWS credentials from Production account - uses: aws-actions/configure-aws-credentials@v1 - with: - role-to-assume: arn:aws:iam::222222222222:role/my-github-actions-role-prod - aws-region: us-west-2 - - - name: Copy files to the production website with the AWS CLI - run: | - aws s3 sync . s3://my-s3-prod-website-bucket + - name: Checkout + uses: actions/checkout@v2 + + - name: Configure AWS credentials from Test account + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::111111111111:role/my-github-actions-role-test + aws-region: us-east-1 + + - name: Copy files to the test website with the AWS CLI + run: | + aws s3 sync . s3://my-s3-test-website-bucket + + - name: Configure AWS credentials from Production account + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::222222222222:role/my-github-actions-role-prod + aws-region: us-west-2 + + - name: Copy files to the production website with the AWS CLI + run: | + aws s3 sync . s3://my-s3-prod-website-bucket ``` See [action.yml](action.yml) for the full documentation for this action's inputs and outputs. @@ -75,12 +80,25 @@ See [action.yml](action.yml) for the full documentation for this action's inputs ## Credentials 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: -* Do not store credentials in your repository's code. -* [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. -* [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. + +- Do not store credentials in your repository's code. +- [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. +- [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. + +To prevent accidental deploys to the incorrect environment, you may define a comma-separated list of allowed account IDs to configure credentials for: + +```yaml +- name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role + aws-region: us-east-2 + allowed-account-ids: 123456789100 +``` ## Assuming a Role -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 credentials needed for your actions. + +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 credentials needed for your actions. Specifying `role-to-assume` **without** providing an `aws-access-key-id` or a `web-identity-token-file` will signal to the action that you wish to use the OIDC provider. The default session duration is **1 hour** when using the OIDC provider to directly assume an IAM Role or when an `aws-session-token` is directly provided. @@ -93,53 +111,57 @@ The default audience is `sts.amazonaws.com` which you can replace by specifying The following table describes which identity is used based on which values are supplied to the Action: -| **Identity Used** | `aws-access-key-id` | `role-to-assume` | `web-identity-token-file` | -| --------------------------------------------------------------- | ------------------- | ---------------- | ------------------------- | +| **Identity Used** | `aws-access-key-id` | `role-to-assume` | `web-identity-token-file` | +| ---------------------------------------------------------------- | ------------------- | ---------------- | ------------------------- | | [✅ Recommended] Assume Role directly using GitHub OIDC provider | | ✔ | | -| IAM User | ✔ | | | -| Assume Role using IAM User credentials | ✔ | ✔ | | -| Assume Role using WebIdentity Token File credentials | | ✔ | ✔ | +| IAM User | ✔ | | | +| Assume Role using IAM User credentials | ✔ | ✔ | | +| Assume Role using WebIdentity Token File credentials | | ✔ | ✔ | ### Examples ```yaml - - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-region: us-east-2 - role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role - role-session-name: MySessionName +- name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-region: us-east-2 + role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role + role-session-name: MySessionName ``` + In this example, the Action will load the OIDC token from the GitHub-provided environment variable and use it to assume the role `arn:aws:iam::123456789100:role/my-github-actions-role` with the session name `MySessionName`. ```yaml - - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: us-east-2 - role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} - role-external-id: ${{ secrets.AWS_ROLE_EXTERNAL_ID }} - role-duration-seconds: 1200 - role-session-name: MySessionName +- name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-2 + role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} + role-external-id: ${{ secrets.AWS_ROLE_EXTERNAL_ID }} + role-duration-seconds: 1200 + role-session-name: MySessionName ``` -In this example, the secret `AWS_ROLE_TO_ASSUME` contains a string like `arn:aws:iam::123456789100:role/my-github-actions-role`. To assume a role in the same account as the static credentials, you can simply specify the role name, like `role-to-assume: my-github-actions-role`. + +In this example, the secret `AWS_ROLE_TO_ASSUME` contains a string like `arn:aws:iam::123456789100:role/my-github-actions-role`. To assume a role in the same account as the static credentials, you can simply specify the role name, like `role-to-assume: my-github-actions-role`. ```yaml - - name: Configure AWS Credentials for Beta Customers - uses: aws-actions/configure-aws-credentials@v1 - with: - audience: beta-customers - aws-region: us-east-3 - role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role - role-session-name: MySessionName +- name: Configure AWS Credentials for Beta Customers + uses: aws-actions/configure-aws-credentials@v1 + with: + audience: beta-customers + aws-region: us-east-3 + role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role + role-session-name: MySessionName ``` + In this example, the audience has been changed from the default to use a different audience name `beta-customers`. This can help ensure that the role can only affect those AWS accounts whose GitHub OIDC providers have explicitly opted in to the `beta-customers` label. Changing the default audience may be necessary when using non-default [AWS partitions](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html). ### Sample IAM Role CloudFormation Template + ```yaml Parameters: GitHubOrg: @@ -152,7 +174,7 @@ Parameters: Type: String Conditions: - CreateOIDCProvider: !Equals + CreateOIDCProvider: !Equals - !Ref OIDCProviderArn - "" @@ -165,7 +187,7 @@ Resources: - Effect: Allow Action: sts:AssumeRoleWithWebIdentity Principal: - Federated: !If + Federated: !If - CreateOIDCProvider - !Ref GithubOidc - !Ref OIDCProviderArn @@ -178,14 +200,14 @@ Resources: Condition: CreateOIDCProvider Properties: Url: https://token.actions.githubusercontent.com - ClientIdList: + ClientIdList: - sts.amazonaws.com ThumbprintList: - 6938fd4d98bab03faadb97b34396831e3780aea1 Outputs: Role: - Value: !GetAtt Role.Arn + Value: !GetAtt Role.Arn ``` The GitHub OIDC Provider only needs to be created once per account (i.e. multiple IAM Roles that can be assumed by the GitHub's OIDC can share a single OIDC Provider). @@ -194,13 +216,14 @@ To align with the Amazon IAM best practice of [granting least privilege](https:/ For further information on OIDC and GitHub Actions, please see: -* [AWS docs: Creating OpenID Connect (OIDC) identity providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html) -* [AWS docs: IAM JSON policy elements: Condition](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html) -* [GitHub docs: About security hardening with OpenID Connect](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect) -* [GitHub docs: Configuring OpenID Connect in Amazon Web Services](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) -* [GitHub changelog: GitHub Actions: Secure cloud deployments with OpenID Connect](https://github.blog/changelog/2021-10-27-github-actions-secure-cloud-deployments-with-openid-connect/) +- [AWS docs: Creating OpenID Connect (OIDC) identity providers](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc.html) +- [AWS docs: IAM JSON policy elements: Condition](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html) +- [GitHub docs: About security hardening with OpenID Connect](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect) +- [GitHub docs: Configuring OpenID Connect in Amazon Web Services](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) +- [GitHub changelog: GitHub Actions: Secure cloud deployments with OpenID Connect](https://github.blog/changelog/2021-10-27-github-actions-secure-cloud-deployments-with-openid-connect/) ### Session tagging + The session will have the name "GitHubActions" and be tagged with the following tags: (`GITHUB_` environment variable definitions can be [found here](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables)) @@ -214,17 +237,17 @@ The session will have the name "GitHubActions" and be tagged with the following | Branch | GITHUB_REF | | Commit | GITHUB_SHA | -_Note: all tag values must conform to [the requirements](https://docs.aws.amazon.com/STS/latest/APIReference/API_Tag.html). Particularly, `GITHUB_WORKFLOW` will be truncated if it's too long. If `GITHUB_ACTOR` or `GITHUB_WORKFLOW` contain invalid characters, the characters will be replaced with an '*'._ +_Note: all tag values must conform to [the requirements](https://docs.aws.amazon.com/STS/latest/APIReference/API_Tag.html). Particularly, `GITHUB_WORKFLOW` will be truncated if it's too long. If `GITHUB_ACTOR` or `GITHUB_WORKFLOW` contain invalid characters, the characters will be replaced with an '\*'._ -The action will use session tagging by default during role assumption. +The action will use session tagging by default during role assumption. Note that for WebIdentity role assumption, the session tags have to be included in the encoded WebIdentity token. This means that Tags can only be supplied by the OIDC provider and not set during the AssumeRoleWithWebIdentity API call within the Action. You can skip this session tagging by providing `role-skip-session-tagging` as true in the action's inputs: ```yaml - uses: aws-actions/configure-aws-credentials@v1 - with: - role-skip-session-tagging: true +uses: aws-actions/configure-aws-credentials@v1 +with: + role-skip-session-tagging: true ``` ## Self-Hosted Runners @@ -234,26 +257,31 @@ If you run your GitHub Actions in a [self-hosted runner](https://help.github.com If no access key credentials are given in the action inputs, this action will use credentials from the runner environment using the [default methods for the AWS SDK for Javascript](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html). You can use this action to simply configure the region and account ID in the environment, and then use the runner's credentials for all AWS API calls made by your Actions workflow: + ```yaml uses: aws-actions/configure-aws-credentials@v1 with: aws-region: us-east-2 ``` + In this case, your runner's credentials must have permissions to call any AWS APIs called by your Actions workflow. Or, you can use this action to assume a role, and then use the role credentials for all AWS API calls made by your Actions workflow: + ```yaml uses: aws-actions/configure-aws-credentials@v1 with: aws-region: us-east-2 role-to-assume: my-github-actions-role ``` + In this case, your runner's credentials must have permissions to assume the role. You can also assume a role using a web identity token file, such as if using [Amazon EKS IRSA](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html). Pods running in EKS worker nodes that do not run as root can use this file to assume a role with a web identity. You can configure your workflow as follows in order to use this file: + ```yaml uses: aws-actions/configure-aws-credentials@v1 with: @@ -269,6 +297,7 @@ If you run in self-hosted environments and in secured environment where you need Additionally this action will always consider already configured proxy in the environment. Manually configured proxy: + ```yaml uses: aws-actions/configure-aws-credentials@v1 with: @@ -288,13 +317,13 @@ The action will read the underlying proxy configuration from the environment and ### Use with the AWS CLI -This workflow does _not_ install the [AWS CLI](https://aws.amazon.com/cli/) into your environment. Self-hosted runners that intend to run this action prior to executing `aws` commands need to have the AWS CLI [installed](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) if it's not already present. +This workflow does _not_ install the [AWS CLI](https://aws.amazon.com/cli/) into your environment. Self-hosted runners that intend to run this action prior to executing `aws` commands need to have the AWS CLI [installed](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) if it's not already present. Most [GitHub hosted runner environments](https://github.com/actions/virtual-environments) should include the AWS CLI by default. - + ## License Summary This code is made available under the MIT license. ## Security Disclosures -If you would like to report a potential security issue in this project, please do not create a GitHub issue. Instead, please follow the instructions [here](https://aws.amazon.com/security/vulnerability-reporting/) or [email AWS security directly](mailto:aws-security@amazon.com). +If you would like to report a potential security issue in this project, please do not create a GitHub issue. Instead, please follow the instructions [here](https://aws.amazon.com/security/vulnerability-reporting/) or [email AWS security directly](mailto:aws-security@amazon.com). diff --git a/action.yml b/action.yml index b99f07f5e..fab013d67 100644 --- a/action.yml +++ b/action.yml @@ -1,12 +1,12 @@ name: '"Configure AWS Credentials" Action For GitHub Actions' -description: 'Configure AWS credential and region environment variables for use with the AWS CLI and AWS SDKs' +description: "Configure AWS credential and region environment variables for use with the AWS CLI and AWS SDKs" branding: - icon: 'cloud' - color: 'orange' + icon: "cloud" + color: "orange" inputs: audience: - default: 'sts.amazonaws.com' - description: 'The audience to use for the OIDC provider' + default: "sts.amazonaws.com" + description: "The audience to use for the OIDC provider" required: false aws-access-key-id: description: >- @@ -21,10 +21,10 @@ inputs: for example on an EC2 instance. required: false aws-session-token: - description: 'AWS Session Token' + description: "AWS Session Token" required: false aws-region: - description: 'AWS Region, e.g. us-east-2' + description: "AWS Region, e.g. us-east-2" required: true mask-aws-account-id: description: >- @@ -47,21 +47,26 @@ inputs: description: "Role duration in seconds (default: 6 hours, 1 hour for OIDC/specified aws-session-token)" required: false role-session-name: - description: 'Role session name (default: GitHubActions)' + description: "Role session name (default: GitHubActions)" required: false role-external-id: - description: 'The external ID of the role to assume' + description: "The external ID of the role to assume" required: false role-skip-session-tagging: - description: 'Skip session tagging during role assumption' + description: "Skip session tagging during role assumption" required: false http-proxy: - description: 'Proxy to use for the AWS SDK agent' + description: "Proxy to use for the AWS SDK agent" + required: false + allowed-account-ids: + description: >- + Comma-separated list of allowed AWS account IDs to prevent accidental + deploys to the wrong environment. required: false outputs: aws-account-id: - description: 'The AWS account ID for the provided credentials' + description: "The AWS account ID for the provided credentials" runs: - using: 'node12' - main: 'dist/index.js' - post: 'dist/cleanup/index.js' + using: "node12" + main: "dist/index.js" + post: "dist/cleanup/index.js" diff --git a/index.js b/index.js index 06c546b16..0f6bb38f3 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ const MAX_TAG_VALUE_LENGTH = 256; const SANITIZATION_CHARACTER = '_'; const ROLE_SESSION_NAME = 'GitHubActions'; const REGION_REGEX = /^[a-z0-9-]+$/g; +const ACCOUNT_ID_LIST_REGEX = /^\d{12}(,\s?\d{12})*$/ async function assumeRole(params) { // Assume a role to get short-lived credentials using longer-lived credentials. @@ -300,6 +301,7 @@ async function run() { const roleSkipSessionTagging = roleSkipSessionTaggingInput.toLowerCase() === 'true'; const webIdentityTokenFile = core.getInput('web-identity-token-file', { required: false }); const proxyServer = core.getInput('http-proxy', { required: false }); + const allowedAccountIds = core.getInput('allowed-account-ids', { required: false }); if (!region.match(REGION_REGEX)) { throw new Error(`Region is not valid: ${region}`); @@ -307,6 +309,14 @@ async function run() { exportRegion(region); + if (allowedAccountIds && !allowedAccountIds.match(ACCOUNT_ID_LIST_REGEX)) { + let errorMessage = "Allowed account ID list is not valid, must be comma-separated list of 12-digit IDs"; + if (maskAccountId.toLowerCase() == 'false') { + errorMessage += `: ${allowedAccountIds}`; + } + throw new Error(errorMessage); + } + // This wraps the logic for deciding if we should rely on the GH OIDC provider since we may need to reference // the decision in a few differennt places. Consolidating it here makes the logic clearer elsewhere. const useGitHubOIDCProvider = () => { @@ -375,7 +385,20 @@ async function run() { if (!process.env.GITHUB_ACTIONS || accessKeyId) { await validateCredentials(roleCredentials.accessKeyId); } - await exportAccountId(maskAccountId, region); + sourceAccountId = await exportAccountId(maskAccountId, region); + } + + // Check if configured account ID is in the provided allow-list + if (allowedAccountIds) { + // Convert string to a list and trim whitespace. + const accountIdList = allowedAccountIds.split(",").map((id) => id.trim()); + if (!accountIdList.includes(sourceAccountId)) { + let errorMessage = "Account ID of the provided credentials is not in 'allowed-account-ids'"; + if (maskAccountId.toLowerCase() == 'false') { + errorMessage += `: ${sourceAccountId}`; + } + throw new Error(errorMessage); + } } } catch (error) { diff --git a/index.test.js b/index.test.js index e6655adc7..f3ac9f314 100644 --- a/index.test.js +++ b/index.test.js @@ -809,6 +809,37 @@ describe('Configure AWS Credentials', () => { await run(); }); + + test('denies accounts not defined in allow-account-ids', async () => { + process.env.SHOW_STACK_TRACE = 'false'; + + core.getInput = jest + .fn() + .mockImplementation(mockGetInput({ ...DEFAULT_INPUTS, 'allowed-account-ids': '000000000000' })); + + await run(); + expect(core.setFailed).toHaveBeenCalledWith(`Account ID of the provided credentials is not in 'allowed-account-ids'`); + }); + + test('allows accounts defined in allow-account-ids', async () => { + core.getInput = jest + .fn() + .mockImplementation(mockGetInput({ ...DEFAULT_INPUTS, 'allowed-account-ids': `${FAKE_ACCOUNT_ID}, 000000000000` })); + + await run(); + }); + + test('throws if account id list is invalid', async () => { + process.env.SHOW_STACK_TRACE = 'false'; + + // account id is too short + core.getInput = jest + .fn() + .mockImplementation(mockGetInput({ ...DEFAULT_INPUTS, 'allowed-account-ids': '0' })); + + await run(); + expect(core.setFailed).toHaveBeenCalledWith('Allowed account ID list is not valid, must be comma-separated list of 12-digit IDs'); + }) describe('proxy settings', () => {