From 6134561bfed50a6f1b0e722f1d22ae67af7fcaae Mon Sep 17 00:00:00 2001 From: Isabelle Sauve Date: Fri, 12 Nov 2021 08:53:52 -0700 Subject: [PATCH 1/3] Check if path exists and is nonempty before reading host files --- host/host_linux.go | 28 ++++++++++++++-------------- internal/common/common.go | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index d3bafa456..0a713ab24 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -189,7 +189,7 @@ func getOSRelease() (platform string, version string, err error) { func getLSB() (*LSB, error) { ret := &LSB{} - if common.PathExists(common.HostEtc("lsb-release")) { + if common.PathExistsWithContents(common.HostEtc("lsb-release")) { contents, err := common.ReadLines(common.HostEtc("lsb-release")) if err != nil { return ret, err // return empty @@ -210,7 +210,7 @@ func getLSB() (*LSB, error) { ret.Description = field[1] } } - } else if common.PathExists("/usr/bin/lsb_release") { + } else if common.PathExistsWithContents("/usr/bin/lsb_release") { lsb_release, err := exec.LookPath("/usr/bin/lsb_release") if err != nil { return ret, err @@ -248,26 +248,26 @@ func PlatformInformation() (platform string, family string, version string, err lsb = &LSB{} } - if common.PathExists(common.HostEtc("oracle-release")) { + if common.PathExistsWithContents(common.HostEtc("oracle-release")) { platform = "oracle" contents, err := common.ReadLines(common.HostEtc("oracle-release")) if err == nil { version = getRedhatishVersion(contents) } - } else if common.PathExists(common.HostEtc("enterprise-release")) { + } else if common.PathExistsWithContents(common.HostEtc("enterprise-release")) { platform = "oracle" contents, err := common.ReadLines(common.HostEtc("enterprise-release")) if err == nil { version = getRedhatishVersion(contents) } - } else if common.PathExists(common.HostEtc("slackware-version")) { + } else if common.PathExistsWithContents(common.HostEtc("slackware-version")) { platform = "slackware" contents, err := common.ReadLines(common.HostEtc("slackware-version")) if err == nil { version = getSlackwareVersion(contents) } - } else if common.PathExists(common.HostEtc("debian_version")) { + } else if common.PathExistsWithContents(common.HostEtc("debian_version")) { if lsb.ID == "Ubuntu" { platform = "ubuntu" version = lsb.Release @@ -275,7 +275,7 @@ func PlatformInformation() (platform string, family string, version string, err platform = "linuxmint" version = lsb.Release } else { - if common.PathExists("/usr/bin/raspi-config") { + if common.PathExistsWithContents("/usr/bin/raspi-config") { platform = "raspbian" } else { platform = "debian" @@ -285,41 +285,41 @@ func PlatformInformation() (platform string, family string, version string, err version = contents[0] } } - } else if common.PathExists(common.HostEtc("redhat-release")) { + } else if common.PathExistsWithContents(common.HostEtc("redhat-release")) { contents, err := common.ReadLines(common.HostEtc("redhat-release")) if err == nil { version = getRedhatishVersion(contents) platform = getRedhatishPlatform(contents) } - } else if common.PathExists(common.HostEtc("system-release")) { + } else if common.PathExistsWithContents(common.HostEtc("system-release")) { contents, err := common.ReadLines(common.HostEtc("system-release")) if err == nil { version = getRedhatishVersion(contents) platform = getRedhatishPlatform(contents) } - } else if common.PathExists(common.HostEtc("gentoo-release")) { + } else if common.PathExistsWithContents(common.HostEtc("gentoo-release")) { platform = "gentoo" contents, err := common.ReadLines(common.HostEtc("gentoo-release")) if err == nil { version = getRedhatishVersion(contents) } - } else if common.PathExists(common.HostEtc("SuSE-release")) { + } else if common.PathExistsWithContents(common.HostEtc("SuSE-release")) { contents, err := common.ReadLines(common.HostEtc("SuSE-release")) if err == nil { version = getSuseVersion(contents) platform = getSusePlatform(contents) } // TODO: slackware detecion - } else if common.PathExists(common.HostEtc("arch-release")) { + } else if common.PathExistsWithContents(common.HostEtc("arch-release")) { platform = "arch" version = lsb.Release - } else if common.PathExists(common.HostEtc("alpine-release")) { + } else if common.PathExistsWithContents(common.HostEtc("alpine-release")) { platform = "alpine" contents, err := common.ReadLines(common.HostEtc("alpine-release")) if err == nil && len(contents) > 0 { version = contents[0] } - } else if common.PathExists(common.HostEtc("os-release")) { + } else if common.PathExistsWithContents(common.HostEtc("os-release")) { p, v, err := getOSRelease() if err == nil { platform = p diff --git a/internal/common/common.go b/internal/common/common.go index a62be9ca2..b158155a2 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -285,6 +285,25 @@ func PathExists(filename string) bool { return false } +func PathExistsWithContents(filename string) bool { + if _, err := os.Stat(filename); err != nil { + return false + } + + f, err := os.Open(filename) + if err != nil { + return false + } + defer f.Close() + + r := bufio.NewReader(f) + _, err = r.ReadString('\n') + if err != nil { + return false + } + return true +} + //GetEnv retrieves the environment variable key. If it does not exist it returns the default. func GetEnv(key string, dfault string, combineWith ...string) string { value := os.Getenv(key) From 8f8129f14b50d1b2814e0835be3498706e8cac12 Mon Sep 17 00:00:00 2001 From: Isabelle Sauve Date: Fri, 12 Nov 2021 09:49:00 -0700 Subject: [PATCH 2/3] Trim double quotes from platform --- host/host_linux.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/host/host_linux.go b/host/host_linux.go index 0a713ab24..96f71335b 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -342,6 +342,8 @@ func PlatformInformation() (platform string, family string, version string, err version = lsb.Release } + platform = strings.Trim(platform, "\"") + switch platform { case "debian", "ubuntu", "linuxmint", "raspbian": family = "debian" From b4103c51ce6bf4099dd4843c65ac44b5f2aa1118 Mon Sep 17 00:00:00 2001 From: Isabelle Sauve Date: Fri, 12 Nov 2021 10:52:59 -0700 Subject: [PATCH 3/3] Use ReadSlice instead of ReadString --- internal/common/common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/common.go b/internal/common/common.go index b158155a2..cd1f44591 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -297,7 +297,7 @@ func PathExistsWithContents(filename string) bool { defer f.Close() r := bufio.NewReader(f) - _, err = r.ReadString('\n') + _, err = r.ReadSlice('\n') if err != nil { return false }