From 4ff3e2e2e8419612b90a35ec4f3b8b622db61b8a Mon Sep 17 00:00:00 2001 From: Nadav Strahilevitz Date: Tue, 1 Aug 2023 12:16:19 +0000 Subject: [PATCH] fix: trim quote characters in osinfo Some distributions may use quotes in their os-info file (ex: centos). The osinfo helpers must handle these cases for correct parsing. --- helpers/osinfo.go | 6 ++++-- helpers/osinfo_test.go | 7 +++++++ helpers/testdata/os-release-centos | 13 +++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 helpers/testdata/os-release-centos diff --git a/helpers/osinfo.go b/helpers/osinfo.go index ff6a55bb..961d53b8 100644 --- a/helpers/osinfo.go +++ b/helpers/osinfo.go @@ -160,7 +160,7 @@ type OSInfo struct { // GetOSReleaseFieldValue provides access to internal OSInfo OSReleaseField's func (btfi *OSInfo) GetOSReleaseFieldValue(value OSReleaseField) string { - return btfi.osReleaseFieldValues[value] + return strings.Trim(btfi.osReleaseFieldValues[value], "\"") } // GetOSReleaseFilePath provides the path for the used os-release file as it might @@ -226,7 +226,9 @@ func (btfi *OSInfo) discoverOSDistro() error { } btfi.osReleaseFieldValues[keyID] = val[1] if keyID == OS_ID { - btfi.osReleaseID = stringToOSReleaseID[strings.ToLower(val[1])] + osVal := strings.ToLower(val[1]) + osVal = strings.Trim(osVal, "\"") // trim potential parentheses (in centos) + btfi.osReleaseID = stringToOSReleaseID[osVal] } } diff --git a/helpers/osinfo_test.go b/helpers/osinfo_test.go index cb7d5080..2d56f0a7 100644 --- a/helpers/osinfo_test.go +++ b/helpers/osinfo_test.go @@ -30,6 +30,13 @@ func TestGetOSInfo(t *testing.T) { expectedOSReleaseID: UBUNTU, expectedError: nil, }, + { + testName: "env os-release filepath", + osReleaseFilePath: "testdata/os-release-centos", + expectedOSReleaseFilePath: "testdata/os-release-centos", + expectedOSReleaseID: CENTOS, + expectedError: nil, + }, { testName: "default os-release filepath", osReleaseFilePath: "", diff --git a/helpers/testdata/os-release-centos b/helpers/testdata/os-release-centos new file mode 100644 index 00000000..131b4b5e --- /dev/null +++ b/helpers/testdata/os-release-centos @@ -0,0 +1,13 @@ +NAME="CentOS Stream" +VERSION="8" +ID="centos" +ID_LIKE="rhel fedora" +VERSION_ID="8" +PLATFORM_ID="platform:el8" +PRETTY_NAME="CentOS Stream 8" +ANSI_COLOR="0;31" +CPE_NAME="cpe:/o:centos:centos:8" +HOME_URL="https://centos.org/" +BUG_REPORT_URL="https://bugzilla.redhat.com/" +REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8" +REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream" \ No newline at end of file