-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
349 lines (266 loc) · 8.35 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
provider "aws" {
region = var.region
}
locals {
acmpca_permission_actions = [
"IssueCertificate",
"GetCertificate",
"ListPermissions",
]
}
resource "aws_s3_bucket" "this" {
force_destroy = true
}
data "aws_caller_identity" "this" {}
data "aws_iam_policy_document" "s3" {
statement {
actions = [
"s3:GetBucketAcl",
"s3:GetBucketLocation",
"s3:PutObject",
"s3:PutObjectAcl",
]
resources = [
aws_s3_bucket.this.arn,
"${aws_s3_bucket.this.arn}/*",
]
principals {
identifiers = ["acm-pca.amazonaws.com"]
type = "Service"
}
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [data.aws_caller_identity.this.account_id]
}
}
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.s3.json
}
data "aws_partition" "this" {}
resource "aws_acmpca_certificate_authority" "root" {
type = "ROOT"
permanent_deletion_time_in_days = 7
certificate_authority_configuration {
key_algorithm = "RSA_4096"
signing_algorithm = "SHA512WITHRSA"
subject {
common_name = "Root"
}
}
revocation_configuration {
crl_configuration {
enabled = true
expiration_in_days = 1
s3_bucket_name = aws_s3_bucket.this.id
s3_object_acl = "BUCKET_OWNER_FULL_CONTROL"
}
}
depends_on = [aws_s3_bucket_policy.this]
}
resource "aws_acmpca_certificate" "root" {
certificate_authority_arn = aws_acmpca_certificate_authority.root.arn
certificate_signing_request = aws_acmpca_certificate_authority.root.certificate_signing_request
signing_algorithm = "SHA512WITHRSA"
validity {
type = "YEARS"
value = 15
}
template_arn = "arn:${data.aws_partition.this.partition}:acm-pca:::template/RootCACertificate/V1"
}
resource "aws_acmpca_certificate_authority_certificate" "root" {
certificate = aws_acmpca_certificate.root.certificate
certificate_authority_arn = aws_acmpca_certificate_authority.root.arn
}
resource "aws_acmpca_permission" "root" {
certificate_authority_arn = aws_acmpca_certificate_authority.root.arn
actions = local.acmpca_permission_actions
principal = "acm.amazonaws.com"
}
resource "aws_acmpca_certificate_authority" "intermediate" {
permanent_deletion_time_in_days = 7
certificate_authority_configuration {
key_algorithm = "RSA_4096"
signing_algorithm = "SHA512WITHRSA"
subject {
common_name = "AWS IAM Roles Anywhere"
}
}
revocation_configuration {
crl_configuration {
enabled = true
expiration_in_days = 1
s3_bucket_name = aws_s3_bucket.this.id
s3_object_acl = "BUCKET_OWNER_FULL_CONTROL"
}
}
depends_on = [aws_s3_bucket_policy.this]
}
resource "aws_acmpca_certificate" "intermediate" {
certificate_authority_arn = aws_acmpca_certificate_authority.root.arn
certificate_signing_request = aws_acmpca_certificate_authority.intermediate.certificate_signing_request
signing_algorithm = "SHA512WITHRSA"
validity {
type = "YEARS"
value = 10
}
template_arn = "arn:${data.aws_partition.this.partition}:acm-pca:::template/SubordinateCACertificate_PathLen1/V1"
}
resource "aws_acmpca_certificate_authority_certificate" "intermediate" {
certificate = aws_acmpca_certificate.intermediate.certificate
certificate_authority_arn = aws_acmpca_certificate_authority.intermediate.arn
certificate_chain = aws_acmpca_certificate.intermediate.certificate_chain
}
resource "aws_acmpca_permission" "intermediate" {
certificate_authority_arn = aws_acmpca_certificate_authority.intermediate.arn
actions = local.acmpca_permission_actions
principal = "acm.amazonaws.com"
}
resource "aws_acmpca_certificate_authority" "signing" {
for_each = var.iam_role
permanent_deletion_time_in_days = 7
certificate_authority_configuration {
key_algorithm = "RSA_4096"
signing_algorithm = "SHA512WITHRSA"
subject {
common_name = each.value.name
}
}
revocation_configuration {
crl_configuration {
enabled = true
expiration_in_days = 1
s3_bucket_name = aws_s3_bucket.this.id
s3_object_acl = "BUCKET_OWNER_FULL_CONTROL"
}
}
depends_on = [aws_s3_bucket_policy.this]
}
resource "aws_acmpca_certificate" "signing" {
for_each = aws_acmpca_certificate_authority.signing
certificate_authority_arn = aws_acmpca_certificate_authority.intermediate.arn
certificate_signing_request = each.value.certificate_signing_request
signing_algorithm = "SHA512WITHRSA"
validity {
type = "YEARS"
value = 5
}
template_arn = "arn:${data.aws_partition.this.partition}:acm-pca:::template/SubordinateCACertificate_PathLen0/V1"
}
resource "aws_acmpca_certificate_authority_certificate" "signing" {
for_each = aws_acmpca_certificate.signing
certificate = each.value.certificate
certificate_authority_arn = aws_acmpca_certificate_authority.signing[each.key].arn
certificate_chain = each.value.certificate_chain
}
resource "aws_acmpca_permission" "signing" {
for_each = aws_acmpca_certificate_authority.signing
certificate_authority_arn = each.value.arn
actions = [
"IssueCertificate",
"GetCertificate",
"ListPermissions",
]
principal = "acm.amazonaws.com"
}
resource "aws_rolesanywhere_trust_anchor" "this" {
for_each = aws_acmpca_certificate_authority.signing
name = each.value.certificate_authority_configuration.0.subject.0.common_name
enabled = true
source {
source_type = "AWS_ACM_PCA"
source_data {
acm_pca_arn = each.value.arn
}
}
depends_on = [aws_acmpca_certificate_authority_certificate.signing]
}
data "aws_iam_policy_document" "trust" {
for_each = aws_rolesanywhere_trust_anchor.this
statement {
sid = each.value.name
actions = [
"sts:AssumeRole",
"sts:TagSession",
"sts:SetSourceIdentity",
]
condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = [each.value.arn]
}
principals {
type = "Service"
identifiers = ["rolesanywhere.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "profile" {
statement {
actions = ["*"]
resources = ["*"]
condition {
test = "IpAddress"
variable = "aws:SourceIp"
values = var.cidr_blocks
}
}
}
resource "aws_iam_role" "this" {
for_each = data.aws_iam_policy_document.trust
assume_role_policy = each.value.json
path = "/RolesAnywhere/"
name_prefix = "${each.value.statement.0.sid}-"
}
resource "aws_iam_role_policy_attachment" "this" {
for_each = aws_iam_role.this
role = each.value.id
policy_arn = "arn:aws:iam::aws:policy/${var.iam_role[each.key].managed_policy}"
}
resource "aws_iam_role_policy_attachments_exclusive" "this" {
for_each = aws_iam_role_policy_attachment.this
role_name = each.value.role
policy_arns = [each.value.policy_arn]
}
resource "aws_rolesanywhere_profile" "this" {
for_each = aws_iam_role.this
name = each.value.name
role_arns = [each.value.arn]
enabled = true
session_policy = data.aws_iam_policy_document.profile.json
}
resource "tls_private_key" "this" {
for_each = aws_rolesanywhere_profile.this
algorithm = "RSA"
}
resource "tls_cert_request" "this" {
for_each = tls_private_key.this
private_key_pem = each.value.private_key_pem
subject {
common_name = split("-", aws_rolesanywhere_profile.this[each.key].name).0
}
}
resource "aws_acmpca_certificate" "this" {
for_each = tls_cert_request.this
certificate_authority_arn = aws_acmpca_certificate_authority.signing[each.key].arn
certificate_signing_request = each.value.cert_request_pem
signing_algorithm = "SHA512WITHRSA"
validity {
type = "YEARS"
value = 1
}
}
resource "local_sensitive_file" "this" {
for_each = tls_private_key.this
filename = "${path.module}/${each.key}.pem"
content = each.value.private_key_pem
file_permission = "0600"
}
resource "local_file" "this" {
for_each = aws_acmpca_certificate.this
filename = "${path.module}/${each.key}.crt"
content = join("\n", [each.value.certificate, each.value.certificate_chain])
file_permission = "0600"
}