Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if no remote validator was specified #602

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 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 = 1 << 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 @@ -104,6 +115,7 @@ func run(opts *options, files []string) error {

}
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 @@ -130,12 +142,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
7 changes: 7 additions & 0 deletions docs/csaf_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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 / 2) % 1 == 1`: schema validation failed
- `(n / 4) % 1 == 1`: no remote validator configured
- `(n / 8) % 1 == 1`: failure in remote validation

### Usage

```
Expand Down
Loading