generated from blackbird-cloud/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
122 lines (103 loc) · 2.87 KB
/
main.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
data "aws_caller_identity" "current" {}
data "aws_s3_bucket" "selected" {
bucket = var.s3_bucket_id
}
data "aws_elb_service_account" "this" {
count = var.attach_elb_log_delivery_policy ? 1 : 0
}
data "aws_iam_policy_document" "elb_log_delivery" {
count = var.attach_elb_log_delivery_policy ? 1 : 0
statement {
sid = ""
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["${data.aws_s3_bucket.selected.arn}/*"]
principals {
type = "AWS"
identifiers = data.aws_elb_service_account.this.*.arn
}
}
}
data "aws_iam_policy_document" "lb_log_delivery" {
count = var.attach_lb_log_delivery_policy ? 1 : 0
statement {
sid = "AWSLogDeliveryWrite"
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["${data.aws_s3_bucket.selected.arn}/*"]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
statement {
sid = "AWSLogDeliveryAclCheck"
effect = "Allow"
actions = ["s3:GetBucketAcl"]
resources = [data.aws_s3_bucket.selected.arn]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "require_latest_tls" {
statement {
sid = "denyOutdatedTLS"
effect = "Deny"
actions = ["s3:*"]
resources = [
data.aws_s3_bucket.selected.arn,
"${data.aws_s3_bucket.selected.arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "NumericLessThan"
variable = "s3:TlsVersion"
values = [
"1.2"
]
}
}
}
data "aws_iam_policy_document" "deny_insecure_transport" {
statement {
sid = "denyInsecureTransport"
effect = "Deny"
actions = ["s3:*"]
resources = [
data.aws_s3_bucket.selected.arn,
"${data.aws_s3_bucket.selected.arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
data "aws_iam_policy_document" "combined" {
source_policy_documents = compact([
var.attach_require_latest_tls_policy ? data.aws_iam_policy_document.require_latest_tls.json : "",
var.attach_deny_insecure_transport_policy ? data.aws_iam_policy_document.deny_insecure_transport.json : "",
var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : "",
var.attach_lb_log_delivery_policy ? data.aws_iam_policy_document.lb_log_delivery[0].json : "",
var.policy,
])
}
resource "aws_s3_bucket_policy" "bucket" {
bucket = var.s3_bucket_id
policy = data.aws_iam_policy_document.combined.json
}