Skip to content

Commit 637a671

Browse files
Arnel Jan SarmientoArJSarmiento
authored andcommitted
feat: Add support for AWS Organizations access in GitHub OIDC module, enabling dynamic IAM policy statements based on configuration
1 parent 5566361 commit 637a671

3 files changed

Lines changed: 109 additions & 30 deletions

File tree

modules/aws/github-oidc/main.tf

Lines changed: 99 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ resource "aws_iam_openid_connect_provider" "github" {
1414
client_id_list = ["sts.amazonaws.com"]
1515
}
1616

17+
data "aws_caller_identity" "current" {}
18+
19+
data "aws_partition" "current" {}
20+
21+
locals {
22+
organizations_account_resources = [
23+
"arn:${data.aws_partition.current.partition}:organizations::${data.aws_caller_identity.current.account_id}:account/*/*",
24+
]
25+
26+
organizations_root_resources = [
27+
"arn:${data.aws_partition.current.partition}:organizations::${data.aws_caller_identity.current.account_id}:root/*/*",
28+
]
29+
}
30+
1731
data "aws_iam_policy_document" "oidc" {
1832
statement {
1933
actions = ["sts:AssumeRoleWithWebIdentity"]
@@ -130,33 +144,91 @@ data "aws_iam_policy_document" "iam_and_terraform" {
130144
}
131145
}
132146

133-
# PowerUserAccess denies organizations:* — re-grant for managing
134-
# AWS Organizations resources in the root workspace.
135-
statement {
136-
sid = "OrganizationsAccess"
137-
effect = "Allow"
138-
actions = [
139-
"organizations:DescribeOrganization",
140-
"organizations:ListAccounts",
141-
"organizations:DescribeAccount",
142-
"organizations:ListRoots",
143-
"organizations:ListAWSServiceAccessForOrganization",
144-
"organizations:ListDelegatedAdministrators",
145-
"organizations:EnableAWSServiceAccess",
146-
"organizations:DisableAWSServiceAccess",
147-
"organizations:ListPolicies",
148-
"organizations:DescribePolicy",
149-
"organizations:ListTargetsForPolicy",
150-
"organizations:EnablePolicyType",
151-
"organizations:DisablePolicyType",
152-
"organizations:CreateAccount",
153-
"organizations:DescribeCreateAccountStatus",
154-
"organizations:CloseAccount",
155-
"organizations:TagResource",
156-
"organizations:UntagResource",
157-
"organizations:ListTagsForResource",
158-
]
159-
resources = ["*"]
147+
dynamic "statement" {
148+
for_each = var.enable_organizations_access ? [1] : []
149+
150+
content {
151+
sid = "OrganizationsGlobalRead"
152+
effect = "Allow"
153+
actions = [
154+
"organizations:DescribeOrganization",
155+
"organizations:ListAccounts",
156+
"organizations:ListRoots",
157+
"organizations:ListAWSServiceAccessForOrganization",
158+
]
159+
resources = ["*"]
160+
}
161+
}
162+
163+
dynamic "statement" {
164+
for_each = var.enable_organizations_access ? [1] : []
165+
166+
content {
167+
sid = "OrganizationsManagedAccounts"
168+
effect = "Allow"
169+
actions = [
170+
"organizations:DescribeAccount",
171+
"organizations:ListParents",
172+
"organizations:ListTagsForResource",
173+
"organizations:TagResource",
174+
"organizations:UntagResource",
175+
]
176+
resources = local.organizations_account_resources
177+
}
178+
}
179+
180+
dynamic "statement" {
181+
for_each = var.enable_organizations_access ? [1] : []
182+
183+
content {
184+
sid = "OrganizationsAccountProvisioning"
185+
effect = "Allow"
186+
actions = [
187+
"organizations:CreateAccount",
188+
"organizations:DescribeCreateAccountStatus",
189+
]
190+
resources = ["*"]
191+
}
192+
}
193+
194+
dynamic "statement" {
195+
for_each = var.enable_organizations_access ? [1] : []
196+
197+
content {
198+
sid = "OrganizationsServiceAccess"
199+
effect = "Allow"
200+
actions = [
201+
"organizations:EnableAWSServiceAccess",
202+
"organizations:DisableAWSServiceAccess",
203+
]
204+
resources = ["*"]
205+
206+
condition {
207+
test = "StringEquals"
208+
variable = "organizations:ServicePrincipal"
209+
values = ["sso.amazonaws.com"]
210+
}
211+
}
212+
}
213+
214+
dynamic "statement" {
215+
for_each = var.enable_organizations_access ? [1] : []
216+
217+
content {
218+
sid = "OrganizationsPolicyTypes"
219+
effect = "Allow"
220+
actions = [
221+
"organizations:EnablePolicyType",
222+
"organizations:DisablePolicyType",
223+
]
224+
resources = local.organizations_root_resources
225+
226+
condition {
227+
test = "StringEquals"
228+
variable = "organizations:PolicyType"
229+
values = ["SERVICE_CONTROL_POLICY"]
230+
}
231+
}
160232
}
161233

162234
statement {

modules/aws/github-oidc/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ variable "terraform_state_bucket_arn" {
1515
default = null
1616
nullable = true
1717
}
18+
19+
variable "enable_organizations_access" {
20+
description = "Whether to grant AWS Organizations permissions for the root workspace."
21+
type = bool
22+
default = false
23+
}

workspaces/root/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ module "connections" {
2121
module "github_oidc" {
2222
source = "../../modules/aws/github-oidc"
2323

24-
role_name = "github_oidc_role"
25-
allowed_repos = var.allowed_github_repos
26-
terraform_state_bucket_arn = module.terraform_state.bucket_arn
24+
role_name = "github_oidc_role"
25+
allowed_repos = var.allowed_github_repos
26+
enable_organizations_access = true
27+
terraform_state_bucket_arn = module.terraform_state.bucket_arn
2728
}
2829

2930
module "cdn" {

0 commit comments

Comments
 (0)