Skip to content

Commit a1a00d6

Browse files
authored
Merge pull request #46 from Homebrew/oidc-docker
roles: add role to be able to push to ECR from GitHub
2 parents f9478cf + 1234db2 commit a1a00d6

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

aws/roles.tf

+59
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,62 @@ resource "aws_iam_role_policy_attachment" "github_tf_opentofu_policy_attachment"
8787
role = aws_iam_role.github_tf.name
8888
policy_arn = aws_iam_policy.opentofu_policy.arn
8989
}
90+
91+
data "aws_iam_policy_document" "ecr_policy_document" {
92+
statement {
93+
effect = "Allow"
94+
actions = [
95+
"ecr:GetAuthorizationToken",
96+
"ecr:BatchCheckLayerAvailability",
97+
"ecr:GetDownloadUrlForLayer",
98+
"ecr:GetRepositoryPolicy",
99+
"ecr:DescribeRepositories",
100+
"ecr:ListImages",
101+
"ecr:DescribeImages",
102+
"ecr:BatchGetImage",
103+
"ecr:InitiateLayerUpload",
104+
"ecr:UploadLayerPart",
105+
"ecr:CompleteLayerUpload",
106+
"ecr:PutImage"
107+
]
108+
resources = ["*"]
109+
}
110+
}
111+
112+
resource "aws_iam_policy" "ecr_policy" {
113+
name = "ECRPushPolicy"
114+
path = "/"
115+
description = "Policy to allow push to ECR"
116+
117+
policy = data.aws_iam_policy_document.ecr_policy_document.json
118+
}
119+
120+
resource "aws_iam_role" "github_ecr_push_role" {
121+
name = "GithubActionsRoleECRPush"
122+
description = "Allow GitHub actions to push to ECR"
123+
assume_role_policy = jsonencode({
124+
Statement = [
125+
{
126+
Action = "sts:AssumeRoleWithWebIdentity"
127+
Effect = "Allow"
128+
Condition = {
129+
StringEquals = {
130+
"token.actions.githubusercontent.com:aud" = "sts.amazonaws.com"
131+
}
132+
StringLike = {
133+
"token.actions.githubusercontent.com:sub" = "repo:Homebrew/ci-orchestrator:*"
134+
}
135+
}
136+
Principal = {
137+
Federated = aws_iam_openid_connect_provider.github_actions.arn
138+
}
139+
}
140+
]
141+
Version = "2012-10-17"
142+
})
143+
}
144+
145+
resource "aws_iam_role_policy_attachment" "github_ecr_policy_attachment" {
146+
role = aws_iam_role.github_ecr_push_role.name
147+
policy_arn = aws_iam_policy.ecr_policy.arn
148+
}

0 commit comments

Comments
 (0)