Skip to content

Commit 6d844a0

Browse files
authoredJul 25, 2024
Do not lower-case FQDN (#231)
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.
1 parent 4f5410b commit 6d844a0

File tree

8 files changed

+25
-14
lines changed

8 files changed

+25
-14
lines changed
 

‎.changelog/231.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
`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+
```

‎providers/aix/host_aix_ppc64.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"errors"
3535
"fmt"
3636
"os"
37-
"strings"
3837
"time"
3938

4039
"github.com/elastic/go-sysinfo/internal/registry"
@@ -191,7 +190,7 @@ func (r *reader) hostname(h *host) {
191190
if r.addErr(err) {
192191
return
193192
}
194-
h.info.Hostname = strings.ToLower(v)
193+
h.info.Hostname = v
195194
}
196195

197196
func (r *reader) network(h *host) {

‎providers/darwin/host_darwin.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"errors"
2525
"fmt"
2626
"os"
27-
"strings"
2827
"time"
2928

3029
"github.com/elastic/go-sysinfo/internal/registry"
@@ -226,7 +225,7 @@ func (r *reader) hostname(h *host) {
226225
if r.addErr(err) {
227226
return
228227
}
229-
h.info.Hostname = strings.ToLower(v)
228+
h.info.Hostname = v
230229
}
231230

232231
func (r *reader) network(h *host) {

‎providers/linux/host_linux.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"os"
2525
"path/filepath"
26-
"strings"
2726
"time"
2827

2928
"github.com/prometheus/procfs"
@@ -234,7 +233,7 @@ func (r *reader) hostname(h *host) {
234233
if r.addErr(err) {
235234
return
236235
}
237-
h.info.Hostname = strings.ToLower(v)
236+
h.info.Hostname = v
238237
}
239238

240239
func (r *reader) network(h *host) {

‎providers/shared/fqdn.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func FQDNWithContext(ctx context.Context) (string, error) {
5151
}
5252

5353
// FQDN just calls FQDNWithContext with a background context.
54+
// Deprecated.
5455
func FQDN() (string, error) {
5556
return FQDNWithContext(context.Background())
5657
}
@@ -62,8 +63,18 @@ func fqdn(ctx context.Context, hostname string) (string, error) {
6263
errs = fmt.Errorf("could not get FQDN, all methods failed: failed looking up CNAME: %w",
6364
err)
6465
}
66+
6567
if cname != "" {
66-
return strings.ToLower(strings.TrimSuffix(cname, ".")), nil
68+
cname = strings.TrimSuffix(cname, ".")
69+
70+
// Go might lowercase the cname "for convenience". Therefore, if cname
71+
// is the same as hostname, return hostname as is.
72+
// See https://github.com/golang/go/blob/go1.22.5/src/net/hosts.go#L38
73+
if strings.ToLower(cname) == strings.ToLower(hostname) {
74+
return hostname, nil
75+
}
76+
77+
return cname, nil
6778
}
6879

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

8293
return "", errs

‎providers/shared/fqdn_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestFQDN(t *testing.T) {
3636
timeout time.Duration
3737
}{
3838
// This test case depends on network, particularly DNS,
39-
// being available. If it starts to fail often enough
39+
// being available. If it starts to fail often enough
4040
// due to occasional network/DNS unavailability, we should
4141
// probably just delete this test case.
4242
"long_real_hostname": {
@@ -56,7 +56,7 @@ func TestFQDN(t *testing.T) {
5656
},
5757
"long_mixed_case_hostname": {
5858
osHostname: "eLaSTic.co",
59-
expectedFQDN: "elastic.co",
59+
expectedFQDN: "eLaSTic.co",
6060
expectedErrRegex: "",
6161
},
6262
"nonexistent_timeout": {

‎providers/windows/host_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (h *host) FQDNWithContext(_ context.Context) (string, error) {
8989
return "", fmt.Errorf("could not get windows FQDN: %s", err)
9090
}
9191

92-
return strings.ToLower(strings.TrimSuffix(fqdn, ".")), nil
92+
return strings.TrimSuffix(fqdn, "."), nil
9393
}
9494

9595
func (h *host) FQDN() (string, error) {
@@ -161,7 +161,7 @@ func (r *reader) hostname(h *host) {
161161
if r.addErr(err) {
162162
return
163163
}
164-
h.info.Hostname = strings.ToLower(v)
164+
h.info.Hostname = v
165165
}
166166

167167
func getComputerNameEx(name uint32) (string, error) {

‎types/host.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Host interface {
3030
Info() HostInfo
3131
Memory() (*HostMemoryInfo, error)
3232

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

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

0 commit comments

Comments
 (0)
Failed to load comments.