-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
144 lines (133 loc) · 4.39 KB
/
dump.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
package pgperms
import (
"bytes"
"context"
"fmt"
"github.com/Jille/dfr"
"github.com/samber/lo"
"gopkg.in/yaml.v3"
)
// Dump all permissions from a running cluster and return a config yaml.
func Dump(ctx context.Context, conns *Connections) (string, error) {
c, err := Gather(ctx, conns, nil, nil)
if err != nil {
return "", err
}
c.TablePrivileges = mergePrivileges(c.TablePrivileges)
c.SequencePrivileges = mergePrivileges(c.SequencePrivileges)
c.DatabasePrivileges = mergePrivileges(c.DatabasePrivileges)
c.SchemaPrivileges = mergePrivileges(c.SchemaPrivileges)
c.TypePrivileges = mergePrivileges(c.TypePrivileges)
c.LanguagePrivileges = mergePrivileges(c.LanguagePrivileges)
b, err := yaml.Marshal(c)
if err != nil {
return "", err
}
return string(b), nil
}
// Gather all permissions from a running cluster.
func Gather(ctx context.Context, conns *Connections, interestingRoles, interestingDatabases []string) (*Config, error) {
var d dfr.D
defer d.Run(nil)
var ret Config
roles, err := FetchRoles(ctx, conns.primary)
if err != nil {
return nil, err
}
ret.Roles = roles
if len(interestingRoles) == 0 {
interestingRoles = lo.Keys(roles)
}
ret.Databases, err = fetchDatabases(ctx, conns.primary)
if err != nil {
return nil, err
}
if len(interestingDatabases) == 0 {
interestingDatabases = ret.Databases
}
ret.DatabasePrivileges, err = fetchDatabasesPrivileges(ctx, conns.primary, interestingRoles, interestingDatabases)
if err != nil {
return nil, err
}
for _, dbname := range lo.Intersect(interestingDatabases, ret.Databases) {
dbconn, deref, err := conns.Get(dbname)
if err != nil {
return nil, err
}
derefNow := d.Add(deref)
schemas, err := fetchSchemas(ctx, dbconn, dbname)
if err != nil {
return nil, err
}
ret.Schemas = append(ret.Schemas, schemas...)
schPrivs, err := fetchSchemasPrivileges(ctx, dbconn, dbname, interestingRoles)
if err != nil {
return nil, err
}
ret.SchemaPrivileges = append(ret.SchemaPrivileges, schPrivs...)
tblPrivs, seqPrivs, err := fetchTablePrivileges(ctx, dbconn, dbname, interestingRoles)
if err != nil {
return nil, err
}
ret.TablePrivileges = append(ret.TablePrivileges, tblPrivs...)
ret.SequencePrivileges = append(ret.SequencePrivileges, seqPrivs...)
typPrivs, err := fetchTypePrivileges(ctx, dbconn, dbname, interestingRoles)
if err != nil {
return nil, err
}
ret.TypePrivileges = append(ret.TypePrivileges, typPrivs...)
langPrivs, err := fetchLanguagePrivileges(ctx, dbconn, dbname, interestingRoles)
if err != nil {
return nil, err
}
ret.LanguagePrivileges = append(ret.LanguagePrivileges, langPrivs...)
derefNow(true)
}
return &ret, nil
}
// Sync the desired configuration to a running cluster.
// Queries to be executed are sent to the SyncSink, not executed on the given connections.
func Sync(ctx context.Context, conns *Connections, desired []byte, ss SyncSink) error {
dec := yaml.NewDecoder(bytes.NewReader(desired))
dec.KnownFields(true)
var d Config
if err := dec.Decode(&d); err != nil {
return err
}
if err := ValidateConfig(&d); err != nil {
return err
}
actual, err := Gather(ctx, conns, lo.Keys(d.Roles), d.Databases)
if err != nil {
return err
}
d.TablePrivileges, err = expandTables(ctx, conns, d.TablePrivileges, actual.Databases)
if err != nil {
return err
}
d.SequencePrivileges, err = expandSequences(ctx, conns, d.SequencePrivileges, actual.Databases)
if err != nil {
return err
}
if err := encryptPasswordsInConfig(ctx, conns.primary, d.Roles); err != nil {
return fmt.Errorf("failed to encrypt plain-text passwords in the config: %v", err)
}
SyncDatabases(ss, d.Databases, d.TombstonedDatabases, actual.Databases)
ss.AddBarrier()
SyncRoles(ss, actual.Roles, d.Roles, d.TombstonedRoles)
ss.AddBarrier()
SyncPrivileges(ss, []string{""}, actual.DatabasePrivileges, d.DatabasePrivileges)
ss.AddBarrier()
SyncSchemas(ss, d.Schemas, d.TombstonedSchemas, actual.Schemas)
ss.AddBarrier()
SyncPrivileges(ss, d.Databases, actual.SchemaPrivileges, d.SchemaPrivileges)
ss.AddBarrier()
SyncPrivileges(ss, d.Databases, actual.TypePrivileges, d.TypePrivileges)
ss.AddBarrier()
SyncPrivileges(ss, d.Databases, actual.TablePrivileges, d.TablePrivileges)
ss.AddBarrier()
SyncPrivileges(ss, d.Databases, actual.SequencePrivileges, d.SequencePrivileges)
ss.AddBarrier()
SyncPrivileges(ss, d.Databases, actual.LanguagePrivileges, d.LanguagePrivileges)
return nil
}