Restore behavior of JSON distro block (#643)

This commit is contained in:
Alex Goodman 2022-02-24 11:10:41 -05:00 committed by GitHub
parent 55b71405ab
commit 3a22a56d11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 9 deletions

View file

@ -34,7 +34,7 @@
}
},
"distro": {
"name": "",
"name": "centos",
"version": "8.0",
"idLike": [
"rhel"

View file

@ -165,7 +165,7 @@
"target": "/some/path"
},
"distro": {
"name": "",
"name": "centos",
"version": "8.0",
"idLike": [
"rhel"

View file

@ -230,7 +230,7 @@
}
},
"distro": {
"name": "",
"name": "centos",
"version": "8.0",
"idLike": [
"rhel"

View file

@ -1,6 +1,8 @@
package models
import (
"github.com/anchore/grype/grype/distro"
"github.com/anchore/grype/internal/log"
"github.com/anchore/syft/syft/linux"
)
@ -12,14 +14,34 @@ type distribution struct {
}
// newDistribution creates a struct with the Linux distribution to be represented in JSON.
func newDistribution(d *linux.Release) distribution {
if d == nil {
func newDistribution(r *linux.Release) distribution {
if r == nil {
return distribution{}
}
// attempt to use the strong distro type (like the matchers do)
d, err := distro.NewFromRelease(*r)
if err != nil {
log.Warnf("unable to determine linux distribution: %+v", err)
// as a fallback use the raw release information
return distribution{
Name: r.ID,
Version: r.VersionID,
IDLike: cleanIDLike(r.IDLike),
}
}
return distribution{
Name: d.Name,
Version: d.Version,
IDLike: d.IDLike,
Name: d.Name(),
Version: d.FullVersion(),
IDLike: cleanIDLike(d.IDLike),
}
}
func cleanIDLike(idLike []string) []string {
if idLike == nil {
return make([]string, 0)
}
return idLike
}

View file

@ -1,4 +1,4 @@
Identified distro as centos version 8.0.
Identified distro as redhat version 8.0.
Vulnerability: CVE-1999-0001
Severity: Low
Package: package-1 version 1.1.1 (deb)