Skip to main content

AWS CDK construct to create OIDC roles for CircleCI jobs

Project description

@blimmer/cdk-github-oidc

This repository contains constructs to communicate between GitHub Actions and AWS via an Open ID Connect (OIDC) provider. The process is described in this GitHub Actions documentation.

Security Benefits

By using the OpenID Connect provider, you can communicate with AWS from GitHub Actions without saving static credentials (e.g., AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) in your GitHub Actions secret variables. Removing static credentials is a best practice for security.

Quick Start

Installation

npm install --save @blimmer/cdk-github-oidc

or

yarn add @blimmer/cdk-github-oidc

Create or Import a Provider

Each AWS account must be bootstrapped with an OIDC provider. Use the GithubActionsIdentityProvider construct to create a new provider.

import { GithubActionsIdentityProvider } from "@blimmer/cdk-github-oidc";

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const provider = new GithubActionsIdentityProvider(this, "Provider");
  }
}

If you want to use an existing provider, you can import it using the GithubActionsIdentityProvider.fromAccount() method.

import { GithubActionsIdentityProvider } from "@blimmer/cdk-github-oidc";

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const provider = GithubActionsIdentityProvider.fromAccount(this);
  }
}

Create a Role

Once you have a handle to a provider, you can create a role assumed by GitHub Actions.

import { GithubActionsRole, GithubActionsIdentityProvider, BranchFilter } from "@blimmer/cdk-github-oidc";

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const provider = new GithubActionsIdentityProvider(this, "Provider");

    const role = new GithubActionsRole(this, "Role", {
      provider,
      roleName: "my-github-actions-role",
      description: "Role assumed by GitHub Actions",
      subjectFilters: [new BranchFilter({ owner: "blimmer", repository: "cdk-github-oidc", branch: "*" })],
    });
  }
}

Subject Filters

As you can see in the example above, you must pass one or more SubjectFilters to the GithubActionsRole construct. These filters are used to determine which GitHub Actions workflows can assume the role.

This construct exposes first class support for the following filters:

  • BranchFilter

    // Allow all branches
    new BranchFilter({ owner: "blimmer", repository: "cdk-github-oidc", branch: "*" });
    
    // Specify a branch
    new BranchFilter({ owner: "blimmer", repository: "cdk-github-oidc", branch: "main" });
    
    // Specify a branch pattern
    new BranchFilter({ owner: "blimmer", repository: "cdk-github-oidc", branch: "feature/*" });
    
  • TagFilter

    // Allow all tags
    new TagFilter({ owner: "blimmer", repository: "cdk-github-oidc", tag: "*" });
    
    // Specify a tag
    new TagFilter({ owner: "blimmer", repository: "cdk-github-oidc", tag: "v1.0.0" });
    
    // Specify a tag pattern
    new TagFilter({ owner: "blimmer", repository: "cdk-github-oidc", tag: "v1.*" });
    
  • EnvironmentFilter

    // Allow all environments
    new EnvironmentFilter({ owner: "blimmer", repository: "cdk-github-oidc", environment: "*" });
    
    // Specify an environment
    new EnvironmentFilter({ owner: "blimmer", repository: "cdk-github-oidc", environment: "staging" });
    
  • PullRequestFilter

    // Allow all pull requests
    new PullRequestFilter({ owner: "blimmer", repository: "cdk-github-oidc" });
    

If none of these filters fit your use case, you can implement your via the IGithubActionOidcFilter interface, or use the CustomFilter construct.

You can learn more about subject filters in the Github docs

Granting Permissions to the Role

The GithubActionsRole construct is a Role construct, so you can use all of the same properties and methods as you would with a normal CDK IAM Role construct.

import { GithubActionsRole, GithubActionsIdentityProvider, BranchFilter } from "@blimmer/cdk-github-oidc";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { PolicyStatement } from "aws-cdk-lib/aws-iam";

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const bucket = new Bucket(this, "Bucket");

    const provider = new GithubActionsIdentityProvider(this, "Provider");
    const role = new GithubActionsRole(this, "Role", {
      provider,
      roleName: "my-github-actions-role",
      description: "Role assumed by GitHub Actions",
      subjectFilters: [new BranchFilter({ owner: "blimmer", repository: "cdk-github-oidc", branch: "*" })],
    });

    // Grant access via CDK `grant*` methods
    // https://docs.aws.amazon.com/cdk/v2/guide/permissions.html#permissions_grants
    role.grantReadWrite(bucket);

    // Add a custom policy
    role.addToPolicy(new PolicyStatement({ actions: ["s3:PutObject"], resources: ["arn:aws:s3:::my-bucket/*"] }));
  }
}

Using a Role in a Workflow

To use a role in a GitHub Actions workflow, you can use the aws-actions/configure-aws-credentials action.

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write # Required for OIDC role assumption
    steps:
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/my-github-actions-role
          region: us-west-2

See the aws-actions/configure-aws-credentials docs for more details.

Usage

For detailed API docs, see API.md.

Python

This package is available for Python as cdk-github-oidc.

pip install cdk-github-oidc

Migrating from aws-cdk-github-oidc

This package was inspired by aws-cdk-github-oidc, but this package became somewhat unmaintained.

For a role that looked like this in aws-cdk-github-oidc:

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

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

The equivalent role in this package looks like this:

import { GithubActionsIdentityProvider, GithubActionsRole, BranchFilter } from "@blimmer/cdk-github-oidc";

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

Resources

Contributing

Contributions, issues, and feedback are welcome!

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

cdk_github_oidc-0.0.0.tar.gz (70.7 kB view details)

Uploaded Source

Built Distribution

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

cdk_github_oidc-0.0.0-py3-none-any.whl (68.3 kB view details)

Uploaded Python 3

File details

Details for the file cdk_github_oidc-0.0.0.tar.gz.

File metadata

  • Download URL: cdk_github_oidc-0.0.0.tar.gz
  • Upload date:
  • Size: 70.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for cdk_github_oidc-0.0.0.tar.gz
Algorithm Hash digest
SHA256 347dec65993b4a80f979a8545c674235e77375a05ec4b4bc4878cbd7140171ae
MD5 a493ab6e6f76e6aa9e8e726899632a9c
BLAKE2b-256 289173010e07773280dee3760ee18fd99d6b8f61723493dcbdf4bb136ec15a17

See more details on using hashes here.

File details

Details for the file cdk_github_oidc-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_github_oidc-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75b43451aea6d639469595f36cc63056ef3a759cf1f191dbf794bb94844aec4d
MD5 0a53c7efcba78b1b4bd16342733197f5
BLAKE2b-256 9bec2188a950408e6e42cb2f8d124a937ab313f6022ac4bd17aa6f2bfe9b356b

See more details on using hashes here.

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