Skip to content

Commit 12ad5b6

Browse files
feat: Updated S3 Bucket Block Public Access and ACL(Object Ownership) defaults to work since April 2023 (#226)
Co-authored-by: Anton Babenko <[email protected]>
1 parent c25b19b commit 12ad5b6

File tree

9 files changed

+47
-31
lines changed

9 files changed

+47
-31
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.77.0
3+
rev: v1.77.3
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ module "s3_bucket" {
2828
bucket = "my-s3-bucket"
2929
acl = "private"
3030
31+
control_object_ownership = true
32+
object_ownership = "ObjectWriter"
33+
3134
versioning = {
3235
enabled = true
3336
}
34-
3537
}
3638
```
3739

@@ -47,6 +49,9 @@ module "s3_bucket_for_logs" {
4749
# Allow deletion of non-empty bucket
4850
force_destroy = true
4951
52+
control_object_ownership = true
53+
object_ownership = "ObjectWriter"
54+
5055
attach_elb_log_delivery_policy = true
5156
}
5257
```
@@ -63,6 +68,9 @@ module "s3_bucket_for_logs" {
6368
# Allow deletion of non-empty bucket
6469
force_destroy = true
6570
71+
control_object_ownership = true
72+
object_ownership = "ObjectWriter"
73+
6674
attach_elb_log_delivery_policy = true # Required for ALB logs
6775
attach_lb_log_delivery_policy = true # Required for ALB/NLB logs
6876
}
@@ -182,8 +190,8 @@ No modules.
182190
| <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 |
183191
| <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 |
184192
| <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 |
185-
| <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 |
186-
| <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 |
193+
| <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` | `true` | no |
194+
| <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` | `true` | no |
187195
| <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 |
188196
| <a name="input_bucket_prefix"></a> [bucket\_prefix](#input\_bucket\_prefix) | (Optional, Forces new resource) Creates a unique bucket name beginning with the specified prefix. Conflicts with bucket. | `string` | `null` | no |
189197
| <a name="input_control_object_ownership"></a> [control\_object\_ownership](#input\_control\_object\_ownership) | Whether to manage S3 Bucket Ownership Controls on this bucket. | `bool` | `false` | no |
@@ -192,7 +200,7 @@ No modules.
192200
| <a name="input_expected_bucket_owner"></a> [expected\_bucket\_owner](#input\_expected\_bucket\_owner) | The account ID of the expected bucket owner | `string` | `null` | no |
193201
| <a name="input_force_destroy"></a> [force\_destroy](#input\_force\_destroy) | (Optional, Default:false ) A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error. These objects are not recoverable. | `bool` | `false` | no |
194202
| <a name="input_grant"></a> [grant](#input\_grant) | An ACL policy grant. Conflicts with `acl` | `any` | `[]` | no |
195-
| <a name="input_ignore_public_acls"></a> [ignore\_public\_acls](#input\_ignore\_public\_acls) | Whether Amazon S3 should ignore public ACLs for this bucket. | `bool` | `false` | no |
203+
| <a name="input_ignore_public_acls"></a> [ignore\_public\_acls](#input\_ignore\_public\_acls) | Whether Amazon S3 should ignore public ACLs for this bucket. | `bool` | `true` | no |
196204
| <a name="input_intelligent_tiering"></a> [intelligent\_tiering](#input\_intelligent\_tiering) | Map containing intelligent tiering configuration. | `any` | `{}` | no |
197205
| <a name="input_inventory_configuration"></a> [inventory\_configuration](#input\_inventory\_configuration) | Map containing S3 inventory configuration. | `any` | `{}` | no |
198206
| <a name="input_inventory_self_source_destination"></a> [inventory\_self\_source\_destination](#input\_inventory\_self\_source\_destination) | Whether or not the inventory source bucket is also the destination bucket. | `bool` | `false` | no |
@@ -203,13 +211,13 @@ No modules.
203211
| <a name="input_metric_configuration"></a> [metric\_configuration](#input\_metric\_configuration) | Map containing bucket metric configuration. | `any` | `[]` | no |
204212
| <a name="input_object_lock_configuration"></a> [object\_lock\_configuration](#input\_object\_lock\_configuration) | Map containing S3 object locking configuration. | `any` | `{}` | no |
205213
| <a name="input_object_lock_enabled"></a> [object\_lock\_enabled](#input\_object\_lock\_enabled) | Whether S3 bucket should have an Object Lock configuration enabled. | `bool` | `false` | no |
206-
| <a name="input_object_ownership"></a> [object\_ownership](#input\_object\_ownership) | Object ownership. Valid values: BucketOwnerEnforced, BucketOwnerPreferred or ObjectWriter. 'BucketOwnerEnforced': ACLs are disabled, and the bucket owner automatically owns and has full control over every object in the bucket. 'BucketOwnerPreferred': Objects uploaded to the bucket change ownership to the bucket owner if the objects are uploaded with the bucket-owner-full-control canned ACL. 'ObjectWriter': The uploading account will own the object if the object is uploaded with the bucket-owner-full-control canned ACL. | `string` | `"ObjectWriter"` | no |
214+
| <a name="input_object_ownership"></a> [object\_ownership](#input\_object\_ownership) | Object ownership. Valid values: BucketOwnerEnforced, BucketOwnerPreferred or ObjectWriter. 'BucketOwnerEnforced': ACLs are disabled, and the bucket owner automatically owns and has full control over every object in the bucket. 'BucketOwnerPreferred': Objects uploaded to the bucket change ownership to the bucket owner if the objects are uploaded with the bucket-owner-full-control canned ACL. 'ObjectWriter': The uploading account will own the object if the object is uploaded with the bucket-owner-full-control canned ACL. | `string` | `"BucketOwnerEnforced"` | no |
207215
| <a name="input_owner"></a> [owner](#input\_owner) | Bucket owner's display name and ID. Conflicts with `acl` | `map(string)` | `{}` | no |
208216
| <a name="input_policy"></a> [policy](#input\_policy) | (Optional) A valid bucket policy JSON document. Note that if the policy document is not specific enough (but still valid), Terraform may view the policy as constantly changing in a terraform plan. In this case, please make sure you use the verbose/specific version of the policy. For more information about building AWS IAM policy documents with Terraform, see the AWS IAM Policy Document Guide. | `string` | `null` | no |
209217
| <a name="input_putin_khuylo"></a> [putin\_khuylo](#input\_putin\_khuylo) | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | `bool` | `true` | no |
210218
| <a name="input_replication_configuration"></a> [replication\_configuration](#input\_replication\_configuration) | Map containing cross-region replication configuration. | `any` | `{}` | no |
211219
| <a name="input_request_payer"></a> [request\_payer](#input\_request\_payer) | (Optional) Specifies who should bear the cost of Amazon S3 data transfer. Can be either BucketOwner or Requester. By default, the owner of the S3 bucket would incur the costs of any data transfer. See Requester Pays Buckets developer guide for more information. | `string` | `null` | no |
212-
| <a name="input_restrict_public_buckets"></a> [restrict\_public\_buckets](#input\_restrict\_public\_buckets) | Whether Amazon S3 should restrict public bucket policies for this bucket. | `bool` | `false` | no |
220+
| <a name="input_restrict_public_buckets"></a> [restrict\_public\_buckets](#input\_restrict\_public\_buckets) | Whether Amazon S3 should restrict public bucket policies for this bucket. | `bool` | `true` | no |
213221
| <a name="input_server_side_encryption_configuration"></a> [server\_side\_encryption\_configuration](#input\_server\_side\_encryption\_configuration) | Map containing server-side encryption configuration. | `any` | `{}` | no |
214222
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A mapping of tags to assign to the bucket. | `map(string)` | `{}` | no |
215223
| <a name="input_versioning"></a> [versioning](#input\_versioning) | Map containing versioning configuration. | `map(string)` | `{}` | no |

examples/complete-legacy/main.tf

+8-5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ module "log_bucket" {
6666
acl = "log-delivery-write"
6767
force_destroy = true
6868

69+
control_object_ownership = true
70+
object_ownership = "ObjectWriter"
71+
6972
attach_elb_log_delivery_policy = true
7073
attach_lb_log_delivery_policy = true
7174
attach_deny_insecure_transport_policy = true
@@ -206,11 +209,11 @@ module "s3_bucket" {
206209
}
207210
}
208211

209-
# S3 bucket-level Public Access Block configuration
210-
block_public_acls = true
211-
block_public_policy = true
212-
ignore_public_acls = true
213-
restrict_public_buckets = true
212+
# S3 bucket-level Public Access Block configuration (by default now AWS has made this default as true for S3 bucket-level block public access)
213+
# block_public_acls = true
214+
# block_public_policy = true
215+
# ignore_public_acls = true
216+
# restrict_public_buckets = true
214217

215218
# S3 Bucket Ownership Controls
216219
control_object_ownership = true

examples/complete/main.tf

+11-6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ module "log_bucket" {
7070
acl = "log-delivery-write"
7171
force_destroy = true
7272

73+
control_object_ownership = true
74+
object_ownership = "ObjectWriter"
75+
7376
attach_elb_log_delivery_policy = true
7477
attach_lb_log_delivery_policy = true
7578
attach_deny_insecure_transport_policy = true
@@ -79,7 +82,9 @@ module "log_bucket" {
7982
module "cloudfront_log_bucket" {
8083
source = "../../"
8184

82-
bucket = "cloudfront-logs-${random_pet.this.id}"
85+
bucket = "cloudfront-logs-${random_pet.this.id}"
86+
control_object_ownership = true
87+
object_ownership = "ObjectWriter"
8388

8489
grant = [{
8590
type = "CanonicalUser"
@@ -130,11 +135,11 @@ module "s3_bucket" {
130135
attach_deny_insecure_transport_policy = true
131136
attach_require_latest_tls_policy = true
132137

133-
# S3 bucket-level Public Access Block configuration
134-
block_public_acls = true
135-
block_public_policy = true
136-
ignore_public_acls = true
137-
restrict_public_buckets = true
138+
# S3 bucket-level Public Access Block configuration (by default now AWS has made this default as true for S3 bucket-level block public access)
139+
# block_public_acls = true
140+
# block_public_policy = true
141+
# ignore_public_acls = true
142+
# restrict_public_buckets = true
138143

139144
# S3 Bucket Ownership Controls
140145
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls

variables.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -235,25 +235,25 @@ variable "object_lock_enabled" {
235235
variable "block_public_acls" {
236236
description = "Whether Amazon S3 should block public ACLs for this bucket."
237237
type = bool
238-
default = false
238+
default = true
239239
}
240240

241241
variable "block_public_policy" {
242242
description = "Whether Amazon S3 should block public bucket policies for this bucket."
243243
type = bool
244-
default = false
244+
default = true
245245
}
246246

247247
variable "ignore_public_acls" {
248248
description = "Whether Amazon S3 should ignore public ACLs for this bucket."
249249
type = bool
250-
default = false
250+
default = true
251251
}
252252

253253
variable "restrict_public_buckets" {
254254
description = "Whether Amazon S3 should restrict public bucket policies for this bucket."
255255
type = bool
256-
default = false
256+
default = true
257257
}
258258

259259
variable "control_object_ownership" {
@@ -265,7 +265,7 @@ variable "control_object_ownership" {
265265
variable "object_ownership" {
266266
description = "Object ownership. Valid values: BucketOwnerEnforced, BucketOwnerPreferred or ObjectWriter. 'BucketOwnerEnforced': ACLs are disabled, and the bucket owner automatically owns and has full control over every object in the bucket. 'BucketOwnerPreferred': Objects uploaded to the bucket change ownership to the bucket owner if the objects are uploaded with the bucket-owner-full-control canned ACL. 'ObjectWriter': The uploading account will own the object if the object is uploaded with the bucket-owner-full-control canned ACL."
267267
type = string
268-
default = "ObjectWriter"
268+
default = "BucketOwnerEnforced"
269269
}
270270

271271
variable "putin_khuylo" {

wrappers/main.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ module "wrapper" {
4242
analytics_source_bucket_arn = try(each.value.analytics_source_bucket_arn, var.defaults.analytics_source_bucket_arn, null)
4343
analytics_self_source_destination = try(each.value.analytics_self_source_destination, var.defaults.analytics_self_source_destination, false)
4444
object_lock_enabled = try(each.value.object_lock_enabled, var.defaults.object_lock_enabled, false)
45-
block_public_acls = try(each.value.block_public_acls, var.defaults.block_public_acls, false)
46-
block_public_policy = try(each.value.block_public_policy, var.defaults.block_public_policy, false)
47-
ignore_public_acls = try(each.value.ignore_public_acls, var.defaults.ignore_public_acls, false)
48-
restrict_public_buckets = try(each.value.restrict_public_buckets, var.defaults.restrict_public_buckets, false)
45+
block_public_acls = try(each.value.block_public_acls, var.defaults.block_public_acls, true)
46+
block_public_policy = try(each.value.block_public_policy, var.defaults.block_public_policy, true)
47+
ignore_public_acls = try(each.value.ignore_public_acls, var.defaults.ignore_public_acls, true)
48+
restrict_public_buckets = try(each.value.restrict_public_buckets, var.defaults.restrict_public_buckets, true)
4949
control_object_ownership = try(each.value.control_object_ownership, var.defaults.control_object_ownership, false)
50-
object_ownership = try(each.value.object_ownership, var.defaults.object_ownership, "ObjectWriter")
50+
object_ownership = try(each.value.object_ownership, var.defaults.object_ownership, "BucketOwnerEnforced")
5151
putin_khuylo = try(each.value.putin_khuylo, var.defaults.putin_khuylo, true)
5252
}

wrappers/notification/outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "wrapper" {
22
description = "Map of outputs of a wrapper."
33
value = module.wrapper
4-
# sensitive = false # No sensitive module output found
4+
# sensitive = false # No sensitive module output found
55
}

wrappers/object/outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "wrapper" {
22
description = "Map of outputs of a wrapper."
33
value = module.wrapper
4-
# sensitive = false # No sensitive module output found
4+
# sensitive = false # No sensitive module output found
55
}

wrappers/outputs.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "wrapper" {
22
description = "Map of outputs of a wrapper."
33
value = module.wrapper
4-
# sensitive = false # No sensitive module output found
4+
# sensitive = false # No sensitive module output found
55
}

0 commit comments

Comments
 (0)