Skip to main content

CDK constructs to use OpenID Connect for authenticating your Github Action workflow with AWS IAM

Project description

AWS CDK Github OpenID Connect

cdk-support release codecov


AWS CDK constructs that define:

  • Github Actions as OpenID Connect Identity Provider into AWS IAM
  • IAM Roles that can be assumed by Github Actions workflows

These constructs allows you to harden your AWS deployment security by removing the need to create long-term access keys for Github Actions and instead use OpenID Connect to Authenticate your Github Action workflow with AWS IAM.

Background information

github-aws-oidc


Getting started

npm i -D aws-cdk-github-oidc

OpenID Connect Identity Provider trust for AWS IAM

To create a new Github OIDC provider configuration into AWS IAM:

import { GithubActionsIdentityProvider } from "aws-cdk-github-oidc";

const provider = new GithubActionsIdentityProvider(scope, "GithubProvider");

In the background this creates an OIDC provider trust configuration into AWS IAM with an issuer URL of https://token.actions.githubusercontent.com and audiences (client IDs) configured as ['sts.amazonaws.com'] (which matches the aws-actions/configure-aws-credentials implementation).


Retrieving a reference to an existing Github OIDC provider configuration

Remember, there can be only one (Github OIDC provider per AWS Account), so to retrieve a reference to existing Github OIDC provider use fromAccount static method:

import { GithubActionsIdentityProvider } from "aws-cdk-github-oidc";

const provider = GithubActionsIdentityProvider.fromAccount(
  scope,
  "GithubProvider"
);

Defining a role for Github Actions workflow to assume

import { GithubActionsRole } from "aws-cdk-github-oidc";

const uploadRole = new GithubActionsRole(scope, "UploadRole", {
  provider: provider, // reference into the OIDC provider
  owner: "octo-org", // your repository owner (organization or user) name
  repo: "octo-repo", // your repository name (without the owner name)
  filter: "ref:refs/tags/v*", // JWT sub suffix filter, defaults to '*'
});

// use it like any other role, for example grant S3 bucket write access:
myBucket.grantWrite(uploadRole);

You may pass in any iam.RoleProps into the construct's props, except assumedBy which will be defined by this construct (CDK will fail if you do):

const deployRole = new GithubActionsRole(scope, "DeployRole", {
  provider: provider,
  owner: "octo-org",
  repo: "octo-repo",
  roleName: "MyDeployRole",
  description: "This role deploys stuff to AWS",
  maxSessionDuration: cdk.Duration.hours(2),
});

// You may also use various "add*" policy methods!
// "AdministratorAccess" not really a good idea, just for an example here:
deployRole.addManagedPolicy(
  iam.ManagedPolicy.fromAwsManagedPolicyName("AdministratorAccess")
);

Subject Filter

By default the value of filter property will be '*' which means any workflow (from given repository) from any branch, tag, environment or pull request can assume this role. To further stricten the OIDC trust policy on the role, you may adjust the subject filter as seen on the examples in Github Docs; For example:

filter value Descrition
'ref:refs/tags/v*' Allow only tags with prefix of v
'ref:refs/heads/demo-branch' Allow only from branch demo-branch
'pull_request' Allow only from pull request
'environment:Production' Allow only from Production environment

Github Actions Workflow

To actually utilize this in your Github Actions workflow, use aws-actions/configure-aws-credentials to assume a role.

At the moment you must use the master version (until AWS releases a new tag):

jobs:
  deploy:
    name: Upload to Amazon S3
    runs-on: ubuntu-latest
    permissions:
      id-token: write # needed to interact with GitHub's OIDC Token endpoint.
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@master
        with:
          role-to-assume: arn:aws:iam::123456789012:role/MyUploadRole
          #role-session-name: MySessionName # Optional
          aws-region: us-east-1

      - name: Sync files to S3
        run: |
          aws s3 sync . s3://my-example-bucket

Migration Guide

v2→v3

No additional steps required, as the v3 major version does not introduce any breaking changes (just a lot of internal tooling changes).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws_cdk_github_oidc-3.1.1.tar.gz (106.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk_github_oidc-3.1.1-py3-none-any.whl (104.2 kB view details)

Uploaded Python 3

File details

Details for the file aws_cdk_github_oidc-3.1.1.tar.gz.

File metadata

  • Download URL: aws_cdk_github_oidc-3.1.1.tar.gz
  • Upload date:
  • Size: 106.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.14.4

File hashes

Hashes for aws_cdk_github_oidc-3.1.1.tar.gz
Algorithm Hash digest
SHA256 cb957e88a2ef7e534fd61879f41b5da2410f92488f99c482873a9d48eb79245d
MD5 2b3e2cf1c729726c2b40d33d65017445
BLAKE2b-256 eb88c8915c98682244e3d71c572839efb204ed88650fcb4b0b0a83d1acbc4e43

See more details on using hashes here.

Provenance

The following attestation bundles were made for aws_cdk_github_oidc-3.1.1.tar.gz:

Publisher: release-v3.yml on aripalo/aws-cdk-github-oidc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aws_cdk_github_oidc-3.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aws_cdk_github_oidc-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd861d6c42f1c34576bcc4565e0be04fac87ec110642ee99dd570e703c345376
MD5 41123003b5b3142b371e075e4d45d62e
BLAKE2b-256 02ef8c37cc8e0534d6c1cd254d700b65bfcd278a84998ce561522c0b488b7738

See more details on using hashes here.

Provenance

The following attestation bundles were made for aws_cdk_github_oidc-3.1.1-py3-none-any.whl:

Publisher: release-v3.yml on aripalo/aws-cdk-github-oidc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page