kairos-agent/internal/agent/validate.go

68 lines
1.3 KiB
Go
Raw Normal View History

sparkles: Integrate schema validation (#853) * Change ValidationError to return the actual error Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add validate command Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Warn validation errors when scanning configs Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add schema command to print config json schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add strict-validations flag Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint and remove focus Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Rename command schema to print-schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Fix issue by reading originalData Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Remove test from removed feature Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add comments to exported functions Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Lint Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add test for validate.go Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Remove focus Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add more tests for root schema Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Add more tests Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> --------- Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> Co-authored-by: Itxaka <itxaka.garcia@spectrocloud.com>
2023-02-14 15:15:13 +00:00
package agent
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
schema "github.com/kairos-io/kairos/pkg/config/schemas"
)
// 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 := schema.GenerateSchema(schema.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(file string) error {
var yaml string
if strings.HasPrefix(file, "http") {
resp, err := http.Get(file)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
//Convert the body to type string
yaml = string(body)
} else {
dat, err := os.ReadFile(file)
if err != nil {
return err
}
yaml = string(dat)
}
config, err := schema.NewConfigFromYAML(yaml, schema.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
}