-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
136 lines (122 loc) · 3.93 KB
/
validate.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
package pgperms
import (
"fmt"
"strings"
"github.com/Jille/genericz/slicez"
"github.com/samber/lo"
)
// TODO: Do we want to raise warnings for likely incorrect configurations? (RoleAttributes.Replication without RoleAttributes.Login etc)
type validator struct {
tombstonedRoles []string
definedRoles []string
tombstonedDatabases []string
definedDatabases []string
tombstonedSchemas []string
definedSchemas []string
errors []string
}
// ValidateConfig checks whether the given config is correct.
func ValidateConfig(c *Config) error {
v := validator{
tombstonedRoles: c.TombstonedRoles,
definedRoles: lo.Keys(c.Roles),
tombstonedDatabases: c.TombstonedDatabases,
definedDatabases: c.Databases,
tombstonedSchemas: c.TombstonedSchemas,
definedSchemas: c.Schemas,
}
for name, r := range c.Roles {
if lo.Contains(v.tombstonedRoles, name) {
v.addErrorf("Role %s is both tombstoned and defined", name)
}
v.validateRole(name, r)
}
v.validateDatabases(c.Databases)
v.validateSchemas(c.Schemas)
v.validatePrivileges("databases", c.DatabasePrivileges)
v.validatePrivileges("schemas", c.SchemaPrivileges)
v.validatePrivileges("tables", c.TablePrivileges)
v.validatePrivileges("sequences", c.SequencePrivileges)
switch len(v.errors) {
case 0:
return nil
case 1:
return fmt.Errorf("Config is invalid: %s", v.errors[0])
default:
return fmt.Errorf("Config is invalid:\n* %s", strings.Join(v.errors, "\n* "))
}
}
func (v *validator) addError(msg string) {
v.errors = append(v.errors, msg)
}
func (v *validator) addErrorf(f string, args ...interface{}) {
v.errors = append(v.errors, fmt.Sprintf(f, args...))
}
func (v *validator) checkRole(source, name string) {
if lo.Contains(v.tombstonedRoles, name) {
v.addErrorf("%s: Role %s is tombstoned and shouldn't be used", source, name)
}
}
func (v *validator) validateRole(name string, r RoleAttributes) {
}
func (v *validator) validateDatabases(names []string) {
for _, n := range names {
if !safeCharactersRe.MatchString(n) {
v.addErrorf("Database %q would need its name escaped, which isn't properly supported by this tool yet", n)
}
if lo.Contains(v.tombstonedDatabases, n) {
v.addErrorf("Database %s is both tombstoned and defined", n)
}
}
if dupes := lo.FindDuplicates(names); len(dupes) > 0 {
for _, d := range dupes {
v.addErrorf("Database %s is defined multiple times", d)
}
}
}
func (v *validator) validateSchemas(names []string) {
// TODO: Implement
}
func (v *validator) validatePrivileges(what string, privs []GenericPrivilege) {
for i, p := range privs {
src := fmt.Sprintf("%s_privileges[%d]", strings.TrimSuffix(what, "s"), i+1)
t := p.targets()
if len(t) == 0 {
v.addErrorf("%s: privilege is missing %s field", src, what)
} else if len(t) > 1 {
v.addErrorf("%s: privilege has invalid fields: %v", src, slicez.Diff(t, []string{what}))
}
if what != t[0] {
v.addErrorf("%s: privilege has wrong target field (want %q, got %q)", src, what, t[0])
}
if len(p.Privileges) == 1 && p.Privileges[0] == "ALL PRIVILEGES" {
// OK
} else if unknown := slicez.Diff(p.Privileges, validPrivileges[what]); len(unknown) > 0 {
v.addErrorf("%s: privilege has invalid privileges %v for %s_privileges", src, unknown, what[:len(what)-1])
}
for _, tgt := range p.untypedTargets() {
db, remaining := splitObjectName(tgt)
if db == "" {
db = remaining
remaining = ""
}
if db != "" && !lo.Contains(v.definedDatabases, db) {
v.addErrorf("%s: privilege specified for unmanaged database %q", src, db)
}
if remaining == "" {
continue
}
schema, remaining := splitObjectName(remaining)
if schema == "" {
schema = remaining
}
fullSchema := joinSchemaName(db, schema)
if schema != "" && !lo.Contains(v.definedSchemas, fullSchema) {
v.addErrorf("%s: privilege specified for unmanaged schema %q", src, fullSchema)
}
}
for _, r := range p.Roles {
v.checkRole(src, r)
}
}
}