-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
314 lines (268 loc) · 11 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
################################################################################
# github-pipeline-roles module
# This is a helper module used to simplify deployment of GitHub workflow roles via the role vending machine.
################################################################################
locals {
aws_account_id = var.aws_account_id != "" ? var.aws_account_id : data.aws_caller_identity.current.account_id
rvm_account_id = var.rvm_account_id != "" ? var.rvm_account_id : null
github_organization_name = var.principal_type == "github" || var.principal_type == "breakglass" ? var.github_organization_name : null
role_name = var.principal_type == "github" ? coalesce(var.role_name, "${var.repository_name}-repo-role") : var.role_name
role_description = var.principal_type == "github" ? "Github Workflow Role for ${local.github_organization_name}/${var.repository_name}" : coalesce(var.role_description, "IAM role created for by Role Vending Machine")
github_environment = var.github_environment != "" ? "repo:${local.github_organization_name}/${var.repository_name}:environment:${var.github_environment}" : ""
oidc_subscribers = var.principal_type == "github" ? compact([
"repo:${local.github_organization_name}/${var.repository_name}:ref:refs/heads/${var.github_branch}",
local.github_environment
]) : null
# Readonly roles should be consumable by pull requests, or by the main branch (for use in workflow dispatches)
readonly_oidc_subscribers = var.principal_type == "github" ? concat(local.oidc_subscribers,
["repo:${local.github_organization_name}/${var.repository_name}:pull_request"]
) : null
managed_policies = var.principal_type == "github" ? concat(var.managed_policies, ["arn:aws:iam::aws:policy/ReadOnlyAccess"]) : var.managed_policies
managed_policies_readonly = ["arn:aws:iam::aws:policy/ReadOnlyAccess"]
pod_trust_policy_controls = {
include_source_account = true
include_cluster_arns = true
include_cluster_names = true
include_cluster_namspaces = true
include_cluster_service_account = true
}
eks_cluster_arns = local.pod_trust_policy_controls.include_cluster_arns ? var.eks_cluster_arns : []
eks_cluster_name = local.pod_trust_policy_controls.include_cluster_names ? var.eks_cluster_name : []
eks_namespaces = local.pod_trust_policy_controls.include_cluster_namspaces ? var.eks_namespaces : []
eks_service_account = local.pod_trust_policy_controls.include_cluster_service_account ? var.eks_service_account : []
service_trust_policy_controls = {
include_account_condition = true
include_org_condition = true
}
path = var.principal_type == "breakglass" ? "/breakglass/" : "/RVM/"
tags ={
principal_type = var.principal_type
role_arn = "arn:aws:iam::${local.aws_account_id}:role${local.path}${local.role_name}"
create_date = timestamp()
requester = var.principal_type == "breakglass" ? var.breakglass_user_alias : null
email = var.principal_type == "breakglass" ? var.breakglass_user_email : null
}
}
################################################################################
# Terraform Apply Role / Main Branch
################################################################################
resource "aws_iam_role" "main" {
name = local.role_name
description = local.role_description
path = local.path
max_session_duration = var.max_session_duration
force_detach_policies = var.force_detach_policies
permissions_boundary = var.role_permissions_boundary_arn
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
tags = merge(var.tags, local.tags)
lifecycle {
ignore_changes = [tags["create_date"]]
}
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
effect = "Allow"
actions = var.principal_type == "pod" ? ["sts:AssumeRole", "sts:TagSession"] : var.principal_type == "service" || var.principal_type == "breakglass" ? ["sts:AssumeRole"] : ["sts:AssumeRoleWithWebIdentity"]
principals {
type = var.principal_type == "github" ? "Federated" : var.principal_type == "breakglass" ? "AWS" : "Service"
identifiers = var.principal_type == "github" ? ["arn:aws:iam::${local.aws_account_id}:oidc-provider/token.actions.githubusercontent.com"] : var.principal_type == "pod" ? ["pods.eks.amazonaws.com"] : var.principal_type == "breakglass" ? ["arn:aws:iam::${local.rvm_account_id}:role/github-breakglass-rvm"] : var.service_name
}
dynamic "condition" {
for_each = var.principal_type == "github" ? [1] : []
content {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:aud"
values = ["sts.amazonaws.com"]
}
}
dynamic "condition" {
for_each = var.principal_type == "github" ? [1] : []
content {
test = "StringEquals"
variable = "token.actions.githubusercontent.com:sub"
values = local.oidc_subscribers
}
}
dynamic "condition" {
for_each = var.principal_type == "pod" && local.pod_trust_policy_controls.include_source_account ? { "source_account" = local.aws_account_id } : {}
content {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [condition.value]
}
}
dynamic "condition" {
for_each = var.principal_type == "pod" && length(local.eks_cluster_arns) > 0 ? { "eks-cluster-arn" = local.eks_cluster_arns } : {}
content {
test = "ArnEquals"
variable = "aws:SourceArn"
values = condition.value
}
}
dynamic "condition" {
for_each = var.principal_type == "pod" && length(local.eks_cluster_name) > 0 ? { "eks-cluster-name" = local.eks_cluster_name } : {}
content {
test = "StringEquals"
variable = "aws:PrincipalTag/eks-cluster-name"
values = condition.value
}
}
dynamic "condition" {
for_each = var.principal_type == "pod" && length(local.eks_namespaces) > 0 ? { "kubernetes-namespace" = local.eks_namespaces } : {}
content {
test = "StringEquals"
variable = "aws:PrincipalTag/kubernetes-namespace"
values = condition.value
}
}
dynamic "condition" {
for_each = var.principal_type == "pod" && length(local.eks_service_account) > 0 ? { "kubernetes-service-account" = local.eks_service_account } : {}
content {
test = "StringEquals"
variable = "aws:PrincipalTag/kubernetes-service-account"
values = condition.value
}
}
dynamic "condition" {
for_each = var.principal_type == "service" && local.service_trust_policy_controls.include_org_condition ? [1] : []
content {
test = "StringEquals"
variable = "aws:SourceOrgID"
values = ["$${aws:ResourceOrgId}"]
}
}
dynamic "condition" {
for_each = var.principal_type == "service" && local.service_trust_policy_controls.include_account_condition ? { "source_account" = local.aws_account_id } : {}
content {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [condition.value]
}
}
}
}
resource "aws_iam_role_policy_attachment" "custom" {
count = length(local.managed_policies)
role = aws_iam_role.main.name
policy_arn = local.managed_policies[count.index]
}
resource "aws_iam_role_policy" "workflow_role_state_access" {
count = var.principal_type == "github" ? 1 : 0
name = "tf-remote-state-access"
role = aws_iam_role.main.name
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
],
"Resource" : [
"arn:aws:s3:::${local.aws_account_id}-tf-remote-state",
"arn:aws:s3:::${local.aws_account_id}-tf-remote-state/*",
]
},
{
"Effect" : "Allow",
"Action" : [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:DeleteItem"
],
"Resource" : [
"arn:aws:dynamodb:*:*:table/tf-state-lock"
]
}
]
})
}
resource "aws_iam_role_policy" "inline_policy" {
count = var.inline_policy != "" ? 1 : 0
name = "inline-policy"
role = aws_iam_role.main.name
policy = var.inline_policy
}
################################################################################
# Terraform Plan Role / Read Only
################################################################################
resource "aws_iam_role" "readonly" {
count = var.principal_type == "github" ? 1 : 0
name = "${local.role_name}-readonly"
#name_prefix = var.role_name_prefix
description = local.role_description
path = "/RVM/"
max_session_duration = var.max_session_duration
force_detach_policies = var.force_detach_policies
permissions_boundary = var.role_permissions_boundary_arn
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Sid" : "",
"Effect" : "Allow",
"Principal" : {
"Federated" : "arn:aws:iam::${local.aws_account_id}:oidc-provider/token.actions.githubusercontent.com"
},
"Action" : "sts:AssumeRoleWithWebIdentity",
"Condition" : {
"StringEquals" : {
"token.actions.githubusercontent.com:sub" : local.readonly_oidc_subscribers
},
"StringLike" : {
"token.actions.githubusercontent.com:aud" : "sts.amazonaws.com"
}
}
}
]
})
tags = var.tags
}
resource "aws_iam_role_policy" "workflow_role_state_access_readonly" {
count = var.principal_type == "github" ? 1 : 0
name = "tf-remote-state-access"
role = aws_iam_role.readonly[0].name
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:ListBucket",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
],
"Resource" : [
"arn:aws:s3:::${local.aws_account_id}-tf-remote-state",
"arn:aws:s3:::${local.aws_account_id}-tf-remote-state/*",
]
},
{
"Effect" : "Allow",
"Action" : [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:DeleteItem"
],
"Resource" : [
"arn:aws:dynamodb:*:*:table/tf-state-lock"
]
}
]
})
}
resource "aws_iam_role_policy_attachment" "readonly" {
count = var.principal_type == "github" ? length(local.managed_policies_readonly) : 0
role = aws_iam_role.readonly[0].name
policy_arn = local.managed_policies_readonly[count.index]
}
resource "aws_iam_role_policy" "inline_policy_readonly" {
count = var.principal_type == "github" && var.inline_policy_readonly != "" ? 1 : 0
name = "inline-policy"
role = aws_iam_role.readonly[0].name
policy = var.inline_policy_readonly
}