Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(host,linux): Check if path exists and is nonempty before reading host files #1328

Merged
merged 2 commits into from Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions host/host_linux.go
Expand Up @@ -179,34 +179,34 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
lsb = &lsbStruct{}
}

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
} else if lsb.ID == "LinuxMint" {
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"
Expand Down Expand Up @@ -279,6 +279,8 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
version = lsb.Release
}

platform = strings.Trim(platform, `"`)

switch platform {
case "debian", "ubuntu", "linuxmint", "raspbian":
family = "debian"
Expand Down
17 changes: 17 additions & 0 deletions internal/common/common.go
Expand Up @@ -311,6 +311,23 @@ func PathExists(filename string) bool {
return false
}

// PathExistsWithContents returns the filename exists and it is not empty
func PathExistsWithContents(filename string) bool {
if _, err := os.Stat(filename); err != nil {
return false
}

f, err := os.Open(filename)
shirou marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return false
}
defer f.Close()

r := bufio.NewReader(f)
_, err = r.Peek(4) // check first 4 bytes
return err == nil
}

// 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)
Expand Down
19 changes: 19 additions & 0 deletions internal/common/common_test.go
Expand Up @@ -102,6 +102,25 @@ func TestPathExists(t *testing.T) {
}
}

func TestPathExistsWithContents(t *testing.T) {
if !PathExistsWithContents("common_test.go") {
t.Error("exists but return not exists")
}
if PathExistsWithContents("should_not_exists.go") {
t.Error("not exists but return exists")
}

f, err := os.CreateTemp("", "empty_test.txt")
if err != nil {
t.Errorf("CreateTemp failed, %s", err)
}
defer os.Remove(f.Name()) // clean up

if PathExistsWithContents(f.Name()) {
t.Error("exists but no content file return true")
}
}

func TestHostEtc(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("windows doesn't have etc")
Expand Down