-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.tf
133 lines (115 loc) · 3.53 KB
/
s3.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
module "s3_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0"
context = module.kops_label.context
attributes = ["state"]
}
locals {
default_s3_actions = ["s3:Get*", "s3:List*"]
default_s3_resources = [
"arn:aws:s3:::${aws_s3_bucket.kops_state.id}",
"arn:aws:s3:::${aws_s3_bucket.kops_state.id}/${local.cluster_dns}/*",
]
lb_access_log_account = {
eu-central-1 = "054676820928",
eu-west-2 = "652711504416",
us-east-1 = "127311923021",
us-west-2 = "797873946194",
ap-northeast-2 = "600734575887",
ap-southeast-1 = "114774131450",
ap-east-1 = "754344448648",
# TODO: complete list
}
api_log_s3_policies = [
{
readonly = false
resources = [aws_s3_bucket.kops_state.arn]
actions = ["s3:GetBucketAcl"]
principals = [{
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}]
},
{
readonly = false
resources = [format("%s/%s/*", aws_s3_bucket.kops_state.arn, local.api_log_prefix)]
actions = ["s3:PutObject"]
principals = [{
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}]
},
{
readonly = false
resources = [format("%s/%s/*", aws_s3_bucket.kops_state.arn, local.api_log_prefix)]
actions = ["s3:PutObject"]
principals = [{
type = "AWS"
identifiers = [format("arn:aws:iam::%s:root", local.lb_access_log_account[local.aws_region])]
}]
}
]
required_s3_policies = concat([], var.create_public_api_record ? local.api_log_s3_policies : [])
custom_s3_policies = concat(var.custom_s3_policies, local.required_s3_policies)
}
resource "aws_s3_bucket" "kops_state" {
bucket = module.s3_label.id
tags = module.s3_label.tags
region = local.aws_region
acl = "private"
force_destroy = false
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
lifecycle_rule {
enabled = true
prefix = local.cluster_dns
noncurrent_version_expiration {
days = 90
}
}
lifecycle_rule {
enabled = var.create_public_api_record
prefix = local.api_log_prefix
expiration {
days = 120
}
}
}
data "aws_iam_policy_document" "custom_s3" {
count = length(local.custom_s3_policies) > 0 ? 1 : 0
dynamic "statement" {
for_each = local.custom_s3_policies
content {
effect = "Allow"
resources = lookup(statement.value, "resources", local.default_s3_resources)
actions = lookup(statement.value, "readonly", true) ? local.default_s3_actions : lookup(statement.value, "actions", ["*"])
dynamic "principals" {
for_each = lookup(statement.value, "principals", [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
}
}
}
resource "aws_s3_bucket_policy" "current" {
count = length(local.custom_s3_policies) > 0 ? 1 : 0
bucket = aws_s3_bucket.kops_state.id
policy = join("", data.aws_iam_policy_document.custom_s3.*.json)
}
resource "aws_s3_bucket_public_access_block" "block" {
depends_on = [aws_s3_bucket_policy.current]
bucket = aws_s3_bucket.kops_state.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}