Skip to content

Commit 4acdcfe

Browse files
authored
ROX-19942: support usr/lib/redhat-release and RHEL Atomic Host release (#1263)
1 parent 603bf7b commit 4acdcfe

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

ext/featurens/osrelease/osrelease.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ var (
3232
blocklistFilenames = []string{
3333
"etc/alpine-release",
3434
"etc/centos-release",
35+
"usr/lib/centos-release",
3536
"etc/fedora-release",
3637
"etc/oracle-release",
3738
"etc/redhat-release",
39+
"usr/lib/redhat-release",
3840
"etc/rocky-release",
39-
"usr/lib/centos-release",
4041
}
4142

4243
// RequiredFilenames defines the names of the files required to identify the release.
@@ -50,7 +51,7 @@ func init() {
5051
}
5152

5253
func (d detector) Detect(files analyzer.Files, _ *featurens.DetectorOptions) *database.Namespace {
53-
var OS, version string
54+
var os, version string
5455

5556
for _, filePath := range blocklistFilenames {
5657
if _, hasFile := files.Get(filePath); hasFile {
@@ -64,14 +65,14 @@ func (d detector) Detect(files analyzer.Files, _ *featurens.DetectorOptions) *da
6465
continue
6566
}
6667

67-
OS, version = osrelease.GetOSAndVersionFromOSRelease(f.Contents)
68+
os, version = osrelease.GetOSAndVersionFromOSRelease(f.Contents)
6869
}
6970

7071
// Determine the VersionFormat.
7172
// This intentionally does not support alpine,
7273
// as this detector does not handle alpine correctly.
7374
var versionFormat string
74-
switch OS {
75+
switch os {
7576
case "debian", "ubuntu":
7677
versionFormat = dpkg.ParserName
7778
case "centos", "rhel", "amzn", "oracle":
@@ -80,9 +81,9 @@ func (d detector) Detect(files analyzer.Files, _ *featurens.DetectorOptions) *da
8081
return nil
8182
}
8283

83-
if OS != "" && version != "" {
84+
if os != "" && version != "" {
8485
return &database.Namespace{
85-
Name: OS + ":" + version,
86+
Name: os + ":" + version,
8687
VersionFormat: versionFormat,
8788
}
8889
}

ext/featurens/osrelease/osrelease_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/stackrox/scanner/database"
2121
"github.com/stackrox/scanner/ext/featurens"
2222
"github.com/stackrox/scanner/ext/versionfmt/dpkg"
23+
"github.com/stackrox/scanner/ext/versionfmt/rpm"
2324
"github.com/stackrox/scanner/pkg/analyzer"
2425
"github.com/stackrox/scanner/pkg/tarutil"
2526
)
@@ -88,6 +89,30 @@ REDHAT_SUPPORT_PRODUCT="Fedora"
8889
REDHAT_SUPPORT_PRODUCT_VERSION=20`)},
8990
}),
9091
},
92+
{
93+
ExpectedNamespace: &database.Namespace{Name: "rhel:7", VersionFormat: rpm.ParserName},
94+
Files: tarutil.CreateNewLayerFiles(map[string]analyzer.FileData{
95+
"etc/os-release": {Contents: []byte(
96+
`NAME="Red Hat Enterprise Linux Atomic Host"
97+
VERSION="7.9"
98+
ID="rhel"
99+
ID_LIKE="fedora"
100+
VARIANT="Atomic Host"
101+
VARIANT_ID=atomic.host
102+
VERSION_ID="7.9"
103+
PRETTY_NAME="Red Hat Enterprise Linux Atomic Host 7.9"
104+
ANSI_COLOR="0;31"
105+
CPE_NAME="cpe:/o:redhat:enterprise_linux:7.9:GA:atomic-host"
106+
HOME_URL="https://www.redhat.com/"
107+
BUG_REPORT_URL="https://bugzilla.redhat.com/"
108+
109+
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
110+
REDHAT_BUGZILLA_PRODUCT_VERSION="7.9"
111+
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
112+
REDHAT_SUPPORT_PRODUCT_VERSION="7.9"
113+
`)},
114+
}),
115+
},
91116
{
92117
ExpectedNamespace: nil,
93118
Files: tarutil.CreateNewLayerFiles(nil),

ext/featurens/redhatrelease/redhatrelease.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ var (
3333
amazonReleaseRegexp = regexp.MustCompile(`(?P<os>Amazon) (Linux release|Linux AMI release) (?P<version>[\d]+\.[\d]+|[\d]+)`)
3434
oracleReleaseRegexp = regexp.MustCompile(`(?P<os>Oracle) (Linux Server release) (?P<version>[\d]+)`)
3535
centosReleaseRegexp = regexp.MustCompile(`(?P<os>[^\s]*) (Linux release|release) (?P<version>[\d]+)`)
36-
redhatReleaseRegexp = regexp.MustCompile(`(?P<os>Red Hat Enterprise Linux) (Client release|Server release|Workstation release|release) (?P<version>[\d]+)`)
36+
redhatReleaseRegexp = regexp.MustCompile(`(?P<os>Red Hat Enterprise Linux) (Client release|Atomic Host release|Server release|Workstation release|release) (?P<version>[\d]+)`)
3737
rhcosReleaseRegexp = regexp.MustCompile(`(?P<os>Red Hat Enterprise Linux) (CoreOS release) (?P<version>[\d]+[\.]?[\d]*)`) // RHCOS can differ a lot between minor versions, so we also keep the minor for it
3838
rockyReleaseRegexp = regexp.MustCompile(`(?P<os>Rocky) (Linux release) (?P<version>[\d]+)`)
3939

4040
// RequiredFilenames defines the names of the files required to identify the RHEL-based release.
41-
RequiredFilenames = []string{"etc/oracle-release", "etc/centos-release", "etc/redhat-release", "etc/rocky-release", "etc/system-release"}
41+
RequiredFilenames = []string{
42+
"etc/centos-release",
43+
"usr/lib/centos-release",
44+
"etc/oracle-release",
45+
"etc/redhat-release",
46+
"usr/lib/redhat-release",
47+
"etc/rocky-release",
48+
"etc/system-release",
49+
}
4250
)
4351

4452
type detector struct{}

ext/featurens/redhatrelease/redhatrelease_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ func TestDetector(t *testing.T) {
105105
"etc/rocky-release": {Contents: []byte(`Rocky Linux release 9.0 (Blue Onyx)`)},
106106
}),
107107
},
108+
{
109+
ExpectedNamespace: &database.Namespace{Name: "rhel:7", VersionFormat: rpm.ParserName},
110+
Files: tarutil.CreateNewLayerFiles(map[string]analyzer.FileData{
111+
"etc/redhat-release": {Contents: []byte(`Red Hat Enterprise Linux Atomic Host release 7.9`)},
112+
}),
113+
},
108114
{
109115
ExpectedNamespace: nil,
110116
Files: tarutil.CreateNewLayerFiles(nil),

pkg/osrelease/osrelease.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@ package osrelease
22

33
import (
44
"bufio"
5-
"regexp"
65
"strings"
76

87
"github.com/stackrox/rox/pkg/set"
98
"github.com/stackrox/scanner/ext/featurens/util"
109
)
1110

12-
var (
13-
osPattern = regexp.MustCompile(`^ID=(.*)`)
14-
versionPattern = regexp.MustCompile(`^VERSION_ID=(.*)`)
15-
)
16-
1711
// GetOSAndVersionFromOSRelease returns the value of ID= and VERSION_ID= from /etc/os-release formatted data
1812
func GetOSAndVersionFromOSRelease(data []byte) (os, version string) {
1913
m := GetOSReleaseMap(data, "ID", "VERSION_ID")
20-
return util.NormalizeOSName(m["ID"]), m["VERSION_ID"]
14+
15+
os = util.NormalizeOSName(m["ID"])
16+
version = m["VERSION_ID"]
17+
switch os {
18+
case "centos", "rhel":
19+
// Only use the major version.
20+
version, _, _ = strings.Cut(version, ".")
21+
default:
22+
}
23+
24+
return os, version
2125
}
2226

2327
// GetOSReleaseMap returns a map where keys and value are extracted from the

0 commit comments

Comments
 (0)