Skip to content
Merged
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
173 changes: 173 additions & 0 deletions backend/internal/cve/grype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package cve

import (
"context"
"encoding/json"
"errors"
"fmt"
"os/exec"
"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)
}
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 {
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 != "" {
fixed = append(fixed, v)
}
}
if len(fixed) == 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
}
}
}
a := m.Artifact
f := model.CveFinding{
ID: id,
PkgName: a.Name,
InstalledVersion: a.Version,
FixedVersion: strings.Join(fixed, ", "),
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"
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
}

// 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)
}
103 changes: 103 additions & 0 deletions backend/internal/cve/grype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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)
}
}

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])
}
}

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)
}
}
3 changes: 3 additions & 0 deletions backend/internal/cve/rescan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}:
Expand Down
Loading
Loading