Skip to content

Commit

Permalink
Do not lower-case FQDN (#231)
Browse files Browse the repository at this point in the history
This PR reverts #180

`Host.FQDNWithContext()` and the deprecated `Host.FQDN()` now return the FQDN as is; it isn't lowercased anymore. This also affects `types.HostInfo#Hostname` which, when it's the FQDN, won't be lowercased.

Complying with ECS for `host.name` and `host.hostname`
(https://www.elastic.co/guide/en/ecs/current/ecs-host.html#field-host-name)
should not be handled by go-sysinfo. The users of go-sysinfo should
ensure compliance with ECS if necessary.
  • Loading branch information
AndersonQ authored Jul 25, 2024
1 parent 4f5410b commit 6d844a0
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/231.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
`Host.FQDNWithContext()` and the deprecated `Host.FQDN()` now return the FQDN as is; it isn't lowercased anymore. This also affects `types.HostInfo#Hostname` which, when it's the FQDN, won't be lowercased.
```
3 changes: 1 addition & 2 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/elastic/go-sysinfo/internal/registry"
Expand Down Expand Up @@ -191,7 +190,7 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}
h.info.Hostname = strings.ToLower(v)
h.info.Hostname = v
}

func (r *reader) network(h *host) {
Expand Down
3 changes: 1 addition & 2 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/elastic/go-sysinfo/internal/registry"
Expand Down Expand Up @@ -226,7 +225,7 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}
h.info.Hostname = strings.ToLower(v)
h.info.Hostname = v
}

func (r *reader) network(h *host) {
Expand Down
3 changes: 1 addition & 2 deletions providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/prometheus/procfs"
Expand Down Expand Up @@ -234,7 +233,7 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}
h.info.Hostname = strings.ToLower(v)
h.info.Hostname = v
}

func (r *reader) network(h *host) {
Expand Down
15 changes: 13 additions & 2 deletions providers/shared/fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func FQDNWithContext(ctx context.Context) (string, error) {
}

// FQDN just calls FQDNWithContext with a background context.
// Deprecated.
func FQDN() (string, error) {
return FQDNWithContext(context.Background())
}
Expand All @@ -62,8 +63,18 @@ func fqdn(ctx context.Context, hostname string) (string, error) {
errs = fmt.Errorf("could not get FQDN, all methods failed: failed looking up CNAME: %w",
err)
}

if cname != "" {
return strings.ToLower(strings.TrimSuffix(cname, ".")), nil
cname = strings.TrimSuffix(cname, ".")

// Go might lowercase the cname "for convenience". Therefore, if cname
// is the same as hostname, return hostname as is.
// See https://github.com/golang/go/blob/go1.22.5/src/net/hosts.go#L38
if strings.ToLower(cname) == strings.ToLower(hostname) {
return hostname, nil
}

return cname, nil
}

ips, err := net.DefaultResolver.LookupIP(ctx, "ip", hostname)
Expand All @@ -76,7 +87,7 @@ func fqdn(ctx context.Context, hostname string) (string, error) {
if err != nil || len(names) == 0 {
continue
}
return strings.ToLower(strings.TrimSuffix(names[0], ".")), nil
return strings.TrimSuffix(names[0], "."), nil
}

return "", errs
Expand Down
4 changes: 2 additions & 2 deletions providers/shared/fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestFQDN(t *testing.T) {
timeout time.Duration
}{
// This test case depends on network, particularly DNS,
// being available. If it starts to fail often enough
// being available. If it starts to fail often enough
// due to occasional network/DNS unavailability, we should
// probably just delete this test case.
"long_real_hostname": {
Expand All @@ -56,7 +56,7 @@ func TestFQDN(t *testing.T) {
},
"long_mixed_case_hostname": {
osHostname: "eLaSTic.co",
expectedFQDN: "elastic.co",
expectedFQDN: "eLaSTic.co",
expectedErrRegex: "",
},
"nonexistent_timeout": {
Expand Down
4 changes: 2 additions & 2 deletions providers/windows/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (h *host) FQDNWithContext(_ context.Context) (string, error) {
return "", fmt.Errorf("could not get windows FQDN: %s", err)
}

return strings.ToLower(strings.TrimSuffix(fqdn, ".")), nil
return strings.TrimSuffix(fqdn, "."), nil
}

func (h *host) FQDN() (string, error) {
Expand Down Expand Up @@ -161,7 +161,7 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}
h.info.Hostname = strings.ToLower(v)
h.info.Hostname = v
}

func getComputerNameEx(name uint32) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions types/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Host interface {
Info() HostInfo
Memory() (*HostMemoryInfo, error)

// FQDNWithContext returns the fully-qualified domain name of the host, lowercased.
// FQDNWithContext returns the fully-qualified domain name of the host.
FQDNWithContext(ctx context.Context) (string, error)

// FQDN calls FQDNWithContext with a background context.
Expand Down Expand Up @@ -77,7 +77,7 @@ type HostInfo struct {
NativeArchitecture string `json:"native_architecture"` // Native OS hardware architecture (e.g. x86_64, arm, ppc, mips).
BootTime time.Time `json:"boot_time"` // Host boot time.
Containerized *bool `json:"containerized,omitempty"` // Is the process containerized.
Hostname string `json:"name"` // Hostname, lowercased.
Hostname string `json:"name"` // Hostname.
IPs []string `json:"ip,omitempty"` // List of all IPs.
KernelVersion string `json:"kernel_version"` // Kernel version.
MACs []string `json:"mac"` // List of MAC addresses.
Expand Down

0 comments on commit 6d844a0

Please sign in to comment.