From 57953e495f10c26312a05eec3d1e7acb2a40e363 Mon Sep 17 00:00:00 2001 From: koplas Date: Wed, 4 Dec 2024 13:23:57 +0100 Subject: [PATCH 1/6] Warn if no remote validator was specified --- cmd/csaf_validator/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/csaf_validator/main.go b/cmd/csaf_validator/main.go index b07c2f49..69855099 100644 --- a/cmd/csaf_validator/main.go +++ b/cmd/csaf_validator/main.go @@ -69,6 +69,8 @@ func run(opts *options, files []string) error { "preparing remote validator failed: %w", err) } defer validator.Close() + } else { + log.Printf("warn: no remote validator specified") } // Select amount level of output for remote validation. From 938ceb872ac4b5460379c86b89b6ca0db6ed72f2 Mon Sep 17 00:00:00 2001 From: koplas Date: Wed, 4 Dec 2024 13:53:56 +0100 Subject: [PATCH 2/6] Return exit code based on validation result --- cmd/csaf_validator/main.go | 13 +++++++++++++ docs/csaf_validator.md | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/cmd/csaf_validator/main.go b/cmd/csaf_validator/main.go index 69855099..4a9e8273 100644 --- a/cmd/csaf_validator/main.go +++ b/cmd/csaf_validator/main.go @@ -22,6 +22,13 @@ import ( "github.com/gocsaf/csaf/v3/util" ) +const ( + exitCodeAllValid = 0 + exitCodeSchemaInvalid = 1 << 0 + exitCodeNoRemoteValidator = 1 << 1 + exitCodeFailedRemoteValidation = 1 << 2 +) + 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"` @@ -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() @@ -70,6 +78,7 @@ func run(opts *options, files []string) error { } defer validator.Close() } else { + exitCode |= exitCodeNoRemoteValidator log.Printf("warn: no remote validator specified") } @@ -106,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) @@ -132,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(exitCodeAllValid) return nil } diff --git a/docs/csaf_validator.md b/docs/csaf_validator.md index dfa0c9a3..74dbaaf7 100644 --- a/docs/csaf_validator.md +++ b/docs/csaf_validator.md @@ -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 the following codes: +- `0`: all valid +- `2⁰`: schema invalid +- `2¹`: no remote validator configured +- `2²`: failure in remote validation + ### Usage ``` From 16e86051c5d1b0912a179eb2b30ba568da4e81ce Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Wed, 4 Dec 2024 14:27:24 +0100 Subject: [PATCH 3/6] Be more precise about exit codes. --- cmd/csaf_validator/main.go | 8 ++++---- docs/csaf_validator.md | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/csaf_validator/main.go b/cmd/csaf_validator/main.go index 4a9e8273..9e844b70 100644 --- a/cmd/csaf_validator/main.go +++ b/cmd/csaf_validator/main.go @@ -23,10 +23,10 @@ import ( ) const ( - exitCodeAllValid = 0 - exitCodeSchemaInvalid = 1 << 0 - exitCodeNoRemoteValidator = 1 << 1 - exitCodeFailedRemoteValidation = 1 << 2 + exitCodeSchemaInvalid = 1 << iota + exitCodeNoRemoteValidator + exitCodeFailedRemoteValidation + exitCodeAllValid = 0 ) type options struct { diff --git a/docs/csaf_validator.md b/docs/csaf_validator.md index 74dbaaf7..64ded6dc 100644 --- a/docs/csaf_validator.md +++ b/docs/csaf_validator.md @@ -3,11 +3,11 @@ 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 the following codes: -- `0`: all valid -- `2⁰`: schema invalid -- `2¹`: no remote validator configured -- `2²`: failure in remote validation +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 From df65ad13cbd222d2a2b1784287bd9e2e8b22ba7b Mon Sep 17 00:00:00 2001 From: koplas Date: Tue, 10 Dec 2024 10:13:42 +0100 Subject: [PATCH 4/6] Fix: return correct exit code --- cmd/csaf_validator/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/csaf_validator/main.go b/cmd/csaf_validator/main.go index 9e844b70..1a34be0d 100644 --- a/cmd/csaf_validator/main.go +++ b/cmd/csaf_validator/main.go @@ -150,7 +150,7 @@ func run(opts *options, files []string) error { } // Exit code is based on validation results - os.Exit(exitCodeAllValid) + os.Exit(exitCode) return nil } From bc5d149f74d2ce5e7ed03316141a31eafbd80ea1 Mon Sep 17 00:00:00 2001 From: koplas Date: Mon, 16 Dec 2024 19:28:24 +0100 Subject: [PATCH 5/6] Use exit code 1 for general errors, fix documentation --- cmd/csaf_validator/main.go | 2 +- docs/csaf_validator.md | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/csaf_validator/main.go b/cmd/csaf_validator/main.go index 1a34be0d..346180bc 100644 --- a/cmd/csaf_validator/main.go +++ b/cmd/csaf_validator/main.go @@ -23,7 +23,7 @@ import ( ) const ( - exitCodeSchemaInvalid = 1 << iota + exitCodeSchemaInvalid = 2 << iota exitCodeNoRemoteValidator exitCodeFailedRemoteValidation exitCodeAllValid = 0 diff --git a/docs/csaf_validator.md b/docs/csaf_validator.md index 64ded6dc..a0e00bbe 100644 --- a/docs/csaf_validator.md +++ b/docs/csaf_validator.md @@ -5,9 +5,10 @@ is a tool to validate local advisories files against the JSON Schema and an opti ### 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 +- `(n & 1) > 0`: general error, see logs +- `(n & 2) > 0`: schema validation failed +- `(n & 4) > 0`: no remote validator configured +- `(n & 8) > 0`: failure in remote validation ### Usage From 8fc7f5bfad0c6022cbcc07cec36b875cb4ad292e Mon Sep 17 00:00:00 2001 From: koplas Date: Tue, 7 Jan 2025 12:23:40 +0100 Subject: [PATCH 6/6] Make documentation more explicit --- docs/csaf_validator.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/csaf_validator.md b/docs/csaf_validator.md index a0e00bbe..87ec831e 100644 --- a/docs/csaf_validator.md +++ b/docs/csaf_validator.md @@ -3,9 +3,11 @@ 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`: general error, see logs +- `(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