generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
/
firehose.tf
319 lines (280 loc) · 10.4 KB
/
firehose.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
# Sharing log data with SecOps via AWS Firehose & a subscription on the cloudwatch log that contains flow log data.
# To build the firehose resources we want the following to be true:
# - var.build_firehose is true
# - var.kinesis_endpoint_url has a value.
# This ensures that we don't try and build the resources without an endpoint provided.
# Note that var.tags_prefix includes the environment name so we don't need to include that in the name as a separate variable.
locals {
go-build = var.build_firehose == true && length(var.kinesis_endpoint_url) > 0 && length(var.kinesis_endpoint_secret_string) > 0
}
resource "aws_kinesis_firehose_delivery_stream" "firehose_stream" {
#checkov:skip=CKV_AWS_241: We are using the default key for encryption.
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-xsiam-delivery-stream"
destination = "http_endpoint"
tags = try(var.tags_common, {})
server_side_encryption {
enabled = true
}
http_endpoint_configuration {
url = var.kinesis_endpoint_url
name = "${var.tags_prefix}-endpoint"
access_key = var.kinesis_endpoint_secret_string
buffering_size = 5
buffering_interval = 300
role_arn = aws_iam_role.xsiam_kinesis_firehose_role[count.index].arn
s3_backup_mode = "FailedDataOnly"
cloudwatch_logging_options {
enabled = true
log_group_name = aws_cloudwatch_log_group.xsiam_delivery_group[count.index].name
log_stream_name = aws_cloudwatch_log_stream.xsiam_delivery_stream[count.index].name
}
s3_configuration {
role_arn = aws_iam_role.xsiam_kinesis_firehose_role[count.index].arn
bucket_arn = aws_s3_bucket.xsiam_firehose_bucket[count.index].arn
buffering_size = 10
buffering_interval = 400
compression_format = "GZIP"
}
request_configuration {
content_encoding = "GZIP"
common_attributes {
name = "business_area"
value = var.tags_prefix
}
}
}
}
resource "aws_cloudwatch_log_group" "xsiam_delivery_group" {
#checkov:skip=CKV_AWS_158:"Temporarily skip KMS encryption check while logging solution is being updated"
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-xsiam-delivery-group"
tags = try(var.tags_common, {})
retention_in_days = 400 # Because it's more than a year.
}
resource "aws_cloudwatch_log_stream" "xsiam_delivery_stream" {
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-errors"
log_group_name = aws_cloudwatch_log_group.xsiam_delivery_group[count.index].name
}
resource "aws_iam_role" "xsiam_kinesis_firehose_role" {
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-xsiam-delivery-stream-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "firehose.amazonaws.com"
}
}
]
})
tags = try(var.tags_common, {})
}
#tfsec:ignore:aws-iam-no-policy-wildcards
resource "aws_iam_role_policy" "xsiam_kinesis_firehose_role_policy" {
#checkov:skip=CKV_AWS_355: - Ignore for now whilst we look into this.
count = local.go-build == true ? 1 : 0
role = aws_iam_role.xsiam_kinesis_firehose_role[count.index].id
name = "${var.tags_prefix}-xsiam-kinesis-firehose-role-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "logaccess"
Effect = "Allow"
Action = [
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:GetLogEvents"
]
Resource = "*"
}
]
}
)
}
resource "aws_iam_role_policy_attachment" "kinesis_firehose_error_log_role_attachment" {
count = local.go-build == true ? 1 : 0
policy_arn = aws_iam_policy.xsiam_kinesis_firehose_error_log_policy[count.index].arn
role = aws_iam_role.xsiam_kinesis_firehose_role[count.index].name
}
#tfsec:ignore:aws-iam-no-policy-wildcards
resource "aws_iam_policy" "xsiam_kinesis_firehose_error_log_policy" {
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-xsiam-kinesis-firehose-error-log-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"logs:PutLogEvents",
]
Effect = "Allow"
Resource = [
"${aws_cloudwatch_log_group.xsiam_delivery_group[count.index].arn}/*"
]
}
]
})
tags = try(var.tags_common, {})
}
resource "aws_iam_role_policy_attachment" "kinesis_role_attachment" {
count = local.go-build == true ? 1 : 0
policy_arn = aws_iam_policy.s3_kinesis_xsiam_policy[count.index].arn
role = aws_iam_role.xsiam_kinesis_firehose_role[count.index].name
}
#tfsec:ignore:aws-iam-no-policy-wildcards - this is to allow s3:AbortMultiPartUpload.
resource "aws_iam_policy" "s3_kinesis_xsiam_policy" {
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-s3-kinesis-xsiam-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject"
]
Effect = "Allow"
Resource = [
aws_s3_bucket.xsiam_firehose_bucket[count.index].arn,
"${aws_s3_bucket.xsiam_firehose_bucket[count.index].arn}/*"
]
}
]
})
tags = try(var.tags_common, {})
}
# Cloudwatch Log Subscription Filter & IAM Resources.
# This acts as the interface between the flow log data in cloudwatch & the Firehose Stream.
resource "aws_cloudwatch_log_subscription_filter" "nacs_server_xsiam_subscription" {
count = local.go-build == true ? 1 : 0
name = "${var.tags_prefix}-nacs_server_xsiam_subscription"
role_arn = aws_iam_role.put_record_role[count.index].arn
log_group_name = aws_flow_log.cloudwatch.log_group_name
filter_pattern = ""
destination_arn = aws_kinesis_firehose_delivery_stream.firehose_stream[count.index].arn
}
resource "aws_iam_role" "put_record_role" {
count = local.go-build == true ? 1 : 0
name_prefix = "${var.tags_prefix}-put-record-role"
tags = try(var.tags_common, {})
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "logs.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy" "put_record_policy" {
count = local.go-build == true ? 1 : 0
name_prefix = "${var.tags_prefix}-put-record-policy"
tags = try(var.tags_common, {})
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"firehose:PutRecord",
"firehose:PutRecordBatch"
],
"Resource": [
"${aws_kinesis_firehose_delivery_stream.firehose_stream[count.index].arn}"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "put_record_policy_attachment" {
count = local.go-build == true ? 1 : 0
role = aws_iam_role.put_record_role[count.index].name
policy_arn = aws_iam_policy.put_record_policy[count.index].arn
}
# S3 Bucket to hold the transfer failure logs. We are using the default s3 key and no logging as it is not needed. We also have public access blocked by default
#tfsec:ignore:aws-ssm-secret-use-customer-key
#tfsec:ignore:aws-s3-encryption-customer-key
#tfsec:ignore:aws-s3-enable-bucket-logging
#tfsec:ignore:aws-s3-specify-public-access-block
#tfsec:ignore:aws-s3-block-public-acls
#tfsec:ignore:aws-s3-block-public-policy
#tfsec:ignore:aws-s3-no-public-buckets
#tfsec:ignore:aws-s3-ignore-public-acls
resource "aws_s3_bucket" "xsiam_firehose_bucket" {
#checkov:skip=CKV_AWS_241: We have encryption already in place using the default s3 kms key.
#checkov:skip=CKV_AWS_21: We already have versioning enabled.
#checkov:skip=CKV_AWS_145: We use the default encryption key.
#checkov:skip=CKV2_AWS_62: We do not need event notifications enabled.
#checkov:skip=CKV_AWS_144: We are not using cross-region replication.
#checkov:skip=CKV_AWS_18: No access logging required
#checkov:skip=CKV2_AWS_61: Lifecycle is enabled but this error still gets thrown.
#checkov:skip=CKV2_AWS_6: Public Access Block enabled - see below - but the error still gets thrown.
count = local.go-build == true ? 1 : 0
bucket = "${var.tags_prefix}-xsiam-firehose-bucket"
tags = try(var.tags_common, {})
}
#tfsec:ignore:aws-ssm-secret-use-customer-key
#tfsec:ignore:aws-s3-encryption-customer-key
resource "aws_s3_bucket_server_side_encryption_configuration" "xsiam_firehose_bucket_encryption" {
count = local.go-build == true ? 1 : 0
bucket = aws_s3_bucket.xsiam_firehose_bucket[count.index].id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "xsiam_firehose_bucket_config" {
#checkov:skip=CKV_AWS_300: Event notifications not used.
count = local.go-build == true ? 1 : 0
bucket = aws_s3_bucket.xsiam_firehose_bucket[count.index].id
rule {
id = "delete-old"
expiration {
days = 366
}
status = "Enabled"
transition {
days = 60
storage_class = "GLACIER"
}
}
}
# Ideally we would not be using versioning of s3 files but it's added for the tfsec & checkov checks.
resource "aws_s3_bucket_versioning" "xsiam_firehose_bucket_versioning" {
count = local.go-build == true ? 1 : 0
bucket = aws_s3_bucket.xsiam_firehose_bucket[count.index].id
versioning_configuration {
status = "Enabled"
}
}
# By default s3 already blocks public access but this added for the tfsec & checkov checks.
resource "aws_s3_bucket_public_access_block" "xsiam_firehose_bucket_block_public" {
count = local.go-build == true ? 1 : 0
bucket = aws_s3_bucket.xsiam_firehose_bucket[count.index].id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}