-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
356 lines (314 loc) · 11.5 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
350
351
352
353
354
355
356
data "aws_caller_identity" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
lambda_assume_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
# DyanmoDb table for storing old keys
resource "aws_dynamodb_table" "iam_key_rotator" {
# checkov:skip=CKV_AWS_119: SSE is enabled by default using AWS owned SSE. If required customer owned key can be used
# checkov:skip=CKV_AWS_28: Enabling PITR depends on user
name = var.table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "user"
range_key = "ak"
attribute {
name = "user"
type = "S"
}
attribute {
name = "ak"
type = "S"
}
ttl {
attribute_name = "delete_on"
enabled = true
}
server_side_encryption {
enabled = var.enable_sse
kms_key_arn = var.kms_key_arn
}
point_in_time_recovery {
enabled = var.enable_pitr
}
stream_enabled = true
stream_view_type = "OLD_IMAGE"
tags = var.tags
}
# ====== Lambda Layers =====
resource "aws_lambda_layer_version" "pytz" {
filename = "pytz.zip"
source_code_hash = filebase64sha256("pytz.zip")
description = "https://pypi.org/project/pytz/"
layer_name = "pytz"
compatible_runtimes = ["python3.6", "python3.7", "python3.8", "python3.9"]
}
resource "aws_lambda_layer_version" "requests" {
filename = "requests.zip"
source_code_hash = filebase64sha256("requests.zip")
description = "https://pypi.org/project/requests/"
layer_name = "requests"
compatible_runtimes = ["python3.6", "python3.7", "python3.8", "python3.9"]
}
resource "aws_lambda_layer_version" "cryptography" {
filename = "cryptography.zip"
source_code_hash = filebase64sha256("cryptography.zip")
description = "https://cryptography.io/en/latest/"
layer_name = "cryptography"
compatible_runtimes = ["python3.6", "python3.7", "python3.8", "python3.9"]
}
# ====== iam-key-creator ======
resource "aws_iam_role" "iam_key_creator" {
name = var.key_creator_role_name
assume_role_policy = local.lambda_assume_policy
force_detach_policies = true
tags = var.tags
}
resource "aws_iam_role_policy" "iam_key_creator_policy" {
name = "${var.key_creator_role_name}-policy"
role = aws_iam_role.iam_key_creator.id
policy = jsonencode({
Version = "2012-10-17"
Statement = flatten([
[{
Effect = "Allow"
Action = [
"iam:ListUserTags",
"iam:ListAccessKeys",
"iam:ListUsers",
"iam:CreateAccessKey",
"iam:ListAccountAliases"
]
Resource = ["*"]
},
{
Effect = "Allow"
Action = [
"dynamodb:PutItem"
]
Resource = [aws_dynamodb_table.iam_key_rotator.arn]
},
{
Effect = "Allow"
Action = [
"ssm:GetParameter"
]
Resource = ["arn:aws:ssm:${var.region}:${local.account_id}:parameter/ikr/*"]
}],
var.encrypt_key_pair ? [{
Effect = "Allow"
Action = ["ssm:PutParameter"]
Resource = ["arn:aws:ssm:${var.region}:${local.account_id}:parameter/ikr/*"]
}] : [],
var.mail_client == "ses" ? [{
Effect = "Allow"
Action = ["ses:SendEmail"]
Resource = ["*"]
}] : []
])
})
}
resource "aws_iam_role_policy_attachment" "iam_key_creator_logs" {
role = aws_iam_role.iam_key_creator.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_ssm_parameter" "mailgun" {
count = var.mail_client == "mailgun" ? 1 : 0
name = "/ikr/secret/mailgun"
value = var.mailgun_api_key
type = "SecureString"
tags = var.tags
}
resource "aws_ssm_parameter" "smtp_password" {
count = var.mail_client == "smtp" ? 1 : 0
name = "/ikr/secret/smtp"
value = var.smtp_password
type = "SecureString"
tags = var.tags
}
resource "aws_cloudwatch_log_group" "iam_key_creator" {
# checkov:skip=CKV_AWS_338: Retention period is user dependant
name = "/aws/lambda/${var.key_creator_function_name}"
retention_in_days = var.cw_log_group_retention
kms_key_id = var.cw_logs_kms_key_arn
tags = var.tags
}
resource "aws_lambda_function" "iam_key_creator" {
# checkov:skip=CKV_AWS_50: Enabling X-Ray tracing depends on user
# checkov:skip=CKV_AWS_115: Setting reserved concurrent execution depends on user
# checkov:skip=CKV_AWS_116: DLQ not required
# checkov:skip=CKV_AWS_117: VPC deployment not required
# checkov:skip=CKV_AWS_173: By default environment variables are encrypted at rest
# checkov:skip=CKV_AWS_272: Code-signing not required
function_name = var.key_creator_function_name
description = "Create new access key pair for IAM user"
role = aws_iam_role.iam_key_creator.arn
filename = data.archive_file.creator.output_path
source_code_hash = data.archive_file.creator.output_base64sha256
handler = "creator.handler"
runtime = var.lambda_runtime
memory_size = var.function_memory_size
timeout = var.function_timeout
reserved_concurrent_executions = var.reserved_concurrent_executions
layers = [aws_lambda_layer_version.pytz.arn, aws_lambda_layer_version.requests.arn, aws_lambda_layer_version.cryptography.arn]
tracing_config {
mode = var.xray_tracing_mode
}
environment {
variables = {
IAM_KEY_ROTATOR_TABLE = aws_dynamodb_table.iam_key_rotator.name
ROTATE_AFTER_DAYS = var.rotate_after_days
DELETE_AFTER_DAYS = var.delete_after_days
ENCRYPT_KEY_PAIR = var.encrypt_key_pair
MAIL_CLIENT = var.mail_client
MAIL_FROM = var.mail_from
SMTP_PROTOCOL = var.smtp_protocol
SMTP_PORT = var.smtp_port
SMTP_SERVER = var.smtp_server
SMTP_PASSWORD_PARAMETER = var.mail_client == "smtp" ? join(",", aws_ssm_parameter.smtp_password.*.name) : null
MAILGUN_API_URL = var.mailgun_api_url
MAILGUN_API_KEY_NAME = var.mail_client == "mailgun" ? join(",", aws_ssm_parameter.mailgun.*.name) : null
ACCOUNT_ID = local.account_id
}
}
tags = var.tags
}
resource "aws_cloudwatch_event_rule" "iam_key_creator" {
name = "IAMAccessKeyCreator"
description = "Triggers a lambda function periodically which creates a set of new access key pair for a user if the existing key pair is X days old"
state = "ENABLED"
schedule_expression = "cron(${var.cron_expression})"
tags = var.tags
}
resource "aws_cloudwatch_event_target" "iam_key_creator" {
rule = aws_cloudwatch_event_rule.iam_key_creator.name
target_id = "TriggerIAMKeyCreatorLambda"
arn = aws_lambda_function.iam_key_creator.arn
}
resource "aws_lambda_permission" "iam_key_creator" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.iam_key_creator.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.iam_key_creator.arn
}
# ====== iam-key-destructor ======
resource "aws_iam_role" "iam_key_destructor" {
name = var.key_destructor_role_name
assume_role_policy = local.lambda_assume_policy
force_detach_policies = true
tags = var.tags
}
resource "aws_iam_role_policy" "iam_key_destructor_policy" {
name = "${var.key_destructor_role_name}-policy"
role = aws_iam_role.iam_key_destructor.id
policy = jsonencode({
Version = "2012-10-17"
Statement = flatten([
[{
Effect = "Allow"
Action = [
"iam:DeleteAccessKey",
"iam:ListAccountAliases"
]
Resource = ["*"]
},
{
Effect = "Allow"
Action = [
"dynamodb:PutItem"
]
Resource = [aws_dynamodb_table.iam_key_rotator.arn]
},
{
Effect = "Allow"
Action = [
"dynamodb:DescribeStream",
"dynamodb:GetRecords",
"dynamodb:GetShardIterator",
"dynamodb:ListShards",
"dynamodb:ListStreams"
]
Resource = [aws_dynamodb_table.iam_key_rotator.stream_arn]
}],
var.encrypt_key_pair ? [{
Effect = "Allow"
Action = ["ssm:DeleteParameter"]
Resource = ["arn:aws:ssm:${var.region}:${local.account_id}:parameter/ikr/secret/iam/*"]
}] : [],
var.mail_client == "ses" ? [{
Effect = "Allow"
Action = ["ses:SendEmail"]
Resource = ["*"]
}] : []
])
})
}
resource "aws_iam_role_policy_attachment" "iam_key_destructor_logs" {
role = aws_iam_role.iam_key_destructor.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_cloudwatch_log_group" "iam_key_destructor" {
# checkov:skip=CKV_AWS_338: Retention period is user dependant
name = "/aws/lambda/${var.key_destructor_function_name}"
retention_in_days = var.cw_log_group_retention
kms_key_id = var.cw_logs_kms_key_arn
tags = var.tags
}
resource "aws_lambda_function" "iam_key_destructor" {
# checkov:skip=CKV_AWS_50: Enabling X-Ray tracing depends on user
# checkov:skip=CKV_AWS_115: Setting reserved concurrent execution depends on user
# checkov:skip=CKV_AWS_116: DLQ not required
# checkov:skip=CKV_AWS_117: VPC deployment not required
# checkov:skip=CKV_AWS_173: By default environment variables are encrypted at rest
# checkov:skip=CKV_AWS_272: Code-signing not required
function_name = var.key_destructor_function_name
description = "Delete existing access key pair for IAM user"
role = aws_iam_role.iam_key_destructor.arn
filename = data.archive_file.destructor.output_path
source_code_hash = data.archive_file.destructor.output_base64sha256
handler = "destructor.handler"
runtime = var.lambda_runtime
memory_size = var.function_memory_size
timeout = var.function_timeout
reserved_concurrent_executions = var.reserved_concurrent_executions
layers = [aws_lambda_layer_version.requests.arn]
tracing_config {
mode = var.xray_tracing_mode
}
environment {
variables = {
IAM_KEY_ROTATOR_TABLE = aws_dynamodb_table.iam_key_rotator.name
RETRY_AFTER_MINS = var.retry_after_mins
MAIL_CLIENT = var.mail_client
MAIL_FROM = var.mail_from
SMTP_PROTOCOL = var.smtp_protocol
SMTP_PORT = var.smtp_port
SMTP_SERVER = var.smtp_server
SMTP_PASSWORD_PARAMETER = var.mail_client == "smtp" ? join(",", aws_ssm_parameter.smtp_password.*.name) : null
MAILGUN_API_URL = var.mailgun_api_url
MAILGUN_API_KEY_NAME = var.mail_client == "mailgun" ? join(",", aws_ssm_parameter.mailgun.*.name) : null
ACCOUNT_ID = local.account_id
}
}
tags = var.tags
}
resource "aws_lambda_event_source_mapping" "iam_key_destructor" {
event_source_arn = aws_dynamodb_table.iam_key_rotator.stream_arn
function_name = aws_lambda_function.iam_key_destructor.arn
starting_position = "LATEST"
maximum_retry_attempts = 0
depends_on = [aws_iam_role_policy.iam_key_destructor_policy]
}