1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 05:40:11 +01:00

Upgrade jsonschema library to 5.2.0 (#349)

* make jsonschema loading work with current versions of the jsonschema library
   again and simplify the code while at it.
* Improve itest workflow for validation service, to make them more robust.
    * Use a github action to setup nodejs and use a version that is
       required by https://github.com/secvisogram/csaf-validator-service/.
    * Add nodejs16 installation to prepareUbuntuInstanceForITests.sh.
       (so it can be done once in manual settings)
    * Add refreshing of apt cache before apt install because sometimes
       the cached ubuntu image does not have the apt cache current.

---------

Co-authored-by: Bernhard Reiter <bernhard@intevation.de>
This commit is contained in:
Sascha L. Teichmann 2023-03-10 10:39:23 +01:00 committed by GitHub
parent 47d9eccc37
commit aa574406cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 57 deletions

View file

@ -11,6 +11,7 @@ package csaf
import (
"bytes"
_ "embed" // Used for embedding.
"io"
"sort"
"strings"
"sync"
@ -39,62 +40,62 @@ var aggregatorSchema []byte
//go:embed schema/ROLIE_feed_json_schema.json
var rolieSchema []byte
var (
compiledCSAFSchema compiledSchema
compiledProviderSchema compiledSchema
compiledAggregatorSchema compiledSchema
compiledRolieSchema compiledSchema
)
func init() {
compiledCSAFSchema.compiler([]schemaData{
{"https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json", csafSchema},
{"https://www.first.org/cvss/cvss-v2.0.json", cvss20},
{"https://www.first.org/cvss/cvss-v3.0.json", cvss30},
{"https://www.first.org/cvss/cvss-v3.1.json", cvss31},
})
compiledProviderSchema.compiler([]schemaData{
{"https://docs.oasis-open.org/csaf/csaf/v2.0/provider_json_schema.json", providerSchema},
{"https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json", csafSchema},
})
compiledAggregatorSchema.compiler([]schemaData{
{"https://docs.oasis-open.org/csaf/csaf/v2.0/aggregator_json_schema.json", aggregatorSchema},
{"https://docs.oasis-open.org/csaf/csaf/v2.0/provider_json_schema.json", providerSchema},
{"https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json", csafSchema},
})
compiledRolieSchema.compiler([]schemaData{
{"https://raw.githubusercontent.com/tschmidtb51/csaf/ROLIE-schema/csaf_2.0/json_schema/ROLIE_feed_json_schema.json", rolieSchema},
})
}
type schemaData struct {
url string
data []byte
}
type compiledSchema struct {
url string
once sync.Once
compile func()
err error
compiled *jsonschema.Schema
}
func (cs *compiledSchema) compiler(sds []schemaData) {
if len(sds) == 0 {
panic("missing schema data")
const (
csafSchemaURL = "https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json"
providerSchemaURL = "https://docs.oasis-open.org/csaf/csaf/v2.0/provider_json_schema.json"
aggregatorSchemaURL = "https://docs.oasis-open.org/csaf/csaf/v2.0/aggregator_json_schema.json"
cvss20SchemaURL = "https://www.first.org/cvss/cvss-v2.0.json"
cvss30SchemaURL = "https://www.first.org/cvss/cvss-v3.0.json"
cvss31SchemaURL = "https://www.first.org/cvss/cvss-v3.1.json"
rolieSchemaURL = "https://raw.githubusercontent.com/tschmidtb51/csaf/ROLIE-schema/csaf_2.0/json_schema/ROLIE_feed_json_schema.json"
)
var (
compiledCSAFSchema = compiledSchema{url: csafSchemaURL}
compiledProviderSchema = compiledSchema{url: providerSchemaURL}
compiledAggregatorSchema = compiledSchema{url: aggregatorSchemaURL}
compiledRolieSchema = compiledSchema{url: rolieSchemaURL}
)
// loadURL loads the content of an URL from embedded data or
// falls back to the global loader function of the jsonschema package.
func loadURL(s string) (io.ReadCloser, error) {
loader := func(data []byte) (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(data)), nil
}
cs.compile = func() {
c := jsonschema.NewCompiler()
for _, s := range sds {
if cs.err = c.AddResource(
s.url, bytes.NewReader(s.data)); cs.err != nil {
return
}
}
cs.compiled, cs.err = c.Compile(sds[0].url)
switch s {
case csafSchemaURL:
return loader(csafSchema)
case cvss20SchemaURL:
return loader(cvss20)
case cvss30SchemaURL:
return loader(cvss30)
case cvss31SchemaURL:
return loader(cvss31)
case providerSchemaURL:
return loader(providerSchema)
case aggregatorSchemaURL:
return loader(aggregatorSchema)
case rolieSchemaURL:
return loader(rolieSchema)
default:
return jsonschema.LoadURL(s)
}
}
func (cs *compiledSchema) compile() {
c := jsonschema.NewCompiler()
c.LoadURL = loadURL
cs.compiled, cs.err = c.Compile(cs.url)
}
func (cs *compiledSchema) validate(doc any) ([]string, error) {
cs.once.Do(cs.compile)