-
Notifications
You must be signed in to change notification settings - Fork 4
/
bucket.tf
158 lines (140 loc) · 3.77 KB
/
bucket.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
/**
* A terraform module that creates a tagged S3 bucket with federated assumed role access and default kms key encryption.
* Note that the `role_users` and `roles` must be valid roles that exist in the same account that the script is run in.
*/
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
force_destroy = "true"
versioning {
enabled = var.versioning
}
tags = var.tags
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = aws_kms_key.config.arn
}
}
}
lifecycle_rule {
id = "auto-delete-incomplete-after-x-days"
prefix = ""
enabled = var.multipart_delete
abort_incomplete_multipart_upload_days = var.multipart_days
}
}
resource "aws_s3_bucket_policy" "bucket_policy" {
bucket = aws_s3_bucket.bucket.id
policy = data.template_file.bucket_policy.rendered
}
data "aws_caller_identity" "current" {
}
//render dynamic list of users for s3
data "template_file" "s3_user_principal" {
count = length(var.saml_users)
template = "arn:aws:sts::$${account}:assumed-role/$${role}/$${user}"
vars = {
account = data.aws_caller_identity.current.account_id
role = var.saml_role
user = var.saml_users[count.index]
}
}
//render dynamic list of iam users for s3
data "template_file" "s3_iam_user_principal" {
count = length(var.iam_users)
template = "arn:aws:iam::$${account}:user/$${user}"
vars = {
account = data.aws_caller_identity.current.account_id
user = var.iam_users[count.index]
}
}
//render dynamic list of roles for s3
data "template_file" "s3_role_principal" {
count = length(var.app_roles)
template = "arn:aws:sts::$${account}:assumed-role/$${role}/*"
vars = {
account = data.aws_caller_identity.current.account_id
role = var.app_roles[count.index]
}
}
//render bucket policy including dynamic principals
data "template_file" "bucket_policy" {
template = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyWriteToAllExceptRoleUsers",
"Effect": "Deny",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:Delete*",
"s3:Put*",
"s3:Replicate*",
"s3:Restore*"
],
"Resource": [
"${aws_s3_bucket.bucket.arn}",
"${aws_s3_bucket.bucket.arn}/*"
],
"Condition": {
"StringNotLike": {
"aws:arn": $${principals}
}
}
},
{
"Sid": "AllowRoleUsersToRead",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"${aws_s3_bucket.bucket.arn}",
"${aws_s3_bucket.bucket.arn}/*"
],
"Condition": {
"StringLike": {
"aws:arn": $${principals}
}
}
},
{
"Sid": "AllowSamlAccountUsersToReadTagsAndAcl",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetBucketTagging",
"s3:GetBucketAcl"
],
"Resource": [
"${aws_s3_bucket.bucket.arn}",
"${aws_s3_bucket.bucket.arn}/*"
],
"Condition": {
"StringLike": {
"aws:arn": "arn:aws:sts::${data.aws_caller_identity.current.account_id}:*"
}
}
}
]
}
EOF
vars = {
principals = jsonencode(
concat(
data.template_file.s3_user_principal.*.rendered,
data.template_file.s3_role_principal.*.rendered,
data.template_file.s3_iam_user_principal.*.rendered,
),
)
}
}
//the arn of the bucket that was created
output "bucket_arn" {
value = aws_s3_bucket.bucket.arn
}