-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidations.go
More file actions
145 lines (90 loc) · 2.71 KB
/
validations.go
File metadata and controls
145 lines (90 loc) · 2.71 KB
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
package main
import (
"fmt"
"log"
"os"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"
)
const SCHEMA = `{
"type": "object",
"properties": {
"name": { "type": "string" },
"registry": { "type": "string" },
"url": { "type": "string" },
"image_types": {
"type": ["array", "null"],
"items": { "type": "string", "enum": ["snapshots", "releases"] }
},
"auth_strategy": {
"type": "string",
"enum": ["aws_oidc", "azure_oidc", "generic", "ghcr", "dockerhub"]
},
"base_paths": {
"type": ["object", "null"],
"properties": {
"services": { "type": "string" },
"charts": { "type": "string" }
},
"required": ["services", "charts"]
}
},
"required": ["name", "registry"]
}`
func validate() {
if CLI.Validate.All && CLI.Validate.File != "" {
panic("You can't use --all and --file at the same time")
}
if CLI.Validate.All && CLI.Validate.RegistriesDir == "" {
panic("--all requires --registries-dir, please provide it")
}
if CLI.Validate.File != "" && CLI.Validate.RegistriesDir != "" {
panic("--file and --registries-dir are mutually exclusive, please provide only one")
}
if CLI.Validate.All {
log.Printf("Validating all registries in %s", CLI.Validate.RegistriesDir)
registries := parseRegistriesFromDir(CLI.Validate.RegistriesDir)
log.Printf("%d registries found", len(registries))
log.Printf("All registries are valid")
} else if CLI.Validate.File != "" {
validateRegistryFileSchema(CLI.Validate.File)
} else {
panic("You need to provide a registry --file or the --all flag")
}
}
func validateRegistryAgainstSchema(registryData Registry) error {
schemaLoader := gojsonschema.NewStringLoader(SCHEMA)
documentLoader := gojsonschema.NewGoLoader(registryData)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return fmt.Errorf("validation: %v", err)
}
if !result.Valid() {
for _, desc := range result.Errors() {
fmt.Printf("Error: %s\n", desc)
}
return fmt.Errorf("document is not valid %s", result.Errors())
}
return nil
}
func validateRegistryFileSchema(registryFile string) (Registry, error) {
var registry Registry
log.Printf("Reading registry file %s", registryFile)
fileData, err := os.ReadFile(registryFile)
if err != nil {
return Registry{}, err
}
log.Printf("Parsing registry file %s", registryFile)
err = yaml.Unmarshal(fileData, ®istry)
if err != nil {
return Registry{}, err
}
log.Printf("Validating registry file %s", registryFile)
err = validateRegistryAgainstSchema(registry)
if err != nil {
log.Printf("Registry file %s is invalid", registryFile)
return Registry{}, err
}
log.Printf("Registry file %s is valid", registryFile)
return registry, nil
}