-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
executable file
·177 lines (144 loc) · 5.47 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
resource "random_string" "random" {
count = var.role_name == null ? 1 : 0
length = 8
lower = true
special = false
}
locals {
github_environments = (length(var.github_environments) > 0 && var.repo != null) ? [for e in var.github_environments : "repo:${var.repo}:environment:${e}"] : ["ensurethereisnotmatch"]
role_name = (var.repo != null && var.role_name != null) ? var.role_name : "${substr(replace(var.repo != null ? var.repo : "", "/", "-"), 0, 64 - 8)}-${random_string.random[0].id}"
variable_sub = "${var.github_oidc_issuer}:sub"
default_allow_main = contains(var.default_conditions, "allow_main") ? [{
test = "StringLike"
variable = local.variable_sub
values = ["repo:${var.repo}:ref:refs/heads/main"]
}] : []
default_allow_master = contains(var.default_conditions, "allow_master") ? [{
test = "StringLike"
variable = local.variable_sub
values = ["repo:${var.repo}:ref:refs/heads/master"]
}] : []
default_allow_environment = contains(var.default_conditions, "allow_environment") ? [{
test = "StringLike"
variable = local.variable_sub
values = local.github_environments
}] : []
default_allow_all = contains(var.default_conditions, "allow_all") ? [{
test = "StringLike"
variable = local.variable_sub
values = ["repo:${var.repo}:*"]
}] : []
default_deny_pull_request = contains(var.default_conditions, "deny_pull_request") ? [{
test = "StringNotLike"
variable = local.variable_sub
values = ["repo:${var.repo}:pull_request"]
}] : []
conditions = setunion(local.default_allow_main, local.default_allow_master, local.default_allow_environment, local.default_allow_all, local.default_deny_pull_request, var.conditions)
merge_conditions = [
for k, v in { for c in local.conditions : "${c.test}|${c.variable}" => c... } : # group by test & variable
{
"test" : k,
"values" : flatten([for index, sp in v[*].values : v[index].values if v[index].variable == v[0].variable]) # loop again to build the values inner map
}
]
root_principal_arns = [for acc in var.account_ids : "arn:aws:iam::${acc}:root"]
merged_principal_arns = concat(local.root_principal_arns, var.custom_principal_arns)
}
data "aws_iam_policy_document" "assume_role_policy" {
count = var.repo != null ? 1 : 0
dynamic "statement" {
for_each = length(local.merged_principal_arns) > 0 ? [1] : []
content {
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = local.merged_principal_arns
}
}
}
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [var.openid_connect_provider_arn]
}
condition {
test = "StringEquals"
variable = "${var.github_oidc_issuer}:aud"
values = ["sts.amazonaws.com"]
}
dynamic "condition" {
for_each = local.merge_conditions
content {
test = split("|", condition.value.test)[0]
variable = split("|", condition.value.test)[1]
values = condition.value.values
}
}
}
}
resource "aws_iam_role" "this" {
count = var.repo != null ? 1 : 0
name = local.role_name
path = var.role_path
permissions_boundary = var.role_permissions_boundary
assume_role_policy = data.aws_iam_policy_document.assume_role_policy[0].json
max_session_duration = var.role_max_session_duration
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "custom" {
count = length(var.role_policy_arns)
role = join("", aws_iam_role.this.*.name)
policy_arn = var.role_policy_arns[count.index]
}
###############################
# Additional policy statements
###############################
data "aws_iam_policy_document" "additional_inline" {
count = length(var.policy_statement) > 0 ? 1 : 0
dynamic "statement" {
for_each = var.policy_statement
content {
sid = try(statement.value.sid, replace(statement.key, "/[^0-9A-Za-z]*/", ""))
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
resources = try(statement.value.resources, null)
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
}
resource "aws_iam_policy" "additional_inline" {
count = length(var.policy_statement) > 0 ? 1 : 0
name = "${local.role_name}-inline"
path = var.role_path
policy = data.aws_iam_policy_document.additional_inline[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "additional_inline" {
count = length(var.policy_statement) > 0 ? 1 : 0
role = join("", aws_iam_role.this.*.name)
policy_arn = aws_iam_policy.additional_inline[0].arn
}