From 329b080a37c2fa78efb50a415cec3de280d2e917 Mon Sep 17 00:00:00 2001 From: Samuel Dacanay Date: Fri, 11 Sep 2020 15:50:41 -0700 Subject: [PATCH] Add support for Arch (which doesnt return a version) and Open Suse Leap Signed-off-by: Samuel Dacanay remove bad-version test fixture (no longer relevant since it is now an expected case) and add cases for arch and opensuse-leap Signed-off-by: Samuel Dacanay Reduce nesting, simplify empty version logic Signed-off-by: Samuel Dacanay --- syft/distro/distro.go | 12 +++++++- syft/distro/identify.go | 3 +- syft/distro/identify_test.go | 18 ++++++++--- syft/distro/test-fixtures/bad-version | 15 ---------- .../test-fixtures/os/arch/etc/os-release | 10 +++++++ .../os/opensuse-leap/etc/os-release | 10 +++++++ syft/distro/type.go | 30 +++++++++++-------- 7 files changed, 64 insertions(+), 34 deletions(-) delete mode 100644 syft/distro/test-fixtures/bad-version create mode 100644 syft/distro/test-fixtures/os/arch/etc/os-release create mode 100644 syft/distro/test-fixtures/os/opensuse-leap/etc/os-release diff --git a/syft/distro/distro.go b/syft/distro/distro.go index 666b22d56..4de1fd332 100644 --- a/syft/distro/distro.go +++ b/syft/distro/distro.go @@ -20,6 +20,9 @@ func NewUnknownDistro() Distro { } func NewDistro(t Type, ver 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) @@ -32,6 +35,9 @@ func NewDistro(t Type, ver string) (Distro, error) { } func (d Distro) MajorVersion() string { + if d.Version == nil { + return fmt.Sprint("(version unknown)") + } return fmt.Sprintf("%d", d.Version.Segments()[0]) } @@ -40,7 +46,11 @@ func (d Distro) FullVersion() string { } func (d Distro) String() string { - return fmt.Sprintf("%s %s", d.Type, d.RawVersion) + versionStr := "(version unknown)" + if d.RawVersion != "" { + versionStr = d.RawVersion + } + return fmt.Sprintf("%s %s", d.Type, versionStr) } // Name provides a string repr of the distro diff --git a/syft/distro/identify.go b/syft/distro/identify.go index b1088a2c1..af7c7a224 100644 --- a/syft/distro/identify.go +++ b/syft/distro/identify.go @@ -48,6 +48,7 @@ identifyLoop: } if len(refs) == 0 { + log.Debugf("No Refs found from path: %s", entry.path) continue } @@ -84,7 +85,7 @@ func assemble(name, version string) *Distro { distroType, ok := IDMapping[name] // Both distro and version must be present - if len(name) == 0 || len(version) == 0 { + if len(name) == 0 { return nil } diff --git a/syft/distro/identify_test.go b/syft/distro/identify_test.go index b5f272804..fc61965c2 100644 --- a/syft/distro/identify_test.go +++ b/syft/distro/identify_test.go @@ -65,6 +65,15 @@ func TestIdentifyDistro(t *testing.T) { fixture: "test-fixtures/os/unmatchable", Type: UnknownDistroType, }, + { + fixture: "test-fixtures/os/opensuse-leap", + Type: OpenSuseLeap, + Version: "15.2.0", + }, + { + fixture: "test-fixtures/os/arch", + Type: ArchLinux, + }, } observedDistros := internal.NewStringSet() @@ -94,6 +103,11 @@ func TestIdentifyDistro(t *testing.T) { return } + if d.Version == nil { + t.Log("Distro doesn't have a Version") + return + } + if d.Version.String() != test.Version { t.Errorf("expected distro version doesn't match: %v != %v", d.Version.String(), test.Version) } @@ -175,10 +189,6 @@ func TestParseOsReleaseFailures(t *testing.T) { fixture string name string }{ - { - fixture: "test-fixtures/bad-version", - name: "No version", - }, { fixture: "test-fixtures/bad-id", name: "No name ID", diff --git a/syft/distro/test-fixtures/bad-version b/syft/distro/test-fixtures/bad-version deleted file mode 100644 index fa940db6e..000000000 --- a/syft/distro/test-fixtures/bad-version +++ /dev/null @@ -1,15 +0,0 @@ -NAME="Red Hat Enterprise Linux" -VERSION="8.1 (Ootpa)" -ID="rhel" -ID_LIKE="fedora" -PLATFORM_ID="platform:el8" -PRETTY_NAME="Red Hat Enterprise Linux 8.1 (Ootpa)" -ANSI_COLOR="0;31" -CPE_NAME="cpe:/o:redhat:enterprise_linux:8.1:GA" -HOME_URL="https://www.redhat.com/" -BUG_REPORT_URL="https://bugzilla.redhat.com/" - -REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8" -REDHAT_BUGZILLA_PRODUCT_VERSION=8.1 -REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" -REDHAT_SUPPORT_PRODUCT_VERSION="8.1" diff --git a/syft/distro/test-fixtures/os/arch/etc/os-release b/syft/distro/test-fixtures/os/arch/etc/os-release new file mode 100644 index 000000000..e420b15e4 --- /dev/null +++ b/syft/distro/test-fixtures/os/arch/etc/os-release @@ -0,0 +1,10 @@ +NAME="Arch Linux" +PRETTY_NAME="Arch Linux" +ID=arch +BUILD_ID=rolling +ANSI_COLOR="38;2;23;147;209" +HOME_URL="https://www.archlinux.org/" +DOCUMENTATION_URL="https://wiki.archlinux.org/" +SUPPORT_URL="https://bbs.archlinux.org/" +BUG_REPORT_URL="https://bugs.archlinux.org/" +LOGO=archlinux diff --git a/syft/distro/test-fixtures/os/opensuse-leap/etc/os-release b/syft/distro/test-fixtures/os/opensuse-leap/etc/os-release new file mode 100644 index 000000000..1494bc552 --- /dev/null +++ b/syft/distro/test-fixtures/os/opensuse-leap/etc/os-release @@ -0,0 +1,10 @@ +NAME="openSUSE Leap" +VERSION="15.2" +ID="opensuse-leap" +ID_LIKE="suse opensuse" +VERSION_ID="15.2" +PRETTY_NAME="openSUSE Leap 15.2" +ANSI_COLOR="0;32" +CPE_NAME="cpe:/o:opensuse:leap:15.2" +BUG_REPORT_URL="https://bugs.opensuse.org" +HOME_URL="https://www.opensuse.org/" diff --git a/syft/distro/type.go b/syft/distro/type.go index 091d2430d..0f42fca07 100644 --- a/syft/distro/type.go +++ b/syft/distro/type.go @@ -11,7 +11,8 @@ const ( Busybox AmazonLinux OracleLinux - //ArchLinux + ArchLinux + OpenSuseLeap ) type Type int @@ -27,7 +28,8 @@ var distroStr = []string{ "busybox", "amazn", "oraclelinux", - //"archlinux", + "archlinux", + "opensuse-leap", } var All = []Type{ @@ -40,7 +42,8 @@ var All = []Type{ Busybox, AmazonLinux, OracleLinux, - //ArchLinux, + ArchLinux, + OpenSuseLeap, } func (t Type) String() string { @@ -53,14 +56,15 @@ func (t Type) String() string { // IDMapping connects a distro ID like "ubuntu" to a Distro type var IDMapping = map[string]Type{ - "debian": Debian, - "ubuntu": Ubuntu, - "rhel": RedHat, - "centos": CentOS, - "fedora": Fedora, - "alpine": Alpine, - "busybox": Busybox, - "amzn": AmazonLinux, - "ol": OracleLinux, - //"arch": ArchLinux, + "debian": Debian, + "ubuntu": Ubuntu, + "rhel": RedHat, + "centos": CentOS, + "fedora": Fedora, + "alpine": Alpine, + "busybox": Busybox, + "amzn": AmazonLinux, + "ol": OracleLinux, + "arch": ArchLinux, + "opensuse-leap": OpenSuseLeap, }