Skip to content

Commit

Permalink
🧹 ensure we use fqdn for hostname platform id on unix systems (#3166)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Jan 31, 2024
1 parent 7e5ec58 commit 03b18a8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
30 changes: 22 additions & 8 deletions providers/os/id/hostname/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"go.mondoo.com/cnquery/v10/providers/os/connection/shared"
)

// Hostname returns the hostname of the system.

// On Linux systems we prefer `hostname -f` over `/etc/hostname` since systemd is not updating the value all the time.
// On Windows the `hostname` command (without the -f flag) works more reliable than `powershell -c "$env:computername"`
// since it will return a non-zero exit code.
func Hostname(conn shared.Connection, pf *inventory.Platform) (string, bool) {
var hostname string

Expand All @@ -21,23 +26,32 @@ func Hostname(conn shared.Connection, pf *inventory.Platform) (string, bool) {
return hostname, false
}

// linux:
// we prefer the hostname over /etc/hostname since systemd is not updating the value all the time
//
// windows:
// hostname command works more reliable than t.RunCommand("powershell -c \"$env:computername\"")
// since it will return a non-zero exit code.
// on unix systems we try to get the hostname via `hostname -f` first since it returns the fqdn
if pf.IsFamily(inventory.FAMILY_UNIX) {
cmd, err := conn.RunCommand("hostname -f")
if err == nil && cmd.ExitStatus == 0 {
data, err := io.ReadAll(cmd.Stdout)
if err == nil {
return strings.TrimSpace(string(data)), true
}
} else {
log.Debug().Err(err).Msg("could not run `hostname -f` command")
}
}

// This is the preferred way to get the hostname on windows, it is important to not use the -f flag here
cmd, err := conn.RunCommand("hostname")
if err == nil && cmd.ExitStatus == 0 {
data, err := io.ReadAll(cmd.Stdout)
if err == nil {
return strings.TrimSpace(string(data)), true
}
} else {
log.Debug().Err(err).Msg("could not run hostname command")
log.Debug().Err(err).Msg("could not run `hostname` command")
}

// try to use /etc/hostname since it's also working on static analysis
// Fallback to for unix systems to /etc/hostname, since hostname command is not available on all systems
// This mechanism is also working for static analysis
if pf.IsFamily(inventory.FAMILY_LINUX) {
afs := &afero.Afero{Fs: conn.FileSystem()}
ok, err := afs.Exists("/etc/hostname")
Expand Down
12 changes: 12 additions & 0 deletions providers/os/id/hostname/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func TestHostnameLinux(t *testing.T) {
assert.Equal(t, "abefed34cc9c", hostame)
}

func TestHostnameLinuxFqdn(t *testing.T) {
conn, err := mock.New("./testdata/hostname_fqdn.toml", nil)
require.NoError(t, err)
platform, ok := detector.DetectOS(conn)
require.True(t, ok)

hostame, ok := hostname.Hostname(conn, platform)
require.True(t, ok)

assert.Equal(t, "myhost.example.com", hostame)
}

func TestHostnameWindows(t *testing.T) {
conn, err := mock.New("./testdata/hostname_windows.toml", nil)
require.NoError(t, err)
Expand Down
36 changes: 36 additions & 0 deletions providers/os/id/hostname/testdata/hostname_fqdn.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[commands."hostname"]
stdout="myhost"

[commands."hostname -f"]
stdout="myhost.example.com"

[commands."uname -s"]
stdout = "Linux"

[commands."uname -m"]
stdout = "x86_64"

[commands."uname -r"]
stdout = "4.9.125-linuxkit"

[files."/etc/redhat-release"]
content = "Red Hat Enterprise Linux Server release 7.2 (Maipo)"

[files."/etc/os-release"]
content = """
NAME="Red Hat Enterprise Linux Server"
VERSION="7.2 (Maipo)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="7.2"
PRETTY_NAME="Red Hat Enterprise Linux Server 7.2 (Maipo)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:7.2:GA:server"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
REDHAT_BUGZILLA_PRODUCT_VERSION=7.2
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="7.2"
"""

0 comments on commit 03b18a8

Please sign in to comment.