forked from cncf-tags/container-device-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
356 lines (294 loc) · 8.12 KB
/
schema.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
/*
Copyright © 2022 The CDI Authors
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 schema
import (
"bytes"
"embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sigs.k8s.io/yaml"
schema "github.com/xeipuuv/gojsonschema"
"tags.cncf.io/container-device-interface/internal/validation"
)
const (
// BuiltinSchemaName names the builtin schema for Load()/Set().
BuiltinSchemaName = "builtin"
// NoneSchemaName names the NOP-schema for Load()/Set().
NoneSchemaName = "none"
// builtinSchemaFile is the builtin schema URI in our embedded FS.
builtinSchemaFile = "file:///schema.json"
)
// Schema is a JSON validation schema.
type Schema struct {
schema *schema.Schema
}
// Error wraps a JSON validation result.
type Error struct {
Result *schema.Result
}
// Set sets the default validating JSON schema.
func Set(s *Schema) {
current = s
}
// Get returns the active validating JSON schema.
func Get() *Schema {
return current
}
// BuiltinSchema returns the builtin schema if we have a valid one. Otherwise
// it falls back to NopSchema().
func BuiltinSchema() *Schema {
if builtin != nil {
return builtin
}
s, err := schema.NewSchema(
schema.NewReferenceLoaderFileSystem(
builtinSchemaFile,
http.FS(builtinFS),
),
)
if err == nil {
builtin = &Schema{schema: s}
} else {
builtin = NopSchema()
}
return builtin
}
// NopSchema returns an validating JSON Schema that does no real validation.
func NopSchema() *Schema {
return &Schema{}
}
// ReadAndValidate all data from the given reader, using the active schema for validation.
func ReadAndValidate(r io.Reader) ([]byte, error) {
return current.ReadAndValidate(r)
}
// Validate validates the data read from an io.Reader against the active schema.
func Validate(r io.Reader) error {
return current.Validate(r)
}
// ValidateData validates the given JSON document against the active schema.
func ValidateData(data []byte) error {
return current.ValidateData(data)
}
// ValidateFile validates the given JSON file against the active schema.
func ValidateFile(path string) error {
return current.ValidateFile(path)
}
// ValidateType validates a go object against the schema.
func ValidateType(obj interface{}) error {
return current.ValidateType(obj)
}
// Load the given JSON Schema.
func Load(source string) (*Schema, error) {
var (
loader schema.JSONLoader
err error
s *schema.Schema
)
source = strings.TrimSpace(source)
switch {
case source == BuiltinSchemaName:
return BuiltinSchema(), nil
case source == NoneSchemaName, source == "":
return NopSchema(), nil
case strings.HasPrefix(source, "file://"):
case strings.HasPrefix(source, "http://"):
case strings.HasPrefix(source, "https://"):
default:
if !strings.Contains(source, "://") {
source, err = filepath.Abs(source)
if err != nil {
return nil, fmt.Errorf("failed to get JSON schema absolute path for %s: %w",
source, err)
}
source = "file://" + source
}
}
loader = schema.NewReferenceLoader(source)
s, err = schema.NewSchema(loader)
if err != nil {
return nil, fmt.Errorf("failed to load JSON schema: %w", err)
}
return &Schema{schema: s}, nil
}
// ReadAndValidate all data from the given reader, using the schema for validation.
func (s *Schema) ReadAndValidate(r io.Reader) ([]byte, error) {
loader, reader := schema.NewReaderLoader(r)
data, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("failed to read data for validation: %w", err)
}
return data, s.validate(loader)
}
// Validate validates the data read from an io.Reader against the schema.
func (s *Schema) Validate(r io.Reader) error {
_, err := s.ReadAndValidate(r)
return err
}
// ValidateData validates the given JSON data against the schema.
func (s *Schema) ValidateData(data []byte) error {
var (
any map[string]interface{}
err error
)
if !bytes.HasPrefix(bytes.TrimSpace(data), []byte{'{'}) {
err = yaml.Unmarshal(data, &any)
if err != nil {
return fmt.Errorf("failed to YAML unmarshal data for validation: %w", err)
}
data, err = json.Marshal(any)
if err != nil {
return fmt.Errorf("failed to JSON remarshal data for validation: %w", err)
}
}
if err := s.validate(schema.NewBytesLoader(data)); err != nil {
return err
}
return s.validateContents(any)
}
// ValidateFile validates the given JSON file against the schema.
func (s *Schema) ValidateFile(path string) error {
if filepath.Ext(path) == ".json" {
return s.validate(schema.NewReferenceLoader("file://" + path))
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
return s.ValidateData(data)
}
// ValidateType validates a go object against the schema.
func (s *Schema) ValidateType(obj interface{}) error {
l := schema.NewGoLoader(obj)
return s.validate(l)
}
// Validate the (to be) loaded doc against the schema.
func (s *Schema) validate(doc schema.JSONLoader) error {
if s == nil || s.schema == nil {
return nil
}
docErr, jsonErr := s.schema.Validate(doc)
if jsonErr != nil {
return fmt.Errorf("failed to load JSON data for validation: %w", jsonErr)
}
if docErr.Valid() {
return nil
}
return &Error{Result: docErr}
}
type schemaContents map[string]interface{}
func asSchemaContents(i interface{}) (schemaContents, error) {
if i == nil {
return nil, nil
}
if c, ok := i.(map[string]interface{}); ok {
return schemaContents(c), nil
}
return nil, fmt.Errorf("expected map[string]interface{} but got %T", i)
}
func (c schemaContents) getFieldAsString(key string) (string, bool) {
if c == nil {
return "", false
}
if value, ok := c[key]; ok {
if value, ok := value.(string); ok {
return value, true
}
}
return "", false
}
func (c schemaContents) getAnnotations() (map[string]interface{}, bool) {
if c == nil {
return nil, false
}
if annotations, ok := c["annotations"]; ok {
if annotations, ok := annotations.(map[string]interface{}); ok {
return annotations, true
}
}
return nil, false
}
func (c schemaContents) getDevices() ([]schemaContents, error) {
if c == nil {
return nil, nil
}
devicesIfc, ok := c["devices"]
if !ok {
return nil, nil
}
devices, ok := devicesIfc.([]interface{})
if !ok {
return nil, nil
}
var deviceContents []schemaContents
for _, device := range devices {
c, err := asSchemaContents(device)
if err != nil {
return nil, fmt.Errorf("failed to parse device: %w", err)
}
deviceContents = append(deviceContents, c)
}
return deviceContents, nil
}
// validateContents performs additional validation against the schema contents.
func (s *Schema) validateContents(any map[string]interface{}) error {
if any == nil || s == nil {
return nil
}
contents := schemaContents(any)
if specAnnotations, ok := contents.getAnnotations(); ok {
if err := validation.ValidateSpecAnnotations("", specAnnotations); err != nil {
return err
}
}
devices, err := contents.getDevices()
if err != nil {
return err
}
for _, device := range devices {
name, _ := device.getFieldAsString("name")
if annotations, ok := device.getAnnotations(); ok {
if err := validation.ValidateSpecAnnotations(name, annotations); err != nil {
return err
}
}
}
return nil
}
// Error returns the given Result's errors as a single error string.
func (e *Error) Error() string {
if e == nil || e.Result == nil || e.Result.Valid() {
return ""
}
errs := []error{}
for _, err := range e.Result.Errors() {
errs = append(errs, fmt.Errorf("%v", err))
}
if err := errors.Join(errs...); err != nil {
return fmt.Sprintf("%v", err)
}
return ""
}
var (
// our builtin schema
builtin *Schema
// currently loaded schema, builtin by default
current = BuiltinSchema()
)
//go:embed *.json
var builtinFS embed.FS