Skip to content

Commit 8a7996e

Browse files
authored
Merge pull request #28 from hackforla/iam/add-aws-oidc-provider
add gha oidc module and invoke for incubator; add tyler for HomeUniteUs
2 parents ba42645 + 3565f0d commit 8a7996e

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module "iam_oidc_gha_incubator" {
2+
source = "./modules/aws-gha-oidc-providers"
3+
4+
role_name = "gha-incubator"
5+
use_wildcard = true
6+
allow_pull_request = true
7+
github_branch = "refs/heads/*" # allows any branch
8+
github_repo = "hackforla/incubator"
9+
10+
policy_arns = [
11+
"arn:aws:iam::aws:policy/AdministratorAccess"
12+
]
13+
}

terraform/aws-users.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ module "iam_user_JimmyJuarez10" {
99
user_groups = ["read-only-group"]
1010
}
1111

12+
module "iam_user_tylerthome" {
13+
source = "./modules/aws-users"
14+
15+
user_name = "tyler.thome"
16+
user_tags = {
17+
"Project" = "home-unite-us"
18+
"Access Level" = "1"
19+
}
20+
user_groups = ["read-only-group"]
21+
}
22+
1223
module "iam_user_brittanyms" {
1324
source = "./modules/aws-users"
1425

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "role_arn" {
2+
value = aws_iam_role.github_actions_oidc.arn
3+
description = "The ARN of the IAM role for the federated identity"
4+
}
5+
6+
output "provider_arn" {
7+
value = aws_iam_openid_connect_provider.github_actions.arn
8+
description = "The ARN of the OIDC provider"
9+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
variable "role_name" {
2+
description = "The name of the IAM role"
3+
type = string
4+
}
5+
6+
variable "github_repo" {
7+
description = "The repository name (or name wildcard pattern if use_wildcard=true)"
8+
type = string
9+
10+
validation {
11+
condition = can(regex("hackforla/.*", var.github_repo))
12+
error_message = "The github_repo argument must begin with 'hackforla/'"
13+
}
14+
}
15+
16+
variable "github_branch" {
17+
description = "The branch name (or name wildcard pattern if use_wildcard=true)"
18+
type = string
19+
20+
validation {
21+
condition = can(regex("refs/(heads|tags)/.*", var.github_branch))
22+
error_message = "The github_branch argument must begin with 'refs/heads/' or 'refs/tags/"
23+
}
24+
}
25+
26+
variable "github_environment" {
27+
description = "The environment name (optional)"
28+
type = string
29+
default = ""
30+
}
31+
32+
variable "policy_arns" {
33+
description = "The ARN of the policy to attach to the role"
34+
type = list(string)
35+
}
36+
37+
variable "use_wildcard" {
38+
description = "Specifies whether OIDC claim subject should use wildcard pattern"
39+
type = bool
40+
}
41+
42+
variable "allow_pull_request" {
43+
description = "Authorize the token for pull requests"
44+
type = bool
45+
default = false
46+
}
47+
48+
/*
49+
Alternative, which would place more responsibility on user to specify valid OIDC claims:
50+
51+
`variable "claim_patterns" {
52+
description = "Specifies arbitrary "
53+
type = map(string)
54+
}`
55+
*/
56+

0 commit comments

Comments
 (0)