-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfilestorage.tf
188 lines (162 loc) · 5.46 KB
/
filestorage.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
##### Applicant-uploaded files bucket #####
resource "aws_s3_bucket" "civiform_files_s3" {
tags = {
Name = "${var.app_prefix} Civiform Files"
Type = "Civiform Files"
}
bucket = "${var.app_prefix}-civiform-files-s3"
force_destroy = local.force_destroy_s3
}
resource "aws_s3_bucket_public_access_block" "civiform_files_access" {
bucket = aws_s3_bucket.civiform_files_s3.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_policy" "civiform_files_policy" {
bucket = aws_s3_bucket.civiform_files_s3.id
policy = data.aws_iam_policy_document.civiform_files_policy.json
}
resource "aws_kms_key" "file_storage_key" {
description = "This key is used to encrypt files uploaded by the user"
deletion_window_in_days = 10
enable_key_rotation = true
}
resource "aws_s3_bucket_server_side_encryption_configuration" "civiform_files_encryption" {
bucket = aws_s3_bucket.civiform_files_s3.bucket
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.file_storage_key.arn
sse_algorithm = "aws:kms"
}
}
}
data "aws_iam_policy_document" "civiform_files_policy" {
statement {
actions = ["s3:*"]
effect = "Deny"
resources = [
"${aws_s3_bucket.civiform_files_s3.arn}/*"]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "ArnNotEquals"
variable = "aws:PrincipalArn"
values = [aws_iam_role.civiform_ecs_task_execution_role.arn]
}
}
statement {
actions = ["s3:*"]
effect = "Allow"
resources = [aws_s3_bucket.civiform_files_s3.arn,
"${aws_s3_bucket.civiform_files_s3.arn}/*"]
principals {
type = "AWS"
identifiers = [aws_iam_role.civiform_ecs_task_execution_role.arn]
}
}
}
resource "aws_s3_bucket_ownership_controls" "civiform_files_ownership" {
bucket = aws_s3_bucket.civiform_files_s3.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_logging" "civiform_files_logging" {
bucket = aws_s3_bucket.civiform_files_s3.id
target_bucket = aws_s3_bucket.log_bucket.id
target_prefix = "file-access-log/"
}
##### Public files bucket (for program images etc.) #####
resource "aws_s3_bucket" "civiform_public_files_s3" {
tags = {
Name = "${var.app_prefix} Civiform Public Files"
Type = "Civiform Public Files"
}
bucket = "${var.app_prefix}-civiform-public-files-s3"
force_destroy = local.force_destroy_s3
}
resource "aws_s3_bucket_public_access_block" "civiform_public_files_access" {
bucket = aws_s3_bucket.civiform_public_files_s3.id
# We specifically want files in this bucket to be publicly accessible via the
# policy specified in civiform_public_files_policy.
block_public_policy = false
restrict_public_buckets = false
# Because this bucket is BucketOwnerEnforced, we use policies instead of ACLs
# to control access, which is why the public_acls values can still be blocked.
block_public_acls = true
ignore_public_acls = true
}
resource "aws_s3_bucket_policy" "civiform_public_files_policy" {
bucket = aws_s3_bucket.civiform_public_files_s3.id
policy = data.aws_iam_policy_document.civiform_public_files_policy.json
depends_on = [aws_s3_bucket_public_access_block.civiform_public_files_access]
}
data "aws_iam_policy_document" "civiform_public_files_policy" {
statement {
actions = ["s3:*"]
effect = "Allow"
resources = [aws_s3_bucket.civiform_public_files_s3.arn,
"${aws_s3_bucket.civiform_public_files_s3.arn}/*"]
principals {
type = "AWS"
identifiers = [aws_iam_role.civiform_ecs_task_execution_role.arn]
}
}
# Allows anyone to view program images.
statement {
effect = "Allow"
actions = ["s3:GetObject"]
# The location of program images is controlled in the civiform repo in
# server/app/services/cloud/PublicFileNameFormatter.java. Be sure to keep these files in sync.
resources = ["${aws_s3_bucket.civiform_public_files_s3.arn}/program-summary-image/program-*"]
principals {
type = "*"
identifiers = ["*"]
}
}
}
resource "aws_s3_bucket_ownership_controls" "civiform_public_files_ownership" {
bucket = aws_s3_bucket.civiform_public_files_s3.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
##### Log bucket #####
resource "aws_s3_bucket" "log_bucket" {
tags = {
Name = "${var.app_prefix} Civiform Logs"
Type = "Civiform Logs"
}
bucket = "${var.app_prefix}-civiform-fileaccesslogs"
force_destroy = local.force_destroy_s3
}
resource "aws_s3_bucket_acl" "log_bucket_acl" {
bucket = aws_s3_bucket.log_bucket.id
depends_on = [aws_s3_bucket_ownership_controls.file_access_logs_bucket_ownership]
acl = "log-delivery-write"
}
resource "aws_s3_bucket_ownership_controls" "file_access_logs_bucket_ownership" {
bucket = aws_s3_bucket.log_bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_versioning" "logging_versioning" {
bucket = aws_s3_bucket.log_bucket.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "logging_encryption" {
bucket = aws_s3_bucket.log_bucket.bucket
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.file_storage_key.arn
sse_algorithm = "aws:kms"
}
}
}