-
Notifications
You must be signed in to change notification settings - Fork 4
/
validate.go
71 lines (61 loc) · 1.47 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
package schema
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
// JSONSchema builds a JSON Schema based on the Root Schema and the given version
// this is helpful when mapping a validation error.
func JSONSchema(version string) (string, error) {
url := fmt.Sprintf("https://kairos.io/%s/cloud-config.json", version)
schema, err := GenerateSchema(RootSchema{}, url)
if err != nil {
return "", err
}
return schema, nil
}
// Validate ensures that a given schema is Valid according to the Root Schema from the agent.
func Validate(source string) error {
var yaml string
if strings.HasPrefix(source, "http") {
resp, err := http.Get(source)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
//Convert the body to type string
yaml = string(body)
} else {
// Maybe we should just try to read the string for the normal headers? That would identify a full yaml vs a file
dat, err := os.ReadFile(source)
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "file name too long") {
yaml = source
} else {
return err
}
} else {
yaml = string(dat)
}
}
config, err := NewConfigFromYAML(yaml, RootSchema{})
if err != nil {
return err
}
if !config.HasHeader() {
return fmt.Errorf("missing #cloud-config header")
}
if config.IsValid() {
return nil
}
err = config.ValidationError
if err != nil {
return err
}
return nil
}