forked from trussworks/terraform-aws-s3-anti-virus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anti-virus-scan.tf
220 lines (169 loc) · 5.26 KB
/
anti-virus-scan.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
#
# Lambda Function: Anti-Virus Scanning
#
#
# IAM
#
data "aws_iam_policy_document" "assume_role_scan" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "main_scan" {
# Allow creating and writing CloudWatch logs for Lambda function.
statement {
sid = "WriteCloudWatchLogs"
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/${var.name_scan}:*"]
}
statement {
sid = "s3AntiVirusScan"
effect = "Allow"
actions = concat([
"s3:GetObject",
"s3:GetObjectTagging",
"s3:GetObjectVersion",
"s3:PutObjectTagging",
"s3:PutObjectVersionTagging"
], var.av_delete_infected_files == "True" ? ["s3:DeleteObject"] : [])
resources = formatlist("%s/*", data.aws_s3_bucket.main_scan.*.arn)
}
statement {
sid = "s3AntiVirusDefinitions"
effect = "Allow"
actions = [
"s3:GetObject",
"s3:GetObjectTagging",
]
resources = ["arn:${data.aws_partition.current.partition}:s3:::${var.av_definition_s3_bucket}/${var.av_definition_s3_prefix}/*"]
}
statement {
sid = "s3HeadObject"
effect = "Allow"
actions = [
"s3:ListBucket",
]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${var.av_definition_s3_bucket}",
"arn:${data.aws_partition.current.partition}:s3:::${var.av_definition_s3_bucket}/*",
]
}
dynamic "statement" {
for_each = var.kms_key_sns_arn != "" ? [1] : []
content {
sid = "kmsGenerateDataKey"
effect = "Allow"
actions = [
"kms:GenerateDataKey",
"kms:Decrypt"
]
resources = [
var.kms_key_sns_arn
]
}
}
dynamic "statement" {
for_each = length(compact([var.av_scan_start_sns_arn, var.av_status_sns_arn])) != 0 ? toset([0]) : toset([])
content {
sid = "snsPublish"
actions = [
"sns:Publish",
]
resources = compact([var.av_scan_start_sns_arn, var.av_status_sns_arn])
}
}
}
resource "aws_iam_role" "main_scan" {
name = "lambda-${var.name_scan}"
assume_role_policy = data.aws_iam_policy_document.assume_role_scan.json
permissions_boundary = var.permissions_boundary
tags = var.tags
}
resource "aws_iam_role_policy" "main_scan" {
name = "lambda-${var.name_scan}"
role = aws_iam_role.main_scan.id
policy = data.aws_iam_policy_document.main_scan.json
}
#
# S3 Event
#
data "aws_s3_bucket" "main_scan" {
count = length(var.av_scan_buckets)
bucket = var.av_scan_buckets[count.index]
}
resource "aws_s3_bucket_notification" "main_scan" {
count = var.activate_s3_event_notification ? length(var.av_scan_buckets) : 0
bucket = element(data.aws_s3_bucket.main_scan.*.id, count.index)
lambda_function {
id = var.s3_event_notification_name == null ? element(data.aws_s3_bucket.main_scan.*.id, count.index) : var.s3_event_notification_name
lambda_function_arn = aws_lambda_function.main_scan.arn
events = ["s3:ObjectCreated:*"]
}
}
#
# CloudWatch Logs
#
resource "aws_cloudwatch_log_group" "main_scan" {
# This name must match the lambda function name and should not be changed
name = "/aws/lambda/${var.name_scan}"
retention_in_days = var.cloudwatch_logs_retention_days
kms_key_id = var.cloudwatch_kms_arn
tags = merge(
{
"Name" = var.name_scan
},
var.tags
)
}
#
# Lambda Function
#
resource "aws_lambda_function" "main_scan" {
depends_on = [aws_cloudwatch_log_group.main_scan]
description = "Scans s3 objects with clamav for viruses."
s3_bucket = var.lambda_s3_bucket
s3_key = local.lambda_package_key
function_name = var.name_scan
role = aws_iam_role.main_scan.arn
handler = "scan.lambda_handler"
runtime = "python3.9"
memory_size = var.memory_size
timeout = var.timeout_seconds
environment {
variables = {
AV_DEFINITION_S3_BUCKET = var.av_definition_s3_bucket
AV_DEFINITION_S3_PREFIX = var.av_definition_s3_prefix
AV_SCAN_START_SNS_ARN = var.av_scan_start_sns_arn
AV_STATUS_SNS_ARN = var.av_status_sns_arn
AV_STATUS_SNS_PUBLISH_CLEAN = var.av_status_sns_publish_clean
AV_STATUS_SNS_PUBLISH_INFECTED = var.av_status_sns_publish_infected
AV_DELETE_INFECTED_FILES = var.av_delete_infected_files
}
}
architectures = [var.platform]
tags = merge(
{
"Name" = var.name_scan
},
var.tags
)
}
resource "aws_lambda_permission" "main_scan" {
count = length(var.av_scan_buckets)
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.main_scan.function_name
principal = "s3.amazonaws.com"
source_account = data.aws_caller_identity.current.account_id
source_arn = element(data.aws_s3_bucket.main_scan.*.arn, count.index)
statement_id = replace("${var.name_scan}-${element(data.aws_s3_bucket.main_scan.*.id, count.index)}", ".", "-")
}