Last active
October 31, 2021 20:16
-
-
Save guitarrapc/fc64be2fcfafc9bc13bb1e022bb0edf4 to your computer and use it in GitHub Desktop.
Allow GitHub Actions to assume AssumeRoleWithWebIdentity. terraform to create OIDC Provider and IAM Role.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see: https://github.com/guitarrapc/githubactions-lab | |
name: aws oidc credential | |
on: | |
workflow_dispatch: | |
push: | |
branches: ["main"] | |
# allow use id-token | |
permissions: | |
id-token: write | |
contents: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Configure AWS Credentials | |
# must use "master", not "v1". v1 is not yet released to use latest role-to-assume. | |
# Error: Credentials could not be loaded, please check your action inputs: Could not load credentials from any providers | |
uses: aws-actions/configure-aws-credentials@master | |
with: | |
aws-region: ap-northeast-1 | |
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | |
role-session-name: GitHubActions-${{ github.run_id }} | |
role-duration-seconds: 900 # minimum: 900sec, maximum: iam role session duration | |
- name: get-caller-identity is allowed to run on role. | |
run: aws sts get-caller-identity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services | |
// ref: https://github.com/aws-actions/configure-aws-credentials#sample-iam-role-cloudformation-template | |
// docs.github.com is almost correct. However adding aud condition is not satisfy somehow. | |
// oidc provider | |
resource "aws_iam_openid_connect_provider" "main" { | |
url = "https://token.actions.githubusercontent.com" | |
client_id_list = ["sts.amazonaws.com"] | |
thumbprint_list = ["a031c46782e6e6c662c2c87c76da9aa62ccabd8e"] | |
} | |
// role | |
data "aws_iam_policy_document" "github_oid_assume_role_policy" { | |
statement { | |
effect = "Allow" | |
actions = ["sts:AssumeRoleWithWebIdentity"] | |
principals { | |
type = "Federated" | |
identifiers = [aws_iam_openid_connect_provider.main.arn] | |
} | |
# aud があるとはじかれてるので、aud の値がおかしいっぽい。<- /github.com/aws-actions/configure-aws-credentials ではなしになってる :( | |
# condition { | |
# test = "StringEquals" | |
# variable = "token.actions.githubusercontent.com:aud" | |
# values = ["https://github.com/${var.github_owner}"] | |
# } | |
condition { | |
test = "StringLike" | |
variable = "token.actions.githubusercontent.com:sub" | |
values = [for item in var.github_oidc_repo_names : "repo:${var.github_owner}/${item}:*"] | |
} | |
} | |
} | |
data "aws_iam_policy_document" "github_actions" { | |
// allow running `aws sts get-caller-identity` | |
statement { | |
effect = "Allow" | |
actions = ["sts:GetCallerIdentity"] | |
resources = ["*"] | |
} | |
} | |
resource "aws_iam_policy" "github_actions" { | |
name = "githubactions_policy" | |
path = "/" | |
description = "Policy for GitHubActions" | |
policy = data.aws_iam_policy_document.github_actions.json | |
} | |
resource "aws_iam_role" "main" { | |
name = "githubactions-oidc-role" | |
path = "/" | |
description = "Terraform managed." | |
assume_role_policy = data.aws_iam_policy_document.github_oid_assume_role_policy.json | |
} | |
resource "aws_iam_role_policy_attachment" "main" { | |
role = aws_iam_role.main.name | |
policy_arn = aws_iam_policy.github_actions.arn | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment