-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.tf
212 lines (190 loc) · 6.67 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
# Managed By : CloudDrove
# Description : This Script is used to create CloudTrail.
# Copyright @ CloudDrove. All Right Reserved.
#Module : Labels
#Description : This terraform module is designed to generate consistent label names and tags
# for resources. You can use terraform-labels to implement a strict naming
# convention.
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
name = var.name
repository = var.repository
environment = var.environment
managedby = var.managedby
attributes = var.attributes
label_order = var.label_order
}
#Module : CLOUDTRAIL
#Description : Terraform module to provision an AWS CloudTrail with encrypted S3 bucket.
# This bucket is used to store CloudTrail logs.
resource "aws_cloudtrail" "default" {
count = var.enabled_cloudtrail == true ? 1 : 0
name = module.labels.id
enable_logging = var.enable_logging
s3_bucket_name = var.s3_bucket_name
s3_key_prefix = var.s3_key_prefix
enable_log_file_validation = var.enable_log_file_validation
is_multi_region_trail = var.is_multi_region_trail
include_global_service_events = var.include_global_service_events
cloud_watch_logs_role_arn = var.cloud_watch_logs_role_arn
cloud_watch_logs_group_arn = var.cloud_watch_logs_group_arn != "" ? format("%s:*", var.cloud_watch_logs_group_arn) : ""
kms_key_id = join("", aws_kms_key.cloudtrail[*].arn) # aws_kms_key.cloudtrail[0].arn != null ? aws_kms_key.cloudtrail[0].arn : null
is_organization_trail = var.is_organization_trail
tags = module.labels.tags
sns_topic_name = var.sns_topic_name
dynamic "event_selector" {
for_each = var.event_selector ? [true] : []
content {
read_write_type = var.read_write_type
include_management_events = var.include_management_events
dynamic "data_resource" {
for_each = var.event_selector_data_resource ? ["true"] : []
content {
type = var.data_resource_type
values = var.data_resource_values
}
}
}
}
dynamic "insight_selector" {
for_each = var.insight_selector
content {
insight_type = insight_selector.value.insight_type
}
}
lifecycle {
ignore_changes = [event_selector]
}
depends_on = [
aws_kms_key.cloudtrail,
]
}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_iam_policy_document" "cloudtrail_assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
}
# This role is used by CloudTrail to send logs to CloudWatch.
resource "aws_iam_role" "cloudtrail_cloudwatch_role" {
count = var.enable_cloudwatch && var.enabled_cloudtrail ? 1 : 0
name = var.iam_role_name
assume_role_policy = data.aws_iam_policy_document.cloudtrail_assume_role.json
}
resource "aws_cloudwatch_log_group" "cloudtrail" {
count = var.enable_cloudwatch && var.enabled_cloudtrail ? 1 : 0
name = var.cloudwatch_log_group_name
retention_in_days = var.log_retention_days
kms_key_id = join("", aws_kms_key.cloudtrail[*].arn)
}
data "aws_region" "current" {}
data "aws_iam_policy_document" "cloudtrail_cloudwatch_logs" {
statement {
sid = "WriteCloudWatchLogs"
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
#tfsec:ignore:aws-iam-no-policy-wildcards
resources = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:cloudwatch-log-group:*"]
}
}
resource "aws_iam_policy" "cloudtrail_cloudwatch_logs" {
count = var.enable_cloudwatch && var.enabled_cloudtrail ? 1 : 0
name = "cloudtrail-cloudwatch-logs-policy"
policy = data.aws_iam_policy_document.cloudtrail_cloudwatch_logs.json
}
resource "aws_iam_policy_attachment" "main" {
count = var.enable_cloudwatch && var.enabled_cloudtrail ? 1 : 0
name = "cloudtrail-cloudwatch-logs-policy-attachment"
policy_arn = aws_iam_policy.cloudtrail_cloudwatch_logs[0].arn
roles = [aws_iam_role.cloudtrail_cloudwatch_role[0].name]
}
## Supports only for single account cloudtrail.
resource "aws_kms_key" "cloudtrail" {
count = var.kms_enabled && var.enabled_cloudtrail ? 1 : 0
description = "A KMS key used to encrypt CloudTrail log files stored in S3."
deletion_window_in_days = var.key_deletion_window_in_days
enable_key_rotation = "true"
policy = data.aws_iam_policy_document.kms.json
tags = module.labels.tags
}
data "aws_iam_policy_document" "kms" {
version = "2012-10-17"
statement {
sid = "Enable IAM User Permissions"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["kms:*"]
resources = ["*"]
}
statement {
sid = "Allow CloudTrail to encrypt logs"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = ["kms:GenerateDataKey*"]
resources = ["*"]
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
}
statement {
sid = "Allow CloudTrail to describe key"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
actions = ["kms:DescribeKey"]
resources = ["*"]
}
statement {
sid = "Allow principals in the account to decrypt log files"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"kms:Decrypt",
"kms:ReEncryptFrom"
]
resources = ["*"]
condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [data.aws_caller_identity.current.account_id]
}
condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_partition.current.partition}:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"]
}
}
statement {
sid = "Allow alias creation during setup"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["kms:CreateAlias"]
resources = ["*"]
}
}