-
Notifications
You must be signed in to change notification settings - Fork 213
/
helpers.go
659 lines (557 loc) · 18.6 KB
/
helpers.go
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package postgresql
import (
"database/sql"
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/lib/pq"
)
func PGResourceFunc(fn func(*DBConnection, *schema.ResourceData) error) func(*schema.ResourceData, interface{}) error {
return func(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client)
db, err := client.Connect()
if err != nil {
return err
}
return fn(db, d)
}
}
func PGResourceExistsFunc(fn func(*DBConnection, *schema.ResourceData) (bool, error)) func(*schema.ResourceData, interface{}) (bool, error) {
return func(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*Client)
db, err := client.Connect()
if err != nil {
return false, err
}
return fn(db, d)
}
}
// QueryAble is a DB connection (sql.DB/Tx)
type QueryAble interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
// pqQuoteLiteral returns a string literal safe for inclusion in a PostgreSQL
// query as a parameter. The resulting string still needs to be wrapped in
// single quotes in SQL (i.e. fmt.Sprintf(`'%s'`, pqQuoteLiteral("str"))). See
// quote_literal_internal() in postgresql/backend/utils/adt/quote.c:77.
func pqQuoteLiteral(in string) string {
in = strings.Replace(in, `\`, `\\`, -1)
in = strings.Replace(in, `'`, `''`, -1)
return in
}
func isMemberOfRole(db QueryAble, role, member string) (bool, error) {
var _rez int
setOption := true
err := db.QueryRow(
"SELECT 1 FROM information_schema.columns WHERE table_name='pg_auth_members' AND column_name = 'set_option'",
).Scan(&_rez)
switch {
case err == sql.ErrNoRows:
setOption = false
case err != nil:
return false, fmt.Errorf("could not read setOption column: %w", err)
}
query := "SELECT 1 FROM pg_auth_members WHERE pg_get_userbyid(roleid) = $1 AND pg_get_userbyid(member) = $2"
if setOption {
query += " AND set_option"
}
err = db.QueryRow(query, role, member).Scan(&_rez)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, fmt.Errorf("could not read role membership: %w", err)
}
return true, nil
}
// grantRoleMembership grants the role *role* to the user *member*.
// It returns false if the grant is not needed because the user is already
// a member of this role.
func grantRoleMembership(db QueryAble, role, member string) (bool, error) {
if member == role {
return false, nil
}
isMember, err := isMemberOfRole(db, role, member)
if err != nil {
return false, err
}
if isMember {
log.Printf("grantRoleMembership: %s is already a member of %s, nothing to do", member, role)
return false, nil
}
log.Printf("grantRoleMembership: granting %s to %s", role, member)
sql := fmt.Sprintf("GRANT %s TO %s", pq.QuoteIdentifier(role), pq.QuoteIdentifier(member))
if _, err := db.Exec(sql); err != nil {
return false, fmt.Errorf("Error granting role %s to %s: %w", role, member, err)
}
return true, nil
}
// revokeRoleMembership revokes the role *role* from the user *member*.
// It returns false if the revoke is not needed because the user is not a member of this role.
func revokeRoleMembership(db QueryAble, role, member string) (bool, error) {
if member == role {
return false, nil
}
isMember, err := isMemberOfRole(db, role, member)
if err != nil {
return false, err
}
if !isMember {
return false, nil
}
log.Printf("revokeRoleMembership: Revoke %s from %s", role, member)
sql := fmt.Sprintf("REVOKE %s FROM %s", pq.QuoteIdentifier(role), pq.QuoteIdentifier(member))
if _, err := db.Exec(sql); err != nil {
return false, fmt.Errorf("Error revoking role %s from %s: %w", role, member, err)
}
return true, nil
}
// withRolesGranted temporarily grants, if needed, the roles specified to connected user
// (i.e.: the admin configure in the provider) and revoke them as soon as the
// callback func has finished.
func withRolesGranted(txn *sql.Tx, roles []string, fn func() error) error {
// No roles asked, execute the function directly
if len(roles) == 0 {
return fn()
}
currentUser, err := getCurrentUser(txn)
if err != nil {
return err
}
superuser, err := isSuperuser(txn, currentUser)
if err != nil {
return err
}
if superuser {
log.Printf("withRolesGranted: current user %s is superuser, no need to grant roles", currentUser)
return fn()
}
var grantedRoles []string
var revokedRoles []string
for _, role := range roles {
// We need to check if the role we want to grant is a superuser
// in this case Postgres disallows to grant it to a current user which is not superuser.
superuser, err := isSuperuser(txn, role)
if err != nil {
return err
}
if superuser {
log.Printf("withRolesGranted: WARN role %s could not be granted to current user (%s) as it's a superuser", role, currentUser)
continue
}
// We also need to check if the reverse relationship does not exist.
// e.g.: We want to temporary `GRANT foo TO postgres` so `postgres` become a member of role `foo`
// in order to manipulate its objects/privileges.
// But PostgreSQL prevents `foo` to be a member of the role `postgres`,
// and for `postgres` to be a member of the role `foo`, at the same time.
// In this case we will temporarily revoke this privilege.
// So, the following queries will happen (in the same transaction):
// - REVOKE postgres FROM foo
// - GRANT foo TO postgres
//
// Here we execute the wrapped function `fn`
//
// - REVOKE foo FROM postgres
// - GRANT postgres TO foo
// Check the opposite relation and revoke currentUser from role if needed
revoked, err := revokeRoleMembership(txn, currentUser, role)
if err != nil {
return err
}
if revoked {
revokedRoles = append(revokedRoles, role)
}
// Grant the role to currentUser if needed
roleGranted, err := grantRoleMembership(txn, role, currentUser)
if err != nil {
return err
}
if roleGranted {
grantedRoles = append(grantedRoles, role)
}
}
// Execute the wrapped function
if err := fn(); err != nil {
return err
}
// Revoke the temporary granted roles.
for _, role := range grantedRoles {
if _, err := revokeRoleMembership(txn, role, currentUser); err != nil {
return err
}
}
// Grant back the temporary revoked role.
for _, role := range revokedRoles {
// check if the role has not been deleted by the wrapped function
exists, err := roleExists(txn, role)
if err != nil {
return err
}
if !exists {
continue
}
if _, err := grantRoleMembership(txn, currentUser, role); err != nil {
return err
}
}
return nil
}
func sliceContainsStr(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
// allowedPrivileges is the list of privileges allowed per object types in Postgres.
// see: https://www.postgresql.org/docs/current/sql-grant.html
var allowedPrivileges = map[string][]string{
"database": {"ALL", "CREATE", "CONNECT", "TEMPORARY"},
"table": {"ALL", "SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER"},
"sequence": {"ALL", "USAGE", "SELECT", "UPDATE"},
"schema": {"ALL", "CREATE", "USAGE"},
"function": {"ALL", "EXECUTE"},
"procedure": {"ALL", "EXECUTE"},
"routine": {"ALL", "EXECUTE"},
"type": {"ALL", "USAGE"},
"foreign_data_wrapper": {"ALL", "USAGE"},
"foreign_server": {"ALL", "USAGE"},
"column": {"ALL", "SELECT", "INSERT", "UPDATE", "REFERENCES"},
}
// validatePrivileges checks that privileges to apply are allowed for this object type.
func validatePrivileges(d *schema.ResourceData) error {
objectType := d.Get("object_type").(string)
privileges := d.Get("privileges").(*schema.Set).List()
allowed, ok := allowedPrivileges[objectType]
if !ok {
return fmt.Errorf("unknown object type %s", objectType)
}
for _, priv := range privileges {
if !sliceContainsStr(allowed, priv.(string)) {
return fmt.Errorf("%s is not an allowed privilege for object type %s", priv, objectType)
}
}
return nil
}
func resourcePrivilegesEqual(granted *schema.Set, d *schema.ResourceData) bool {
objectType := d.Get("object_type").(string)
wanted := d.Get("privileges").(*schema.Set)
if granted.Equal(wanted) {
return true
}
if !wanted.Contains("ALL") {
return false
}
// implicit check: e.g. for object_type schema -> ALL == ["CREATE", "USAGE"]
log.Printf("The wanted privilege is 'ALL'. therefore, we will check if the current privileges are ALL implicitly")
implicits := []interface{}{}
for _, p := range allowedPrivileges[objectType] {
if p != "ALL" {
implicits = append(implicits, p)
}
}
wantedSet := schema.NewSet(schema.HashString, implicits)
return granted.Equal(wantedSet)
}
func pgArrayToSet(arr pq.ByteaArray) *schema.Set {
s := make([]interface{}, len(arr))
for i, v := range arr {
s[i] = string(v)
}
return schema.NewSet(schema.HashString, s)
}
func stringSliceToSet(slice []string) *schema.Set {
s := make([]interface{}, len(slice))
for i, v := range slice {
s[i] = v
}
return schema.NewSet(schema.HashString, s)
}
func quoteIdentifyIdent(ident string) string {
// When passing a function with arguments like "test(text, char)" this will correctly parse it to "test"(text, char).
// If we were to add quotes around the whole ident postgres would not be able to find the function.
// Usually specifying parameters of a function is not necessary, but postgres allows function overloading where it
// identifies the function by its parameters allowing the developer to have multiple functions with the same name.
// Information:
// https://en.wikipedia.org/wiki/Function_overloading
// https://stackoverflow.com/a/48640797
s := strings.Split(ident, "(")
functionArgTypes := ""
if len(s) > 1 {
functionArgTypes = "(" + s[1]
}
return fmt.Sprintf("%s%s", pq.QuoteIdentifier(s[0]), functionArgTypes)
}
func setToPgIdentList(schema string, idents *schema.Set) string {
quotedIdents := make([]string, idents.Len())
for i, ident := range idents.List() {
quotedIdents[i] = fmt.Sprintf(
"%s.%s",
pq.QuoteIdentifier(schema), quoteIdentifyIdent(ident.(string)),
)
}
return strings.Join(quotedIdents, ",")
}
func setToPgIdentListWithoutSchema(idents *schema.Set) string {
quotedIdents := make([]string, idents.Len())
for i, ident := range idents.List() {
quotedIdents[i] = pq.QuoteIdentifier(ident.(string))
}
return strings.Join(quotedIdents, ",")
}
func setToPgIdentSimpleList(idents *schema.Set) string {
quotedIdents := make([]string, idents.Len())
for i, ident := range idents.List() {
quotedIdents[i] = ident.(string)
}
return strings.Join(quotedIdents, ",")
}
// startTransaction starts a new DB transaction on the specified database.
// If the database is specified and different from the one configured in the provider,
// it will create a new connection pool if needed.
func startTransaction(client *Client, database string) (*sql.Tx, error) {
if database != "" && database != client.databaseName {
client = client.config.NewClient(database)
}
db, err := client.Connect()
if err != nil {
return nil, err
}
txn, err := db.Begin()
if err != nil {
return nil, fmt.Errorf("could not start transaction: %w", err)
}
return txn, nil
}
func dbExists(db QueryAble, dbname string) (bool, error) {
err := db.QueryRow("SELECT datname FROM pg_database WHERE datname=$1", dbname).Scan(&dbname)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, fmt.Errorf("could not check if database exists: %w", err)
}
return true, nil
}
func roleExists(txn *sql.Tx, rolname string) (bool, error) {
err := txn.QueryRow("SELECT 1 FROM pg_roles WHERE rolname=$1", rolname).Scan(&rolname)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, fmt.Errorf("could not check if role exists: %w", err)
}
return true, nil
}
func schemaExists(txn *sql.Tx, schemaname string) (bool, error) {
err := txn.QueryRow("SELECT 1 FROM pg_namespace WHERE nspname=$1", schemaname).Scan(&schemaname)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, fmt.Errorf("could not check if schema exists: %w", err)
}
return true, nil
}
func getCurrentUser(db QueryAble) (string, error) {
var currentUser string
err := db.QueryRow("SELECT CURRENT_USER").Scan(¤tUser)
switch {
case err == sql.ErrNoRows:
return "", fmt.Errorf("SELECT CURRENT_USER returns now row, this is quite disturbing")
case err != nil:
return "", fmt.Errorf("error while looking for the current user: %w", err)
}
return currentUser, nil
}
// deferredRollback can be used to rollback a transaction in a defer.
// It will log an error if it fails
func deferredRollback(txn *sql.Tx) {
err := txn.Rollback()
switch {
case err == sql.ErrTxDone:
// transaction has already been committed or rolled back
log.Printf("[DEBUG]: %v", err)
case err != nil:
log.Printf("[ERR] could not rollback transaction: %v", err)
}
}
func getDatabase(d *schema.ResourceData, databaseName string) string {
if v, ok := d.GetOk(extDatabaseAttr); ok {
databaseName = v.(string)
}
return databaseName
}
func getDatabaseOwner(db QueryAble, database string) (string, error) {
dbQueryString := "$1"
dbQueryValues := []interface{}{database}
// Empty means current DB
if database == "" {
dbQueryString = "current_database()"
dbQueryValues = []interface{}{}
}
query := fmt.Sprintf(`
SELECT rolname
FROM pg_database
JOIN pg_roles ON datdba = pg_roles.oid
WHERE datname = %s
`, dbQueryString)
var owner string
err := db.QueryRow(query, dbQueryValues...).Scan(&owner)
switch {
case err == sql.ErrNoRows:
return "", fmt.Errorf("could not find database '%s' while looking for owner", database)
case err != nil:
return "", fmt.Errorf("error while looking for the owner of database '%s': %w", database, err)
}
return owner, nil
}
func getSchemaOwner(db QueryAble, schemaName string) (string, error) {
query := `
SELECT rolname
FROM pg_namespace
JOIN pg_roles ON nspowner = pg_roles.oid
WHERE nspname = $1
`
var owner string
err := db.QueryRow(query, schemaName).Scan(&owner)
switch {
case err == sql.ErrNoRows:
return "", fmt.Errorf("could not find schema '%s' while looking for owner", schemaName)
case err != nil:
return "", fmt.Errorf("error while looking for the owner of schema '%s': %w", schemaName, err)
}
return owner, nil
}
// getTablesOwner retrieves all the owners for all the tables in the specified schema.
func getTablesOwner(db QueryAble, schemaName string) ([]string, error) {
rows, err := db.Query(
"SELECT DISTINCT tableowner FROM pg_tables WHERE schemaname = $1",
schemaName,
)
if err != nil {
return nil, fmt.Errorf("error while looking for owners of tables in schema '%s': %w", schemaName, err)
}
var owners []string
for rows.Next() {
var owner string
if err := rows.Scan(&owner); err != nil {
return nil, fmt.Errorf("could not scan tables owner: %w", err)
}
owners = append(owners, owner)
}
return owners, nil
}
func resolveOwners(db QueryAble, owners []string) ([]string, error) {
resolvedOwners := []string{}
for _, owner := range owners {
if owner == "pg_database_owner" {
var err error
owner, err = getDatabaseOwner(db, "")
if err != nil {
return nil, err
}
}
resolvedOwners = append(resolvedOwners, owner)
}
return resolvedOwners, nil
}
func isSuperuser(db QueryAble, role string) (bool, error) {
var superuser bool
if err := db.QueryRow("SELECT rolsuper FROM pg_roles WHERE rolname = $1", role).Scan(&superuser); err != nil {
return false, fmt.Errorf("could not check if role %s is superuser: %w", role, err)
}
return superuser, nil
}
const publicRole = "public"
func getRoleOID(db QueryAble, role string) (uint32, error) {
if role == publicRole {
return 0, nil
}
var oid uint32
if err := db.QueryRow("SELECT oid FROM pg_roles WHERE rolname = $1", role).Scan(&oid); err != nil {
return 0, fmt.Errorf("could not find oid for role %s: %w", role, err)
}
return oid, nil
}
// Lock a role and all his members to avoid concurrent updates on some resources
func pgLockRole(txn *sql.Tx, role string) error {
// Disable statement timeout for this connection otherwise the lock could fail
if _, err := txn.Exec("SET statement_timeout = 0"); err != nil {
return fmt.Errorf("could not disable statement_timeout: %w", err)
}
if _, err := txn.Exec("SELECT pg_advisory_xact_lock(oid::bigint) FROM pg_roles WHERE rolname = $1", role); err != nil {
return fmt.Errorf("could not get advisory lock for role %s: %w", role, err)
}
if _, err := txn.Exec(
"SELECT pg_advisory_xact_lock(member::bigint) FROM pg_auth_members JOIN pg_roles ON roleid = pg_roles.oid WHERE rolname = $1",
role,
); err != nil {
return fmt.Errorf("could not get advisory lock for members of role %s: %w", role, err)
}
return nil
}
// Lock a database and all his members to avoid concurrent updates on some resources
func pgLockDatabase(txn *sql.Tx, database string) error {
// Disable statement timeout for this connection otherwise the lock could fail
if _, err := txn.Exec("SET statement_timeout = 0"); err != nil {
return fmt.Errorf("could not disable statement_timeout: %w", err)
}
if _, err := txn.Exec("SELECT pg_advisory_xact_lock(oid::bigint) FROM pg_database WHERE datname = $1", database); err != nil {
return fmt.Errorf("could not get advisory lock for database %s: %w", database, err)
}
return nil
}
func arrayDifference(a, b []interface{}) (diff []interface{}) {
m := make(map[interface{}]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
return
}
func isUniqueArr(arr []interface{}) (interface{}, bool) {
keys := make(map[interface{}]bool, len(arr))
for _, entry := range arr {
if _, value := keys[entry]; value {
return entry, false
}
keys[entry] = true
}
return nil, true
}
func findStringSubmatchMap(expression string, text string) map[string]string {
r := regexp.MustCompile(expression)
parts := r.FindStringSubmatch(text)
paramsMap := make(map[string]string)
for i, name := range r.SubexpNames() {
if i > 0 && i <= len(parts) {
paramsMap[name] = parts[i]
}
}
return paramsMap
}
func defaultDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool {
return old == new
}
// quoteTable can quote a table name with or without a schema prefix
// Example:
//
// my_table -> "my_table"
// public.my_table -> "public"."my_table"
func quoteTableName(tableName string) string {
parts := strings.Split(tableName, ".")
for i := range parts {
parts[i] = pq.QuoteIdentifier(parts[i])
}
return strings.Join(parts, ".")
}