Skip to content

Commit

Permalink
Merge pull request #3 from fabi200123/json-schema
Browse files Browse the repository at this point in the history
Adding Extra specs JSON Schema validation
  • Loading branch information
gabriel-samfira authored Apr 23, 2024
2 parents 6db5286 + 53877ef commit 9ac72e1
Show file tree
Hide file tree
Showing 1,272 changed files with 412,858 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/lxc/incus v0.3.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v1.2.0
)

require (
Expand All @@ -34,6 +35,8 @@ require (
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/zitadel/oidc/v2 v2.12.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zitadel/oidc/v2 v2.12.0 h1:4aMTAy99/4pqNwrawEyJqhRb3yY3PtcDxnoDSryhpn4=
github.com/zitadel/oidc/v2 v2.12.0/go.mod h1:LrRav74IiThHGapQgCHZOUNtnqJG0tcZKHro/91rtLw=
Expand Down
45 changes: 45 additions & 0 deletions provider/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,68 @@ package provider

import (
"encoding/json"
"fmt"

commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)

const jsonSchema string = `
{
"$schema": "http://cloudbase.it/garm-provider-incus/schemas/extra_specs#",
"type": "object",
"description": "Schema defining supported extra specs for the Garm Incus Provider",
"properties": {
"extra_packages": {
"type": "array",
"description": "A list of packages that cloud-init should install on the instance.",
"items": {
"type": "string"
}
},
"disable_updates": {
"type": "boolean",
"description": "Whether to disable updates when cloud-init comes online."
},
"enable_boot_debug": {
"type": "boolean",
"description": "Allows providers to set the -x flag in the runner install script."
}
},
"additionalProperties": false
}
`

type extraSpecs struct {
DisableUpdates bool `json:"disable_updates"`
ExtraPackages []string `json:"extra_packages"`
EnableBootDebug bool `json:"enable_boot_debug"`
}

func jsonSchemaValidation(schema json.RawMessage) error {
schemaLoader := gojsonschema.NewStringLoader(jsonSchema)
extraSpecsLoader := gojsonschema.NewBytesLoader(schema)
result, err := gojsonschema.Validate(schemaLoader, extraSpecsLoader)
if err != nil {
return fmt.Errorf("failed to validate schema: %w", err)
}
if !result.Valid() {
return fmt.Errorf("schema validation failed: %s", result.Errors())
}
return nil
}

func parseExtraSpecsFromBootstrapParams(bootstrapParams commonParams.BootstrapInstance) (extraSpecs, error) {
specs := extraSpecs{}
if bootstrapParams.ExtraSpecs == nil {
return specs, nil
}

if err := jsonSchemaValidation(bootstrapParams.ExtraSpecs); err != nil {
return specs, fmt.Errorf("failed to validate extra specs: %w", err)
}

if err := json.Unmarshal(bootstrapParams.ExtraSpecs, &specs); err != nil {
return specs, errors.Wrap(err, "unmarshaling extra specs")
}
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/BurntSushi/toml/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/BurntSushi/toml/COPYING

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions vendor/github.com/BurntSushi/toml/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9ac72e1

Please sign in to comment.