Skip to content

Commit c56c684

Browse files
aiell0antonbabenko
andauthored
feat: Added optional bucket policy for requiring TLS 1.2 (#126)
Co-authored-by: Anton Babenko <[email protected]>
1 parent b1a3fd9 commit c56c684

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ No modules.
133133
| [aws_iam_policy_document.deny_insecure_transport](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
134134
| [aws_iam_policy_document.elb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
135135
| [aws_iam_policy_document.lb_log_delivery](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
136+
| [aws_iam_policy_document.require_latest_tls](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
136137

137138
## Inputs
138139

@@ -145,6 +146,7 @@ No modules.
145146
| <a name="input_attach_lb_log_delivery_policy"></a> [attach\_lb\_log\_delivery\_policy](#input\_attach\_lb\_log\_delivery\_policy) | Controls if S3 bucket should have ALB/NLB log delivery policy attached | `bool` | `false` | no |
146147
| <a name="input_attach_policy"></a> [attach\_policy](#input\_attach\_policy) | Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy) | `bool` | `false` | no |
147148
| <a name="input_attach_public_policy"></a> [attach\_public\_policy](#input\_attach\_public\_policy) | Controls if a user defined public bucket policy will be attached (set to `false` to allow upstream to apply defaults to the bucket) | `bool` | `true` | no |
149+
| <a name="input_attach_require_latest_tls_policy"></a> [attach\_require\_latest\_tls\_policy](#input\_attach\_require\_latest\_tls\_policy) | Controls if S3 bucket should require the latest version of TLS | `bool` | `false` | no |
148150
| <a name="input_block_public_acls"></a> [block\_public\_acls](#input\_block\_public\_acls) | Whether Amazon S3 should block public ACLs for this bucket. | `bool` | `false` | no |
149151
| <a name="input_block_public_policy"></a> [block\_public\_policy](#input\_block\_public\_policy) | Whether Amazon S3 should block public bucket policies for this bucket. | `bool` | `false` | no |
150152
| <a name="input_bucket"></a> [bucket](#input\_bucket) | (Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name. | `string` | `null` | no |

examples/complete/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module "log_bucket" {
5959
attach_elb_log_delivery_policy = true
6060
attach_lb_log_delivery_policy = true
6161
attach_deny_insecure_transport_policy = true
62+
attach_require_latest_tls_policy = true
6263
}
6364

6465
module "cloudfront_log_bucket" {
@@ -90,6 +91,7 @@ module "s3_bucket" {
9091
policy = data.aws_iam_policy_document.bucket_policy.json
9192

9293
attach_deny_insecure_transport_policy = true
94+
attach_require_latest_tls_policy = true
9395

9496
tags = {
9597
Owner = "Anton"

main.tf

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
attach_policy = var.attach_elb_log_delivery_policy || var.attach_lb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
2+
attach_policy = var.attach_require_latest_tls_policy || var.attach_elb_log_delivery_policy || var.attach_lb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
33
}
44

55
resource "aws_s3_bucket" "this" {
@@ -275,6 +275,7 @@ data "aws_iam_policy_document" "combined" {
275275
source_policy_documents = compact([
276276
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
277277
var.attach_lb_log_delivery_policy ? data.aws_iam_policy_document.lb_log_delivery[0].json : "",
278+
var.attach_require_latest_tls_policy ? data.aws_iam_policy_document.require_latest_tls[0].json : "",
278279
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport[0].json : "",
279280
var.attach_policy ? var.policy : ""
280281
])
@@ -390,6 +391,37 @@ data "aws_iam_policy_document" "deny_insecure_transport" {
390391
}
391392
}
392393

394+
data "aws_iam_policy_document" "require_latest_tls" {
395+
count = var.create_bucket && var.attach_require_latest_tls_policy ? 1 : 0
396+
397+
statement {
398+
sid = "denyOutdatedTLS"
399+
effect = "Deny"
400+
401+
actions = [
402+
"s3:*",
403+
]
404+
405+
resources = [
406+
aws_s3_bucket.this[0].arn,
407+
"${aws_s3_bucket.this[0].arn}/*",
408+
]
409+
410+
principals {
411+
type = "*"
412+
identifiers = ["*"]
413+
}
414+
415+
condition {
416+
test = "NumericLessThan"
417+
variable = "s3:TlsVersion"
418+
values = [
419+
"1.2"
420+
]
421+
}
422+
}
423+
}
424+
393425
resource "aws_s3_bucket_public_access_block" "this" {
394426
count = var.create_bucket && var.attach_public_policy ? 1 : 0
395427

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ variable "attach_deny_insecure_transport_policy" {
2222
default = false
2323
}
2424

25+
variable "attach_require_latest_tls_policy" {
26+
description = "Controls if S3 bucket should require the latest version of TLS"
27+
type = bool
28+
default = false
29+
}
30+
2531
variable "attach_policy" {
2632
description = "Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy)"
2733
type = bool

0 commit comments

Comments
 (0)