Skip to content

Commit

Permalink
Merge pull request #602 from gocsaf/remote-validator-warn
Browse files Browse the repository at this point in the history
Warn if no remote validator was specified
  • Loading branch information
koplas authored Jan 23, 2025
2 parents 93c1a0b + 6e8c2ec commit 8e5236a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 17 additions & 3 deletions cmd/csaf_validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import (
"github.com/gocsaf/csaf/v3/util"
)

const (
exitCodeSchemaInvalid = 2 << iota
exitCodeNoRemoteValidator
exitCodeFailedRemoteValidation
exitCodeAllValid = 0
)

type options struct {
Version bool `long:"version" description:"Display version of the binary"`
RemoteValidator string `long:"validator" description:"URL to validate documents remotely" value-name:"URL"`
Expand Down Expand Up @@ -53,6 +60,7 @@ func main() {

// run validates the given files.
func run(opts *options, files []string) error {
exitCode := exitCodeAllValid

var validator csaf.RemoteValidator
eval := util.NewPathEval()
Expand All @@ -69,6 +77,9 @@ func run(opts *options, files []string) error {
"preparing remote validator failed: %w", err)
}
defer validator.Close()
} else {
exitCode |= exitCodeNoRemoteValidator
log.Printf("warn: no remote validator specified")
}

// Select amount level of output for remote validation.
Expand Down Expand Up @@ -96,14 +107,15 @@ func run(opts *options, files []string) error {
log.Printf("error: loading %q as JSON failed: %v\n", file, err)
continue
}
// Validate agsinst Schema.
// Validate against Schema.
validationErrs, err := csaf.ValidateCSAF(doc)
if err != nil {
log.Printf("error: validating %q against schema failed: %v\n",
file, err)

}
if len(validationErrs) > 0 {
exitCode |= exitCodeSchemaInvalid
fmt.Printf("schema validation errors of %q\n", file)
for _, vErr := range validationErrs {
fmt.Printf(" * %s\n", vErr)
Expand All @@ -112,10 +124,9 @@ func run(opts *options, files []string) error {
fmt.Printf("%q passes the schema validation.\n", file)
}

// Check filename agains ID
// Check filename against ID
if err := util.IDMatchesFilename(eval, doc, filepath.Base(file)); err != nil {
log.Printf("%s: %s.\n", file, err)
continue
}

// Validate against remote validator.
Expand All @@ -130,12 +141,15 @@ func run(opts *options, files []string) error {
if rvr.Valid {
passes = "passes"
} else {
exitCode |= exitCodeFailedRemoteValidation
passes = "does not pass"
}
fmt.Printf("%q %s remote validation.\n", file, passes)
}
}

// Exit code is based on validation results
os.Exit(exitCode)
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions docs/csaf_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

is a tool to validate local advisories files against the JSON Schema and an optional remote validator.

### Exit codes

If no fatal error occurs the program will exit with an exit code `n` with the following conditions:

- `n == 0`: all valid
- `(n & 1) > 0`: a general error occurred, all other flags are unset (see logs for more information)
- `(n & 2) > 0`: schema validation failed
- `(n & 4) > 0`: no remote validator configured
- `(n & 8) > 0`: failure in remote validation

### Usage

```
Expand Down

0 comments on commit 8e5236a

Please sign in to comment.