mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 14:36:33 +01:00
Replace distro type (#742)
* remove strong distro type Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * bump json schema to v3 (breaking distro shape) Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix linting Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * allow for v2 decoding of distro idLikes field in v3 json decoder Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix casing in simple linux release name Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * use discovered name as pretty name in simple linux release Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package linux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/acobaugh/osrelease"
|
||||
"github.com/anchore/syft/internal"
|
||||
"github.com/anchore/syft/internal/log"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
// returns a distro or nil
|
||||
type parseFunc func(string) (*Release, error)
|
||||
|
||||
type parseEntry struct {
|
||||
path string
|
||||
fn parseFunc
|
||||
}
|
||||
|
||||
var identityFiles = []parseEntry{
|
||||
{
|
||||
// most distros provide a link at this location
|
||||
path: "/etc/os-release",
|
||||
fn: parseOsRelease,
|
||||
},
|
||||
{
|
||||
// standard location for rhel & debian distros
|
||||
path: "/usr/lib/os-release",
|
||||
fn: parseOsRelease,
|
||||
},
|
||||
{
|
||||
// check for centos:6
|
||||
path: "/etc/system-release-cpe",
|
||||
fn: parseSystemReleaseCPE,
|
||||
},
|
||||
{
|
||||
// last ditch effort for determining older centos version distro information
|
||||
path: "/etc/redhat-release",
|
||||
fn: parseRedhatRelease,
|
||||
},
|
||||
// /////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// IMPORTANT! checking busybox must be last since other distros contain the busybox binary
|
||||
{
|
||||
// check for busybox
|
||||
path: "/bin/busybox",
|
||||
fn: parseBusyBox,
|
||||
},
|
||||
// /////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
// IdentifyRelease parses distro-specific files to discover and raise linux distribution release details.
|
||||
func IdentifyRelease(resolver source.FileResolver) *Release {
|
||||
for _, entry := range identityFiles {
|
||||
locations, err := resolver.FilesByPath(entry.path)
|
||||
if err != nil {
|
||||
log.Warnf("unable to get path locations from %s: %+v", entry.path, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, location := range locations {
|
||||
contentReader, err := resolver.FileContentsByLocation(location)
|
||||
if err != nil {
|
||||
log.Debugf("unable to get contents from %s: %s", entry.path, err)
|
||||
continue
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadAll(contentReader)
|
||||
internal.CloseAndLogError(contentReader, location.VirtualPath)
|
||||
if err != nil {
|
||||
log.Warnf("unable to read %q: %+v", location.RealPath, err)
|
||||
break
|
||||
}
|
||||
|
||||
release, err := entry.fn(string(content))
|
||||
if err != nil {
|
||||
log.Warnf("unable to parse %q", location.RealPath)
|
||||
}
|
||||
|
||||
if release != nil {
|
||||
return release
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseOsRelease(contents string) (*Release, error) {
|
||||
values, err := osrelease.ReadString(contents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read os-release file: %w", err)
|
||||
}
|
||||
|
||||
var idLike []string
|
||||
for _, s := range strings.Split(values["ID_LIKE"], " ") {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
idLike = append(idLike, s)
|
||||
}
|
||||
|
||||
r := Release{
|
||||
PrettyName: values["PRETTY_NAME"],
|
||||
Name: values["NAME"],
|
||||
ID: values["ID"],
|
||||
IDLike: idLike,
|
||||
Version: values["VERSION"],
|
||||
VersionID: values["VERSION_ID"],
|
||||
Variant: values["VARIANT"],
|
||||
VariantID: values["VARIANT_ID"],
|
||||
HomeURL: values["HOME_URL"],
|
||||
SupportURL: values["SUPPORT_URL"],
|
||||
BugReportURL: values["BUG_REPORT_URL"],
|
||||
PrivacyPolicyURL: values["PRIVACY_POLICY_URL"],
|
||||
CPEName: values["CPE_NAME"],
|
||||
}
|
||||
|
||||
// don't allow for empty contents to result in a Release object being created
|
||||
if cmp.Equal(r, Release{}) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
var busyboxVersionMatcher = regexp.MustCompile(`BusyBox v[\d.]+`)
|
||||
|
||||
func parseBusyBox(contents string) (*Release, error) {
|
||||
matches := busyboxVersionMatcher.FindAllString(contents, -1)
|
||||
for _, match := range matches {
|
||||
parts := strings.Split(match, " ")
|
||||
version := strings.ReplaceAll(parts[1], "v", "")
|
||||
|
||||
return simpleRelease(match, "busybox", version, ""), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// example CPE: cpe:/o:centos:linux:6:GA
|
||||
var systemReleaseCpeMatcher = regexp.MustCompile(`cpe:\/o:(.*?):.*?:(.*?):.*?$`)
|
||||
|
||||
// parseSystemReleaseCPE parses the older centos (6) file to determine distro metadata
|
||||
func parseSystemReleaseCPE(contents string) (*Release, error) {
|
||||
matches := systemReleaseCpeMatcher.FindAllStringSubmatch(contents, -1)
|
||||
for _, match := range matches {
|
||||
if len(match) < 3 {
|
||||
continue
|
||||
}
|
||||
return simpleRelease(match[1], strings.ToLower(match[1]), match[2], match[0]), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// example: "CentOS release 6.10 (Final)"
|
||||
var redhatReleaseMatcher = regexp.MustCompile(`(.*?)\srelease\s(\d\.\d+)`)
|
||||
|
||||
// parseRedhatRelease is a fallback parsing method for determining distro information in older redhat versions
|
||||
func parseRedhatRelease(contents string) (*Release, error) {
|
||||
matches := redhatReleaseMatcher.FindAllStringSubmatch(contents, -1)
|
||||
for _, match := range matches {
|
||||
if len(match) < 3 {
|
||||
continue
|
||||
}
|
||||
return simpleRelease(match[1], strings.ToLower(match[1]), match[2], ""), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func simpleRelease(prettyName, name, version, cpe string) *Release {
|
||||
return &Release{
|
||||
PrettyName: prettyName,
|
||||
Name: name,
|
||||
ID: name,
|
||||
IDLike: []string{name},
|
||||
Version: version,
|
||||
VersionID: version,
|
||||
CPEName: cpe,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,533 @@
|
||||
package linux
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/syft/syft/source"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIdentifyRelease(t *testing.T) {
|
||||
tests := []struct {
|
||||
fixture string
|
||||
release *Release
|
||||
}{
|
||||
{
|
||||
fixture: "test-fixtures/os/alpine",
|
||||
release: &Release{
|
||||
PrettyName: "Alpine Linux v3.11",
|
||||
Name: "Alpine Linux",
|
||||
ID: "alpine",
|
||||
IDLike: nil,
|
||||
VersionID: "3.11.6",
|
||||
HomeURL: "https://alpinelinux.org/",
|
||||
BugReportURL: "https://bugs.alpinelinux.org/",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/amazon",
|
||||
release: &Release{
|
||||
PrettyName: "Amazon Linux 2",
|
||||
Name: "Amazon Linux",
|
||||
ID: "amzn",
|
||||
IDLike: []string{
|
||||
"centos",
|
||||
"rhel",
|
||||
"fedora",
|
||||
},
|
||||
Version: "2",
|
||||
VersionID: "2",
|
||||
HomeURL: "https://amazonlinux.com/",
|
||||
CPEName: "cpe:2.3:o:amazon:amazon_linux:2",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/busybox",
|
||||
release: &Release{
|
||||
PrettyName: "BusyBox v1.31.1",
|
||||
Name: "busybox",
|
||||
ID: "busybox",
|
||||
IDLike: []string{"busybox"},
|
||||
Version: "1.31.1",
|
||||
VersionID: "1.31.1",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/centos",
|
||||
release: &Release{
|
||||
PrettyName: "CentOS Linux 8 (Core)",
|
||||
Name: "CentOS Linux",
|
||||
ID: "centos",
|
||||
IDLike: []string{"rhel",
|
||||
"fedora",
|
||||
},
|
||||
Version: "8 (Core)",
|
||||
VersionID: "8",
|
||||
HomeURL: "https://www.centos.org/",
|
||||
BugReportURL: "https://bugs.centos.org/",
|
||||
CPEName: "cpe:/o:centos:centos:8",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/debian",
|
||||
release: &Release{
|
||||
PrettyName: "Debian GNU/Linux 8 (jessie)",
|
||||
Name: "Debian GNU/Linux",
|
||||
ID: "debian",
|
||||
IDLike: nil,
|
||||
Version: "8 (jessie)",
|
||||
VersionID: "8",
|
||||
HomeURL: "http://www.debian.org/",
|
||||
SupportURL: "http://www.debian.org/support",
|
||||
BugReportURL: "https://bugs.debian.org/",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/fedora",
|
||||
release: &Release{
|
||||
PrettyName: "Fedora 31 (Container Image)",
|
||||
Name: "Fedora",
|
||||
ID: "fedora",
|
||||
IDLike: nil,
|
||||
Version: "31 (Container Image)",
|
||||
VersionID: "31",
|
||||
Variant: "Container Image",
|
||||
VariantID: "container",
|
||||
HomeURL: "https://fedoraproject.org/",
|
||||
SupportURL: "https://fedoraproject.org/wiki/Communicating_and_getting_help",
|
||||
BugReportURL: "https://bugzilla.redhat.com/",
|
||||
PrivacyPolicyURL: "https://fedoraproject.org/wiki/Legal:PrivacyPolicy",
|
||||
CPEName: "cpe:/o:fedoraproject:fedora:31",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/redhat",
|
||||
release: &Release{
|
||||
PrettyName: "Red Hat Enterprise Linux Server 7.3 (Maipo)",
|
||||
Name: "Red Hat Enterprise Linux Server",
|
||||
ID: "rhel",
|
||||
IDLike: []string{"fedora"},
|
||||
Version: "7.3 (Maipo)",
|
||||
VersionID: "7.3",
|
||||
HomeURL: "https://www.redhat.com/",
|
||||
BugReportURL: "https://bugzilla.redhat.com/",
|
||||
CPEName: "cpe:/o:redhat:enterprise_linux:7.3:GA:server",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/ubuntu",
|
||||
release: &Release{
|
||||
PrettyName: "Ubuntu 20.04 LTS",
|
||||
Name: "Ubuntu",
|
||||
ID: "ubuntu",
|
||||
IDLike: []string{"debian"},
|
||||
Version: "20.04 LTS (Focal Fossa)",
|
||||
VersionID: "20.04",
|
||||
HomeURL: "https://www.ubuntu.com/",
|
||||
SupportURL: "https://help.ubuntu.com/",
|
||||
BugReportURL: "https://bugs.launchpad.net/ubuntu/",
|
||||
PrivacyPolicyURL: "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/oraclelinux",
|
||||
release: &Release{
|
||||
PrettyName: "Oracle Linux Server 8.3",
|
||||
Name: "Oracle Linux Server",
|
||||
ID: "ol",
|
||||
IDLike: []string{"fedora"},
|
||||
Version: "8.3",
|
||||
VersionID: "8.3",
|
||||
Variant: "Server",
|
||||
VariantID: "server",
|
||||
HomeURL: "https://linux.oracle.com/",
|
||||
BugReportURL: "https://bugzilla.oracle.com/",
|
||||
CPEName: "cpe:/o:oracle:linux:8:3:server",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/empty",
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/custom",
|
||||
release: &Release{
|
||||
PrettyName: "CentOS Linux 8 (Core)",
|
||||
Name: "Scientific Linux",
|
||||
ID: "scientific",
|
||||
IDLike: []string{
|
||||
"rhel",
|
||||
"fedora",
|
||||
},
|
||||
Version: "16 (Core)",
|
||||
VersionID: "8",
|
||||
HomeURL: "https://www.centos.org/",
|
||||
BugReportURL: "https://bugs.centos.org/",
|
||||
CPEName: "cpe:/o:centos:centos:8",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/opensuse-leap",
|
||||
release: &Release{
|
||||
PrettyName: "openSUSE Leap 15.2",
|
||||
Name: "openSUSE Leap",
|
||||
ID: "opensuse-leap",
|
||||
IDLike: []string{
|
||||
"suse",
|
||||
"opensuse",
|
||||
},
|
||||
Version: "15.2",
|
||||
VersionID: "15.2",
|
||||
HomeURL: "https://www.opensuse.org/",
|
||||
BugReportURL: "https://bugs.opensuse.org",
|
||||
CPEName: "cpe:/o:opensuse:leap:15.2",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/sles",
|
||||
release: &Release{
|
||||
PrettyName: "SUSE Linux Enterprise Server 15 SP2",
|
||||
Name: "SLES",
|
||||
ID: "sles",
|
||||
IDLike: []string{"suse"},
|
||||
Version: "15-SP2",
|
||||
VersionID: "15.2",
|
||||
CPEName: "cpe:/o:suse:sles:15:sp2",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/photon",
|
||||
release: &Release{
|
||||
PrettyName: "VMware Photon OS/Linux",
|
||||
Name: "VMware Photon OS",
|
||||
ID: "photon",
|
||||
IDLike: nil,
|
||||
Version: "2.0",
|
||||
VersionID: "2.0",
|
||||
HomeURL: "https://vmware.github.io/photon/",
|
||||
BugReportURL: "https://github.com/vmware/photon/issues",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/arch",
|
||||
release: &Release{
|
||||
PrettyName: "Arch Linux",
|
||||
Name: "Arch Linux",
|
||||
ID: "arch",
|
||||
IDLike: nil,
|
||||
HomeURL: "https://www.archlinux.org/",
|
||||
SupportURL: "https://bbs.archlinux.org/",
|
||||
BugReportURL: "https://bugs.archlinux.org/",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/partial-fields/missing-id",
|
||||
release: &Release{
|
||||
Name: "Debian GNU/Linux",
|
||||
IDLike: []string{"debian"},
|
||||
VersionID: "8",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/partial-fields/unknown-id",
|
||||
release: &Release{
|
||||
Name: "Debian GNU/Linux",
|
||||
ID: "my-awesome-distro",
|
||||
IDLike: []string{"debian"},
|
||||
VersionID: "8",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/partial-fields/missing-version",
|
||||
release: &Release{
|
||||
Name: "Debian GNU/Linux",
|
||||
IDLike: []string{"debian"},
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/centos6",
|
||||
release: &Release{
|
||||
PrettyName: "centos",
|
||||
Name: "centos",
|
||||
ID: "centos",
|
||||
IDLike: []string{"centos"},
|
||||
Version: "6",
|
||||
VersionID: "6",
|
||||
CPEName: "cpe:/o:centos:linux:6:GA",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/centos5",
|
||||
release: &Release{
|
||||
PrettyName: "CentOS",
|
||||
Name: "centos",
|
||||
ID: "centos",
|
||||
IDLike: []string{"centos"},
|
||||
Version: "5.7",
|
||||
VersionID: "5.7",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/mariner",
|
||||
release: &Release{
|
||||
PrettyName: "CBL-Mariner/Linux",
|
||||
Name: "Common Base Linux Mariner",
|
||||
ID: "mariner",
|
||||
IDLike: nil,
|
||||
Version: "1.0.20210901",
|
||||
VersionID: "1.0",
|
||||
HomeURL: "https://aka.ms/cbl-mariner",
|
||||
SupportURL: "https://aka.ms/cbl-mariner",
|
||||
BugReportURL: "https://aka.ms/cbl-mariner",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/rockylinux",
|
||||
release: &Release{
|
||||
PrettyName: "Rocky Linux 8.4 (Green Obsidian)",
|
||||
Name: "Rocky Linux",
|
||||
ID: "rocky",
|
||||
IDLike: []string{
|
||||
"rhel",
|
||||
"fedora",
|
||||
},
|
||||
Version: "8.4 (Green Obsidian)",
|
||||
VersionID: "8.4",
|
||||
HomeURL: "https://rockylinux.org/",
|
||||
BugReportURL: "https://bugs.rockylinux.org/",
|
||||
CPEName: "cpe:/o:rocky:rocky:8.4:GA",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/os/almalinux",
|
||||
release: &Release{
|
||||
PrettyName: "AlmaLinux 8.4 (Electric Cheetah)",
|
||||
Name: "AlmaLinux",
|
||||
ID: "almalinux",
|
||||
IDLike: []string{
|
||||
"rhel",
|
||||
"centos",
|
||||
"fedora",
|
||||
},
|
||||
Version: "8.4 (Electric Cheetah)",
|
||||
VersionID: "8.4",
|
||||
HomeURL: "https://almalinux.org/",
|
||||
BugReportURL: "https://bugs.almalinux.org/",
|
||||
CPEName: "cpe:/o:almalinux:almalinux:8.4:GA",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.fixture, func(t *testing.T) {
|
||||
s, err := source.NewFromDirectory(test.fixture)
|
||||
require.NoError(t, err)
|
||||
|
||||
resolver, err := s.FileResolver(source.SquashedScope)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.release, IdentifyRelease(resolver))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOsRelease(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
fixture string
|
||||
release *Release
|
||||
}{
|
||||
{
|
||||
fixture: "test-fixtures/ubuntu-20.04",
|
||||
|
||||
release: &Release{
|
||||
PrettyName: "Ubuntu 20.04 LTS",
|
||||
Name: "Ubuntu",
|
||||
ID: "ubuntu",
|
||||
IDLike: []string{"debian"},
|
||||
Version: "20.04 LTS (Focal Fossa)",
|
||||
VersionID: "20.04",
|
||||
HomeURL: "https://www.ubuntu.com/",
|
||||
SupportURL: "https://help.ubuntu.com/",
|
||||
BugReportURL: "https://bugs.launchpad.net/ubuntu/",
|
||||
PrivacyPolicyURL: "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
fixture: "test-fixtures/debian-8",
|
||||
|
||||
release: &Release{
|
||||
PrettyName: "Debian GNU/Linux 8 (jessie)",
|
||||
Name: "Debian GNU/Linux",
|
||||
ID: "debian",
|
||||
IDLike: nil,
|
||||
Version: "8 (jessie)",
|
||||
VersionID: "8",
|
||||
HomeURL: "http://www.debian.org/",
|
||||
SupportURL: "http://www.debian.org/support",
|
||||
BugReportURL: "https://bugs.debian.org/",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
fixture: "test-fixtures/centos-8",
|
||||
|
||||
release: &Release{
|
||||
PrettyName: "CentOS Linux 8 (Core)",
|
||||
Name: "CentOS Linux",
|
||||
ID: "centos",
|
||||
IDLike: []string{
|
||||
"rhel",
|
||||
"fedora",
|
||||
},
|
||||
Version: "8 (Core)",
|
||||
VersionID: "8",
|
||||
HomeURL: "https://www.centos.org/",
|
||||
BugReportURL: "https://bugs.centos.org/",
|
||||
CPEName: "cpe:/o:centos:centos:8",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
fixture: "test-fixtures/rhel-8",
|
||||
|
||||
release: &Release{
|
||||
PrettyName: "Red Hat Enterprise Linux 8.1 (Ootpa)",
|
||||
Name: "Red Hat Enterprise Linux",
|
||||
ID: "rhel",
|
||||
IDLike: []string{"fedora"},
|
||||
Version: "8.1 (Ootpa)",
|
||||
VersionID: "8.1",
|
||||
HomeURL: "https://www.redhat.com/",
|
||||
BugReportURL: "https://bugzilla.redhat.com/",
|
||||
CPEName: "cpe:/o:redhat:enterprise_linux:8.1:GA",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
fixture: "test-fixtures/unprintable",
|
||||
|
||||
release: &Release{
|
||||
PrettyName: "Debian GNU/Linux 8 (jessie)",
|
||||
Name: "Debian GNU/Linux",
|
||||
ID: "debian",
|
||||
IDLike: nil,
|
||||
Version: "8 (jessie)",
|
||||
VersionID: "8",
|
||||
HomeURL: "http://www.debian.org/",
|
||||
SupportURL: "http://www.debian.org/support",
|
||||
BugReportURL: "https://bugs.debian.org/",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.fixture,
|
||||
func(t *testing.T) {
|
||||
release,
|
||||
err := parseOsRelease(retrieveFixtureContentsAsString(test.fixture,
|
||||
t))
|
||||
require.NoError(t,
|
||||
err)
|
||||
assert.Equal(t,
|
||||
test.release,
|
||||
release)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestParseSystemReleaseCPE(t *testing.T) {
|
||||
tests := []struct {
|
||||
fixture string
|
||||
release *Release
|
||||
}{
|
||||
{
|
||||
fixture: "test-fixtures/os/centos6/etc/system-release-cpe",
|
||||
release: &Release{
|
||||
PrettyName: "centos",
|
||||
Name: "centos",
|
||||
ID: "centos",
|
||||
IDLike: []string{"centos"},
|
||||
Version: "6",
|
||||
VersionID: "6",
|
||||
CPEName: "cpe:/o:centos:linux:6:GA",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/bad-system-release-cpe",
|
||||
release: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.fixture, func(t *testing.T) {
|
||||
contents := retrieveFixtureContentsAsString(test.fixture, t)
|
||||
release, err := parseSystemReleaseCPE(contents)
|
||||
require.NoError(t, err)
|
||||
if test.release == nil {
|
||||
assert.Nil(t, release)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, test.release, release)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRedhatRelease(t *testing.T) {
|
||||
tests := []struct {
|
||||
fixture string
|
||||
name string
|
||||
release *Release
|
||||
}{
|
||||
{
|
||||
fixture: "test-fixtures/os/centos5/etc/redhat-release",
|
||||
name: "Centos 5",
|
||||
release: &Release{
|
||||
PrettyName: "CentOS",
|
||||
Name: "centos",
|
||||
ID: "centos",
|
||||
IDLike: []string{"centos"},
|
||||
Version: "5.7",
|
||||
VersionID: "5.7",
|
||||
},
|
||||
},
|
||||
{
|
||||
fixture: "test-fixtures/bad-redhat-release",
|
||||
name: "Centos 5 Bad Redhat Release",
|
||||
release: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
release, err := parseRedhatRelease(retrieveFixtureContentsAsString(test.fixture, t))
|
||||
require.NoError(t, err)
|
||||
if test.release == nil {
|
||||
assert.Nil(t, release)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, test.release, release)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func retrieveFixtureContentsAsString(fixturePath string, t *testing.T) string {
|
||||
fixture, err := os.Open(fixturePath)
|
||||
if err != nil {
|
||||
t.Fatalf("could not open test fixture=%s: %+v", fixturePath, err)
|
||||
}
|
||||
defer fixture.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(fixture)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to read fixture file: %+v", err)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package linux
|
||||
|
||||
// Release represents Linux Distribution release information as specified from https://www.freedesktop.org/software/systemd/man/os-release.html
|
||||
type Release struct {
|
||||
PrettyName string // A pretty operating system name in a format suitable for presentation to the user.
|
||||
Name string // identifies the operating system, without a version component, and suitable for presentation to the user.
|
||||
ID string // identifies the operating system, excluding any version information and suitable for processing by scripts or usage in generated filenames.
|
||||
IDLike []string // list of operating system identifiers in the same syntax as the ID= setting. It should list identifiers of operating systems that are closely related to the local operating system in regards to packaging and programming interfaces.
|
||||
Version string // identifies the operating system version, excluding any OS name information, possibly including a release code name, and suitable for presentation to the user.
|
||||
VersionID string // identifies the operating system version, excluding any OS name information or release code name, and suitable for processing by scripts or usage in generated filenames.
|
||||
Variant string // identifies a specific variant or edition of the operating system suitable for presentation to the user.
|
||||
VariantID string // identifies a specific variant or edition of the operating system. This may be interpreted by other packages in order to determine a divergent default configuration.
|
||||
HomeURL string
|
||||
SupportURL string
|
||||
BugReportURL string
|
||||
PrivacyPolicyURL string
|
||||
CPEName string // A CPE name for the operating system, in URI binding syntax
|
||||
}
|
||||
|
||||
func (r *Release) String() string {
|
||||
if r == nil {
|
||||
return "unknown"
|
||||
}
|
||||
if r.PrettyName != "" {
|
||||
return r.PrettyName
|
||||
}
|
||||
if r.Name != "" {
|
||||
return r.Name
|
||||
}
|
||||
if r.Version != "" {
|
||||
return r.ID + " " + r.Version
|
||||
}
|
||||
|
||||
return r.ID + " " + r.VersionID
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
CentOS release 5 (Final)
|
||||
@@ -0,0 +1 @@
|
||||
cpe:/o:centos:6:GA
|
||||
@@ -0,0 +1,17 @@
|
||||
NAME="CentOS Linux"
|
||||
VERSION="8 (Core)"
|
||||
ID="centos"
|
||||
ID_LIKE="rhel fedora"
|
||||
VERSION_ID="8"
|
||||
PLATFORM_ID="platform:el8"
|
||||
PRETTY_NAME="CentOS Linux 8 (Core)"
|
||||
ANSI_COLOR="0;31"
|
||||
CPE_NAME="cpe:/o:centos:centos:8"
|
||||
HOME_URL="https://www.centos.org/"
|
||||
BUG_REPORT_URL="https://bugs.centos.org/"
|
||||
|
||||
CENTOS_MANTISBT_PROJECT="CentOS-8"
|
||||
CENTOS_MANTISBT_PROJECT_VERSION="8"
|
||||
REDHAT_SUPPORT_PRODUCT="centos"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION="8"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="8"
|
||||
VERSION="8 (jessie)"
|
||||
ID=debian
|
||||
HOME_URL="http://www.debian.org/"
|
||||
SUPPORT_URL="http://www.debian.org/support"
|
||||
BUG_REPORT_URL="https://bugs.debian.org/"
|
||||
@@ -0,0 +1,15 @@
|
||||
NAME="AlmaLinux"
|
||||
VERSION="8.4 (Electric Cheetah)"
|
||||
ID="almalinux"
|
||||
ID_LIKE="rhel centos fedora"
|
||||
VERSION_ID="8.4"
|
||||
PLATFORM_ID="platform:el8"
|
||||
PRETTY_NAME="AlmaLinux 8.4 (Electric Cheetah)"
|
||||
ANSI_COLOR="0;34"
|
||||
CPE_NAME="cpe:/o:almalinux:almalinux:8.4:GA"
|
||||
HOME_URL="https://almalinux.org/"
|
||||
DOCUMENTATION_URL="https://wiki.almalinux.org/"
|
||||
BUG_REPORT_URL="https://bugs.almalinux.org/"
|
||||
|
||||
ALMALINUX_MANTISBT_PROJECT="AlmaLinux-8"
|
||||
ALMALINUX_MANTISBT_PROJECT_VERSION="8.4"
|
||||
@@ -0,0 +1,6 @@
|
||||
NAME="Alpine Linux"
|
||||
ID=alpine
|
||||
VERSION_ID=3.11.6
|
||||
PRETTY_NAME="Alpine Linux v3.11"
|
||||
HOME_URL="https://alpinelinux.org/"
|
||||
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
|
||||
@@ -0,0 +1,9 @@
|
||||
NAME="Amazon Linux"
|
||||
VERSION="2"
|
||||
ID="amzn"
|
||||
ID_LIKE="centos rhel fedora"
|
||||
VERSION_ID="2"
|
||||
PRETTY_NAME="Amazon Linux 2"
|
||||
ANSI_COLOR="0;33"
|
||||
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
|
||||
HOME_URL="https://amazonlinux.com/"
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
junk...BusyBox v1.31.1more junk
|
||||
@@ -0,0 +1,16 @@
|
||||
NAME="CentOS Linux"
|
||||
VERSION="8 (Core)"
|
||||
ID="centos"
|
||||
ID_LIKE="rhel fedora"
|
||||
VERSION_ID="8"
|
||||
PLATFORM_ID="platform:el8"
|
||||
PRETTY_NAME="CentOS Linux 8 (Core)"
|
||||
ANSI_COLOR="0;31"
|
||||
CPE_NAME="cpe:/o:centos:centos:8"
|
||||
HOME_URL="https://www.centos.org/"
|
||||
BUG_REPORT_URL="https://bugs.centos.org/"
|
||||
|
||||
CENTOS_MANTISBT_PROJECT="CentOS-8"
|
||||
CENTOS_MANTISBT_PROJECT_VERSION="8"
|
||||
REDHAT_SUPPORT_PRODUCT="centos"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION="8"
|
||||
@@ -0,0 +1 @@
|
||||
CentOS release 5.7 (Final)
|
||||
@@ -0,0 +1 @@
|
||||
cpe:/o:centos:linux:6:GA
|
||||
@@ -0,0 +1,17 @@
|
||||
NAME="Scientific Linux"
|
||||
VERSION="16 (Core)"
|
||||
ID="scientific"
|
||||
ID_LIKE="rhel fedora"
|
||||
VERSION_ID="8"
|
||||
PLATFORM_ID="platform:el8"
|
||||
PRETTY_NAME="CentOS Linux 8 (Core)"
|
||||
ANSI_COLOR="0;31"
|
||||
CPE_NAME="cpe:/o:centos:centos:8"
|
||||
HOME_URL="https://www.centos.org/"
|
||||
BUG_REPORT_URL="https://bugs.centos.org/"
|
||||
|
||||
CENTOS_MANTISBT_PROJECT="CentOS-8"
|
||||
CENTOS_MANTISBT_PROJECT_VERSION="8"
|
||||
REDHAT_SUPPORT_PRODUCT="centos"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION="8"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="8"
|
||||
VERSION="8 (jessie)"
|
||||
ID=debian
|
||||
HOME_URL="http://www.debian.org/"
|
||||
SUPPORT_URL="http://www.debian.org/support"
|
||||
BUG_REPORT_URL="https://bugs.debian.org/"
|
||||
@@ -0,0 +1,21 @@
|
||||
NAME=Fedora
|
||||
VERSION="31 (Container Image)"
|
||||
ID=fedora
|
||||
VERSION_ID=31
|
||||
VERSION_CODENAME=""
|
||||
PLATFORM_ID="platform:f31"
|
||||
PRETTY_NAME="Fedora 31 (Container Image)"
|
||||
ANSI_COLOR="0;34"
|
||||
LOGO=fedora-logo-icon
|
||||
CPE_NAME="cpe:/o:fedoraproject:fedora:31"
|
||||
HOME_URL="https://fedoraproject.org/"
|
||||
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f31/system-administrators-guide/"
|
||||
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
|
||||
BUG_REPORT_URL="https://bugzilla.redhat.com/"
|
||||
REDHAT_BUGZILLA_PRODUCT="Fedora"
|
||||
REDHAT_BUGZILLA_PRODUCT_VERSION=31
|
||||
REDHAT_SUPPORT_PRODUCT="Fedora"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION=31
|
||||
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
|
||||
VARIANT="Container Image"
|
||||
VARIANT_ID=container
|
||||
@@ -0,0 +1,9 @@
|
||||
NAME="Common Base Linux Mariner"
|
||||
VERSION="1.0.20210901"
|
||||
ID=mariner
|
||||
VERSION_ID=1.0
|
||||
PRETTY_NAME="CBL-Mariner/Linux"
|
||||
ANSI_COLOR="1;34"
|
||||
HOME_URL="https://aka.ms/cbl-mariner"
|
||||
BUG_REPORT_URL="https://aka.ms/cbl-mariner"
|
||||
SUPPORT_URL="https://aka.ms/cbl-mariner"
|
||||
@@ -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/"
|
||||
@@ -0,0 +1,18 @@
|
||||
NAME="Oracle Linux Server"
|
||||
VERSION="8.3"
|
||||
ID="ol"
|
||||
ID_LIKE="fedora"
|
||||
VARIANT="Server"
|
||||
VARIANT_ID="server"
|
||||
VERSION_ID="8.3"
|
||||
PLATFORM_ID="platform:el8"
|
||||
PRETTY_NAME="Oracle Linux Server 8.3"
|
||||
ANSI_COLOR="0;31"
|
||||
CPE_NAME="cpe:/o:oracle:linux:8:3:server"
|
||||
HOME_URL="https://linux.oracle.com/"
|
||||
BUG_REPORT_URL="https://bugzilla.oracle.com/"
|
||||
|
||||
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
|
||||
ORACLE_BUGZILLA_PRODUCT_VERSION=8.3
|
||||
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
|
||||
ORACLE_SUPPORT_PRODUCT_VERSION=8.3
|
||||
@@ -0,0 +1,8 @@
|
||||
NAME="VMware Photon OS"
|
||||
VERSION="2.0"
|
||||
ID=photon
|
||||
VERSION_ID=2.0
|
||||
PRETTY_NAME="VMware Photon OS/Linux"
|
||||
ANSI_COLOR="1;34"
|
||||
HOME_URL="https://vmware.github.io/photon/"
|
||||
BUG_REPORT_URL="https://github.com/vmware/photon/issues"
|
||||
@@ -0,0 +1,15 @@
|
||||
NAME="Red Hat Enterprise Linux Server"
|
||||
VERSION="7.3 (Maipo)"
|
||||
ID="rhel"
|
||||
ID_LIKE="fedora"
|
||||
VERSION_ID="7.3"
|
||||
PRETTY_NAME="Red Hat Enterprise Linux Server 7.3 (Maipo)"
|
||||
ANSI_COLOR="0;31"
|
||||
CPE_NAME="cpe:/o:redhat:enterprise_linux:7.3:GA:server"
|
||||
HOME_URL="https://www.redhat.com/"
|
||||
BUG_REPORT_URL="https://bugzilla.redhat.com/"
|
||||
|
||||
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
|
||||
REDHAT_BUGZILLA_PRODUCT_VERSION=7.3
|
||||
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
|
||||
REDHAT_SUPPORT_PRODUCT_VERSION="7.3"
|
||||
@@ -0,0 +1,13 @@
|
||||
NAME="Rocky Linux"
|
||||
VERSION="8.4 (Green Obsidian)"
|
||||
ID="rocky"
|
||||
ID_LIKE="rhel fedora"
|
||||
VERSION_ID="8.4"
|
||||
PLATFORM_ID="platform:el8"
|
||||
PRETTY_NAME="Rocky Linux 8.4 (Green Obsidian)"
|
||||
ANSI_COLOR="0;32"
|
||||
CPE_NAME="cpe:/o:rocky:rocky:8.4:GA"
|
||||
HOME_URL="https://rockylinux.org/"
|
||||
BUG_REPORT_URL="https://bugs.rockylinux.org/"
|
||||
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
|
||||
ROCKY_SUPPORT_PRODUCT_VERSION="8"
|
||||
@@ -0,0 +1,9 @@
|
||||
NAME="SLES"
|
||||
VERSION="15-SP2"
|
||||
VERSION_ID="15.2"
|
||||
PRETTY_NAME="SUSE Linux Enterprise Server 15 SP2"
|
||||
ID="sles"
|
||||
ID_LIKE="suse"
|
||||
ANSI_COLOR="0;32"
|
||||
CPE_NAME="cpe:/o:suse:sles:15:sp2"
|
||||
DOCUMENTATION_URL="https://documentation.suse.com/"
|
||||
@@ -0,0 +1,12 @@
|
||||
NAME="Ubuntu"
|
||||
VERSION="20.04 LTS (Focal Fossa)"
|
||||
ID=ubuntu
|
||||
ID_LIKE=debian
|
||||
PRETTY_NAME="Ubuntu 20.04 LTS"
|
||||
VERSION_ID="20.04"
|
||||
HOME_URL="https://www.ubuntu.com/"
|
||||
SUPPORT_URL="https://help.ubuntu.com/"
|
||||
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
|
||||
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
|
||||
VERSION_CODENAME=focal
|
||||
UBUNTU_CODENAME=focal
|
||||
@@ -0,0 +1,3 @@
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="8"
|
||||
ID_LIKE=debian
|
||||
@@ -0,0 +1,2 @@
|
||||
NAME="Debian GNU/Linux"
|
||||
ID_LIKE=debian
|
||||
@@ -0,0 +1,4 @@
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="8"
|
||||
ID=my-awesome-distro
|
||||
ID_LIKE=debian
|
||||
@@ -0,0 +1,16 @@
|
||||
NAME="Red Hat Enterprise Linux"
|
||||
VERSION="8.1 (Ootpa)"
|
||||
ID="rhel"
|
||||
ID_LIKE="fedora"
|
||||
VERSION_ID="8.1"
|
||||
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"
|
||||
@@ -0,0 +1,12 @@
|
||||
NAME="Ubuntu"
|
||||
VERSION="20.04 LTS (Focal Fossa)"
|
||||
ID=ubuntu
|
||||
ID_LIKE=debian
|
||||
PRETTY_NAME="Ubuntu 20.04 LTS"
|
||||
VERSION_ID="20.04"
|
||||
HOME_URL="https://www.ubuntu.com/"
|
||||
SUPPORT_URL="https://help.ubuntu.com/"
|
||||
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
|
||||
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
|
||||
VERSION_CODENAME=focal
|
||||
UBUNTU_CODENAME=focal
|
||||
@@ -0,0 +1,8 @@
|
||||
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
||||
NAME="Debian GNU/Linux"
|
||||
VERSION_ID="8"
|
||||
VERSION="8 (jessie)"
|
||||
ID=debian
|
||||
HOME_URL="http://www.debian.org/"
|
||||
SUPPORT_URL="http://www.debian.org/support"
|
||||
BUG_REPORT_URL="https://bugs.debian.org/"
|
||||
Reference in New Issue
Block a user