diff --git a/syft/distro/distro.go b/syft/distro/distro.go index d84c5a9db..ce71c5346 100644 --- a/syft/distro/distro.go +++ b/syft/distro/distro.go @@ -19,10 +19,12 @@ func NewDistro(t Type, ver, like string) (Distro, error) { if ver == "" { return Distro{Type: t}, nil } + verObj, err := hashiVer.NewVersion(ver) if err != nil { return Distro{}, fmt.Errorf("could not create distro version: %w", err) } + return Distro{ Type: t, Version: verObj, @@ -39,7 +41,7 @@ func (d Distro) Name() string { // MajorVersion returns the major version value from the pseudo-semantically versioned distro version value. func (d Distro) MajorVersion() string { if d.Version == nil { - return fmt.Sprint("(version unknown)") + return "(version unknown)" } return fmt.Sprintf("%d", d.Version.Segments()[0]) } diff --git a/syft/distro/identify.go b/syft/distro/identify.go index fd1691ba3..f4d894d64 100644 --- a/syft/distro/identify.go +++ b/syft/distro/identify.go @@ -97,11 +97,11 @@ identifyLoop: return distro } -func assemble(name, version, like string) *Distro { - distroType, ok := IDMapping[name] +func assemble(id, version, like string) *Distro { + distroType, ok := IDMapping[id] // Both distro and version must be present - if len(name) == 0 && len(version) == 0 { + if len(id) == 0 && len(version) == 0 { return nil } @@ -110,15 +110,17 @@ func assemble(name, version, like string) *Distro { distroType, ok = IDMapping[like] } - if ok { - distro, err := NewDistro(distroType, version, like) - if err != nil { - return nil - } - return &distro + // If we still can't match allow name to be used in constructor + if !ok { + distroType = Type(id) } - return nil + distro, err := NewDistro(distroType, version, like) + if err != nil { + return nil + } + + return &distro } func parseOsRelease(contents string) *Distro { diff --git a/syft/distro/identify_test.go b/syft/distro/identify_test.go index 2e3fe307d..40c22c71a 100644 --- a/syft/distro/identify_test.go +++ b/syft/distro/identify_test.go @@ -2,16 +2,19 @@ package distro import ( "fmt" - hashiVer "github.com/hashicorp/go-version" "io/ioutil" "os" "testing" + hashiVer "github.com/hashicorp/go-version" + "github.com/anchore/syft/internal" "github.com/anchore/syft/syft/source" "github.com/stretchr/testify/assert" ) +const CustomDistro Type = "scientific" + func TestIdentifyDistro(t *testing.T) { tests := []struct { fixture string @@ -68,8 +71,9 @@ func TestIdentifyDistro(t *testing.T) { Type: UnknownDistroType, }, { - fixture: "test-fixtures/os/unmatchable", - Type: UnknownDistroType, + fixture: "test-fixtures/os/custom", + Type: CustomDistro, + Version: "8.0.0", }, { fixture: "test-fixtures/os/opensuse-leap", diff --git a/syft/distro/test-fixtures/os/unmatchable/etc/os-release b/syft/distro/test-fixtures/os/custom/etc/os-release similarity index 100% rename from syft/distro/test-fixtures/os/unmatchable/etc/os-release rename to syft/distro/test-fixtures/os/custom/etc/os-release