Skip to content

Commit 3184d67

Browse files
committed
Validate generated user-data yaml with jsonschema
Signed-off-by: Anders F Björklund <[email protected]>
1 parent f8fbae4 commit 3184d67

9 files changed

+3989
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/opencontainers/go-digest v1.0.0
3535
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
3636
github.com/rjeczalik/notify v0.9.3
37+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
3738
github.com/sethvargo/go-password v0.2.0
3839
github.com/sirupsen/logrus v1.9.3
3940
github.com/spf13/cobra v1.8.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
239239
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
240240
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
241241
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=
242244
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
243245
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
244246
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=

pkg/cidata/schema.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

pkg/cidata/schema_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

pkg/cidata/schemas/LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.

pkg/cidata/schemas/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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/$@

0 commit comments

Comments
 (0)