Skip to content

Commit bbb7578

Browse files
Limit write access to specific prefix
This uses Cognito as a dispatch authority to convert OIDC claims to IAM condition values, and then fitlers the resulting role to only writing into the passed sha. See https://awsteele.com/blog/2023/10/25/aws-role-session-tags-for-github-actions.html for some related context.
1 parent 855e505 commit bbb7578

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

terraform/rustc-ci/impl/artifacts.tf

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ resource "aws_s3_bucket_inventory" "artifacts" {
150150
}
151151
}
152152

153-
resource "aws_iam_role" "try_builds" {
154-
name = "${var.iam_prefix}--try-role"
153+
data "aws_iam_openid_connect_provider" "gha" {
154+
url = "https://token.actions.githubusercontent.com"
155+
}
156+
157+
resource "aws_iam_role" "oidc" {
158+
name = "${var.iam_prefix}--role"
155159

156160
assume_role_policy = jsonencode({
157161
Version = "2012-10-17"
@@ -160,11 +164,29 @@ resource "aws_iam_role" "try_builds" {
160164
Effect = "Allow"
161165
Action = "sts:AssumeRoleWithWebIdentity"
162166
Principal = {
163-
Federated = "arn:aws:iam::890664054962:oidc-provider/token.actions.githubusercontent.com"
167+
Federated = "cognito-identity.amazonaws.com"
168+
}
169+
Condition = {
170+
StringEquals = {
171+
"cognito-identity.amazonaws.com:aud" = "${aws_cognito_identity_pool.main.id}"
172+
// This forces the caller to set the session name according to the caller's run & sha
173+
"sts:RoleSessionName" = "$${aws:RequestTag/run_id}@$${aws:RequestTag/sha}"
174+
"aws:RequestTag/repository" = "${var.source_repo}"
175+
// For now only allow new bors & try builds
176+
"aws:RequestTag/ref" = "refs/heads/automation/bors/try"
177+
"aws:RequestTag/event_name" = "push"
178+
}
179+
}
180+
},
181+
{
182+
Effect = "Allow"
183+
Action = "sts:TagSession"
184+
Principal = {
185+
Federated = "cognito-identity.amazonaws.com"
164186
}
165187
Condition = {
166188
StringEquals = {
167-
"token.actions.githubusercontent.com:sub" = "repo:${var.repo}:ref:refs/heads/automation/bors/try"
189+
"cognito-identity.amazonaws.com:aud" = "${aws_cognito_identity_pool.main.id}"
168190
}
169191
}
170192
}
@@ -180,10 +202,8 @@ resource "aws_iam_role" "try_builds" {
180202
Sid = "ArtifactsBucketWrite"
181203
Effect = "Allow"
182204
Resource = [
183-
"${aws_s3_bucket.artifacts.arn}/rustc-builds-try",
184-
"${aws_s3_bucket.artifacts.arn}/rustc-builds-try/*",
185-
"${aws_s3_bucket.artifacts.arn}/rustc-builds-try-alt",
186-
"${aws_s3_bucket.artifacts.arn}/rustc-builds-try-alt/*",
205+
"${aws_s3_bucket.artifacts.arn}/rustc-builds/$${aws:PrincipalTag/sha}/*",
206+
"${aws_s3_bucket.artifacts.arn}/rustc-builds-alt/$${aws:PrincipalTag/sha}/*",
187207
]
188208
Action = [
189209
"s3:GetObject",

terraform/rustc-ci/impl/cognito.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "aws_cognito_identity_pool" "main" {
2+
identity_pool_name = "${var.iam_prefix}--rustc-ci"
3+
allow_classic_flow = true
4+
allow_unauthenticated_identities = false
5+
openid_connect_provider_arns = ["${data.aws_iam_openid_connect_provider.gha.arn}"]
6+
}
7+
8+
resource "aws_cognito_identity_pool_provider_principal_tag" "gha_mapper" {
9+
identity_pool_id = aws_cognito_identity_pool.main.id
10+
identity_provider_name = data.aws_iam_openid_connect_provider.gha.arn
11+
use_defaults = false
12+
13+
// This maps the claims on the left (from GHA, see https://token.actions.githubusercontent.com/.well-known/openid-configuration)
14+
// to "RequestTag"'s on the right. These are then matchable in the AssumeRole policy.
15+
principal_tags = {
16+
actor = "actor"
17+
workflow_sha = "sha"
18+
run_id = "run_id"
19+
event = "event_name"
20+
ref = "ref"
21+
repository = "repository"
22+
}
23+
}

0 commit comments

Comments
 (0)