この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
2021/11/16 この記事の内容は古くなっています。新しい内容を反映した次の記事をご参考ください。 https://dev.classmethod.jp/articles/github-actions-oidc-configure-aws-credentials/
吉川@広島です。
- GitHub ActionsでAWSの永続的なクレデンシャルを渡すことなくIAM Roleが利用できるようになったようです | DevelopersIO
- aws-actions/configure-aws-credentialsがOIDCプロバイダを介したSwitchRoleに対応していたので実装を辿ってみた | DevelopersIO
アクセスキーを発行せずにOIDCプロバイダ+IAMロールでGitHub Actionsに権限を与えることができるという素晴らしいニュースですね。
で、このOIDCを使った権限取得がaws-actions/configure-aws-credentialsで実装されたということで早速試してみました。
注意点(2021/10/1時点)
OIDCの機能追加は現時点では正式リリースバージョンにまだ含まれていないようです。そのためバージョンではなくコミットハッシュを指定して機能を利用する方法を紹介しています。
IDプロバイダとIAMロールを作成
以下のどちらかの方法でIDプロバイダとIAMロールを作成します。
CloudFormationで作成する場合
- AWS federation comes to GitHub Actions | Aidan Steele’s blog (usually about AWS)
- GitHub ActionsでAWSの永続的なクレデンシャルを渡すことなくIAM Roleが利用できるようになったようです | DevelopersIO
- aws-actions/configure-aws-credentials: Configure AWS credential environment variables for use in other GitHub Actions.
上記を参考に作成しました。
Parameters:
GitHubOrg:
Type: String
Default: "" # GitHubリポジトリのOrganization名を入力
RepositoryName:
Type: String
Default: "" # GitHubリポジトリ名を入力
OIDCProviderArn:
Description: Arn for the GitHub OIDC Provider.
Default: "" # 空欄でOK
Type: String
Conditions:
CreateOIDCProvider: !Equals
- !Ref OIDCProviderArn
- ""
Resources:
Role:
Type: AWS::IAM::Role
Properties:
RoleName: ExampleGithubRole # ロール名は任意
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRoleWithWebIdentity
Principal:
Federated: !Ref GithubOidc
Condition:
StringLike:
vstoken.actions.githubusercontent.com:sub: !Sub repo:${GitHubOrg}/${RepositoryName}:*
GithubOidc:
Type: AWS::IAM::OIDCProvider
Condition: CreateOIDCProvider
Properties:
Url: https://vstoken.actions.githubusercontent.com
ClientIdList: [sigstore] # sigstoreである必要がある
ThumbprintList: [a031c46782e6e6c662c2c87c76da9aa62ccabd8e]
Outputs:
Role:
Value: !GetAtt Role.Arn
手動で作成する場合
まずID Providerを作成します。
- プロバイダのタイプ: OpenIDConnect
- プロバイダのURL: https://vstoken.actions.githubusercontent.com
- 対象者: sigstore
として作成します。
対象者の値がなぜ sigstore
なのか? についてですが、
このconfigure-aws-credencialsのソース内の
const { data } = await axios.get(`${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=sigstore`, {
headers: {"Authorization": `bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}`}
}
);
audience=sigstore
の値と合致させる必要があるため、という理解をしています。
続いてIAM Roleを作成します。
- ロール名: 任意(ここではExampleGithubRoleとする)
- ポリシー: 任意(ここではReadOnlyAccessとする)
として作成します。
IAMロールを作成したら信頼関係を編集します。
{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::{ACCOUNT_ID}:oidc-provider/vstoken.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringLike": {
"vstoken.actions.githubusercontent.com:sub": "repo:{GITHUB_ORGANIZATION_NAME}/{GITHUB_REPO_NAME}:*"
}
}
}
]
}
- {ACCOUNT_ID}
- {GITHUB_ORGANIZATION_NAME}
- {GITHUB_REPO_NAME}
にそれぞれ適切な値を埋めて更新します。
GitHub Actions
Could not load credentials from any providers · Issue #271 · aws-actions/configure-aws-credentials
こちらのIssueを参考に、次のようなworkflowファイルを作成しました。
name: Build
on:
push:
jobs:
build:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@b8c74de753fbcb4868bf2011fb2e15826ce973af # コミットハッシュを指定
with:
aws-region: ap-northeast-1
role-to-assume: arn:aws:iam::{AWS_ACCOUNT_ID}:role/ExampleGithubRole
role-session-name: MySessionName
- run: aws sts get-caller-identity
- {AWS_ACCOUNT_ID}
に適切な値を埋め込みます。ロール名をExampleGithubRoleにしなかった場合はこちらも変更が必要です。
以上で aws sts get-caller-identity
の結果を取得できました。