1+ locals {
2+ oidc_aws_audience = " sts.amazonaws.com"
3+ oidc_github_idp = " token.actions.githubusercontent.com"
4+
5+ # compose the OIDC subject using opinionated set of claims
6+ # TODO: discuss alternative with maintainers
7+ # see 'claims_supported' for all possibilities (some of which would require custom GHA):
8+ # https://token.actions.githubusercontent.com/.well-known/openid-configuration
9+ ordered_claim_names = [
10+ " repo" , " environment" , " ref"
11+ ]
12+
13+ # map user-supplied args to claim names, some of which may be empty
14+ claims_with_values = zipmap (local. ordered_claim_names , [
15+ var . github_repo , var . github_environment , var . github_branch
16+ ])
17+
18+ # construct 'sub' claim parts by selecting non-empty arg values, then combine
19+ claims = [
20+ for claim in local . ordered_claim_names : format (
21+ " %s:%s" ,
22+ claim ,
23+ local . claims_with_values [claim ]
24+ ) if length (local. claims_with_values [claim ]) > 0
25+ ]
26+
27+ oidc_gha_sub = join (" :" , var. allow_pull_request ? concat (
28+ local. claims , [" pull_request" ]
29+ ) : local. claims
30+ )
31+
32+ /*
33+ Alternative, which would place more responsibility on user to specify valid OIDC claims:
34+
35+ `oidc_expected_claims = join(":", [for k,v in var.claim_patterns : "${k}:${v}"])`
36+ */
37+
38+ }
39+
40+ data "aws_caller_identity" "current" {}
41+
42+ resource "aws_iam_openid_connect_provider" "github_actions" {
43+ url = " https://${ local . oidc_github_idp } "
44+
45+ client_id_list = [
46+ local . oidc_aws_audience
47+ ]
48+
49+ thumbprint_list = [" 1b511abead59c6ce207077c0bf0e0043b1382612" ]
50+ }
51+
52+ resource "aws_iam_role" "github_actions_oidc" {
53+
54+ name = var. role_name
55+ managed_policy_arns = var. policy_arns
56+
57+ assume_role_policy = jsonencode ({
58+ " Version" : " 2012-10-17" ,
59+ " Statement" : [{
60+ " Effect" : " Allow" ,
61+ " Principal" : {
62+ " Federated" : " arn:aws:iam::${ data . aws_caller_identity . current . account_id } :oidc-provider/${ local . oidc_github_idp } "
63+ },
64+ " Action" : " sts:AssumeRoleWithWebIdentity" ,
65+ " Condition" : var.use_wildcard ? {
66+ " StringLike" : {
67+ " token.actions.githubusercontent.com:sub" : local.oidc_gha_sub
68+ },
69+ " StringEquals" : {
70+ " token.actions.githubusercontent.com:aud" : local.oidc_aws_audience,
71+ }
72+ } : {
73+ " StringEquals" : {
74+ " token.actions.githubusercontent.com:aud" : local.oidc_aws_audience,
75+ " token.actions.githubusercontent.com:sub" : local.oidc_gha_sub
76+ }
77+ }
78+ }]
79+ })
80+ }
0 commit comments