generated from aws-ia/terraform-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.tf
259 lines (209 loc) · 8.04 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
locals {
namespace = try(coalesce(var.namespace, "default")) # Need to explicitly set default for use with IRSA
}
################################################################################
# Helm Release
################################################################################
resource "helm_release" "this" {
count = var.create && var.create_release ? 1 : 0
name = try(coalesce(var.name, var.chart), "")
description = var.description
namespace = local.namespace
create_namespace = var.create_namespace
chart = var.chart
version = var.chart_version # conflicts with reserved keyword
repository = var.repository
values = var.values
timeout = var.timeout
repository_key_file = var.repository_key_file
repository_cert_file = var.repository_cert_file
repository_ca_file = var.repository_ca_file
repository_username = var.repository_username
repository_password = var.repository_password
devel = var.devel
verify = var.verify
keyring = var.keyring
disable_webhooks = var.disable_webhooks
reuse_values = var.reuse_values
reset_values = var.reset_values
force_update = var.force_update
recreate_pods = var.recreate_pods
cleanup_on_fail = var.cleanup_on_fail
max_history = var.max_history
atomic = var.atomic
skip_crds = var.skip_crds
render_subchart_notes = var.render_subchart_notes
disable_openapi_validation = var.disable_openapi_validation
wait = var.wait
wait_for_jobs = var.wait_for_jobs
dependency_update = var.dependency_update
replace = var.replace
lint = var.lint
dynamic "postrender" {
for_each = length(var.postrender) > 0 ? [var.postrender] : []
content {
binary_path = postrender.value.binary_path
args = try(postrender.value.args, null)
}
}
dynamic "set" {
for_each = var.set
content {
name = set.value.name
value = set.value.value
type = try(set.value.type, null)
}
}
dynamic "set" {
for_each = { for k, v in toset(var.set_irsa_names) : k => v if var.create && var.create_role }
iterator = each
content {
name = each.value
value = aws_iam_role.this[0].arn
}
}
dynamic "set_sensitive" {
for_each = var.set_sensitive
content {
name = set_sensitive.value.name
value = set_sensitive.value.value
type = try(set_sensitive.value.type, null)
}
}
}
################################################################################
# IAM Role for Service Account(s) (IRSA)
################################################################################
data "aws_partition" "current" {
count = local.create_role ? 1 : 0
}
data "aws_caller_identity" "current" {
count = local.create_role ? 1 : 0
}
locals {
create_role = var.create && var.create_role
account_id = try(data.aws_caller_identity.current[0].account_id, "*")
partition = try(data.aws_partition.current[0].partition, "*")
role_name = try(coalesce(var.role_name, var.name), "")
role_name_condition = var.role_name_use_prefix ? "${local.role_name}-*" : local.role_name
}
data "aws_iam_policy_document" "assume" {
count = local.create_role ? 1 : 0
dynamic "statement" {
# https://aws.amazon.com/blogs/security/announcing-an-update-to-iam-role-trust-policy-behavior/
for_each = var.allow_self_assume_role ? [1] : []
content {
sid = "ExplicitSelfRoleAssumption"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "ArnLike"
variable = "aws:PrincipalArn"
values = ["arn:${local.partition}:iam::${local.account_id}:role${var.role_path}${local.role_name_condition}"]
}
}
}
dynamic "statement" {
for_each = var.oidc_providers
content {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [statement.value.provider_arn]
}
condition {
test = var.assume_role_condition_test
variable = "${replace(statement.value.provider_arn, "/^(.*provider/)/", "")}:sub"
values = ["system:serviceaccount:${try(statement.value.namespace, local.namespace)}:${statement.value.service_account}"]
}
# https://aws.amazon.com/premiumsupport/knowledge-center/eks-troubleshoot-oidc-and-irsa/?nc1=h_ls
condition {
test = var.assume_role_condition_test
variable = "${replace(statement.value.provider_arn, "/^(.*provider/)/", "")}:aud"
values = ["sts.amazonaws.com"]
}
}
}
}
resource "aws_iam_role" "this" {
count = local.create_role ? 1 : 0
name = var.role_name_use_prefix ? null : local.role_name
name_prefix = var.role_name_use_prefix ? "${local.role_name}-" : null
path = var.role_path
description = var.role_description
assume_role_policy = data.aws_iam_policy_document.assume[0].json
max_session_duration = var.max_session_duration
permissions_boundary = var.role_permissions_boundary_arn
force_detach_policies = true
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "additional" {
for_each = { for k, v in var.role_policies : k => v if local.create_role }
role = aws_iam_role.this[0].name
policy_arn = each.value
}
################################################################################
# IAM Policy
################################################################################
locals {
create_policy = local.create_role && var.create_policy
policy_name = try(coalesce(var.policy_name, local.role_name), "")
perms = concat(var.source_policy_documents, var.override_policy_documents, var.policy_statements)
}
data "aws_iam_policy_document" "this" {
count = local.create_policy && length(local.perms) > 0 ? 1 : 0
source_policy_documents = var.source_policy_documents
override_policy_documents = var.override_policy_documents
dynamic "statement" {
for_each = var.policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, 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
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
resource "aws_iam_policy" "this" {
count = local.create_policy && length(local.perms) > 0 ? 1 : 0
name = var.policy_name_use_prefix ? null : local.policy_name
name_prefix = var.policy_name_use_prefix ? "${local.policy_name}-" : null
path = coalesce(var.policy_path, var.role_path)
description = try(coalesce(var.policy_description, var.role_description), null)
policy = data.aws_iam_policy_document.this[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "this" {
count = local.create_policy && length(local.perms) > 0 ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.this[0].arn
}