File tree 9 files changed +3989
-0
lines changed
9 files changed +3989
-0
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ require (
34
34
github.com/opencontainers/go-digest v1.0.0
35
35
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
36
36
github.com/rjeczalik/notify v0.9.3
37
+ github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
37
38
github.com/sethvargo/go-password v0.2.0
38
39
github.com/sirupsen/logrus v1.9.3
39
40
github.com/spf13/cobra v1.8.0
Original file line number Diff line number Diff line change @@ -239,6 +239,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
239
239
github.com/rogpeppe/go-internal v1.10.0 /go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog =
240
240
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk =
241
241
github.com/russross/blackfriday/v2 v2.1.0 /go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM =
242
+ github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4 =
243
+ github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 /go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY =
242
244
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI =
243
245
github.com/sethvargo/go-password v0.2.0 /go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE =
244
246
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ =
Original file line number Diff line number Diff line change
1
+ package cidata
2
+
3
+ import (
4
+ _ "embed"
5
+ "strings"
6
+
7
+ "github.com/santhosh-tekuri/jsonschema/v5"
8
+ "gopkg.in/yaml.v3"
9
+ )
10
+
11
+ // schemaURL is the identifier, not the context
12
+ const schemaURL = "https://raw.githubusercontent.com/canonical/cloud-init/main/cloudinit/config/schemas/schema-cloud-config-v1.json"
13
+
14
+ //go:embed schemas/schema-cloud-config-v1.json
15
+ var schemaText string
16
+
17
+ func validateCloudConfig (userData []byte ) error {
18
+ var m interface {}
19
+ err := yaml .Unmarshal (userData , & m )
20
+ if err != nil {
21
+ return err
22
+ }
23
+ compiler := jsonschema .NewCompiler ()
24
+ compiler .ExtractAnnotations = true
25
+ if err := compiler .AddResource (schemaURL , strings .NewReader (schemaText )); err != nil {
26
+ return err
27
+ }
28
+ schema , err := compiler .Compile (schemaURL )
29
+ if err != nil {
30
+ return err
31
+ }
32
+ if err := schema .Validate (m ); err != nil {
33
+ return err
34
+ }
35
+ return err
36
+ }
Original file line number Diff line number Diff line change
1
+ package cidata
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "gotest.tools/v3/assert"
7
+ )
8
+
9
+ func TestValidate (t * testing.T ) {
10
+ config := `#cloud-config
11
+ users:
12
+ - default
13
+ `
14
+ err := validateCloudConfig ([]byte (config ))
15
+ assert .NilError (t , err )
16
+ }
Original file line number Diff line number Diff line change
1
+ Copyright 2015 Canonical Ltd.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
Original file line number Diff line number Diff line change
1
+
2
+ CLOUD_INIT_VERSION = 24.1.3
3
+
4
+ all : versions.schema.cloud-config.json schema-cloud-config-v1.json
5
+
6
+ % .json :
7
+ curl -fsSLO https://raw.githubusercontent.com/canonical/cloud-init/$(CLOUD_INIT_VERSION ) /cloudinit/config/schemas/$@
You can’t perform that action at this time.
0 commit comments