mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
feat(scanner): report per-scanner health and the bypass behind a decision
The manager now exposes the cached health, and the CLI version, of every registered scanner. A scan decision also names the admin bypass that applied to the package, so callers can tell a bypassed package from a cleared one.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
// Package scanver remembers the version a scanner's CLI reported at its last health check.
|
||||
package scanver
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var versionToken = regexp.MustCompile(`\d+\.\d+(?:\.\d+)?[0-9A-Za-z.+-]*`)
|
||||
|
||||
// Cache holds the last version seen; the zero value reports no version.
|
||||
type Cache struct {
|
||||
v atomic.Pointer[string]
|
||||
}
|
||||
|
||||
// Record stores the first version-like token in a CLI's version output.
|
||||
func (c *Cache) Record(output string) {
|
||||
if tok := versionToken.FindString(output); tok != "" {
|
||||
c.v.Store(&tok)
|
||||
}
|
||||
}
|
||||
|
||||
// Version returns the recorded version, or "" when none was seen.
|
||||
func (c *Cache) Version() string {
|
||||
if p := c.v.Load(); p != nil {
|
||||
return *p
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package scanver
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRecord(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
output string
|
||||
want string
|
||||
}{
|
||||
{name: "trivy", output: "Version: 0.58.1\nVulnerability DB:\n Version: 2\n", want: "0.58.1"},
|
||||
{name: "grype", output: "Application: grype\nVersion: 0.87.0\n", want: "0.87.0"},
|
||||
{name: "npm", output: "10.9.2\n", want: "10.9.2"},
|
||||
{name: "prerelease", output: "pip-audit 2.8.0rc1", want: "2.8.0rc1"},
|
||||
{name: "no version", output: "command not found", want: ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var c Cache
|
||||
c.Record(tt.output)
|
||||
if got := c.Version(); got != tt.want {
|
||||
t.Fatalf("Version() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordKeepsLastGoodVersion(t *testing.T) {
|
||||
var c Cache
|
||||
c.Record("1.2.3")
|
||||
c.Record("garbage")
|
||||
if got := c.Version(); got != "1.2.3" {
|
||||
t.Fatalf("Version() = %q, want 1.2.3", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user