Skip to content

Commit cb25a07

Browse files
feat(redhat): add os-release detection for RHEL-based images (#9458)
Co-authored-by: DmitriyLewen <[email protected]>
1 parent 8dce58c commit cb25a07

File tree

8 files changed

+222
-35
lines changed

8 files changed

+222
-35
lines changed

pkg/fanal/analyzer/os/release/release.go

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ var requiredFiles = []string{
2727
type osReleaseAnalyzer struct{}
2828

2929
func (a osReleaseAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
30-
var id, versionID string
30+
var family types.OSType
31+
var versionID string
3132
scanner := bufio.NewScanner(input.Content)
3233
for scanner.Scan() {
3334
line := scanner.Text()
@@ -40,46 +41,14 @@ func (a osReleaseAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInp
4041

4142
switch key {
4243
case "ID":
43-
id = strings.Trim(value, `"'`)
44+
id := strings.Trim(value, `"'`)
45+
family = idToOSFamily(id)
4446
case "VERSION_ID":
4547
versionID = strings.Trim(value, `"'`)
4648
default:
4749
continue
4850
}
4951

50-
var family types.OSType
51-
switch id {
52-
case "alpine":
53-
family = types.Alpine
54-
case "bottlerocket":
55-
family = types.Bottlerocket
56-
case "opensuse-tumbleweed":
57-
family = types.OpenSUSETumbleweed
58-
case "opensuse-leap", "opensuse": // opensuse for leap:42, opensuse-leap for leap:15
59-
family = types.OpenSUSELeap
60-
case "sles":
61-
family = types.SLES
62-
// There are various rebrands of SLE Micro, there is also one brief (and reverted rebrand)
63-
// for SLE Micro 6.0. which was called "SL Micro 6.0" until very short before release
64-
// and there is a "SLE Micro for Rancher" rebrand, which is used by SUSEs K8S based offerings.
65-
case "sle-micro", "sl-micro", "sle-micro-rancher":
66-
family = types.SLEMicro
67-
case "photon":
68-
family = types.Photon
69-
case "wolfi":
70-
family = types.Wolfi
71-
case "chainguard":
72-
family = types.Chainguard
73-
case "azurelinux":
74-
family = types.Azure
75-
case "mariner":
76-
family = types.CBLMariner
77-
case "echo":
78-
family = types.Echo
79-
case "minimos":
80-
family = types.MinimOS
81-
}
82-
8352
if family != "" && versionID != "" {
8453
return &analyzer.AnalysisResult{
8554
OS: types.OS{
@@ -93,6 +62,54 @@ func (a osReleaseAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInp
9362
return nil, nil
9463
}
9564

65+
func idToOSFamily(id string) types.OSType {
66+
switch id {
67+
case "rhel":
68+
return types.RedHat
69+
case "centos":
70+
return types.CentOS
71+
case "rocky":
72+
return types.Rocky
73+
case "almalinux":
74+
return types.Alma
75+
case "ol":
76+
return types.Oracle
77+
case "fedora":
78+
return types.Fedora
79+
case "alpine":
80+
return types.Alpine
81+
case "bottlerocket":
82+
return types.Bottlerocket
83+
case "opensuse-tumbleweed":
84+
return types.OpenSUSETumbleweed
85+
case "opensuse-leap", "opensuse": // opensuse for leap:42, opensuse-leap for leap:15
86+
return types.OpenSUSELeap
87+
case "sles":
88+
return types.SLES
89+
// There are various rebrands of SLE Micro, there is also one brief (and reverted rebrand)
90+
// for SLE Micro 6.0. which was called "SL Micro 6.0" until very short before release
91+
// and there is a "SLE Micro for Rancher" rebrand, which is used by SUSEs K8S based offerings.
92+
case "sle-micro", "sl-micro", "sle-micro-rancher":
93+
return types.SLEMicro
94+
case "photon":
95+
return types.Photon
96+
case "wolfi":
97+
return types.Wolfi
98+
case "chainguard":
99+
return types.Chainguard
100+
case "azurelinux":
101+
return types.Azure
102+
case "mariner":
103+
return types.CBLMariner
104+
case "echo":
105+
return types.Echo
106+
case "minimos":
107+
return types.MinimOS
108+
}
109+
// This OS is not supported for this analyzer.
110+
return ""
111+
}
112+
96113
func (a osReleaseAnalyzer) Required(filePath string, _ os.FileInfo) bool {
97114
return slices.Contains(requiredFiles, filePath)
98115
}

pkg/fanal/analyzer/os/release/release_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,66 @@ func Test_osReleaseAnalyzer_Analyze(t *testing.T) {
1919
want *analyzer.AnalysisResult
2020
wantErr string
2121
}{
22+
{
23+
name: "Fedora",
24+
inputFile: "testdata/fedora",
25+
want: &analyzer.AnalysisResult{
26+
OS: types.OS{
27+
Family: types.Fedora,
28+
Name: "42",
29+
},
30+
},
31+
},
32+
{
33+
name: "Red Hat Enterprise Linux",
34+
inputFile: "testdata/rhel",
35+
want: &analyzer.AnalysisResult{
36+
OS: types.OS{
37+
Family: types.RedHat,
38+
Name: "9.4",
39+
},
40+
},
41+
},
42+
{
43+
name: "CentOS",
44+
inputFile: "testdata/centos",
45+
want: &analyzer.AnalysisResult{
46+
OS: types.OS{
47+
Family: types.CentOS,
48+
Name: "7",
49+
},
50+
},
51+
},
52+
{
53+
name: "Rocky Linux",
54+
inputFile: "testdata/rocky",
55+
want: &analyzer.AnalysisResult{
56+
OS: types.OS{
57+
Family: types.Rocky,
58+
Name: "9.3",
59+
},
60+
},
61+
},
62+
{
63+
name: "AlmaLinux",
64+
inputFile: "testdata/alma",
65+
want: &analyzer.AnalysisResult{
66+
OS: types.OS{
67+
Family: types.Alma,
68+
Name: "9.4",
69+
},
70+
},
71+
},
72+
{
73+
name: "Oracle Linux",
74+
inputFile: "testdata/oracle",
75+
want: &analyzer.AnalysisResult{
76+
OS: types.OS{
77+
Family: types.Oracle,
78+
Name: "8.10",
79+
},
80+
},
81+
},
2282
{
2383
name: "alpine",
2484
inputFile: "testdata/alpine",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
NAME="AlmaLinux"
2+
VERSION="9.4 (Seafoam Ocelot)"
3+
ID="almalinux"
4+
ID_LIKE="rhel centos fedora"
5+
VERSION_ID="9.4"
6+
PLATFORM_ID="platform:el9"
7+
PRETTY_NAME="AlmaLinux 9.4 (Seafoam Ocelot)"
8+
ANSI_COLOR="0;34"
9+
LOGO="fedora-logo-icon"
10+
CPE_NAME="cpe:/o:almalinux:almalinux:9::baseos"
11+
HOME_URL="https://almalinux.org/"
12+
DOCUMENTATION_URL="https://wiki.almalinux.org/"
13+
BUG_REPORT_URL="https://bugs.almalinux.org/"
14+
15+
ALMALINUX_MANTISBT_PROJECT="AlmaLinux-9"
16+
ALMALINUX_MANTISBT_PROJECT_VERSION="9.4"
17+
REDHAT_SUPPORT_PRODUCT="AlmaLinux"
18+
REDHAT_SUPPORT_PRODUCT_VERSION="9.4"
19+
SUPPORT_END=2032-06-01
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NAME="CentOS Linux"
2+
VERSION="7 (Core)"
3+
ID="centos"
4+
ID_LIKE="rhel fedora"
5+
VERSION_ID="7"
6+
PRETTY_NAME="CentOS Linux 7 (Core)"
7+
ANSI_COLOR="0;31"
8+
CPE_NAME="cpe:/o:centos:centos:7"
9+
HOME_URL="https://www.centos.org/"
10+
BUG_REPORT_URL="https://bugs.centos.org/"
11+
12+
CENTOS_MANTISBT_PROJECT="CentOS-7"
13+
CENTOS_MANTISBT_PROJECT_VERSION="7"
14+
REDHAT_SUPPORT_PRODUCT="centos"
15+
REDHAT_SUPPORT_PRODUCT_VERSION="7"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
NAME="Fedora Linux"
2+
VERSION="42 (Container Image)"
3+
RELEASE_TYPE=stable
4+
ID=fedora
5+
VERSION_ID=42
6+
VERSION_CODENAME=""
7+
PLATFORM_ID="platform:f42"
8+
PRETTY_NAME="Fedora Linux 42 (Container Image)"
9+
ANSI_COLOR="0;38;2;60;110;180"
10+
LOGO=fedora-logo-icon
11+
CPE_NAME="cpe:/o:fedoraproject:fedora:42"
12+
DEFAULT_HOSTNAME="fedora"
13+
HOME_URL="https://fedoraproject.org/"
14+
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f42/"
15+
SUPPORT_URL="https://ask.fedoraproject.org/"
16+
BUG_REPORT_URL="https://bugzilla.redhat.com/"
17+
REDHAT_BUGZILLA_PRODUCT="Fedora"
18+
REDHAT_BUGZILLA_PRODUCT_VERSION=42
19+
REDHAT_SUPPORT_PRODUCT="Fedora"
20+
REDHAT_SUPPORT_PRODUCT_VERSION=42
21+
SUPPORT_END=2026-05-13
22+
VARIANT="Container Image"
23+
VARIANT_ID=container
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
NAME="Oracle Linux Server"
2+
VERSION="8.10"
3+
ID="ol"
4+
ID_LIKE="fedora"
5+
VARIANT="Server"
6+
VARIANT_ID="server"
7+
VERSION_ID="8.10"
8+
PLATFORM_ID="platform:el8"
9+
PRETTY_NAME="Oracle Linux Server 8.10"
10+
ANSI_COLOR="0;31"
11+
CPE_NAME="cpe:/o:oracle:linux:8:10:server"
12+
HOME_URL="https://linux.oracle.com/"
13+
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
14+
15+
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
16+
ORACLE_BUGZILLA_PRODUCT_VERSION=8.10
17+
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
18+
ORACLE_SUPPORT_PRODUCT_VERSION=8.10
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
NAME="Red Hat Enterprise Linux"
2+
VERSION="9.4 (Plow)"
3+
ID="rhel"
4+
ID_LIKE="fedora"
5+
VERSION_ID="9.4"
6+
PLATFORM_ID="platform:el9"
7+
PRETTY_NAME="Red Hat Enterprise Linux 9.4 (Plow)"
8+
ANSI_COLOR="0;31"
9+
LOGO="fedora-logo-icon"
10+
CPE_NAME="cpe:/o:redhat:enterprise_linux:9::baseos"
11+
HOME_URL="https://www.redhat.com/"
12+
DOCUMENTATION_URL="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9"
13+
BUG_REPORT_URL="https://issues.redhat.com/"
14+
15+
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 9"
16+
REDHAT_BUGZILLA_PRODUCT_VERSION=9.4
17+
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
18+
REDHAT_SUPPORT_PRODUCT_VERSION="9.4"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
NAME="Rocky Linux"
2+
VERSION="9.3 (Blue Onyx)"
3+
ID="rocky"
4+
ID_LIKE="rhel centos fedora"
5+
VERSION_ID="9.3"
6+
PLATFORM_ID="platform:el9"
7+
PRETTY_NAME="Rocky Linux 9.3 (Blue Onyx)"
8+
ANSI_COLOR="0;32"
9+
LOGO="fedora-logo-icon"
10+
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
11+
HOME_URL="https://rockylinux.org/"
12+
BUG_REPORT_URL="https://bugs.rockylinux.org/"
13+
SUPPORT_END="2032-05-31"
14+
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
15+
ROCKY_SUPPORT_PRODUCT_VERSION="9.3"
16+
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
17+
REDHAT_SUPPORT_PRODUCT_VERSION="9.3"

0 commit comments

Comments
 (0)