-
Notifications
You must be signed in to change notification settings - Fork 34
/
schemaupdate.go
301 lines (246 loc) · 7.97 KB
/
schemaupdate.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
// Copyright 2023 Stacklok, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package schemaupdate contains utility functions to compare two schemas
// for updates
package schemaupdate
import (
"fmt"
"reflect"
"dario.cat/mergo"
"github.com/barkimedes/go-deepcopy"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/types/known/structpb"
"k8s.io/apimachinery/pkg/util/sets"
)
// ValidateSchemaUpdate validates that the new json schema doesn't break
// profiles using this rule type
func ValidateSchemaUpdate(oldRuleSchema *structpb.Struct, newRuleSchema *structpb.Struct) error {
if len(newRuleSchema.GetFields()) == 0 {
// If the new schema is empty (including nil), we're good
// The rule type has removed the schema and profiles
// won't break
return nil
}
if schemaIsNilOrEmpty(oldRuleSchema) && !schemaIsNilOrEmpty(newRuleSchema) {
// If old is nil and new is not, we need to verify that
// the new definition is not introducing required fields
newrs := newRuleSchema.AsMap()
if _, ok := newrs["required"]; ok {
return fmt.Errorf("cannot add required fields to rule schema")
}
// If no required fields are being added, we're good
// profiles using this rule type won't break
return nil
}
oldSchemaMap := oldRuleSchema.AsMap()
newSchemaMap := newRuleSchema.AsMap()
oldTypeCast, err := getOrInferType(oldSchemaMap)
if err != nil {
return err
}
newTypeCast, err := getOrInferType(newSchemaMap)
if err != nil {
return err
}
if oldTypeCast != newTypeCast {
return fmt.Errorf("cannot change type of rule schema")
}
if oldTypeCast != "object" && oldTypeCast != "array" {
// the change is fine
return nil
}
if oldTypeCast == "array" {
return validateArraySchemaUpdate(oldSchemaMap, newSchemaMap)
}
// objects need further validation. We need to make sure that
// the new schema is a superset of the old schema
return validateObjectSchemaUpdate(oldSchemaMap, newSchemaMap)
}
func getOrInferType(schemaMap map[string]any) (string, error) {
typ, ok := schemaMap["type"]
if !ok {
return "object", nil
}
typCast, ok := typ.(string)
if !ok {
return "", fmt.Errorf("invalid type field")
}
return typCast, nil
}
func validateObjectSchemaUpdate(oldSchemaMap, newSchemaMap map[string]any) error {
if err := validateRequired(oldSchemaMap, newSchemaMap); err != nil {
return fmt.Errorf("failed to validate required fields: %v", err)
}
if err := validateProperties(oldSchemaMap, newSchemaMap); err != nil {
return fmt.Errorf("failed to validate properties: %v", err)
}
return nil
}
func validateProperties(oldSchemaMap, newSchemaMap map[string]any) error {
dst, err := deepcopy.Anything(newSchemaMap)
if err != nil {
return fmt.Errorf("failed to deepcopy old schema: %v", err)
}
castedDst := dst.(map[string]any)
err = mergo.Merge(&castedDst, &oldSchemaMap, mergo.WithOverride, mergo.WithSliceDeepCopy)
if err != nil {
return fmt.Errorf("failed to merge old and new schema: %v", err)
}
// We need to ignore the description field when comparing the old and new schema to allow
// to update the ruletype text. We also need to ignore changing defaults as they are advisory
// for the UI at the moment
opts := []cmp.Option{
cmp.FilterPath(isScalarDescription, cmp.Ignore()),
cmp.FilterPath(isDefaultValue, cmp.Ignore()),
}
// The new schema should be a superset of the old schema
// if it's not, we may break profiles using this rule type
if !cmp.Equal(newSchemaMap, castedDst, opts...) {
return fmt.Errorf("cannot remove properties from rule schema")
}
return nil
}
func isScalarDescription(p cmp.Path) bool {
if mi, ok := p.Last().(cmp.MapIndex); ok {
key := mi.Key()
left, right := mi.Values()
// we can ignore description if it was a string and is a string
if key.String() == "description" && isValueString(left) && isValueString(right) {
return true
}
}
return false
}
func isDefaultValue(p cmp.Path) bool {
if mi, ok := p.Last().(cmp.MapIndex); ok {
key := mi.Key()
// we ignore default if it has a type sibling, assuming that this is a type default, not an attribute
// named default. Further down we also check that the type has a string value
if key.String() == "default" {
if hasTypeSibling(p.Index(len(p) - 2)) {
return true
}
}
}
return false
}
func hasTypeSibling(p cmp.PathStep) bool {
left, right := p.Values()
return isMapWithKey(left, "type") && isMapWithKey(right, "type")
}
func isMapWithKey(value reflect.Value, key string) bool {
if !value.IsValid() {
return false
}
if !value.CanInterface() {
return false
}
valIf := value.Interface()
valMap, ok := valIf.(map[string]any)
if !ok {
return false
}
for k := range valMap {
if k == key {
return true
}
}
return false
}
func isValueString(value reflect.Value) bool {
if !value.IsValid() {
return false
}
if !value.CanInterface() {
return false
}
valIf := value.Interface()
if _, ok := valIf.(string); ok {
return true
}
return false
}
func validateRequired(oldSchemaMap, newSchemaMap map[string]any) error {
// If the new schema has required fields, we need to make sure
// that the old schema has those fields as well
oldRequired, hasOldRequired := oldSchemaMap["required"]
newRequired, hasNewRequired := newSchemaMap["required"]
if !hasNewRequired && !hasOldRequired {
// If we don't have required fields in either schema, we're good
// profiles using this rule type won't break
return nil
}
if !hasNewRequired && hasOldRequired {
// If we don't have required fields in the new schema but do
// in the old schema, we're good.
// profiles using this rule type won't break
return nil
}
if hasNewRequired && !hasOldRequired {
// If we have required fields in the new schema but not the old
// schema, we may break profiles using this rule type
return fmt.Errorf("cannot add required fields to rule schema")
}
oldRequiredSlice, ok := oldRequired.([]interface{})
if !ok {
return fmt.Errorf("invalid old required field")
}
newRequiredSlice, ok := newRequired.([]interface{})
if !ok {
return fmt.Errorf("invalid new required field")
}
// We need to make sure that the old required fields are
// a superset of the new required fields
oldSet := sets.New(oldRequiredSlice...)
newSet := sets.New(newRequiredSlice...)
if !oldSet.IsSuperset(newSet) {
return fmt.Errorf("cannot add required fields to rule schema")
}
return nil
}
func schemaIsNilOrEmpty(schema *structpb.Struct) bool {
if schema == nil {
return true
}
return len(schema.AsMap()) == 0
}
func validateArraySchemaUpdate(oldSchemaMap, newSchemaMap map[string]any) error {
if err := validateItems(oldSchemaMap, newSchemaMap); err != nil {
return fmt.Errorf("failed to validate items: %v", err)
}
return nil
}
func validateItems(oldSchemaMap, newSchemaMap map[string]any) error {
oldItems, hasOldItems := oldSchemaMap["items"]
newItems, hasNewItems := newSchemaMap["items"]
if !hasNewItems || !hasOldItems {
// If we don't have items in either schema, we're good
// profiles using this rule type won't break
return fmt.Errorf("cannot remove items from rule schema")
}
oldItemsMap, ok := oldItems.(map[string]any)
if !ok {
return fmt.Errorf("invalid old items field")
}
newItemsMap, ok := newItems.(map[string]any)
if !ok {
return fmt.Errorf("invalid new items field")
}
// The new schema should be a superset of the old schema
// if it's not, we may break profiles using this rule type
if !reflect.DeepEqual(newItemsMap, oldItemsMap) {
return fmt.Errorf("cannot change items type of rule schema")
}
return nil
}