Merge pull request #258 from anchore/distro-like

Report the ID_LIKE value in the JSON presenter
This commit is contained in:
Alfredo Deza 2020-11-11 11:11:13 -05:00 committed by GitHub
commit 29d464c38a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 27 additions and 14 deletions

View File

@ -10,6 +10,7 @@ type Distro struct {
Type Type Type Type
Version *hashiVer.Version Version *hashiVer.Version
RawVersion string RawVersion string
IDLike string
} }
// NewUnknownDistro creates a standardized Distro object for unidentifiable distros // NewUnknownDistro creates a standardized Distro object for unidentifiable distros
@ -19,7 +20,7 @@ func NewUnknownDistro() Distro {
} }
} }
func NewDistro(t Type, ver string) (Distro, error) { func NewDistro(t Type, ver, like string) (Distro, error) {
if ver == "" { if ver == "" {
return Distro{Type: t}, nil return Distro{Type: t}, nil
} }
@ -31,6 +32,7 @@ func NewDistro(t Type, ver string) (Distro, error) {
Type: t, Type: t,
Version: verObj, Version: verObj,
RawVersion: ver, RawVersion: ver,
IDLike: like,
}, nil }, nil
} }

View File

@ -33,7 +33,7 @@ func TestDistro_FullVersion(t *testing.T) {
for _, test := range tests { for _, test := range tests {
name := fmt.Sprintf("%s:%s", test.dist, test.version) name := fmt.Sprintf("%s:%s", test.dist, test.version)
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
d, err := NewDistro(test.dist, test.version) d, err := NewDistro(test.dist, test.version, "")
if err != nil { if err != nil {
t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err) t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err)
} }
@ -53,29 +53,34 @@ func TestDistro_MajorVersion(t *testing.T) {
dist Type dist Type
version string version string
expected string expected string
like string
}{ }{
{ {
version: "8", version: "8",
expected: "8", expected: "8",
like: "",
}, },
{ {
version: "18.04", version: "18.04",
expected: "18", expected: "18",
like: "debian",
}, },
{ {
version: "0", version: "0",
expected: "0", expected: "0",
like: "",
}, },
{ {
version: "18.1.2", version: "18.1.2",
expected: "18", expected: "18",
like: "debian",
}, },
} }
for _, test := range tests { for _, test := range tests {
name := fmt.Sprintf("%s:%s", test.dist, test.version) name := fmt.Sprintf("%s:%s", test.dist, test.version)
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
d, err := NewDistro(test.dist, test.version) d, err := NewDistro(test.dist, test.version, test.like)
if err != nil { if err != nil {
t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err) t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err)
} }

View File

@ -81,7 +81,7 @@ identifyLoop:
return distro return distro
} }
func assemble(name, version string) *Distro { func assemble(name, version, like string) *Distro {
distroType, ok := IDMapping[name] distroType, ok := IDMapping[name]
// Both distro and version must be present // Both distro and version must be present
@ -90,7 +90,7 @@ func assemble(name, version string) *Distro {
} }
if ok { if ok {
distro, err := NewDistro(distroType, version) distro, err := NewDistro(distroType, version, like)
if err != nil { if err != nil {
return nil return nil
} }
@ -101,7 +101,7 @@ func assemble(name, version string) *Distro {
} }
func parseOsRelease(contents string) *Distro { func parseOsRelease(contents string) *Distro {
id, vers := "", "" id, vers, like := "", "", ""
for _, line := range strings.Split(contents, "\n") { for _, line := range strings.Split(contents, "\n") {
parts := strings.Split(line, "=") parts := strings.Split(line, "=")
prefix := parts[0] prefix := parts[0]
@ -112,10 +112,12 @@ func parseOsRelease(contents string) *Distro {
id = strings.TrimSpace(value) id = strings.TrimSpace(value)
case "VERSION_ID": case "VERSION_ID":
vers = strings.TrimSpace(value) vers = strings.TrimSpace(value)
case "ID_LIKE":
like = strings.TrimSpace(value)
} }
} }
return assemble(id, vers) return assemble(id, vers, like)
} }
var busyboxVersionMatcher = regexp.MustCompile(`BusyBox v[\d\.]+`) var busyboxVersionMatcher = regexp.MustCompile(`BusyBox v[\d\.]+`)
@ -125,7 +127,7 @@ func parseBusyBox(contents string) *Distro {
for _, match := range matches { for _, match := range matches {
parts := strings.Split(match, " ") parts := strings.Split(match, " ")
version := strings.ReplaceAll(parts[1], "v", "") version := strings.ReplaceAll(parts[1], "v", "")
distro := assemble("busybox", version) distro := assemble("busybox", version, "")
if distro != nil { if distro != nil {
return distro return distro
} }

View File

@ -62,7 +62,7 @@ func TestCycloneDxDirsPresenter(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
d, err := distro.NewDistro(distro.Ubuntu, "20.04") d, err := distro.NewDistro(distro.Ubuntu, "20.04", "debian")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -152,7 +152,7 @@ func TestCycloneDxImgsPresenter(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
d, err := distro.NewDistro(distro.RedHat, "8") d, err := distro.NewDistro(distro.RedHat, "8", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -16,6 +16,7 @@ type Document struct {
type Distribution struct { type Distribution struct {
Name string `json:"name"` Name string `json:"name"`
Version string `json:"version"` Version string `json:"version"`
IDLike string `json:"idLike"`
} }
func NewDocument(catalog *pkg.Catalog, s scope.Scope, d distro.Distro) (Document, error) { func NewDocument(catalog *pkg.Catalog, s scope.Scope, d distro.Distro) (Document, error) {
@ -35,6 +36,7 @@ func NewDocument(catalog *pkg.Catalog, s scope.Scope, d distro.Distro) (Document
doc.Distro = Distribution{ doc.Distro = Distribution{
Name: distroName, Name: distroName,
Version: d.FullVersion(), Version: d.FullVersion(),
IDLike: d.IDLike,
} }
for _, p := range catalog.Sorted() { for _, p := range catalog.Sorted() {

View File

@ -29,6 +29,7 @@
}, },
"distro": { "distro": {
"name": "", "name": "",
"version": "" "version": "",
"idLike": ""
} }
} }

View File

@ -59,6 +59,7 @@
}, },
"distro": { "distro": {
"name": "", "name": "",
"version": "" "version": "",
"idLike": ""
} }
} }

View File

@ -24,7 +24,7 @@ func TestDistroImage(t *testing.T) {
t.Fatalf("could not find distro") t.Fatalf("could not find distro")
} }
expected, err := distro.NewDistro(distro.Busybox, "1.31.1") expected, err := distro.NewDistro(distro.Busybox, "1.31.1", "")
if err != nil { if err != nil {
t.Fatalf("could not create distro: %+v", err) t.Fatalf("could not create distro: %+v", err)
} }

View File

@ -62,7 +62,7 @@ func testJsonSchema(t *testing.T, catalog *pkg.Catalog, theScope *scope.Scope, p
output := bytes.NewBufferString("") output := bytes.NewBufferString("")
d, err := distro.NewDistro(distro.CentOS, "5") d, err := distro.NewDistro(distro.CentOS, "5", "rhel fedora")
if err != nil { if err != nil {
t.Fatalf("bad distro: %+v", err) t.Fatalf("bad distro: %+v", err)
} }