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,20 +171,38 @@ func parseSystemReleaseCPE(contents string) (*Release, error) {
} }
// example: "CentOS release 6.10 (Final)" // 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 // parseRedhatRelease is a fallback parsing method for determining distro information in older redhat versions
func parseRedhatRelease(contents string) (*Release, error) { func parseRedhatRelease(contents string) (*Release, error) {
matches := redhatReleaseMatcher.FindAllStringSubmatch(contents, -1) contents = strings.TrimSpace(contents)
for _, match := range matches { matches := internal.MatchNamedCaptureGroups(redhatReleaseMatcher, contents)
if len(match) < 3 { name := matches["name"]
continue version := matches["version"]
} versionID := matches["versionid"]
return simpleRelease(match[1], strings.ToLower(match[1]), match[2], ""), nil if name == "" || versionID == "" {
}
return nil, nil 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 { func simpleRelease(prettyName, name, version, cpe string) *Release {
return &Release{ return &Release{
PrettyName: prettyName, PrettyName: prettyName,

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{ release: &Release{
PrettyName: "Red Hat Enterprise Linux Server 7.3 (Maipo)", PrettyName: "Red Hat Enterprise Linux Server 7.3 (Maipo)",
Name: "Red Hat Enterprise Linux Server", 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", 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", fixture: "test-fixtures/os/ubuntu",
release: &Release{ release: &Release{
@ -266,8 +277,8 @@ func TestIdentifyRelease(t *testing.T) {
{ {
fixture: "test-fixtures/os/centos5", fixture: "test-fixtures/os/centos5",
release: &Release{ release: &Release{
PrettyName: "CentOS", PrettyName: "CentOS release 5.7 (Final)",
Name: "centos", Name: "CentOS",
ID: "centos", ID: "centos",
IDLike: []string{"centos"}, IDLike: []string{"centos"},
Version: "5.7", Version: "5.7",
@ -506,8 +517,8 @@ func TestParseRedhatRelease(t *testing.T) {
fixture: "test-fixtures/os/centos5/etc/redhat-release", fixture: "test-fixtures/os/centos5/etc/redhat-release",
name: "Centos 5", name: "Centos 5",
release: &Release{ release: &Release{
PrettyName: "CentOS", PrettyName: "CentOS release 5.7 (Final)",
Name: "centos", Name: "CentOS",
ID: "centos", ID: "centos",
IDLike: []string{"centos"}, IDLike: []string{"centos"},
Version: "5.7", Version: "5.7",

View File

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