-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
211 lines (180 loc) · 4.99 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
################################################################################
# generate random credentials
resource "random_string" "rw_user" {
length = 14
special = false
number = false
upper = false
lower = true
}
resource "random_string" "rw_pass" {
length = 128
special = false
number = true
upper = true
lower = true
}
resource "random_string" "ro_user" {
length = 14
special = false
number = false
upper = false
lower = true
}
resource "random_string" "ro_pass" {
length = 128
special = false
number = true
upper = true
lower = true
}
locals {
db_host = "${var.pg_host}"
db_port = "${var.pg_port}"
db_rw_user = "urw${random_string.rw_user.result}"
db_rw_pass = "${random_string.rw_pass.result}"
db_ro_user = "uro${random_string.ro_user.result}"
db_ro_pass = "${random_string.ro_pass.result}"
db_name = "${var.database_name}"
}
################################################################################
# The ro+rw roles that users inherit from
locals {
db_rw_role = "${local.db_name}_rw_role"
db_ro_role = "${local.db_name}_ro_role"
}
resource "postgresql_role" "rw" {
name = "${local.db_rw_role}"
login = false
inherit = false
skip_reassign_owned = true
}
resource "postgresql_role" "ro" {
name = "${local.db_ro_role}"
login = false
inherit = false
skip_reassign_owned = true
}
# the primary database user, who is also the owner of the
# database we just created.
resource "postgresql_role" "rw_user" {
name = "${local.db_rw_user}"
password = "${local.db_rw_pass}"
login = true
inherit = true
skip_reassign_owned = true
roles = [
"${local.db_rw_role}",
"${local.db_ro_role}",
]
depends_on = [
"postgresql_role.rw",
"postgresql_role.ro",
]
}
resource "postgresql_role" "ro_user" {
name = "${local.db_ro_user}"
password = "${local.db_ro_pass}"
login = true
inherit = true
skip_reassign_owned = true
roles = [
"${local.db_ro_role}",
]
depends_on = [
"postgresql_role.ro",
]
}
################################################################################
# create the database
resource "postgresql_database" "db" {
name = "${local.db_name}"
template = "template1"
lc_collate = "DEFAULT"
encoding = "DEFAULT"
lc_ctype = "DEFAULT"
owner = "${local.db_rw_user}"
allow_connections = true
depends_on = [
"postgresql_role.rw_user"
]
}
################################################################################
# set up permissions
# https://aws.amazon.com/blogs/database/managing-postgresql-users-and-roles/
# https://dba.stackexchange.com/questions/17790/created-user-can-access-all-databases-in-postgresql-without-any-grants
# https://blog.dbrhino.com/locking-down-permissions-in-postgresql-and-redshift.html
resource "null_resource" "db_setup" {
provisioner "local-exec" {
command = <<END
psql --command="
ALTER SCHEMA public OWNER TO \"${local.db_rw_user}\";
REVOKE ALL ON DATABASE \"${local.db_name}\" FROM public;
"
END
environment = {
PGHOST = "${var.pg_host}"
PGPORT = "${var.pg_port}"
PGUSER = "${var.pg_user}"
PGPASSWORD = "${var.pg_password}"
PGDATABASE = "${local.db_name}"
}
}
depends_on = [
"postgresql_role.rw",
"postgresql_role.ro",
"postgresql_role.rw_user",
"postgresql_database.db",
]
}
resource "null_resource" "db_setup_defaults" {
provisioner "local-exec" {
command = <<END
psql --command="
REVOKE ALL ON DATABASE \"${local.db_name}\" FROM public;
REVOKE ALL ON SCHEMA public FROM public;
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM public;
---- ro role
GRANT CONNECT ON DATABASE \"${local.db_name}\" TO \"${local.db_ro_role}\";
GRANT USAGE ON SCHEMA public TO \"${local.db_ro_role}\";
ALTER DEFAULT PRIVILEGES
GRANT SELECT ON TABLES TO \"${local.db_ro_role}\";
ALTER DEFAULT PRIVILEGES
GRANT SELECT ON SEQUENCES TO \"${local.db_ro_role}\";
--ALTER DEFAULT PRIVILEGES FOR ROLE \"${local.db_ro_role}\"
--GRANT EXECUTE ON FUNCTIONS TO \"${local.db_ro_role}\";
ALTER DEFAULT PRIVILEGES
GRANT USAGE ON TYPES TO \"${local.db_ro_role}\";
ALTER DEFAULT PRIVILEGES
GRANT USAGE ON SCHEMAS TO \"${local.db_ro_role}\";
---- rw role
GRANT CONNECT ON DATABASE \"${local.db_name}\" TO \"${local.db_rw_role}\";
GRANT ALL ON SCHEMA public TO \"${local.db_rw_role}\";
ALTER DEFAULT PRIVILEGES
GRANT ALL ON TABLES TO \"${local.db_rw_role}\";
ALTER DEFAULT PRIVILEGES
GRANT ALL ON SEQUENCES TO \"${local.db_rw_role}\";
ALTER DEFAULT PRIVILEGES
GRANT ALL ON FUNCTIONS TO \"${local.db_rw_role}\";
ALTER DEFAULT PRIVILEGES
GRANT ALL ON TYPES TO \"${local.db_rw_role}\";
ALTER DEFAULT PRIVILEGES
GRANT ALL ON SCHEMAS TO \"${local.db_rw_role}\";
"
END
environment = {
PGHOST = "${local.db_host}"
PGPORT = "${local.db_port}"
PGUSER = "${local.db_rw_user}"
PGPASSWORD = "${local.db_rw_pass}"
PGDATABASE = "${local.db_name}"
}
}
depends_on = [
"postgresql_role.rw",
"postgresql_role.ro",
"postgresql_role.rw_user",
"postgresql_database.db",
"null_resource.db_setup",
]
}