Fix /etc/redhat-release file parsing when resolving distro details (#3688)

This commit is contained in:
Alex Goodman 2025-02-26 07:42:29 -05:00 committed by GitHub
parent f44b709542
commit 5e2723187d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 13 deletions

View File

@ -171,18 +171,36 @@ func parseSystemReleaseCPE(contents string) (*Release, error) {
}
// example: "CentOS release 6.10 (Final)"
var redhatReleaseMatcher = regexp.MustCompile(`(.*?)\srelease\s(\d\.\d+)`)
var redhatReleaseMatcher = regexp.MustCompile(`(?P<name>.*?)\srelease\s(?P<version>(?P<versionid>\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
}
contents = strings.TrimSpace(contents)
matches := internal.MatchNamedCaptureGroups(redhatReleaseMatcher, contents)
name := matches["name"]
version := matches["version"]
versionID := matches["versionid"]
if name == "" || versionID == "" {
return nil, nil
}
id := strings.ToLower(name)
switch {
case strings.HasPrefix(id, "red hat enterprise linux"):
id = "rhel"
case strings.HasPrefix(id, "centos"):
// ignore the parenthetical version information
version = versionID
}
return &Release{
PrettyName: contents,
Name: name,
ID: id,
IDLike: []string{id},
Version: version,
VersionID: versionID,
}, nil
}
func simpleRelease(prettyName, name, version, cpe string) *Release {

View File

@ -107,7 +107,7 @@ func TestIdentifyRelease(t *testing.T) {
},
},
{
fixture: "test-fixtures/os/redhat",
fixture: "test-fixtures/os/redhat/from-os-release",
release: &Release{
PrettyName: "Red Hat Enterprise Linux Server 7.3 (Maipo)",
Name: "Red Hat Enterprise Linux Server",
@ -120,6 +120,17 @@ func TestIdentifyRelease(t *testing.T) {
CPEName: "cpe:/o:redhat:enterprise_linux:7.3:GA:server",
},
},
{
fixture: "test-fixtures/os/redhat/from-redhat-release",
release: &Release{
PrettyName: "Red Hat Enterprise Linux release 8.10 (Ootpa)",
Name: "Red Hat Enterprise Linux",
ID: "rhel",
IDLike: []string{"rhel"},
Version: "8.10 (Ootpa)",
VersionID: "8.10",
},
},
{
fixture: "test-fixtures/os/ubuntu",
release: &Release{
@ -266,8 +277,8 @@ func TestIdentifyRelease(t *testing.T) {
{
fixture: "test-fixtures/os/centos5",
release: &Release{
PrettyName: "CentOS",
Name: "centos",
PrettyName: "CentOS release 5.7 (Final)",
Name: "CentOS",
ID: "centos",
IDLike: []string{"centos"},
Version: "5.7",
@ -506,8 +517,8 @@ func TestParseRedhatRelease(t *testing.T) {
fixture: "test-fixtures/os/centos5/etc/redhat-release",
name: "Centos 5",
release: &Release{
PrettyName: "CentOS",
Name: "centos",
PrettyName: "CentOS release 5.7 (Final)",
Name: "CentOS",
ID: "centos",
IDLike: []string{"centos"},
Version: "5.7",

View File

@ -0,0 +1 @@
Red Hat Enterprise Linux release 8.10 (Ootpa)