-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpolicy_sql_generator.go
319 lines (272 loc) · 11.5 KB
/
policy_sql_generator.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
package diff
import (
"errors"
"fmt"
"strings"
"github.com/google/go-cmp/cmp"
"github.com/stripe/pg-schema-diff/internal/schema"
)
var (
migrationHazardRLSEnabled = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Enabling RLS on a table could cause queries to fail if not correctly configured.",
}
migrationHazardRLSDisabled = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Disabling RLS on a table could allow unauthorized access to data.",
}
migrationHazardRLSForced = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Forcing RLS on a table could cause queries to fail if not correctly configured.",
}
migrationHazardRLSUnforced = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Disabling forcing RLS on a table could allow unauthorized access to data.",
}
migrationHazardPermissivePolicyAdded = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Adding a permissive policy could allow unauthorized access to data.",
}
migrationHazardPermissivePolicyRemoved = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Removing a permissive policy could cause queries to fail if not correctly configured.",
}
migrationHazardRestrictivePolicyAdded = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Adding a restrictive policy could cause queries to fail if not correctly configured.",
}
migrationHazardRestrictivePolicyRemoved = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Removing a restrictive policy could allow unauthorized access to data.",
}
migrationHazardPolicyAltered = MigrationHazard{
Type: MigrationHazardTypeAuthzUpdate,
Message: "Altering a policy could cause queries to fail if not correctly configured or allow unauthorized access to data.",
}
)
// When building/altering RLS policies, we must maintain the following order:
// 1. Create/alter table such that all necessary columns exist
// 2. Create/alter policies
// 3. Enable RLS -- This MUST be done last
//
// If not done in this order, we may create an outtage for a user's queries where RLS rejects their queries because
// the policy allowing them hasn't been created yet. The same is true for disabling RLS, but in the reverse order. RLS
// must be disabled before policies are dropped.
//
// Another quirk of policies: Policies on partitions must be dropped before the base table is altered, otherwise
// the SQL could fail because, e.g., the policy references a column that no longer exists.
func enableRLSForTable(t schema.Table) Statement {
return Statement{
DDL: fmt.Sprintf("%s ENABLE ROW LEVEL SECURITY", alterTablePrefix(t.SchemaQualifiedName)),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{migrationHazardRLSEnabled},
}
}
func disableRLSForTable(t schema.Table) Statement {
return Statement{
DDL: fmt.Sprintf("%s DISABLE ROW LEVEL SECURITY", alterTablePrefix(t.SchemaQualifiedName)),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{migrationHazardRLSDisabled},
}
}
func forceRLSForTable(t schema.Table) Statement {
return Statement{
DDL: fmt.Sprintf("%s FORCE ROW LEVEL SECURITY", alterTablePrefix(t.SchemaQualifiedName)),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{migrationHazardRLSForced},
}
}
func unforceRLSForTable(t schema.Table) Statement {
return Statement{
DDL: fmt.Sprintf("%s NO FORCE ROW LEVEL SECURITY", alterTablePrefix(t.SchemaQualifiedName)),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{migrationHazardRLSUnforced},
}
}
type policyDiff struct {
oldAndNew[schema.Policy]
}
func buildPolicyDiffs(psg sqlVertexGenerator[schema.Policy, policyDiff], old, new []schema.Policy) (listDiff[schema.Policy, policyDiff], error) {
return diffLists(old, new, func(old, new schema.Policy, _, _ int) (_ policyDiff, requiresRecreate bool, _ error) {
diff := policyDiff{
oldAndNew: oldAndNew[schema.Policy]{
old: old, new: new,
},
}
if _, err := psg.Alter(diff); err != nil {
if errors.Is(err, ErrNotImplemented) {
// If we can't generate the alter SQL, we'll have to recreate the policy.
return diff, true, nil
}
return policyDiff{}, false, fmt.Errorf("generating alter SQL: %w", err)
}
return diff, false, nil
})
}
type policySQLVertexGenerator struct {
table schema.Table
oldTable *schema.Table
newSchemaColumnsByName map[string]schema.Column
oldSchemaColumnsByName map[string]schema.Column
}
func newPolicySQLVertexGenerator(oldTable *schema.Table, table schema.Table) (sqlVertexGenerator[schema.Policy, policyDiff], error) {
var oldSchemaColumnsByName map[string]schema.Column
if oldTable != nil {
if oldTable.SchemaQualifiedName != table.SchemaQualifiedName {
return nil, fmt.Errorf("old and new tables must have the same schema-qualified name. new=%s, old=%s", table.SchemaQualifiedName.GetFQEscapedName(), oldTable.SchemaQualifiedName.GetFQEscapedName())
}
oldSchemaColumnsByName = buildSchemaObjByNameMap(oldTable.Columns)
}
return legacyToNewSqlVertexGenerator[schema.Policy, policyDiff](&policySQLVertexGenerator{
table: table,
newSchemaColumnsByName: buildSchemaObjByNameMap(table.Columns),
oldTable: oldTable,
oldSchemaColumnsByName: oldSchemaColumnsByName,
}), nil
}
func (psg *policySQLVertexGenerator) Add(p schema.Policy) ([]Statement, error) {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("CREATE POLICY %s ON %s", p.EscapedName, psg.table.GetFQEscapedName()))
typeModifier := "RESTRICTIVE"
if p.IsPermissive {
typeModifier = "PERMISSIVE"
}
sb.WriteString(fmt.Sprintf("\n\tAS %s", typeModifier))
cmdSQL, err := policyCharToSQL(p.Cmd)
if err != nil {
return nil, err
}
sb.WriteString(fmt.Sprintf("\n\tFOR %s", cmdSQL))
sb.WriteString(fmt.Sprintf("\n\tTO %s", strings.Join(p.AppliesTo, ", ")))
if p.UsingExpression != "" {
sb.WriteString(fmt.Sprintf("\n\tUSING (%s)", p.UsingExpression))
}
if p.CheckExpression != "" {
sb.WriteString(fmt.Sprintf("\n\tWITH CHECK (%s)", p.CheckExpression))
}
hazard := migrationHazardRestrictivePolicyAdded
if p.IsPermissive {
hazard = migrationHazardPermissivePolicyAdded
}
return []Statement{{
DDL: sb.String(),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{hazard},
}}, nil
}
func policyCharToSQL(c schema.PolicyCmd) (string, error) {
switch c {
case schema.SelectPolicyCmd:
return "SELECT", nil
case schema.InsertPolicyCmd:
return "INSERT", nil
case schema.UpdatePolicyCmd:
return "UPDATE", nil
case schema.DeletePolicyCmd:
return "DELETE", nil
case schema.AllPolicyCmd:
return "ALL", nil
default:
return "", fmt.Errorf("unknown policy command: %v", c)
}
}
func (psg *policySQLVertexGenerator) Delete(p schema.Policy) ([]Statement, error) {
hazard := migrationHazardRestrictivePolicyRemoved
if p.IsPermissive {
hazard = migrationHazardPermissivePolicyRemoved
}
return []Statement{{
DDL: fmt.Sprintf("DROP POLICY %s ON %s", p.EscapedName, psg.table.GetFQEscapedName()),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{hazard},
}}, nil
}
func (psg *policySQLVertexGenerator) Alter(diff policyDiff) ([]Statement, error) {
oldCopy := diff.old
// alterPolicyParts represents the set of strings to include in the ALTER POLICY ... ON TABLE ... statement
var alterPolicyParts []string
if !cmp.Equal(oldCopy.AppliesTo, diff.new.AppliesTo) {
alterPolicyParts = append(alterPolicyParts, fmt.Sprintf("TO %s", strings.Join(diff.new.AppliesTo, ", ")))
oldCopy.AppliesTo = diff.new.AppliesTo
}
if oldCopy.UsingExpression != diff.new.UsingExpression && diff.new.UsingExpression != "" {
// Weirdly, you can't actually drop a "USING EXPRESSION" clause from an ALL policy even though you
// can have an ALL policy with only a check expression.
alterPolicyParts = append(alterPolicyParts, fmt.Sprintf("USING (%s)", diff.new.UsingExpression))
oldCopy.UsingExpression = diff.new.UsingExpression
}
if oldCopy.CheckExpression != diff.new.CheckExpression && diff.new.CheckExpression != "" {
// Same quirk as above with ALL policies.
alterPolicyParts = append(alterPolicyParts, fmt.Sprintf("WITH CHECK (%s)", diff.new.CheckExpression))
oldCopy.CheckExpression = diff.new.CheckExpression
}
oldCopy.Columns = diff.new.Columns
if diff := cmp.Diff(oldCopy, diff.new); diff != "" {
return nil, fmt.Errorf("unsupported diff %s: %w", diff, ErrNotImplemented)
}
if len(alterPolicyParts) == 0 {
// There is no diff
return nil, nil
}
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("ALTER POLICY %s ON %s\n\t", diff.new.EscapedName, psg.table.GetFQEscapedName()))
sb.WriteString(strings.Join(alterPolicyParts, "\n\t"))
return []Statement{{
DDL: sb.String(),
Timeout: statementTimeoutDefault,
LockTimeout: lockTimeoutDefault,
Hazards: []MigrationHazard{migrationHazardPolicyAltered},
}}, nil
}
func (psg *policySQLVertexGenerator) GetSQLVertexId(p schema.Policy, diffType diffType) sqlVertexId {
return buildPolicyVertexId(psg.table.SchemaQualifiedName, p.EscapedName, diffType)
}
func buildPolicyVertexId(owningTable schema.SchemaQualifiedName, policyEscapedName string, diffType diffType) sqlVertexId {
return buildSchemaObjVertexId("policy", fmt.Sprintf("%s.%s", owningTable.GetFQEscapedName(), policyEscapedName), diffType)
}
func (psg *policySQLVertexGenerator) GetAddAlterDependencies(newPolicy, oldPolicy schema.Policy) ([]dependency, error) {
deps := []dependency{
mustRun(psg.GetSQLVertexId(newPolicy, diffTypeDelete)).before(psg.GetSQLVertexId(newPolicy, diffTypeAddAlter)),
}
newTargetColumns, err := getTargetColumns(newPolicy.Columns, psg.newSchemaColumnsByName)
if err != nil {
return nil, fmt.Errorf("getting target columns: %w", err)
}
// Run after the new columns are added/altered
for _, tc := range newTargetColumns {
deps = append(deps, mustRun(psg.GetSQLVertexId(newPolicy, diffTypeAddAlter)).after(buildColumnVertexId(tc.Name, diffTypeAddAlter)))
}
if !cmp.Equal(oldPolicy, schema.Policy{}) {
// Run before the old columns are deleted (if they are deleted)
oldTargetColumns, err := getTargetColumns(oldPolicy.Columns, psg.oldSchemaColumnsByName)
if err != nil {
return nil, fmt.Errorf("getting target columns: %w", err)
}
for _, tc := range oldTargetColumns {
// It only needs to run before the delete if the column is actually being deleted
if _, stillExists := psg.newSchemaColumnsByName[tc.GetName()]; !stillExists {
deps = append(deps, mustRun(psg.GetSQLVertexId(newPolicy, diffTypeAddAlter)).before(buildColumnVertexId(tc.Name, diffTypeDelete)))
}
}
}
return deps, nil
}
func (psg *policySQLVertexGenerator) GetDeleteDependencies(pol schema.Policy) ([]dependency, error) {
var deps []dependency
columns, err := getTargetColumns(pol.Columns, psg.oldSchemaColumnsByName)
if err != nil {
return nil, fmt.Errorf("getting target columns: %w", err)
}
// The policy needs to be deleted before all the columns it references are deleted or add/altered
for _, c := range columns {
deps = append(deps, mustRun(psg.GetSQLVertexId(pol, diffTypeDelete)).before(buildColumnVertexId(c.Name, diffTypeDelete)))
deps = append(deps, mustRun(psg.GetSQLVertexId(pol, diffTypeDelete)).before(buildColumnVertexId(c.Name, diffTypeAddAlter)))
}
return deps, nil
}