From 682ca5c9e2b986d79850cfd491c13e3bf8862672 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Mon, 23 Jan 2023 12:52:02 +0100 Subject: [PATCH] Use httploader to validate HTTP references --- pkg/validator/validator.go | 1 + .../jsonschema/v5/httploader/httploader.go | 38 +++++++++++++++++++ vendor/modules.txt | 1 + 3 files changed, 40 insertions(+) create mode 100644 vendor/github.com/santhosh-tekuri/jsonschema/v5/httploader/httploader.go diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 96ef959..34b9036 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -7,6 +7,7 @@ import ( "io" jsonschema "github.com/santhosh-tekuri/jsonschema/v5" + _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" "github.com/yannh/kubeconform/pkg/cache" "github.com/yannh/kubeconform/pkg/registry" "github.com/yannh/kubeconform/pkg/resource" diff --git a/vendor/github.com/santhosh-tekuri/jsonschema/v5/httploader/httploader.go b/vendor/github.com/santhosh-tekuri/jsonschema/v5/httploader/httploader.go new file mode 100644 index 0000000..4198cfe --- /dev/null +++ b/vendor/github.com/santhosh-tekuri/jsonschema/v5/httploader/httploader.go @@ -0,0 +1,38 @@ +// Package httploader implements loader.Loader for http/https url. +// +// The package is typically only imported for the side effect of +// registering its Loaders. +// +// To use httploader, link this package into your program: +// +// import _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" +package httploader + +import ( + "fmt" + "io" + "net/http" + + "github.com/santhosh-tekuri/jsonschema/v5" +) + +// Client is the default HTTP Client used to Get the resource. +var Client = http.DefaultClient + +// Load loads resource from given http(s) url. +func Load(url string) (io.ReadCloser, error) { + resp, err := Client.Get(url) + if err != nil { + return nil, err + } + if resp.StatusCode != http.StatusOK { + _ = resp.Body.Close() + return nil, fmt.Errorf("%s returned status code %d", url, resp.StatusCode) + } + return resp.Body, nil +} + +func init() { + jsonschema.Loaders["http"] = Load + jsonschema.Loaders["https"] = Load +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 097cca1..706d0bb 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,6 +1,7 @@ # github.com/santhosh-tekuri/jsonschema/v5 v5.1.1 ## explicit; go 1.15 github.com/santhosh-tekuri/jsonschema/v5 +github.com/santhosh-tekuri/jsonschema/v5/httploader # gopkg.in/yaml.v2 v2.4.0 ## explicit; go 1.15 gopkg.in/yaml.v2