Skip to content

Commit 379ad81

Browse files
authored
bugfix - deny all insecure traffic to all s3 (#182)
* bugfix - deny all insecure traffic to all s3 * sefsev
1 parent ae3c155 commit 379ad81

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file is for you! Edit it to implement your own hooks (make targets) into
2-
# the project as automated steps to be executed on locally and in the CD pipeline.
2+
# the project as automated steps to be executed on locally and in the CD pipeline
33
# ==============================================================================
44
include scripts/init.mk
55

infrastructure/modules/s3/s3.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,39 @@ resource "aws_s3_bucket_versioning" "storage_bucket_versioning_config" {
1414
}
1515
}
1616

17+
# ensure only secure transport is allowed
18+
19+
resource "aws_s3_bucket_policy" "tfstate_bucket" {
20+
bucket = aws_s3_bucket.storage_bucket.id
21+
policy = data.aws_iam_policy_document.storage_s3_bucket_policy.json
22+
}
23+
24+
data "aws_iam_policy_document" "storage_s3_bucket_policy" {
25+
statement {
26+
sid = "AllowSslRequestsOnly"
27+
actions = [
28+
"s3:*",
29+
]
30+
effect = "Deny"
31+
resources = [
32+
aws_s3_bucket.storage_bucket.arn,
33+
"${aws_s3_bucket.storage_bucket.arn}/*",
34+
]
35+
principals {
36+
type = "*"
37+
identifiers = ["*"]
38+
}
39+
condition {
40+
test = "Bool"
41+
values = [
42+
"false",
43+
]
44+
45+
variable = "aws:SecureTransport"
46+
}
47+
}
48+
}
49+
1750
# Block public access to the bucket
1851
resource "aws_s3_bucket_public_access_block" "storage_bucket_block_public_access" {
1952
bucket = aws_s3_bucket.storage_bucket.id
@@ -77,6 +110,36 @@ resource "aws_s3_bucket_logging" "storage_bucket_logging_config" {
77110
target_prefix = "bucket_logs/"
78111
}
79112

113+
resource "aws_s3_bucket_policy" "storage_bucket_access_logs" {
114+
bucket = aws_s3_bucket.storage_bucket_access_logs.id
115+
policy = data.aws_iam_policy_document.access_logs_s3_bucket_policy.json
116+
}
117+
data "aws_iam_policy_document" "access_logs_s3_bucket_policy" {
118+
statement {
119+
sid = "AllowSslRequestsOnly"
120+
actions = [
121+
"s3:*",
122+
]
123+
effect = "Deny"
124+
resources = [
125+
aws_s3_bucket.storage_bucket_access_logs.arn,
126+
"${aws_s3_bucket.storage_bucket_access_logs.arn}/*",
127+
]
128+
principals {
129+
type = "*"
130+
identifiers = ["*"]
131+
}
132+
condition {
133+
test = "Bool"
134+
values = [
135+
"false",
136+
]
137+
138+
variable = "aws:SecureTransport"
139+
}
140+
}
141+
}
142+
80143
resource "aws_s3_bucket_server_side_encryption_configuration" "storage_bucket_access_logs_server_side_encryption_config" {
81144
bucket = aws_s3_bucket.storage_bucket_access_logs.id
82145

infrastructure/stacks/api-layer/iam_policies.tf

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,6 @@ resource "aws_iam_role_policy" "external_dynamodb_write_policy" {
2929
policy = data.aws_iam_policy_document.dynamodb_write_policy_doc.json
3030
}
3131

32-
33-
# Deny all S3 actions on the access logs bucket unless requests use secure (SSL) transport.
34-
data "aws_iam_policy_document" "storage_bucket_access_logs_policy" {
35-
statement {
36-
sid = "AllowSSLRequestsOnly"
37-
actions = [
38-
"s3:*",
39-
]
40-
effect = "Deny"
41-
resources = [
42-
module.s3_rules_bucket.storage_bucket_access_logs_arn,
43-
"${module.s3_rules_bucket.storage_bucket_access_logs_arn}/*",
44-
]
45-
principals {
46-
type = "*"
47-
identifiers = ["*"]
48-
}
49-
condition {
50-
test = "Bool"
51-
values = [
52-
"false",
53-
]
54-
55-
variable = "aws:SecureTransport"
56-
}
57-
}
58-
}
59-
60-
resource "aws_s3_bucket_policy" "storage_bucket_access_logs_policy" {
61-
bucket = module.s3_rules_bucket.storage_bucket_access_logs_id
62-
policy = data.aws_iam_policy_document.storage_bucket_access_logs_policy.json
63-
}
64-
6532
# Policy doc for S3 Rules bucket
6633
data "aws_iam_policy_document" "s3_rules_bucket_policy" {
6734
statement {
@@ -82,7 +49,6 @@ data "aws_iam_policy_document" "s3_rules_bucket_policy" {
8249
}
8350
}
8451

85-
8652
# Attach s3 read policy to Lambda role
8753
resource "aws_iam_role_policy" "lambda_s3_read_policy" {
8854
name = "S3ReadAccess"

0 commit comments

Comments
 (0)