-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccess-control.tf
136 lines (107 loc) · 3.86 KB
/
access-control.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
data "aws_canonical_user_id" "this" {}
locals {
default_grants = [
{
type = "CanonicalUser"
grantee = data.aws_canonical_user_id.this.id
permission = "FULL_CONTROL"
}
]
grants = concat(
local.default_grants,
var.grants
)
}
###################################################
# Policy for S3 Bucket
###################################################
data "aws_iam_policy_document" "this" {
source_policy_documents = concat(
var.tls_required ? [data.aws_iam_policy_document.tls_required.json] : [],
var.logging.is_target_bucket ? [data.aws_iam_policy_document.access_logging.json] : [],
)
override_policy_documents = var.policy != null ? [var.policy] : null
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.this.json
}
###################################################
# Object Ownership for S3 Bucket
###################################################
resource "aws_s3_bucket_ownership_controls" "this" {
bucket = aws_s3_bucket.this.bucket
rule {
object_ownership = var.object_ownership
}
}
###################################################
# ACL for S3 Bucket
###################################################
# INFO: Not supported attributes
# - `expected_bucket_owner`
# - `acl`
# - `access_control_policy.owner.display_name`
resource "aws_s3_bucket_acl" "this" {
count = var.object_ownership != "BucketOwnerEnforced" ? 1 : 0
bucket = aws_s3_bucket.this.bucket
access_control_policy {
dynamic "grant" {
for_each = local.grants
content {
grantee {
type = grant.value.type
id = grant.value.type == "CanonicalUser" ? grant.value.grantee : null
uri = grant.value.type == "Group" ? grant.value.grantee : null
email_address = grant.value.type == "AmazonCustomerByEmail" ? grant.value.grantee : null
}
permission = grant.value.permission
}
}
owner {
id = data.aws_canonical_user_id.this.id
}
}
}
###################################################
# Public Access Block for S3 Bucket
###################################################
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.bucket
# Block new public ACLs and uploading public objects
block_public_acls = (var.block_public_access.enabled
|| var.block_public_access.block_public_acls_enabled)
# Retroactively remove public access granted through public ACLs
ignore_public_acls = (var.block_public_access.enabled
|| var.block_public_access.ignore_public_acls_enabled)
# Block new public bucket policies
block_public_policy = (var.block_public_access.enabled
|| var.block_public_access.block_public_policy_enabled)
# Retroactivley block public and cross-account access if bucket has public policies
restrict_public_buckets = (var.block_public_access.enabled
|| var.block_public_access.restrict_public_buckets_enabled)
# To avoid OperationAborted: A conflicting conditional operation is currently in progress
depends_on = [
aws_s3_bucket_policy.this,
]
}
###################################################
# CORS (Cross-Origin Resource Sharing) for S3 Bucket
###################################################
# INFO: Not supported attributes
# - `expected_bucket_owner`
resource "aws_s3_bucket_cors_configuration" "this" {
count = length(var.cors_rules) > 0 ? 1 : 0
bucket = aws_s3_bucket.this.bucket
dynamic "cors_rule" {
for_each = var.cors_rules
content {
id = coalesce(cors_rule.value.id, cors_rule.key)
allowed_headers = cors_rule.value.allowed_headers
allowed_methods = cors_rule.value.allowed_methods
allowed_origins = cors_rule.value.allowed_origins
expose_headers = cors_rule.value.expose_headers
max_age_seconds = cors_rule.value.max_age
}
}
}