Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate generated user-data yaml with jsonschema #2267

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 52 additions & 4 deletions cmd/limactl/validate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"bufio"
"fmt"
"io"
"os"

"github.com/lima-vm/lima/cmd/limactl/guessarg"
"github.com/lima-vm/lima/pkg/cidata"
"github.com/lima-vm/lima/pkg/store"
"github.com/spf13/cobra"

Expand All @@ -21,15 +25,59 @@ func newValidateCommand() *cobra.Command {
return validateCommand
}

func firstLine(f string) (string, error) {
file, err := os.Open(f)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
if err := scanner.Err(); err != nil {
return "", err
}
return scanner.Text(), nil
}

func validateCloudConfig(f string) error {
file, err := os.Open(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
defer file.Close()
b, err := io.ReadAll(file)
if err != nil {
return err
}
return cidata.ValidateCloudConfig(b)
}

func validateLimayaml(f string) error {
_, err := store.LoadYAMLByFilePath(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
return err
}
return nil
}

func validateAction(_ *cobra.Command, args []string) error {
for _, f := range args {
_, err := store.LoadYAMLByFilePath(f)
line, err := firstLine(f)
if err != nil {
return fmt.Errorf("failed to load YAML file %q: %w", f, err)
}
if _, err := guessarg.InstNameFromYAMLPath(f); err != nil {
return err
}
if line == "#cloud-config" {
if err := validateCloudConfig(f); err != nil {
return err
}
} else {
if err := validateLimayaml(f); err != nil {
return err
}
}
logrus.Infof("%q: OK", f)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/opencontainers/go-digest v1.0.0
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/rjeczalik/notify v0.9.3
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
35 changes: 35 additions & 0 deletions pkg/cidata/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cidata

import (
_ "embed"
"strings"

"github.com/santhosh-tekuri/jsonschema/v5"
"gopkg.in/yaml.v3"
)

// schemaURL is the identifier, not the context
const schemaURL = "https://raw.githubusercontent.com/canonical/cloud-init/main/cloudinit/config/schemas/schema-cloud-config-v1.json"
afbjorklund marked this conversation as resolved.
Show resolved Hide resolved

//go:embed schemas/schema-cloud-config-v1.json
var schemaText string

func ValidateCloudConfig(userData []byte) error {
var m interface{}
err := yaml.Unmarshal(userData, &m)
if err != nil {
return err
}
compiler := jsonschema.NewCompiler()
if err := compiler.AddResource(schemaURL, strings.NewReader(schemaText)); err != nil {
return err
}
schema, err := compiler.Compile(schemaURL)
if err != nil {
return err
}
if err := schema.Validate(m); err != nil {
return err
}
return err
}
16 changes: 16 additions & 0 deletions pkg/cidata/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cidata

import (
"testing"

"gotest.tools/v3/assert"
)

func TestValidate(t *testing.T) {
config := `#cloud-config
users:
- default
`
err := ValidateCloudConfig([]byte(config))
assert.NilError(t, err)
}
13 changes: 13 additions & 0 deletions pkg/cidata/schemas/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2015 Canonical Ltd.

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.
8 changes: 8 additions & 0 deletions pkg/cidata/schemas/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

CLOUD_INIT_VERSION = 24.1.3

all: versions.schema.cloud-config.json schema-cloud-config-v1.json

%.json:
curl -fsSLO https://raw.githubusercontent.com/canonical/cloud-init/$(CLOUD_INIT_VERSION)/cloudinit/config/schemas/$@
if [ "$@" = "schema-cloud-config-v1.json" ]; then patch -N $@ ssh-authorized-keys.patch; fi