Skip to content

Commit 5119ee6

Browse files
authored
⭐ Add additional Linux distros + fix detection logic (#6591)
* Add additional Linux distros + fix detection logic Make sure we properly return true on gentoo and bottlerocket. Add addition common distros on the desktop Simplify Gentoo OS detection. It's a rolling release and everyone has a simple /etc/os-release file at this point so we can rely on that. Signed-off-by: Tim Smith <tsmith84@gmail.com> * Refactor things to work more like the other platforms Signed-off-by: Tim Smith <tsmith84@gmail.com> --------- Signed-off-by: Tim Smith <tsmith84@gmail.com>
1 parent fd28d90 commit 5119ee6

File tree

6 files changed

+158
-46
lines changed

6 files changed

+158
-46
lines changed

providers/os/detector/detector_all.go

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,39 @@ var nobara = &PlatformResolver{
359359
},
360360
}
361361

362+
var qubes = &PlatformResolver{
363+
Name: "qubes",
364+
IsFamily: false,
365+
Detect: func(r *PlatformResolver, pf *inventory.Platform, conn shared.Connection) (bool, error) {
366+
if pf.Name == "qubes" {
367+
return true, nil
368+
}
369+
return false, nil
370+
},
371+
}
372+
373+
var tails = &PlatformResolver{
374+
Name: "tails",
375+
IsFamily: false,
376+
Detect: func(r *PlatformResolver, pf *inventory.Platform, conn shared.Connection) (bool, error) {
377+
if pf.Name == "tails" {
378+
return true, nil
379+
}
380+
return false, nil
381+
},
382+
}
383+
384+
var kdeneon = &PlatformResolver{
385+
Name: "neon",
386+
IsFamily: false,
387+
Detect: func(r *PlatformResolver, pf *inventory.Platform, conn shared.Connection) (bool, error) {
388+
if pf.Name == "neon" {
389+
return true, nil
390+
}
391+
return false, nil
392+
},
393+
}
394+
362395
// rhel PlatformResolver only detects redhat and no derivatives
363396
var rhel = &PlatformResolver{
364397
Name: "redhat",
@@ -555,27 +588,25 @@ var bottlerocket = &PlatformResolver{
555588
return false, nil
556589
}
557590

558-
content := strings.TrimSpace(string(c))
559-
osr, err := ParseOsRelease(content)
560-
if err != nil || osr["ID"] != "bottlerocket" {
561-
return false, nil
562-
}
563-
564-
if len(osr["ID"]) > 0 {
565-
pf.Name = osr["ID"]
566-
}
567-
568-
if len(osr["PRETTY_NAME"]) > 0 {
569-
pf.Title = osr["PRETTY_NAME"]
570-
}
571-
if len(osr["VERSION_ID"]) > 0 {
572-
pf.Version = osr["VERSION_ID"]
591+
osr, err := ParseOsRelease(strings.TrimSpace(string(c)))
592+
if err == nil {
593+
if len(osr["ID"]) > 0 {
594+
pf.Name = osr["ID"]
595+
}
596+
if len(osr["PRETTY_NAME"]) > 0 {
597+
pf.Title = osr["PRETTY_NAME"]
598+
}
599+
if len(osr["VERSION_ID"]) > 0 {
600+
pf.Version = osr["VERSION_ID"]
601+
}
602+
if len(osr["BUILD_ID"]) > 0 {
603+
pf.Build = osr["BUILD_ID"]
604+
}
573605
}
574606

575-
if len(osr["BUILD_ID"]) > 0 {
576-
pf.Build = osr["BUILD_ID"]
607+
if pf.Name == "bottlerocket" {
608+
return true, nil
577609
}
578-
579610
return false, nil
580611
},
581612
}
@@ -637,30 +668,9 @@ var gentoo = &PlatformResolver{
637668
Name: "gentoo",
638669
IsFamily: false,
639670
Detect: func(r *PlatformResolver, pf *inventory.Platform, conn shared.Connection) (bool, error) {
640-
f, err := conn.FileSystem().Open("/etc/gentoo-release")
641-
if err != nil {
642-
return false, nil
643-
}
644-
defer f.Close()
645-
646-
c, err := io.ReadAll(f)
647-
if err != nil || len(c) == 0 {
648-
log.Debug().Err(err)
649-
return false, nil
650-
}
651-
652-
content := strings.TrimSpace(string(c))
653-
name, release, err := ParseRhelVersion(content)
654-
if err == nil {
655-
// only set title if not already properly detected by lsb or os-release
656-
if len(pf.Title) == 0 {
657-
pf.Title = name
658-
}
659-
if len(pf.Version) == 0 {
660-
pf.Version = release
661-
}
671+
if pf.Name == "gentoo" {
672+
return true, nil
662673
}
663-
664674
return false, nil
665675
},
666676
}
@@ -1058,7 +1068,7 @@ var redhatFamily = &PlatformResolver{
10581068
IsFamily: true,
10591069
// NOTE: oracle pretends to be redhat with /etc/redhat-release and Red Hat Linux, therefore we
10601070
// want to check that platform before redhat
1061-
Children: []*PlatformResolver{oracle, rhel, centos, fedora, scientific, eurolinux, nobara},
1071+
Children: []*PlatformResolver{oracle, rhel, centos, fedora, scientific, eurolinux, nobara, qubes},
10621072
Detect: func(r *PlatformResolver, pf *inventory.Platform, conn shared.Connection) (bool, error) {
10631073
f, err := conn.FileSystem().Open("/etc/redhat-release")
10641074
if err != nil {
@@ -1106,7 +1116,7 @@ var redhatFamily = &PlatformResolver{
11061116
var debianFamily = &PlatformResolver{
11071117
Name: "debian",
11081118
IsFamily: true,
1109-
Children: []*PlatformResolver{mxlinux, debian, ubuntu, raspbian, kali, linuxmint, popos, elementary, zorin, parrot, cumulus, gardenlinux},
1119+
Children: []*PlatformResolver{mxlinux, debian, ubuntu, raspbian, kali, linuxmint, popos, elementary, zorin, parrot, cumulus, gardenlinux, tails, kdeneon},
11101120
Detect: func(r *PlatformResolver, pf *inventory.Platform, conn shared.Connection) (bool, error) {
11111121
return true, nil
11121122
},

providers/os/detector/detector_platform_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,3 +1109,36 @@ func TestCachyOSDetector(t *testing.T) {
11091109
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
11101110
assert.Equal(t, []string{"arch", "linux", "unix", "os"}, di.Family)
11111111
}
1112+
1113+
func TestTailsDetector(t *testing.T) {
1114+
di, err := detectPlatformFromMock("./testdata/detect-tails.toml")
1115+
assert.Nil(t, err, "was able to create the provider")
1116+
1117+
assert.Equal(t, "tails", di.Name, "os name should be identified")
1118+
assert.Equal(t, "Tails", di.Title, "os title should be identified")
1119+
assert.Equal(t, "6.11", di.Version, "os version should be identified")
1120+
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
1121+
assert.Equal(t, []string{"debian", "linux", "unix", "os"}, di.Family)
1122+
}
1123+
1124+
func TestKDENeonDetector(t *testing.T) {
1125+
di, err := detectPlatformFromMock("./testdata/detect-kdeneon.toml")
1126+
assert.Nil(t, err, "was able to create the provider")
1127+
1128+
assert.Equal(t, "neon", di.Name, "os name should be identified")
1129+
assert.Equal(t, "KDE neon 6.2", di.Title, "os title should be identified")
1130+
assert.Equal(t, "22.04", di.Version, "os version should be identified")
1131+
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
1132+
assert.Equal(t, []string{"debian", "linux", "unix", "os"}, di.Family)
1133+
}
1134+
1135+
func TestQubesOSDetector(t *testing.T) {
1136+
di, err := detectPlatformFromMock("./testdata/detect-qubes.toml")
1137+
assert.Nil(t, err, "was able to create the provider")
1138+
1139+
assert.Equal(t, "qubes", di.Name, "os name should be identified")
1140+
assert.Equal(t, "Qubes OS 4.2 (R4.2)", di.Title, "os title should be identified")
1141+
assert.Equal(t, "4.2", di.Version, "os version should be identified")
1142+
assert.Equal(t, "x86_64", di.Arch, "os arch should be identified")
1143+
assert.Equal(t, []string{"redhat", "linux", "unix", "os"}, di.Family)
1144+
}

providers/os/detector/testdata/detect-gentoo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ stdout = "x86_64"
77
[commands."uname -r"]
88
stdout = "6.1.69-gentoo-dist"
99

10-
[files."/etc/gentoo-release"]
11-
content = "Gentoo Base System release 2.18"
12-
1310
[files."/etc/os-release"]
1411
content = """
1512
NAME=Gentoo
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[commands."uname -s"]
2+
stdout = "Linux"
3+
4+
[commands."uname -m"]
5+
stdout = "x86_64"
6+
7+
[commands."uname -r"]
8+
stdout = "6.8.0-45-generic"
9+
10+
[files."/etc/os-release"]
11+
content = """
12+
PRETTY_NAME="KDE neon 6.2"
13+
NAME="KDE neon"
14+
VERSION_ID="22.04"
15+
VERSION="6.2"
16+
VERSION_CODENAME=jammy
17+
ID=neon
18+
ID_LIKE="ubuntu debian"
19+
HOME_URL="https://neon.kde.org/"
20+
SUPPORT_URL="https://neon.kde.org/"
21+
BUG_REPORT_URL="https://neon.kde.org/"
22+
PRIVACY_POLICY_URL="https://kde.org/privacypolicy/"
23+
UBUNTU_CODENAME=jammy
24+
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[commands."uname -s"]
2+
stdout = "Linux"
3+
4+
[commands."uname -m"]
5+
stdout = "x86_64"
6+
7+
[commands."uname -r"]
8+
stdout = "6.6.21-1.qubes.fc37.x86_64"
9+
10+
[files."/etc/redhat-release"]
11+
content = "Qubes release 4.2 (R4.2)"
12+
13+
[files."/etc/os-release"]
14+
content = """
15+
NAME="Qubes OS"
16+
VERSION="4.2 (R4.2)"
17+
ID=qubes
18+
ID_LIKE="fedora rhel centos"
19+
VERSION_ID=4.2
20+
PRETTY_NAME="Qubes OS 4.2 (R4.2)"
21+
ANSI_COLOR="0;31"
22+
CPE_NAME="cpe:/o:qubesos:qubes:4.2"
23+
HOME_URL="https://www.qubes-os.org/"
24+
BUG_REPORT_URL="https://github.com/QubesOS/qubes-issues/issues"
25+
"""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[commands."uname -s"]
2+
stdout = "Linux"
3+
4+
[commands."uname -m"]
5+
stdout = "x86_64"
6+
7+
[commands."uname -r"]
8+
stdout = "6.1.0-26-amd64"
9+
10+
[files."/etc/os-release"]
11+
content = """
12+
NAME="Tails"
13+
ID="tails"
14+
ID_LIKE="debian"
15+
PRETTY_NAME="Tails"
16+
VERSION="6.11"
17+
HOME_URL="https://tails.net/"
18+
SUPPORT_URL="https://tails.net/support/"
19+
BUG_REPORT_URL="https://tails.net/doc/first_steps/bug_reporting/"
20+
TAILS_PRODUCT_NAME="Tails"
21+
TAILS_VERSION_ID="6.11"
22+
VERSION_ID="6.11"
23+
"""

0 commit comments

Comments
 (0)