-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.tf
169 lines (140 loc) · 4.06 KB
/
security.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
159
160
161
162
163
164
165
166
167
168
169
resource "aws_security_group" "sg" {
description = "Controls access to the ${local.name} DB"
vpc_id = local.vpc_id
name_prefix = "${local.name}-db-sg-"
tags = merge(
local.tags,
{
"Name" = "${local.name} DB SG"
}
)
}
resource "aws_security_group_rule" "egress" {
security_group_id = aws_security_group.sg.id
description = "Allow all traffic out"
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
source_security_group_id = var.egress_source_sg_id
cidr_blocks = var.egress_cidr_blocks
}
resource "aws_iam_role" "proxy_role" {
count = var.use_proxy ? 1 : 0
name = var.use_prefix ? null : local.proxy_name
name_prefix = var.use_prefix ? local.proxy_name : null
description = "Role for the ${local.proxy_name} DB proxy"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "rds.amazonaws.com"
}
},
]
})
tags = merge(
local.tags,
{
"Name" = "${local.proxy_name} DB Role"
}
)
}
resource "aws_secretsmanager_secret" "proxy_secret" {
count = var.use_proxy ? 1 : 0
name = var.use_prefix ? null : local.proxy_name
name_prefix = var.use_prefix ? local.proxy_name : null
description = "Secret for the ${local.proxy_name} DB proxy"
tags = merge(
local.tags,
{
"Name" = "${local.proxy_name} DB Secret"
}
)
}
resource "aws_secretsmanager_secret_version" "proxy_secret_version" {
count = var.use_proxy ? 1 : 0
secret_id = aws_secretsmanager_secret.proxy_secret[0].id
secret_string = jsonencode({
username = local.proxy_username,
password = local.proxy_password,
engine = lower(var.proxy_engine_family)
host = aws_rds_cluster.db.endpoint
dbClusterIdentifier = aws_rds_cluster.db.id
})
}
resource "aws_iam_role_policy" "proxy_policy" {
count = var.use_proxy ? 1 : 0
name = var.use_prefix ? null : local.proxy_name
name_prefix = var.use_prefix ? local.proxy_name : null
role = aws_iam_role.proxy_role[0].id
policy = data.aws_iam_policy_document.proxy_policy[0].json
}
data "aws_kms_key" "proxy_kms_key" {
count = var.use_proxy ? 1 : 0
key_id = var.proxy_kms_key_id
}
data "aws_iam_policy_document" "proxy_policy" {
count = var.use_proxy ? 1 : 0
statement {
effect = "Allow"
actions = [
"secretsmanager:GetSecretValue",
]
resources = [
aws_secretsmanager_secret.proxy_secret[0].arn,
]
}
statement {
effect = "Allow"
actions = [
"kms:Decrypt",
]
resources = [
data.aws_kms_key.proxy_kms_key[0].arn,
]
condition {
test = "StringEquals"
variable = "kms:ViaService"
values = [
"secretsmanager.${data.aws_region.current.name}.amazonaws.com",
]
}
}
}
resource "aws_security_group" "proxy_sg" {
count = var.use_proxy ? 1 : 0
description = "Controls access to the ${local.proxy_name} DB proxy"
vpc_id = local.vpc_id
name_prefix = "${local.name}-db-proxy-sg-"
tags = merge(
local.tags,
{
"Name" = "${local.name} DB proxy SG"
}
)
}
resource "aws_security_group_rule" "proxy_egress" {
count = var.use_proxy ? 1 : 0
security_group_id = aws_security_group.proxy_sg[0].id
description = "Allow all traffic out"
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
source_security_group_id = var.egress_source_sg_id
cidr_blocks = var.egress_cidr_blocks
}
resource "aws_security_group_rule" "proxy_to_db" {
count = var.use_proxy ? 1 : 0
security_group_id = aws_security_group.sg.id
description = "Allow traffic from the proxy to the DB"
type = "ingress"
protocol = "tcp"
from_port = aws_rds_cluster.db.port
to_port = aws_rds_cluster.db.port
source_security_group_id = aws_security_group.proxy_sg[0].id
}