From 768021351fcb9322322b76f19f5f7c636865728c Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 17 Nov 2026 13:07:17 +0000 Subject: [PATCH 1/6] feat(cve): parse grype output into findings Signed-off-by: Ricardo Dias --- backend/internal/cve/grype.go | 115 +++++++++++++++++++++++ backend/internal/cve/grype_test.go | 42 +++++++++ backend/internal/cve/testdata/grype.json | 30 ++++++ backend/internal/model/types.go | 25 ++--- frontend/src/types/api.ts | 1 + 5 files changed, 201 insertions(+), 12 deletions(-) create mode 100644 backend/internal/cve/grype.go create mode 100644 backend/internal/cve/grype_test.go create mode 100644 backend/internal/cve/testdata/grype.json diff --git a/backend/internal/cve/grype.go b/backend/internal/cve/grype.go new file mode 100644 index 0000000..86c57c2 --- /dev/null +++ b/backend/internal/cve/grype.go @@ -0,0 +1,115 @@ +package cve + +import ( + "encoding/json" + "fmt" + "strings" + + "github.com/percona/obs-dashboard/internal/model" +) + +type grypeOutput struct { + Matches []grypeMatch `json:"matches"` +} + +type grypeMatch struct { + Vulnerability struct { + ID string `json:"id"` + Severity string `json:"severity"` + Fix struct { + Versions []string `json:"versions"` + } `json:"fix"` + } `json:"vulnerability"` + RelatedVulnerabilities []struct { + ID string `json:"id"` + } `json:"relatedVulnerabilities"` + Artifact struct { + Name string `json:"name"` + Version string `json:"version"` + Type string `json:"type"` + Locations []struct { + Path string `json:"path"` + } `json:"locations"` + Upstreams []struct { + Name string `json:"name"` + } `json:"upstreams"` + } `json:"artifact"` +} + +// parseGrypeOutput converts `grype -o json` output into findings. rpms is the +// RPM list from the Trivy scan of the same image, used for Src/Vendor/Owner. +func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) { + var out grypeOutput + if err := json.Unmarshal(data, &out); err != nil { + return nil, fmt.Errorf("parse grype JSON: %w", err) + } + type key struct{ id, pkg, path string } + seen := map[key]bool{} + var findings []model.CveFinding + for _, m := range out.Matches { + sev := strings.ToUpper(m.Vulnerability.Severity) + if (sev != "HIGH" && sev != "CRITICAL") || len(m.Vulnerability.Fix.Versions) == 0 { + continue + } + id := m.Vulnerability.ID + if !strings.HasPrefix(id, "CVE-") { + for _, r := range m.RelatedVulnerabilities { + if strings.HasPrefix(r.ID, "CVE-") { + id = r.ID + break + } + } + } + fixed := make([]string, len(m.Vulnerability.Fix.Versions)) + for i, v := range m.Vulnerability.Fix.Versions { + fixed[i] = strings.TrimPrefix(v, "0:") + } + a := m.Artifact + f := model.CveFinding{ + ID: id, + PkgName: a.Name, + InstalledVersion: a.Version, + FixedVersion: strings.Join(fixed, ", "), + Severity: sev, + Scanners: []string{"grype"}, + } + switch a.Type { + case "rpm": + f.Class, f.Type = "os-pkgs", "redhat" + case "go-module": + f.Class, f.Type = "lang-pkgs", "gobinary" + case "python": + f.Class, f.Type = "lang-pkgs", "python-pkg" + default: + f.Class, f.Type = "lang-pkgs", a.Type + } + if a.Type != "rpm" && len(a.Locations) > 0 { + f.Path = strings.TrimPrefix(a.Locations[0].Path, "/") + } + if a.Type == "rpm" { + found := false + for _, r := range rpms { + if r.Name == a.Name { + f.Src, f.Vendor, found = r.Src, r.Vendor, true + break + } + } + if !found && len(a.Upstreams) > 0 { + f.Src = a.Upstreams[0].Name + } + } + switch f.Type { + case "gobinary": + f.Owner = goBinaryOwner(f.Path, rpms) + case "python-pkg": + f.Owner = pythonOwner(f.PkgName, f.InstalledVersion, rpms) + } + k := key{f.ID, f.PkgName, f.Path} + if seen[k] { + continue + } + seen[k] = true + findings = append(findings, f) + } + return findings, nil +} diff --git a/backend/internal/cve/grype_test.go b/backend/internal/cve/grype_test.go new file mode 100644 index 0000000..1d50598 --- /dev/null +++ b/backend/internal/cve/grype_test.go @@ -0,0 +1,42 @@ +package cve + +import ( + "os" + "reflect" + "strings" + "testing" + + "github.com/percona/obs-dashboard/internal/model" +) + +func TestParseGrypeOutput(t *testing.T) { + data, err := os.ReadFile("testdata/grype.json") + if err != nil { + t.Fatal(err) + } + rpms := []rpmInfo{ + {Name: "krb5-libs", Version: "1.21.1", Src: "krb5", Vendor: "Red Hat, Inc."}, + {Name: "gosu", Version: "1.17", Src: "gosu", Vendor: "obs://obs.pg.labs.percona.com/isv:percona:ppg:common:deps"}, + } + got, err := parseGrypeOutput(data, rpms) + if err != nil { + t.Fatal(err) + } + g := []string{"grype"} + want := []model.CveFinding{ + {ID: "CVE-2026-0001", PkgName: "krb5-libs", InstalledVersion: "1.21.1-10.el9_8.1", FixedVersion: "1.21.1-10.el9_9.2", Severity: "HIGH", Class: "os-pkgs", Type: "redhat", Src: "krb5", Vendor: "Red Hat, Inc.", Scanners: g}, + {ID: "CVE-2026-0100", PkgName: "krb5-libs", InstalledVersion: "1.21.1-10.el9_8.1", FixedVersion: "1.21.1-10.el9_9.3", Severity: "HIGH", Class: "os-pkgs", Type: "redhat", Src: "krb5", Vendor: "Red Hat, Inc.", Scanners: g}, + {ID: "CVE-2026-0101", PkgName: "newpkg", InstalledVersion: "2:3.0-1.el10", FixedVersion: "2:3.0-2.el10, 2:3.1-1.el10", Severity: "CRITICAL", Class: "os-pkgs", Type: "redhat", Src: "newsrc", Scanners: g}, + {ID: "CVE-2026-0005", PkgName: "golang.org/x/net", InstalledVersion: "v0.54.0", FixedVersion: "0.55.0", Severity: "HIGH", Class: "lang-pkgs", Type: "gobinary", Path: "usr/sbin/gosu", Owner: "gosu", Scanners: g}, + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("got %+v\nwant %+v", got, want) + } +} + +func TestParseGrypeOutputInvalidJSON(t *testing.T) { + _, err := parseGrypeOutput([]byte("not json"), nil) + if err == nil || !strings.HasPrefix(err.Error(), "parse grype JSON") { + t.Fatalf("err = %v", err) + } +} diff --git a/backend/internal/cve/testdata/grype.json b/backend/internal/cve/testdata/grype.json new file mode 100644 index 0000000..e90f9fd --- /dev/null +++ b/backend/internal/cve/testdata/grype.json @@ -0,0 +1,30 @@ +{ + "matches": [ + {"vulnerability": {"id": "CVE-2026-0001", "severity": "High", "fix": {"versions": ["0:1.21.1-10.el9_9.2"], "state": "fixed"}}, + "relatedVulnerabilities": [{"id": "CVE-2026-0001"}], + "artifact": {"name": "krb5-libs", "version": "1.21.1-10.el9_8.1", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "krb5"}]}}, + {"vulnerability": {"id": "CVE-2026-0100", "severity": "High", "fix": {"versions": ["0:1.21.1-10.el9_9.3"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "krb5-libs", "version": "1.21.1-10.el9_8.1", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "krb5"}]}}, + {"vulnerability": {"id": "CVE-2026-0100", "severity": "High", "fix": {"versions": ["0:1.21.1-10.el9_9.3"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "krb5-libs", "version": "1.21.1-10.el9_8.1", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "krb5"}]}}, + {"vulnerability": {"id": "CVE-2026-0101", "severity": "Critical", "fix": {"versions": ["2:3.0-2.el10", "2:3.1-1.el10"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "newpkg", "version": "2:3.0-1.el10", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "newsrc"}]}}, + {"vulnerability": {"id": "CVE-2026-0102", "severity": "Medium", "fix": {"versions": ["0:1.3-1"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "zlib", "version": "1.2-1", "type": "rpm", "locations": [], "upstreams": []}}, + {"vulnerability": {"id": "CVE-2026-0103", "severity": "High", "fix": {"versions": [], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "zlib", "version": "1.2-1", "type": "rpm", "locations": [], "upstreams": []}}, + {"vulnerability": {"id": "GHSA-aaaa-bbbb-cccc", "severity": "High", "fix": {"versions": ["0.55.0"], "state": "fixed"}}, + "relatedVulnerabilities": [{"id": "GHSA-aaaa-bbbb-cccc"}, {"id": "CVE-2026-0005"}], + "artifact": {"name": "golang.org/x/net", "version": "v0.54.0", "type": "go-module", "locations": [{"path": "/usr/sbin/gosu"}], "upstreams": []}} + ], + "ignoredMatches": [ + {"vulnerability": {"id": "CVE-2026-0999", "severity": "High", "fix": {"versions": ["1"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "kernel-headers", "version": "5", "type": "rpm", "locations": [], "upstreams": []}} + ] +} diff --git a/backend/internal/model/types.go b/backend/internal/model/types.go index c6c8494..733b5f6 100644 --- a/backend/internal/model/types.go +++ b/backend/internal/model/types.go @@ -57,18 +57,19 @@ type Target struct { } type CveFinding struct { - ID string `json:"id"` - PkgName string `json:"pkg"` - InstalledVersion string `json:"installed"` - FixedVersion string `json:"fixed"` - Severity string `json:"severity"` - Title string `json:"title"` - Class string `json:"class,omitempty"` // trivy result Class: "os-pkgs" | "lang-pkgs" - Type string `json:"pkg_type,omitempty"` // trivy result Type: "redhat", "gobinary", "python-pkg", … - Path string `json:"path,omitempty"` // binary (gobinary) or package file (other lang-pkgs); empty for RPMs - Src string `json:"src,omitempty"` // RPM source package name - Vendor string `json:"vendor,omitempty"` // RPM vendor (trivy "Maintainer") - Owner string `json:"owner,omitempty"` // source package of the RPM that ships Path, when resolved + ID string `json:"id"` + PkgName string `json:"pkg"` + InstalledVersion string `json:"installed"` + FixedVersion string `json:"fixed"` + Severity string `json:"severity"` + Title string `json:"title"` + Class string `json:"class,omitempty"` // trivy result Class: "os-pkgs" | "lang-pkgs" + Type string `json:"pkg_type,omitempty"` // trivy result Type: "redhat", "gobinary", "python-pkg", … + Path string `json:"path,omitempty"` // binary (gobinary) or package file (other lang-pkgs); empty for RPMs + Src string `json:"src,omitempty"` // RPM source package name + Vendor string `json:"vendor,omitempty"` // RPM vendor (trivy "Maintainer") + Owner string `json:"owner,omitempty"` // source package of the RPM that ships Path, when resolved + Scanners []string `json:"scanners,omitempty"` // "trivy", "grype" or both } type CveScan struct { diff --git a/frontend/src/types/api.ts b/frontend/src/types/api.ts index f4ef531..32cf11e 100644 --- a/frontend/src/types/api.ts +++ b/frontend/src/types/api.ts @@ -56,6 +56,7 @@ export interface CveFinding { fixed: string severity: 'HIGH' | 'CRITICAL' title: string + scanners?: string[] } export interface CveScan { From 47cf8866c13555ff1a7247bc6acf9ec5bfee70e0 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 17 Nov 2026 13:09:40 +0000 Subject: [PATCH 2/6] feat(cve): run grype alongside trivy and merge findings Signed-off-by: Ricardo Dias --- backend/internal/cve/grype.go | 63 ++++++++- backend/internal/cve/grype_test.go | 49 +++++++ backend/internal/cve/rescan_test.go | 3 + backend/internal/cve/scanner.go | 69 +++++---- backend/internal/cve/scanner_test.go | 171 ++++++++++++++++++++++- backend/internal/cve/testdata/grype.json | 3 + 6 files changed, 325 insertions(+), 33 deletions(-) diff --git a/backend/internal/cve/grype.go b/backend/internal/cve/grype.go index 86c57c2..0a33880 100644 --- a/backend/internal/cve/grype.go +++ b/backend/internal/cve/grype.go @@ -1,8 +1,11 @@ package cve import ( + "context" "encoding/json" + "errors" "fmt" + "os/exec" "strings" "github.com/percona/obs-dashboard/internal/model" @@ -48,7 +51,16 @@ func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) { var findings []model.CveFinding for _, m := range out.Matches { sev := strings.ToUpper(m.Vulnerability.Severity) - if (sev != "HIGH" && sev != "CRITICAL") || len(m.Vulnerability.Fix.Versions) == 0 { + if sev != "HIGH" && sev != "CRITICAL" { + continue + } + var fixed []string + for _, v := range m.Vulnerability.Fix.Versions { + if v = strings.TrimPrefix(v, "0:"); v != "" { + fixed = append(fixed, v) + } + } + if len(fixed) == 0 { continue } id := m.Vulnerability.ID @@ -60,10 +72,6 @@ func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) { } } } - fixed := make([]string, len(m.Vulnerability.Fix.Versions)) - for i, v := range m.Vulnerability.Fix.Versions { - fixed[i] = strings.TrimPrefix(v, "0:") - } a := m.Artifact f := model.CveFinding{ ID: id, @@ -113,3 +121,48 @@ func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) { } return findings, nil } + +// mergeFindings returns trivy's findings followed by the grype findings trivy +// lacks. Key: (ID, PkgName, Path). On a duplicate the trivy finding is kept and +// "grype" is appended to its Scanners. +func mergeFindings(trivy, grype []model.CveFinding) []model.CveFinding { + type key struct{ id, pkg, path string } + merged := append([]model.CveFinding(nil), trivy...) + idx := make(map[key]int, len(merged)) + for i, f := range merged { + idx[key{f.ID, f.PkgName, f.Path}] = i + } + for _, f := range grype { + k := key{f.ID, f.PkgName, f.Path} + if i, ok := idx[k]; ok { + merged[i].Scanners = append(append([]string(nil), merged[i].Scanners...), "grype") + continue + } + idx[k] = len(merged) + merged = append(merged, f) + } + return merged +} + +// runGrype scans imageRef and returns its findings, enriched from rpms. +func (s *Scanner) runGrype(ctx context.Context, imageRef, platform string, rpms []rpmInfo) ([]model.CveFinding, error) { + s.trivyMu.Lock() + out, err := s.execFn(ctx, "grype", + "registry:"+imageRef, + "--platform", "linux/"+platform, + "--only-fixed", + "--by-cve", + "-o", "json", + "-q", + ) + s.trivyMu.Unlock() + if err != nil { + stderr := "" + var exitErr *exec.ExitError + if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 { + stderr = ": " + strings.TrimSpace(string(exitErr.Stderr)) + } + return nil, fmt.Errorf("grype: %w%s", err, stderr) + } + return parseGrypeOutput(out, rpms) +} diff --git a/backend/internal/cve/grype_test.go b/backend/internal/cve/grype_test.go index 1d50598..8df461e 100644 --- a/backend/internal/cve/grype_test.go +++ b/backend/internal/cve/grype_test.go @@ -40,3 +40,52 @@ func TestParseGrypeOutputInvalidJSON(t *testing.T) { t.Fatalf("err = %v", err) } } + +func TestParseGrypeOutputTypeMapping(t *testing.T) { + data := []byte(`{"matches":[ +{"vulnerability":{"id":"CVE-2026-0200","severity":"High","fix":{"versions":["2.0"]}},"artifact":{"name":"requests","version":"1.0","type":"python","locations":[{"path":"/opt/app/requests-1.0.dist-info/METADATA"}]}}, +{"vulnerability":{"id":"CVE-2026-0201","severity":"Critical","fix":{"versions":["4.17.21"]}},"artifact":{"name":"lodash","version":"4.0.0","type":"npm","locations":[{"path":"/app/node_modules/lodash/package.json"}]}} +]}`) + got, err := parseGrypeOutput(data, nil) + if err != nil { + t.Fatal(err) + } + g := []string{"grype"} + want := []model.CveFinding{ + {ID: "CVE-2026-0200", PkgName: "requests", InstalledVersion: "1.0", FixedVersion: "2.0", Severity: "HIGH", Class: "lang-pkgs", Type: "python-pkg", Path: "opt/app/requests-1.0.dist-info/METADATA", Scanners: g}, + {ID: "CVE-2026-0201", PkgName: "lodash", InstalledVersion: "4.0.0", FixedVersion: "4.17.21", Severity: "CRITICAL", Class: "lang-pkgs", Type: "npm", Path: "app/node_modules/lodash/package.json", Scanners: g}, + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("got %+v\nwant %+v", got, want) + } +} + +func TestMergeFindings(t *testing.T) { + trivy := []model.CveFinding{ + {ID: "CVE-1", PkgName: "a", Title: "t", Scanners: []string{"trivy"}}, + {ID: "CVE-2", PkgName: "x/net", Path: "usr/bin/one", Scanners: []string{"trivy"}}, + } + grype := []model.CveFinding{ + {ID: "CVE-1", PkgName: "a", Scanners: []string{"grype"}}, + {ID: "CVE-2", PkgName: "x/net", Path: "usr/bin/two", Scanners: []string{"grype"}}, + {ID: "CVE-1", PkgName: "b", Scanners: []string{"grype"}}, + } + got := mergeFindings(trivy, grype) + if len(got) != 4 { + t.Fatalf("len = %d, want 4", len(got)) + } + if got[0].ID != "CVE-1" || got[0].PkgName != "a" || got[0].Title != "t" || !reflect.DeepEqual(got[0].Scanners, []string{"trivy", "grype"}) { + t.Errorf("got[0] = %+v", got[0]) + } + if !reflect.DeepEqual(got[1].Scanners, []string{"trivy"}) { + t.Errorf("got[1] = %+v", got[1]) + } + for _, f := range got[2:] { + if !reflect.DeepEqual(f.Scanners, []string{"grype"}) { + t.Errorf("grype-only finding = %+v", f) + } + } + if got[2].Path != "usr/bin/two" || got[3].PkgName != "b" { + t.Errorf("order: %+v %+v", got[2], got[3]) + } +} diff --git a/backend/internal/cve/rescan_test.go b/backend/internal/cve/rescan_test.go index cfb1512..71b3335 100644 --- a/backend/internal/cve/rescan_test.go +++ b/backend/internal/cve/rescan_test.go @@ -92,6 +92,9 @@ func TestRescanVulnerableSkipsPendingUntilScanned(t *testing.T) { release := make(chan struct{}) scanned := make(chan struct{}, 1) s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { + if name == "grype" { + return []byte(grypeEmptyJSON), nil + } <-release select { case scanned <- struct{}{}: diff --git a/backend/internal/cve/scanner.go b/backend/internal/cve/scanner.go index 53f8d9f..2402d69 100644 --- a/backend/internal/cve/scanner.go +++ b/backend/internal/cve/scanner.go @@ -297,26 +297,27 @@ func (s *Scanner) scanPackage(ctx context.Context, req ScanRequest) { }) slog.Info("cve: scanning", "pkg", req.Package, "arch", target.Arch, "image", imageRef) - scan, err := s.runTrivy(ctx, imageRef, platform, target.Repo, target.Arch) + scan, rpms, err := s.runTrivy(ctx, imageRef, platform, target.Repo, target.Arch) if err != nil { slog.Warn("cve: trivy failed", "pkg", req.Package, "arch", target.Arch, "err", err) - s.appendEvent(&model.Event{ - ID: "evt_" + ulid.Make().String(), - Type: model.EventCVEScanFailed, - Tags: req.Tags, - Project: req.Project, - Package: req.Package, - Repo: target.Repo, - Arch: target.Arch, - What: "CVE scan failed", - Why: err.Error(), - Version: req.PrimaryTag, - URL: obsURL, - At: time.Now().UTC(), - Instance: target.Instance, - }) + s.appendScanFailed(req, target, obsURL, err) continue } + grypeFindings, err := s.runGrype(ctx, imageRef, platform, rpms) + if err != nil { + slog.Warn("cve: grype failed", "pkg", req.Package, "arch", target.Arch, "err", err) + s.appendScanFailed(req, target, obsURL, err) + continue + } + scan.Findings = mergeFindings(scan.Findings, grypeFindings) + for _, f := range scan.Findings { + switch f.Severity { + case "CRITICAL": + scan.CriticalCount++ + case "HIGH": + scan.HighCount++ + } + } if err := store.UpsertCveScan(s.db, req.Project, req.Package, scan); err != nil { slog.Error("cve: upsert scan", "pkg", req.Package, "err", err) @@ -353,7 +354,26 @@ func (s *Scanner) scanPackage(ctx context.Context, req ScanRequest) { s.hub.Notify(hubpkg.PackageUpdate(pkg)) } -func (s *Scanner) runTrivy(ctx context.Context, imageRef, platform, repo, arch string) (model.CveScan, error) { +// appendScanFailed records a CVEScanFailed event for target. +func (s *Scanner) appendScanFailed(req ScanRequest, target model.Target, obsURL string, err error) { + s.appendEvent(&model.Event{ + ID: "evt_" + ulid.Make().String(), + Type: model.EventCVEScanFailed, + Tags: req.Tags, + Project: req.Project, + Package: req.Package, + Repo: target.Repo, + Arch: target.Arch, + What: "CVE scan failed", + Why: err.Error(), + Version: req.PrimaryTag, + URL: obsURL, + At: time.Now().UTC(), + Instance: target.Instance, + }) +} + +func (s *Scanner) runTrivy(ctx context.Context, imageRef, platform, repo, arch string) (model.CveScan, []rpmInfo, error) { s.trivyMu.Lock() out, err := s.execFn(ctx, "trivy", "image", @@ -378,7 +398,7 @@ func (s *Scanner) runTrivy(ctx context.Context, imageRef, platform, repo, arch s if exitErr != nil && len(exitErr.Stderr) > 0 { stderr = ": " + strings.TrimSpace(string(exitErr.Stderr)) } - return model.CveScan{}, fmt.Errorf("trivy: %w%s", err, stderr) + return model.CveScan{}, nil, fmt.Errorf("trivy: %w%s", err, stderr) } return parseTrivyOutput(out, imageRef, repo, arch) } @@ -409,10 +429,10 @@ type trivyOutput struct { } `json:"Results"` } -func parseTrivyOutput(data []byte, imageRef, repo, arch string) (model.CveScan, error) { +func parseTrivyOutput(data []byte, imageRef, repo, arch string) (model.CveScan, []rpmInfo, error) { var raw trivyOutput if err := json.Unmarshal(data, &raw); err != nil { - return model.CveScan{}, fmt.Errorf("parse trivy JSON: %w", err) + return model.CveScan{}, nil, fmt.Errorf("parse trivy JSON: %w", err) } scan := model.CveScan{ Repo: repo, @@ -448,6 +468,7 @@ func parseTrivyOutput(data []byte, imageRef, repo, arch string) (model.CveScan, Title: v.Title, Class: result.Class, Type: result.Type, + Scanners: []string{"trivy"}, } switch { case result.Class == "os-pkgs": @@ -475,15 +496,9 @@ func parseTrivyOutput(data []byte, imageRef, repo, arch string) (model.CveScan, } } scan.Findings = append(scan.Findings, f) - switch v.Severity { - case "CRITICAL": - scan.CriticalCount++ - case "HIGH": - scan.HighCount++ - } } } - return scan, nil + return scan, rpms, nil } func (s *Scanner) appendEvent(evt *model.Event) { diff --git a/backend/internal/cve/scanner_test.go b/backend/internal/cve/scanner_test.go index 859ce31..f38a490 100644 --- a/backend/internal/cve/scanner_test.go +++ b/backend/internal/cve/scanner_test.go @@ -2,8 +2,11 @@ package cve_test import ( "context" + "errors" "os" + "os/exec" "slices" + "strings" "sync" "testing" "time" @@ -14,6 +17,7 @@ import ( "github.com/percona/obs-dashboard/internal/store" ) +const grypeEmptyJSON = `{"matches":[]}` const trivyCleanJSON = `{"SchemaVersion":2,"Results":[]}` const trivyVulnJSON = `{"SchemaVersion":2,"Results":[{"Vulnerabilities":[ {"VulnerabilityID":"CVE-2024-1","PkgName":"libssl3","InstalledVersion":"3.1.0","FixedVersion":"3.1.1","Severity":"CRITICAL","Title":"overflow"}, @@ -69,7 +73,10 @@ func TestScannerParsesTrivyVulns(t *testing.T) { execDone := make(chan struct{}) s := cve.NewScanner(db, h, 1, cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { - defer close(execDone) + if name == "grype" { + defer close(execDone) + return []byte(grypeEmptyJSON), nil + } return []byte(trivyVulnJSON), nil })) ctx, cancel := context.WithCancel(context.Background()) @@ -113,6 +120,9 @@ func TestScanPackageBuildsPerTargetImageRef(t *testing.T) { var mu sync.Mutex var refs []string s := cve.NewScanner(db, h, 1, cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { + if name == "grype" { + return []byte(grypeEmptyJSON), nil + } mu.Lock() refs = append(refs, args[len(args)-1]) // trivy image ref is the last arg mu.Unlock() @@ -203,6 +213,9 @@ func TestScannerUsesTargetInstanceURLs(t *testing.T) { defer db.Close() var gotRef string exec := func(ctx context.Context, name string, args ...string) ([]byte, error) { + if name == "grype" { + return []byte(grypeEmptyJSON), nil + } gotRef = args[len(args)-1] return []byte(`{"Results":[]}`), nil } @@ -227,6 +240,9 @@ func TestParseTrivyOutputClassificationFields(t *testing.T) { } defer db.Close() s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { + if name == "grype" { + return []byte(grypeEmptyJSON), nil + } return data, nil })) s.ScanNow(context.Background(), cve.ScanRequest{ @@ -278,6 +294,9 @@ func TestRunTrivyListsAllPackages(t *testing.T) { defer db.Close() var args []string s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(func(ctx context.Context, name string, a ...string) ([]byte, error) { + if name == "grype" { + return []byte(grypeEmptyJSON), nil + } args = a return []byte(trivyCleanJSON), nil })) @@ -300,6 +319,9 @@ func TestParseTrivyOutputEmptyPkgIDStaysUnclassified(t *testing.T) { } defer db.Close() s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { + if name == "grype" { + return []byte(grypeEmptyJSON), nil + } return data, nil })) s.ScanNow(context.Background(), cve.ScanRequest{ @@ -315,3 +337,150 @@ func TestParseTrivyOutputEmptyPkgIDStaysUnclassified(t *testing.T) { t.Errorf("vendor=%q src=%q, want both empty", f.Vendor, f.Src) } } + +// scannerExec returns an ExecFn serving trivyOut for "trivy" and grypeOut for +// "grype", recording each call's name and args. +func scannerExec(trivyOut, grypeOut []byte, calls *[][]string) cve.ExecFn { + return func(ctx context.Context, name string, args ...string) ([]byte, error) { + *calls = append(*calls, append([]string{name}, args...)) + if name == "grype" { + return grypeOut, nil + } + return trivyOut, nil + } +} + +func readFixtures(t *testing.T) (trivy, grype []byte) { + t.Helper() + trivy, err := os.ReadFile("testdata/trivy-listall.json") + if err != nil { + t.Fatal(err) + } + grype, err = os.ReadFile("testdata/grype.json") + if err != nil { + t.Fatal(err) + } + return trivy, grype +} + +var mergeReq = cve.ScanRequest{ + Project: "proj", Package: "mypkg", PrimaryTag: "1.0", + Targets: []model.Target{{Repo: "images", Arch: "x86_64", State: "succeeded"}}, +} + +func TestScanMergesTrivyAndGrype(t *testing.T) { + trivyOut, grypeOut := readFixtures(t) + db, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + var calls [][]string + s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(scannerExec(trivyOut, grypeOut, &calls)), cve.WithURLs(stubURLs{})) + req := mergeReq + req.Targets = []model.Target{{Repo: "images", Arch: "x86_64", State: "succeeded", Instance: "percona"}} + s.ScanNow(context.Background(), req) + + if len(calls) != 2 || calls[0][0] != "trivy" { + t.Fatalf("calls = %v", calls) + } + wantGrype := []string{"grype", "registry:reg.percona/proj/images/mypkg:1.0", "--platform", "linux/amd64", "--only-fixed", "--by-cve", "-o", "json", "-q"} + if !slices.Equal(calls[1], wantGrype) { + t.Errorf("grype call = %v, want %v", calls[1], wantGrype) + } + scans, err := store.QueryCveScans(db, "proj", "mypkg") + if err != nil || len(scans) != 1 { + t.Fatalf("scans=%d err=%v", len(scans), err) + } + scan := scans[0] + if len(scan.Findings) != 10 { + t.Fatalf("findings = %d, want 10", len(scan.Findings)) + } + if scan.CriticalCount != 3 || scan.HighCount != 7 { + t.Errorf("counts: critical=%d high=%d, want 3/7", scan.CriticalCount, scan.HighCount) + } + byID := map[string]model.CveFinding{} + for _, f := range scan.Findings { + byID[f.ID] = f + } + both, trivyOnly, grypeOnly := []string{"trivy", "grype"}, []string{"trivy"}, []string{"grype"} + if f := byID["CVE-2026-0001"]; !slices.Equal(f.Scanners, both) || f.Title != "krb5 flaw" { + t.Errorf("CVE-2026-0001 = %+v", f) + } + if f := byID["CVE-2026-0005"]; !slices.Equal(f.Scanners, both) || f.Path != "usr/sbin/gosu" { + t.Errorf("CVE-2026-0005 = %+v", f) + } + if f := byID["CVE-2026-0100"]; !slices.Equal(f.Scanners, grypeOnly) || f.Src != "krb5" || f.Vendor != "Red Hat, Inc." { + t.Errorf("CVE-2026-0100 = %+v", f) + } + if f := byID["CVE-2026-0101"]; !slices.Equal(f.Scanners, grypeOnly) { + t.Errorf("CVE-2026-0101 = %+v", f) + } + if f := byID["CVE-2026-0007"]; !slices.Equal(f.Scanners, trivyOnly) { + t.Errorf("CVE-2026-0007 = %+v", f) + } +} + +func TestScanFailsWhenGrypeFails(t *testing.T) { + trivyOut, grypeOut := readFixtures(t) + db, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + var calls [][]string + s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(scannerExec(trivyOut, grypeOut, &calls))) + s.ScanNow(context.Background(), mergeReq) + first, err := store.QueryCveScans(db, "proj", "mypkg") + if err != nil || len(first) != 1 { + t.Fatalf("first scans=%d err=%v", len(first), err) + } + + failingGrype := cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { + if name == "grype" { + return exec.Command("sh", "-c", "echo db update failed >&2; exit 1").Output() + } + return []byte(trivyCleanJSON), nil + }) + time.Sleep(5 * time.Millisecond) + failingGrype(s) // swap the exec fake on the same scanner/DB + s.ScanNow(context.Background(), mergeReq) + + after, err := store.QueryCveScans(db, "proj", "mypkg") + if err != nil || len(after) != 1 { + t.Fatalf("after scans=%d err=%v", len(after), err) + } + if !after[0].ScannedAt.Equal(first[0].ScannedAt) || len(after[0].Findings) != len(first[0].Findings) { + t.Errorf("stored scan changed: %v/%d -> %v/%d", first[0].ScannedAt, len(first[0].Findings), after[0].ScannedAt, len(after[0].Findings)) + } + events, err := store.QueryEvents(db, "proj", time.Now().Add(-time.Hour), time.Now().Add(time.Hour)) + if err != nil { + t.Fatal(err) + } + var failed []*model.Event + for _, e := range events { + if e.Type == model.EventCVEScanFailed { + failed = append(failed, e) + } + } + if len(failed) != 1 || !strings.HasPrefix(failed[0].Why, "grype:") || !strings.Contains(failed[0].Why, "db update failed") { + t.Fatalf("failed events = %+v", failed) + } +} + +func TestScanSkipsGrypeWhenTrivyFails(t *testing.T) { + db, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + var names []string + s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(func(ctx context.Context, name string, args ...string) ([]byte, error) { + names = append(names, name) + return nil, errors.New("trivy boom") + })) + s.ScanNow(context.Background(), mergeReq) + if !slices.Equal(names, []string{"trivy"}) { + t.Errorf("calls = %v, want only trivy", names) + } +} diff --git a/backend/internal/cve/testdata/grype.json b/backend/internal/cve/testdata/grype.json index e90f9fd..dad5998 100644 --- a/backend/internal/cve/testdata/grype.json +++ b/backend/internal/cve/testdata/grype.json @@ -18,6 +18,9 @@ {"vulnerability": {"id": "CVE-2026-0103", "severity": "High", "fix": {"versions": [], "state": "fixed"}}, "relatedVulnerabilities": [], "artifact": {"name": "zlib", "version": "1.2-1", "type": "rpm", "locations": [], "upstreams": []}}, + {"vulnerability": {"id": "CVE-2026-0105", "severity": "High", "fix": {"versions": [""], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "zlib", "version": "1.2-1", "type": "rpm", "locations": [], "upstreams": []}}, {"vulnerability": {"id": "GHSA-aaaa-bbbb-cccc", "severity": "High", "fix": {"versions": ["0.55.0"], "state": "fixed"}}, "relatedVulnerabilities": [{"id": "GHSA-aaaa-bbbb-cccc"}, {"id": "CVE-2026-0005"}], "artifact": {"name": "golang.org/x/net", "version": "v0.54.0", "type": "go-module", "locations": [{"path": "/usr/sbin/gosu"}], "upstreams": []}} From 639dddaa8337c1f4c869388d9cdf49a1ed85d02b Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 17 Nov 2026 13:12:32 +0000 Subject: [PATCH 3/6] build: ship grype in the runtime image Signed-off-by: Ricardo Dias --- Dockerfile | 2 + backend/internal/model/types.go | 6 +- .../plans/2026-11-17-grype-alongside-trivy.md | 313 ++++++++++++++++++ ...-11-17-grype-alongside-trivy.md.tasks.json | 30 ++ ...2026-11-17-grype-alongside-trivy-design.md | 141 ++++++++ 5 files changed, 489 insertions(+), 3 deletions(-) create mode 100644 docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md create mode 100644 docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json create mode 100644 docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md diff --git a/Dockerfile b/Dockerfile index 4c607e2..1da3875 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,10 +17,12 @@ RUN CGO_ENABLED=0 go build -ldflags "-X github.com/percona/obs-dashboard/interna # Stage 3: minimal runtime image FROM ghcr.io/aquasecurity/trivy:latest AS trivy +FROM ghcr.io/anchore/grype:latest AS grype FROM alpine:3.20 RUN apk add --no-cache ca-certificates COPY --from=trivy /usr/local/bin/trivy /usr/local/bin/trivy +COPY --from=grype /grype /usr/local/bin/grype COPY --from=backend-build /obsboard /obsboard COPY --from=frontend-build /app/dist /frontend ENV FRONTEND_DIR=/frontend diff --git a/backend/internal/model/types.go b/backend/internal/model/types.go index 733b5f6..256665e 100644 --- a/backend/internal/model/types.go +++ b/backend/internal/model/types.go @@ -63,11 +63,11 @@ type CveFinding struct { FixedVersion string `json:"fixed"` Severity string `json:"severity"` Title string `json:"title"` - Class string `json:"class,omitempty"` // trivy result Class: "os-pkgs" | "lang-pkgs" - Type string `json:"pkg_type,omitempty"` // trivy result Type: "redhat", "gobinary", "python-pkg", … + Class string `json:"class,omitempty"` // "os-pkgs" | "lang-pkgs" (trivy's result Class; mapped for grype) + Type string `json:"pkg_type,omitempty"` // "redhat", "gobinary", "python-pkg", … (trivy's result Type; mapped for grype) Path string `json:"path,omitempty"` // binary (gobinary) or package file (other lang-pkgs); empty for RPMs Src string `json:"src,omitempty"` // RPM source package name - Vendor string `json:"vendor,omitempty"` // RPM vendor (trivy "Maintainer") + Vendor string `json:"vendor,omitempty"` // RPM vendor (trivy "Maintainer"; grype's equivalent for grype) Owner string `json:"owner,omitempty"` // source package of the RPM that ships Path, when resolved Scanners []string `json:"scanners,omitempty"` // "trivy", "grype" or both } diff --git a/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md b/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md new file mode 100644 index 0000000..949bb7d --- /dev/null +++ b/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md @@ -0,0 +1,313 @@ +# Grype alongside Trivy Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers-extended-cc:subagent-driven-development (recommended) or superpowers-extended-cc:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** The CVE scanner runs Grype after Trivy on every image and stores one merged list of findings, so RHEL 10 images stop showing as falsely clean. + +**Architecture:** A new `grype.go` in `backend/internal/cve` turns Grype JSON into `model.CveFinding`s, enriched from the RPM list Trivy already returns. `scanner.go` runs Grype after Trivy under the same lock and merges the two lists, Trivy winning on duplicates. The `Dockerfile` ships the Grype binary next to Trivy. + +**Tech Stack:** Go (backend, `go test`), SQLite store (findings are a JSON column, no migration), Docker multi-stage build, TypeScript types in `frontend/src/types/api.ts`. + +**Spec:** `docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md` + +## Global Constraints + +- Merge key is exactly `(ID, PkgName, Path)`. `Path` is empty for RPM findings and has no leading `/`. +- Scanner names in `CveFinding.Scanners` are the literal strings `"trivy"` and `"grype"`, in that order when both. +- Severities stored are upper-case `HIGH` / `CRITICAL` only. Every stored finding has a non-empty `FixedVersion`. +- Grype is invoked as: `grype registry: --platform linux/ --only-fixed --by-cve -o json -q`. +- No UI change. No new config. No DB migration. +- Commits: `git commit -s`, no `Co-Authored-By` trailer (repo `CLAUDE.md`). +- Run Go tests from `backend/`: `go test ./internal/cve/ ./internal/model/ ./internal/store/`. + +**User decisions (already made):** +- "plan running grype alongside trivy and merging by CVE ID". +- Fail-if-either-fails: if either scanner fails the scan counts as failed and the previous result stays. +- Each finding records which scanner(s) reported it; stored, not shown in the UI. +- No EPSS or other new UI. +- "looks good except pinning the trivy version. Keep trivy to latest" — the `Dockerfile` keeps `ghcr.io/aquasecurity/trivy:latest`. + +## Review Focus + +1. Grype reports the same CVE for the same package more than once (it does, per match detail) → stored once. Test in Task 1. +2. Grype's primary ID is a GHSA even with `--by-cve` → the first `relatedVulnerabilities` ID starting with `CVE-` is used, so it merges with Trivy's CVE. Test in Task 1. +3. A Grype RPM that is not in Trivy's package list → finding kept with `Src` from Grype's upstream and empty `Vendor`, no panic. Test in Task 1. +4. The same library CVE in two different Go binaries → two findings stay two after the merge. Test in Task 2. +5. Grype exits non-zero or prints non-JSON → `CVEScanFailed` with Grype's stderr, and the previously stored scan is untouched. Test in Task 2. + +--- + +### Task 1: Parse Grype output into findings + +**Goal:** `parseGrypeOutput` turns Grype JSON into enriched, filtered, de-duplicated `model.CveFinding`s, and findings can carry scanner names. + +**Files:** +- Create: `backend/internal/cve/grype.go` +- Create: `backend/internal/cve/grype_test.go` (`package cve`, like `owner_test.go`) +- Create: `backend/internal/cve/testdata/grype.json` +- Modify: `backend/internal/model/types.go:59-72` (`CveFinding`) +- Modify: `frontend/src/types/api.ts:52-59` (`CveFinding`) + +**Acceptance Criteria:** +- [ ] `model.CveFinding` has `Scanners []string` with tag `json:"scanners,omitempty"`; the TS interface has `scanners?: string[]`. +- [ ] `parseGrypeOutput` on the fixture returns exactly 4 findings with the fields in Step 2. +- [ ] Medium findings, findings with no fix version, and repeated matches are not returned. +- [ ] Every returned finding has `Scanners == []string{"grype"}`. + +**Verify:** `cd backend && go test ./internal/cve/ -run 'Grype' -v` → PASS + +**Steps:** + +- [ ] **Step 1: Write the fixture `backend/internal/cve/testdata/grype.json`** + +It pairs with `testdata/trivy-listall.json` (RPMs `krb5-libs`/src `krb5`/"Red Hat, Inc." and `gosu`/"obs://…" exist there). + +```json +{ + "matches": [ + {"vulnerability": {"id": "CVE-2026-0001", "severity": "High", "fix": {"versions": ["0:1.21.1-10.el9_9.2"], "state": "fixed"}}, + "relatedVulnerabilities": [{"id": "CVE-2026-0001"}], + "artifact": {"name": "krb5-libs", "version": "1.21.1-10.el9_8.1", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "krb5"}]}}, + {"vulnerability": {"id": "CVE-2026-0100", "severity": "High", "fix": {"versions": ["0:1.21.1-10.el9_9.3"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "krb5-libs", "version": "1.21.1-10.el9_8.1", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "krb5"}]}}, + {"vulnerability": {"id": "CVE-2026-0100", "severity": "High", "fix": {"versions": ["0:1.21.1-10.el9_9.3"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "krb5-libs", "version": "1.21.1-10.el9_8.1", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "krb5"}]}}, + {"vulnerability": {"id": "CVE-2026-0101", "severity": "Critical", "fix": {"versions": ["2:3.0-2.el10", "2:3.1-1.el10"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "newpkg", "version": "2:3.0-1.el10", "type": "rpm", "locations": [{"path": "/var/lib/rpm/rpmdb.sqlite"}], "upstreams": [{"name": "newsrc"}]}}, + {"vulnerability": {"id": "CVE-2026-0102", "severity": "Medium", "fix": {"versions": ["0:1.3-1"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "zlib", "version": "1.2-1", "type": "rpm", "locations": [], "upstreams": []}}, + {"vulnerability": {"id": "CVE-2026-0103", "severity": "High", "fix": {"versions": [], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "zlib", "version": "1.2-1", "type": "rpm", "locations": [], "upstreams": []}}, + {"vulnerability": {"id": "GHSA-aaaa-bbbb-cccc", "severity": "High", "fix": {"versions": ["0.55.0"], "state": "fixed"}}, + "relatedVulnerabilities": [{"id": "GHSA-aaaa-bbbb-cccc"}, {"id": "CVE-2026-0005"}], + "artifact": {"name": "golang.org/x/net", "version": "v0.54.0", "type": "go-module", "locations": [{"path": "/usr/sbin/gosu"}], "upstreams": []}} + ], + "ignoredMatches": [ + {"vulnerability": {"id": "CVE-2026-0999", "severity": "High", "fix": {"versions": ["1"], "state": "fixed"}}, + "relatedVulnerabilities": [], + "artifact": {"name": "kernel-headers", "version": "5", "type": "rpm", "locations": [], "upstreams": []}} + ] +} +``` + +- [ ] **Step 2: Write the failing test `TestParseGrypeOutput` in `grype_test.go`** + +Call with these RPMs (copied from the Trivy fixture): + +```go +rpms := []rpmInfo{ + {Name: "krb5-libs", Version: "1.21.1", Src: "krb5", Vendor: "Red Hat, Inc."}, + {Name: "gosu", Version: "1.17", Src: "gosu", Vendor: "obs://obs.pg.labs.percona.com/isv:percona:ppg:common:deps"}, +} +``` + +Assert `len(got) == 4`, in this order, with exactly these fields (unlisted string fields empty, `Scanners` always `["grype"]`): + +| ID | PkgName | InstalledVersion | FixedVersion | Severity | Class | Type | Path | Src | Vendor | Owner | +|---|---|---|---|---|---|---|---|---|---|---| +| CVE-2026-0001 | krb5-libs | 1.21.1-10.el9_8.1 | 1.21.1-10.el9_9.2 | HIGH | os-pkgs | redhat | | krb5 | Red Hat, Inc. | | +| CVE-2026-0100 | krb5-libs | 1.21.1-10.el9_8.1 | 1.21.1-10.el9_9.3 | HIGH | os-pkgs | redhat | | krb5 | Red Hat, Inc. | | +| CVE-2026-0101 | newpkg | 2:3.0-1.el10 | 2:3.0-2.el10, 2:3.1-1.el10 | CRITICAL | os-pkgs | redhat | | newsrc | | | +| CVE-2026-0005 | golang.org/x/net | v0.54.0 | 0.55.0 | HIGH | lang-pkgs | gobinary | usr/sbin/gosu | | | gosu | + +Add `TestParseGrypeOutputInvalidJSON`: `parseGrypeOutput([]byte("not json"), nil)` returns a non-nil error whose text starts with `parse grype JSON`. + +- [ ] **Step 3: Run to verify it fails** + +Run: `cd backend && go test ./internal/cve/ -run Grype -v` +Expected: FAIL to compile, `undefined: parseGrypeOutput` + +- [ ] **Step 4: Add `Scanners` to the model and the TS type** + +`model.CveFinding`: `Scanners []string \`json:"scanners,omitempty"\` // "trivy", "grype" or both`. TS: `scanners?: string[]`. + +- [ ] **Step 5: Implement in `grype.go`** + +```go +// parseGrypeOutput converts `grype -o json` output into findings. rpms is the +// RPM list from the Trivy scan of the same image, used for Src/Vendor/Owner. +func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) +``` + +Rules, all pinned by the test: +- Read only `matches`; never `ignoredMatches`. +- Keep a match only if `severity` is `High` or `Critical` (case-insensitive) and `fix.versions` is non-empty. +- `ID`: `vulnerability.id`; if it does not start with `CVE-`, the first `relatedVulnerabilities[].id` that does; otherwise the original ID. +- `FixedVersion`: versions joined with `", "`, each with a leading `0:` removed (other epochs kept). +- Type mapping: `rpm` → `os-pkgs`/`redhat`; `go-module` → `lang-pkgs`/`gobinary`; `python` → `lang-pkgs`/`python-pkg`; anything else → `lang-pkgs`/the Grype type unchanged. +- `Path`: empty for `rpm`; otherwise `locations[0].path` with the leading `/` trimmed (empty if no locations). +- RPM enrichment: first `rpms` entry with the same `Name` gives `Src` and `Vendor`; if none, `Src = upstreams[0].name` (if any), `Vendor` empty. +- `Owner`: `goBinaryOwner(Path, rpms)` for `gobinary`, `pythonOwner(PkgName, InstalledVersion, rpms)` for `python-pkg` (both in `owner.go`). +- De-duplicate on `(ID, PkgName, Path)`, keeping the first, preserving input order. +- JSON error: `fmt.Errorf("parse grype JSON: %w", err)`. + +- [ ] **Step 6: Run to verify it passes** + +Run: `cd backend && go test ./internal/cve/ ./internal/model/ -v -run 'Grype|Owner|Norm'` → PASS. Then `cd frontend && npx tsc --noEmit` (or the repo's type-check script in `package.json`) → no errors. + +- [ ] **Step 7: Commit** + +```bash +git add backend/internal/cve/grype.go backend/internal/cve/grype_test.go backend/internal/cve/testdata/grype.json backend/internal/model/types.go frontend/src/types/api.ts +git commit -s -m "feat(cve): parse grype output into findings" +``` + +```json:metadata +{"files": ["backend/internal/cve/grype.go", "backend/internal/cve/grype_test.go", "backend/internal/cve/testdata/grype.json", "backend/internal/model/types.go", "frontend/src/types/api.ts"], "verifyCommand": "cd backend && go test ./internal/cve/ -run 'Grype' -v", "acceptanceCriteria": ["CveFinding has Scanners []string json:\"scanners,omitempty\" and the TS interface has scanners?: string[]", "parseGrypeOutput on the fixture returns exactly the 4 findings in the Step 2 table", "Medium, no-fix and repeated matches are not returned", "Every returned finding has Scanners == [\"grype\"]"], "modelTier": "standard"} +``` + +--- + +### Task 2: Run Grype in the scanner and merge + +**Goal:** Each scan runs Trivy then Grype and stores the merged findings; either failing fails the scan. + +**Files:** +- Modify: `backend/internal/cve/scanner.go` (`scanPackage` ~line 300, `runTrivy` ~356, `parseTrivyOutput` ~412) +- Modify: `backend/internal/cve/grype.go` (add `runGrype`, `mergeFindings`) +- Modify: `backend/internal/cve/scanner_test.go` (existing fakes + new tests) +- Modify: `backend/internal/cve/grype_test.go` (merge unit test) + +**Interfaces (from Task 1):** `parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error)`; `model.CveFinding.Scanners`. + +**Acceptance Criteria:** +- [ ] For one target the exec function is called exactly twice: `trivy` first, then `grype` with the argument list from Global Constraints. +- [ ] Scanning with `trivy-listall.json` + `grype.json` stores 10 findings: Trivy's 8 plus `CVE-2026-0100` and `CVE-2026-0101`; `CVE-2026-0001` and `CVE-2026-0005` (path `usr/sbin/gosu`) have `Scanners == ["trivy","grype"]` and keep Trivy's `Title`. +- [ ] `CriticalCount` / `HighCount` are computed from the merged list. +- [ ] Grype failure → one `CVEScanFailed` event whose `Why` starts with `grype:` and contains Grype's stderr; the previously stored scan row is unchanged. +- [ ] Trivy failure → Grype is not executed. +- [ ] All pre-existing tests in `backend/internal/cve` pass. + +**Verify:** `cd backend && go test ./internal/cve/ ./internal/store/ -v` → PASS + +**Steps:** + +- [ ] **Step 1: Write the failing tests** + +In `grype_test.go`, `TestMergeFindings`: + +```go +trivy := []model.CveFinding{ + {ID: "CVE-1", PkgName: "a", Title: "t", Scanners: []string{"trivy"}}, + {ID: "CVE-2", PkgName: "x/net", Path: "usr/bin/one", Scanners: []string{"trivy"}}, +} +grype := []model.CveFinding{ + {ID: "CVE-1", PkgName: "a", Scanners: []string{"grype"}}, // duplicate + {ID: "CVE-2", PkgName: "x/net", Path: "usr/bin/two", Scanners: []string{"grype"}}, // other binary + {ID: "CVE-1", PkgName: "b", Scanners: []string{"grype"}}, // other package +} +got := mergeFindings(trivy, grype) +// len 4; got[0] is CVE-1/a with Title "t" and Scanners ["trivy","grype"]; +// got[1] unchanged with Scanners ["trivy"]; got[2], got[3] are the two grype-only ones, Scanners ["grype"]. +``` + +In `scanner_test.go` add a helper fake and three tests: + +```go +// scannerExec returns an ExecFn serving trivyOut for "trivy" and grypeOut for +// "grype", recording each call's name and args. +func scannerExec(trivyOut, grypeOut []byte, calls *[][]string) cve.ExecFn +``` + +- `TestScanMergesTrivyAndGrype`: fixtures `trivy-listall.json` + `grype.json` via `ScanNow`. Assert `len(calls) == 2`, `calls[0][0] == "trivy"`, `calls[1]` equals `["grype", "registry:", "--platform", "linux/amd64", "--only-fixed", "--by-cve", "-o", "json", "-q"]`. Assert stored findings: 10 total (Trivy's 8 + `CVE-2026-0100` + `CVE-2026-0101`); `CVE-2026-0001` has `Scanners ["trivy","grype"]` and `Title "krb5 flaw"`; `CVE-2026-0005` has `Scanners ["trivy","grype"]`; `CVE-2026-0100` has `Scanners ["grype"]`, `Src "krb5"`, `Vendor "Red Hat, Inc."`; `CVE-2026-0007` has `Scanners ["trivy"]`. Assert `CriticalCount == 3`, `HighCount == 7`. +- `TestScanFailsWhenGrypeFails`: first `ScanNow` with both succeeding (stores a row); second with a fake where `grype` returns `&exec.ExitError{Stderr: []byte("db update failed")}`-style error (construct by running `exec.Command("sh", "-c", "echo db update failed >&2; exit 1").Output()` inside the fake and returning its error). Assert the stored scan's `ScannedAt` and findings are those of the first scan, and the last event for the package has `Type == model.EventCVEScanFailed` and `Why` starting with `grype:` and containing `db update failed`. +- `TestScanSkipsGrypeWhenTrivyFails`: fake returns an error for `trivy`; assert no call named `grype`. + +Update existing fakes that ignore the command name so they still mean what they say: `TestScannerParsesTrivyVulns` (its `defer close(execDone)` would run twice — close only on the `grype` call, return `{"matches":[]}` for it), `TestRunTrivyListsAllPackages` (capture `args` only when `name == "trivy"`), and any other fake in `scanner_test.go` / `rescan_test.go` that returns Trivy JSON unconditionally: return `{"matches":[]}` when `name == "grype"`. + +- [ ] **Step 2: Run to verify they fail** + +Run: `cd backend && go test ./internal/cve/ -v -run 'Merge|ScanMerges|ScanFails|ScanSkips'` +Expected: FAIL to compile, `undefined: mergeFindings` + +- [ ] **Step 3: Split the Trivy parser** + +Change `parseTrivyOutput(data []byte, imageRef, repo, arch string) (model.CveScan, error)` to also return the RPM list: `(model.CveScan, []rpmInfo, error)`. It sets `Scanners: []string{"trivy"}` on every finding. `runTrivy` returns the same triple. Counts are no longer computed there (Step 5 does it). + +- [ ] **Step 4: Add to `grype.go`** + +```go +// mergeFindings returns trivy's findings followed by the grype findings trivy +// lacks. Key: (ID, PkgName, Path). On a duplicate the trivy finding is kept and +// "grype" is appended to its Scanners. +func mergeFindings(trivy, grype []model.CveFinding) []model.CveFinding + +// runGrype scans imageRef and returns its findings, enriched from rpms. +func (s *Scanner) runGrype(ctx context.Context, imageRef, platform string, rpms []rpmInfo) ([]model.CveFinding, error) +``` + +`runGrype` holds `s.trivyMu` around the exec, as `runTrivy` does. Any exec error is a failure (Grype exits 0 with findings): return `fmt.Errorf("grype: %w%s", err, stderr)` with stderr formatted exactly as `runTrivy` does for its error. + +- [ ] **Step 5: Wire into `scanPackage`** + +After `runTrivy` succeeds, call `runGrype`. On its error, log `cve: grype failed` and emit the same `CVEScanFailed` event the Trivy branch emits (extract the duplicated event literal into a small helper used by both branches), then `continue`. On success set `scan.Findings = mergeFindings(scan.Findings, grypeFindings)` and compute `scan.CriticalCount` / `scan.HighCount` from the merged list before `store.UpsertCveScan`. + +- [ ] **Step 6: Run to verify everything passes** + +Run: `cd backend && go vet ./... && go test ./...` → PASS + +- [ ] **Step 7: Commit** + +```bash +git add backend/internal/cve +git commit -s -m "feat(cve): run grype alongside trivy and merge findings" +``` + +```json:metadata +{"files": ["backend/internal/cve/scanner.go", "backend/internal/cve/grype.go", "backend/internal/cve/scanner_test.go", "backend/internal/cve/grype_test.go", "backend/internal/cve/rescan_test.go"], "verifyCommand": "cd backend && go vet ./... && go test ./...", "acceptanceCriteria": ["Per target the exec function is called exactly twice: trivy, then grype with: registry: --platform linux/ --only-fixed --by-cve -o json -q", "Fixtures trivy-listall.json + grype.json store 10 findings, CriticalCount 3, HighCount 7; CVE-2026-0001 and CVE-2026-0005 have Scanners [trivy,grype] and Trivy's Title", "Grype failure emits CVEScanFailed with Why starting 'grype:' containing stderr, stored scan unchanged", "Trivy failure means grype is not executed", "All pre-existing tests in backend pass"], "modelTier": "standard"} +``` + +--- + +### Task 3: Ship Grype in the image and document it + +**Goal:** The runtime image contains a working `grype` binary and the docs say the scanner uses both tools. + +**Files:** +- Modify: `Dockerfile:18-23` +- Modify: `docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md` (status line) +- Modify: `backend/internal/model/types.go` (comments on `Class`/`Type`/`Vendor` that say "trivy" only) + +**Acceptance Criteria:** +- [ ] `Dockerfile` has `FROM ghcr.io/anchore/grype:latest AS grype` and `COPY --from=grype /grype /usr/local/bin/grype`. +- [ ] The Trivy stage is still `FROM ghcr.io/aquasecurity/trivy:latest AS trivy`. +- [ ] In the built image, `grype version` and `trivy --version` both exit 0. +- [ ] The spec's status line reads `Status: implemented.` + +**Verify:** `docker build -t obsboard:grype-check . && docker run --rm --entrypoint grype obsboard:grype-check version && docker run --rm --entrypoint trivy obsboard:grype-check --version` → both print a version, exit 0 + +**Steps:** + +- [ ] **Step 1: Edit the `Dockerfile`** + +Add the Grype stage after the Trivy stage and the `COPY` after Trivy's `COPY`. Do not change the Trivy lines. The Grype image is `scratch`-based with a static binary at `/grype`; it runs on Alpine as is. + +- [ ] **Step 2: Build and run the verify command** + +Expected: Grype prints `Application: grype` and a version; Trivy prints `Version:`. + +- [ ] **Step 3: Update comments and spec status** + +In `types.go`, reword the `Class`, `Type` and `Vendor` field comments so they no longer claim the value always comes from Trivy (e.g. `// "os-pkgs" | "lang-pkgs" (trivy's result Class; mapped for grype)`). Set the spec status to `implemented`. + +- [ ] **Step 4: Commit** + +```bash +git add Dockerfile backend/internal/model/types.go docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json +git commit -s -m "build: ship grype in the runtime image" +``` + +```json:metadata +{"files": ["Dockerfile", "backend/internal/model/types.go", "docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md"], "verifyCommand": "docker build -t obsboard:grype-check . && docker run --rm --entrypoint grype obsboard:grype-check version && docker run --rm --entrypoint trivy obsboard:grype-check --version", "acceptanceCriteria": ["Dockerfile has FROM ghcr.io/anchore/grype:latest AS grype and COPY --from=grype /grype /usr/local/bin/grype", "Trivy stage is still ghcr.io/aquasecurity/trivy:latest", "grype version and trivy --version both exit 0 in the built image", "Spec status line reads 'Status: implemented.'"], "modelTier": "mechanical"} +``` + +--- + +## After the plan + +Not a task, for whoever deploys: existing stored scans gain Grype findings at the next nightly rescan (`NightlyScheduler.enqueueAll`). To confirm in production, open a ubi10 image in the CVE tab and check that krb5 findings appear. diff --git a/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json b/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json new file mode 100644 index 0000000..652ad11 --- /dev/null +++ b/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json @@ -0,0 +1,30 @@ +{ + "planPath": "docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md", + "tasks": [ + { + "id": 0, + "subject": "Task 1: Parse Grype output into findings", + "status": "completed", + "description": "**Goal:** `parseGrypeOutput` turns Grype JSON into enriched, filtered, de-duplicated `model.CveFinding`s, and findings can carry scanner names.\n\n**Files:**\n- Create: `backend/internal/cve/grype.go`\n- Create: `backend/internal/cve/grype_test.go` (`package cve`, like `owner_test.go`)\n- Create: `backend/internal/cve/testdata/grype.json`\n- Modify: `backend/internal/model/types.go:59-72` (`CveFinding`)\n- Modify: `frontend/src/types/api.ts:52-59` (`CveFinding`)\n\n**Acceptance Criteria:**\n- [ ] `model.CveFinding` has `Scanners []string` with tag `json:\"scanners,omitempty\"`; the TS interface has `scanners?: string[]`.\n- [ ] `parseGrypeOutput` on the fixture returns exactly 4 findings with the fields in Step 2.\n- [ ] Medium findings, findings with no fix version, and repeated matches are not returned.\n- [ ] Every returned finding has `Scanners == []string{\"grype\"}`.\n\n**Verify:** `cd backend && go test ./internal/cve/ -run 'Grype' -v` \u2192 PASS\n\nFull steps: docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md (section 'Task 1: Parse Grype output into findings'). Spec: docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md\n\n```json:metadata\n{\"files\": [\"backend/internal/cve/grype.go\", \"backend/internal/cve/grype_test.go\", \"backend/internal/cve/testdata/grype.json\", \"backend/internal/model/types.go\", \"frontend/src/types/api.ts\"], \"verifyCommand\": \"cd backend && go test ./internal/cve/ -run 'Grype' -v\", \"acceptanceCriteria\": [\"CveFinding has Scanners []string json:\\\"scanners,omitempty\\\" and the TS interface has scanners?: string[]\", \"parseGrypeOutput on the fixture returns exactly the 4 findings in the Step 2 table\", \"Medium, no-fix and repeated matches are not returned\", \"Every returned finding has Scanners == [\\\"grype\\\"]\"], \"modelTier\": \"standard\"}\n```" + }, + { + "id": 1, + "subject": "Task 2: Run Grype in the scanner and merge", + "status": "completed", + "description": "**Goal:** Each scan runs Trivy then Grype and stores the merged findings; either failing fails the scan.\n\n**Files:**\n- Modify: `backend/internal/cve/scanner.go` (`scanPackage` ~line 300, `runTrivy` ~356, `parseTrivyOutput` ~412)\n- Modify: `backend/internal/cve/grype.go` (add `runGrype`, `mergeFindings`)\n- Modify: `backend/internal/cve/scanner_test.go` (existing fakes + new tests)\n- Modify: `backend/internal/cve/grype_test.go` (merge unit test)\n\n**Acceptance Criteria:**\n- [ ] For one target the exec function is called exactly twice: `trivy` first, then `grype` with the argument list from Global Constraints.\n- [ ] Scanning with `trivy-listall.json` + `grype.json` stores 10 findings: Trivy's 8 plus `CVE-2026-0100` and `CVE-2026-0101`; `CVE-2026-0001` and `CVE-2026-0005` (path `usr/sbin/gosu`) have `Scanners == [\"trivy\",\"grype\"]` and keep Trivy's `Title`.\n- [ ] `CriticalCount` / `HighCount` are computed from the merged list.\n- [ ] Grype failure \u2192 one `CVEScanFailed` event whose `Why` starts with `grype:` and contains Grype's stderr; the previously stored scan row is unchanged.\n- [ ] Trivy failure \u2192 Grype is not executed.\n- [ ] All pre-existing tests in `backend/internal/cve` pass.\n\n**Verify:** `cd backend && go test ./internal/cve/ ./internal/store/ -v` \u2192 PASS\n\nFull steps: docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md (section 'Task 2: Run Grype in the scanner and merge'). Spec: docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md\n\n```json:metadata\n{\"files\": [\"backend/internal/cve/scanner.go\", \"backend/internal/cve/grype.go\", \"backend/internal/cve/scanner_test.go\", \"backend/internal/cve/grype_test.go\", \"backend/internal/cve/rescan_test.go\"], \"verifyCommand\": \"cd backend && go vet ./... && go test ./...\", \"acceptanceCriteria\": [\"Per target the exec function is called exactly twice: trivy, then grype with: registry: --platform linux/ --only-fixed --by-cve -o json -q\", \"Fixtures trivy-listall.json + grype.json store 10 findings, CriticalCount 3, HighCount 7; CVE-2026-0001 and CVE-2026-0005 have Scanners [trivy,grype] and Trivy's Title\", \"Grype failure emits CVEScanFailed with Why starting 'grype:' containing stderr, stored scan unchanged\", \"Trivy failure means grype is not executed\", \"All pre-existing tests in backend pass\"], \"modelTier\": \"standard\"}\n```", + "blockedBy": [ + 0 + ] + }, + { + "id": 2, + "subject": "Task 3: Ship Grype in the image and document it", + "status": "pending", + "description": "**Goal:** The runtime image contains a working `grype` binary and the docs say the scanner uses both tools.\n\n**Files:**\n- Modify: `Dockerfile:18-23`\n- Modify: `docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md` (status line)\n- Modify: `backend/internal/model/types.go` (comments on `Class`/`Type`/`Vendor` that say \"trivy\" only)\n\n**Acceptance Criteria:**\n- [ ] `Dockerfile` has `FROM ghcr.io/anchore/grype:latest AS grype` and `COPY --from=grype /grype /usr/local/bin/grype`.\n- [ ] The Trivy stage is still `FROM ghcr.io/aquasecurity/trivy:latest AS trivy`.\n- [ ] In the built image, `grype version` and `trivy --version` both exit 0.\n- [ ] The spec's status line reads `Status: implemented.`\n\n**Verify:** `docker build -t obsboard:grype-check . && docker run --rm --entrypoint grype obsboard:grype-check version && docker run --rm --entrypoint trivy obsboard:grype-check --version` \u2192 both print a version, exit 0\n\nFull steps: docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md (section 'Task 3: Ship Grype in the image and document it'). Spec: docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md\n\n```json:metadata\n{\"files\": [\"Dockerfile\", \"backend/internal/model/types.go\", \"docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md\"], \"verifyCommand\": \"docker build -t obsboard:grype-check . && docker run --rm --entrypoint grype obsboard:grype-check version && docker run --rm --entrypoint trivy obsboard:grype-check --version\", \"acceptanceCriteria\": [\"Dockerfile has FROM ghcr.io/anchore/grype:latest AS grype and COPY --from=grype /grype /usr/local/bin/grype\", \"Trivy stage is still ghcr.io/aquasecurity/trivy:latest\", \"grype version and trivy --version both exit 0 in the built image\", \"Spec status line reads 'Status: implemented.'\"], \"modelTier\": \"mechanical\"}\n```", + "blockedBy": [ + 1 + ] + } + ], + "lastUpdated": "2026-11-17T14:20:00Z" +} \ No newline at end of file diff --git a/docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md b/docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md new file mode 100644 index 0000000..99d0d83 --- /dev/null +++ b/docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md @@ -0,0 +1,141 @@ +# Grype alongside Trivy in the CVE scanner + +Date: 2026-11-17. Status: implemented. + +Lines marked **(call)** are choices made by the author of this document, not +decisions the user made. + +## 1. Why + +Trivy reports no OS vulnerabilities at all on RHEL 10 images. Measured today +on `releases/18/containers/ubi10/percona-distribution-postgresql:18`: Trivy +0.69.3 and 0.76.2 (latest) both return 0 OS findings for 485 RPMs, while Grype +0.122.0 returns 14 fixable HIGH CVEs. Red Hat's own `ubi10/ubi-minimal` shows +the same gap, so it is Trivy's data, not our image. Every ubi10 image in the +dashboard therefore shows as clean regardless of its real state. + +Grype cannot simply replace Trivy: it reports nothing for Python packages that +an RPM installed (Trivy finds e.g. dnspython `CVE-2026-128089`, HIGH, in the +pgadmin4 image). On RHEL 8 and 9 the two agree almost exactly. + +## 2. Decided + +- Run Grype next to Trivy for every image and arch, and store one merged + list of findings. +- Merge by CVE ID: one finding per (CVE ID, package name, path). Path is + empty for RPMs; it keeps the same library CVE in two different Go binaries + apart, as Trivy reports them today **(call)**. When both scanners report a + finding, Trivy's record is kept. Grype only adds findings Trivy lacks. +- If either scanner fails, the scan fails: a `CVEScanFailed` event is emitted + and the previous stored result stays. A half result would make the counts + and the `cve_since` / `clean_since` periods flap. +- Each finding records which scanner(s) reported it. Stored and returned by + the API, not shown in the UI. +- Trivy stays on `ghcr.io/aquasecurity/trivy:latest` in the `Dockerfile`. No + pinning. A rebuild of the image picks up the current release. +- No new UI. No EPSS, risk or KEV data. + +## 3. Approaches considered + +1. **Both scanners on every image, merged** (chosen). One code path, covers + both gaps, and protects against the next silent gap in either tool. Costs + about double the scan time, since each tool pulls the image itself. +2. **Grype only for RHEL 10 images.** Cheaper, but needs OS detection before + choosing a scanner and leaves the dashboard blind to the next Trivy gap. +3. **Replace Trivy with Grype.** Loses the Python findings and the RPM vendor + data the CVE tab depends on. + +## 4. Design + +### Running Grype + +In `backend/internal/cve/scanner.go`, after `runTrivy` succeeds for a target, +run: + + grype registry: --platform linux/ --only-fixed --by-cve -o json -q + +under the same `trivyMu` lock **(call)**: one scanner process at a time keeps +memory and registry load as they are today. `--by-cve` makes Grype report the +CVE ID instead of a GHSA ID when it knows one. Grype has no severity filter +flag for output, so findings other than `High` and `Critical` are dropped +while parsing. Grype's own `ignoredMatches` (the kernel-headers noise) are +never read. + +Grype exits 0 when it finds vulnerabilities (no `--fail-on`), so any non-zero +exit is a failure. Its stderr goes into the event's `Why`, as for Trivy. + +### Mapping a Grype match to `model.CveFinding` + +| CveFinding | From Grype | +|---|---| +| `ID` | `vulnerability.id` | +| `PkgName` | `artifact.name` | +| `InstalledVersion` | `artifact.version` | +| `FixedVersion` | `vulnerability.fix.versions` joined with `, `, each with a leading `0:` epoch removed so it reads like Trivy's | +| `Severity` | `vulnerability.severity` upper-cased | +| `Title` | empty: Grype has no short title **(call)** | +| `Class` / `Type` | `rpm` → `os-pkgs` / `redhat`; `go-module` → `lang-pkgs` / `gobinary`; `python` → `lang-pkgs` / `python-pkg`; anything else → `lang-pkgs` / Grype's type as is | +| `Path` | `artifact.locations[0].path` for non-RPM artifacts, without the leading `/` so it matches Trivy's paths | + +Grype's RPM metadata has no vendor, and the CVE tab needs `Src`, `Vendor` and +`Owner` to tell what the team can bump. Trivy runs with `--list-all-pkgs` and +lists every RPM with its source name and maintainer even when it matches no +vulnerabilities (485 packages on the ubi10 image). So Grype findings are +enriched from Trivy's package list: + +- RPM finding: look the package up by name in Trivy's RPM list and copy `Src` + and `Vendor` (Trivy's package `Version` has no release part, so versions are + not compared). If it is not there, use `artifact.upstreams[0].name` + for `Src` and leave `Vendor` empty. +- Go binary finding: `Owner = goBinaryOwner(path, rpms)`. +- Python finding: `Owner = pythonOwner(name, version, rpms)`. + +These are the existing helpers in `owner.go`, unchanged. + +### Merging + +`parseTrivyOutput` is split so that it also returns the RPM list. A new +function merges: start from Trivy's findings; for each Grype finding, if a +Trivy finding has the same `ID`, `PkgName` and `Path`, add `grype` to its scanner +list; otherwise append the Grype finding. `CriticalCount` and `HighCount` are +computed from the merged list. + +`model.CveFinding` gains `Scanners []string` (`json:"scanners,omitempty"`), +holding `trivy`, `grype` or both. Findings are stored as JSON in +`cve_scans.findings_json`, so there is no schema migration; rows written +before this change simply have no `scanners`. + +### Packaging + +`Dockerfile` adds a stage `FROM ghcr.io/anchore/grype:latest AS grype` and +copies `/grype` to `/usr/local/bin/grype`, next to the existing Trivy copy. +Grype follows `latest` like Trivy **(call)**, for consistency with the +decision not to pin. Grype downloads its vulnerability database on first run +into its cache directory, as Trivy does. + +### Naming + +Log lines, error prefixes and event text that say "trivy" for the whole scan +stay as they are where they refer to Trivy, and say "grype" for Grype +failures. The lock keeps its name **(call)**. + +## 5. Testing + +- A Grype JSON fixture in `backend/internal/cve/testdata/`, trimmed from a + real ubi10 scan, next to `trivy-listall.json`. +- Unit tests in `scanner_test.go` using `WithExecFn`, which already switches + on the command name: + - both report the same CVE and package → one finding, both scanners listed, + Trivy's fields kept; + - Grype-only RPM finding → added, with `Src` and `Vendor` from Trivy's + package list; + - Trivy-only Python finding → kept; + - Grype Medium finding → dropped; + - Grype fails → `CVEScanFailed`, nothing stored; + - Trivy fails → Grype is not run. +- Manual check after deploy: a ubi10 image shows the krb5 findings. + +## 6. Left to the builder + +- Exact function names and how `parseTrivyOutput` is split. +- Whether the README's scanner section needs a line about Grype. From 6741c360c4eaa8fb17c45361b39f4ef6810a4dd9 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 17 Nov 2026 13:15:58 +0000 Subject: [PATCH 4/6] fix(cve): review fixes for grype alongside trivy Signed-off-by: Ricardo Dias --- backend/internal/cve/grype.go | 9 +++++-- backend/internal/cve/grype_test.go | 12 +++++++++ backend/internal/cve/scanner.go | 3 +++ backend/internal/cve/scanner_test.go | 38 +++++++++++++++++++++++++++- backend/internal/model/types.go | 2 +- 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/backend/internal/cve/grype.go b/backend/internal/cve/grype.go index 0a33880..c237d8b 100644 --- a/backend/internal/cve/grype.go +++ b/backend/internal/cve/grype.go @@ -12,7 +12,7 @@ import ( ) type grypeOutput struct { - Matches []grypeMatch `json:"matches"` + Matches *[]grypeMatch `json:"matches"` } type grypeMatch struct { @@ -46,14 +46,18 @@ func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) { if err := json.Unmarshal(data, &out); err != nil { return nil, fmt.Errorf("parse grype JSON: %w", err) } + if out.Matches == nil { + return nil, errors.New("parse grype JSON: no matches field") + } type key struct{ id, pkg, path string } seen := map[key]bool{} var findings []model.CveFinding - for _, m := range out.Matches { + for _, m := range *out.Matches { sev := strings.ToUpper(m.Vulnerability.Severity) if sev != "HIGH" && sev != "CRITICAL" { continue } + // A leading "0:" epoch is stripped so versions read like Trivy's; entries left empty are dropped. var fixed []string for _, v := range m.Vulnerability.Fix.Versions { if v = strings.TrimPrefix(v, "0:"); v != "" { @@ -81,6 +85,7 @@ func parseGrypeOutput(data []byte, rpms []rpmInfo) ([]model.CveFinding, error) { Severity: sev, Scanners: []string{"grype"}, } + // Only rpm, go-module and python map to Trivy's types; other ecosystems may not merge with Trivy's findings because the two tools name packages differently. switch a.Type { case "rpm": f.Class, f.Type = "os-pkgs", "redhat" diff --git a/backend/internal/cve/grype_test.go b/backend/internal/cve/grype_test.go index 8df461e..98e2031 100644 --- a/backend/internal/cve/grype_test.go +++ b/backend/internal/cve/grype_test.go @@ -89,3 +89,15 @@ func TestMergeFindings(t *testing.T) { t.Errorf("order: %+v %+v", got[2], got[3]) } } + +func TestParseGrypeOutputMatchesField(t *testing.T) { + for _, in := range []string{`{}`, `null`} { + if _, err := parseGrypeOutput([]byte(in), nil); err == nil || !strings.Contains(err.Error(), "no matches field") { + t.Errorf("%s: err = %v", in, err) + } + } + got, err := parseGrypeOutput([]byte(`{"matches":[]}`), nil) + if err != nil || len(got) != 0 { + t.Errorf("empty matches: got=%v err=%v", got, err) + } +} diff --git a/backend/internal/cve/scanner.go b/backend/internal/cve/scanner.go index 2402d69..45f20a1 100644 --- a/backend/internal/cve/scanner.go +++ b/backend/internal/cve/scanner.go @@ -303,6 +303,9 @@ func (s *Scanner) scanPackage(ctx context.Context, req ScanRequest) { s.appendScanFailed(req, target, obsURL, err) continue } + if ctx.Err() != nil { + return + } grypeFindings, err := s.runGrype(ctx, imageRef, platform, rpms) if err != nil { slog.Warn("cve: grype failed", "pkg", req.Package, "arch", target.Arch, "err", err) diff --git a/backend/internal/cve/scanner_test.go b/backend/internal/cve/scanner_test.go index f38a490..db64742 100644 --- a/backend/internal/cve/scanner_test.go +++ b/backend/internal/cve/scanner_test.go @@ -442,7 +442,6 @@ func TestScanFailsWhenGrypeFails(t *testing.T) { } return []byte(trivyCleanJSON), nil }) - time.Sleep(5 * time.Millisecond) failingGrype(s) // swap the exec fake on the same scanner/DB s.ScanNow(context.Background(), mergeReq) @@ -484,3 +483,40 @@ func TestScanSkipsGrypeWhenTrivyFails(t *testing.T) { t.Errorf("calls = %v, want only trivy", names) } } + +func TestScanSkipsGrypeWhenContextCancelled(t *testing.T) { + db, err := store.Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + var names []string + s := cve.NewScanner(db, hub.New(), 1, cve.WithExecFn(func(_ context.Context, name string, args ...string) ([]byte, error) { + names = append(names, name) + if name == "trivy" { + cancel() + return []byte(trivyCleanJSON), nil + } + return []byte(grypeEmptyJSON), nil + })) + s.ScanNow(ctx, mergeReq) + + if slices.Contains(names, "grype") { + t.Errorf("grype was called: %v", names) + } + scans, err := store.QueryCveScans(db, "proj", "mypkg") + if err != nil || len(scans) != 0 { + t.Fatalf("scans=%d err=%v", len(scans), err) + } + events, err := store.QueryEvents(db, "proj", time.Now().Add(-time.Hour), time.Now().Add(time.Hour)) + if err != nil { + t.Fatal(err) + } + for _, e := range events { + if e.Type == model.EventCVEScanFailed { + t.Errorf("unexpected scan-failed event: %+v", e) + } + } +} diff --git a/backend/internal/model/types.go b/backend/internal/model/types.go index 256665e..c31bf6c 100644 --- a/backend/internal/model/types.go +++ b/backend/internal/model/types.go @@ -67,7 +67,7 @@ type CveFinding struct { Type string `json:"pkg_type,omitempty"` // "redhat", "gobinary", "python-pkg", … (trivy's result Type; mapped for grype) Path string `json:"path,omitempty"` // binary (gobinary) or package file (other lang-pkgs); empty for RPMs Src string `json:"src,omitempty"` // RPM source package name - Vendor string `json:"vendor,omitempty"` // RPM vendor (trivy "Maintainer"; grype's equivalent for grype) + Vendor string `json:"vendor,omitempty"` // RPM vendor (trivy "Maintainer"; for grype findings copied from trivy's package list, empty if the RPM is not listed) Owner string `json:"owner,omitempty"` // source package of the RPM that ships Path, when resolved Scanners []string `json:"scanners,omitempty"` // "trivy", "grype" or both } From 85b296544248cdf58c4c33986dab9dc0f003ddd2 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 17 Nov 2026 13:18:30 +0000 Subject: [PATCH 5/6] docs: mark the grype alongside trivy plan tasks complete Signed-off-by: Ricardo Dias --- .../plans/2026-11-17-grype-alongside-trivy.md.tasks.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json b/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json index 652ad11..d1c6943 100644 --- a/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json +++ b/docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md.tasks.json @@ -19,12 +19,12 @@ { "id": 2, "subject": "Task 3: Ship Grype in the image and document it", - "status": "pending", + "status": "completed", "description": "**Goal:** The runtime image contains a working `grype` binary and the docs say the scanner uses both tools.\n\n**Files:**\n- Modify: `Dockerfile:18-23`\n- Modify: `docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md` (status line)\n- Modify: `backend/internal/model/types.go` (comments on `Class`/`Type`/`Vendor` that say \"trivy\" only)\n\n**Acceptance Criteria:**\n- [ ] `Dockerfile` has `FROM ghcr.io/anchore/grype:latest AS grype` and `COPY --from=grype /grype /usr/local/bin/grype`.\n- [ ] The Trivy stage is still `FROM ghcr.io/aquasecurity/trivy:latest AS trivy`.\n- [ ] In the built image, `grype version` and `trivy --version` both exit 0.\n- [ ] The spec's status line reads `Status: implemented.`\n\n**Verify:** `docker build -t obsboard:grype-check . && docker run --rm --entrypoint grype obsboard:grype-check version && docker run --rm --entrypoint trivy obsboard:grype-check --version` \u2192 both print a version, exit 0\n\nFull steps: docs/superpowers/plans/2026-11-17-grype-alongside-trivy.md (section 'Task 3: Ship Grype in the image and document it'). Spec: docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md\n\n```json:metadata\n{\"files\": [\"Dockerfile\", \"backend/internal/model/types.go\", \"docs/superpowers/specs/2026-11-17-grype-alongside-trivy-design.md\"], \"verifyCommand\": \"docker build -t obsboard:grype-check . && docker run --rm --entrypoint grype obsboard:grype-check version && docker run --rm --entrypoint trivy obsboard:grype-check --version\", \"acceptanceCriteria\": [\"Dockerfile has FROM ghcr.io/anchore/grype:latest AS grype and COPY --from=grype /grype /usr/local/bin/grype\", \"Trivy stage is still ghcr.io/aquasecurity/trivy:latest\", \"grype version and trivy --version both exit 0 in the built image\", \"Spec status line reads 'Status: implemented.'\"], \"modelTier\": \"mechanical\"}\n```", "blockedBy": [ 1 ] } ], - "lastUpdated": "2026-11-17T14:20:00Z" + "lastUpdated": "2026-11-17T14:40:00Z" } \ No newline at end of file From bb9175522766c420aa920225b8491f70f8b22fad Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Tue, 17 Nov 2026 15:33:07 +0000 Subject: [PATCH 6/6] docs: changelog entry for grype alongside trivy Signed-off-by: Ricardo Dias --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5274df..d0edb58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,17 @@ an example value. See `deploy/host/README.md`. ## [Unreleased] +### Added + +- The CVE scanner now runs Grype after Trivy on every image and stores one merged list of findings. Trivy reports no OS vulnerabilities on RHEL 10 images, so ubi10 images used to show as clean; Grype finds them. When both scanners report the same CVE for the same package, it is listed once. +- Each finding returned by the CVE endpoints has a `scanners` field listing which scanner reported it: `trivy`, `grype` or both. Findings stored before this version have no `scanners` field until the image is rescanned. + +### Changed + +- A scan now fails when either scanner fails, and the previous result is kept. Grype downloads its vulnerability database the first time it runs in a container, so the container needs outbound access to Grype's database host as well as Trivy's. +- Each scan takes about twice as long, because each scanner pulls the image itself. +- Existing images pick up Grype's findings at the next nightly rescan. + ## [0.2.11] - 2026-11-17 ### Added