Skip to content

Commit

Permalink
allow to choose hostname and FQDN to be lowercased or not
Browse files Browse the repository at this point in the history
providers.LowercaseHostname and providers.SetLowerHostname allow to control hostname case sensitivity

This addresses the behavior change introduced in v1.11.0 and reverted in v1.14.1. By default, hostnames are not lowercased.
  • Loading branch information
AndersonQ committed Aug 22, 2024
1 parent c3666c9 commit 0b70157
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/258.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Allow to choose hostname and FQDN to be lowercased or not
```
6 changes: 6 additions & 0 deletions providers/aix/host_aix_ppc64.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)
Expand Down Expand Up @@ -190,6 +192,10 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}

if providers.LowercaseHostname() {
v = strings.ToLower(v)
}
h.info.Hostname = v
}

Expand Down
6 changes: 6 additions & 0 deletions providers/darwin/host_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)
Expand Down Expand Up @@ -225,6 +227,10 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}

if providers.LowercaseHostname() {
v = strings.ToLower(v)
}
h.info.Hostname = v
}

Expand Down
13 changes: 13 additions & 0 deletions providers/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package providers

Check failure on line 1 in providers/doc.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header
//
// # Hostname Behavior
//
// Starting from version v1.11.0, the host provider started automatically
// lowercasing hostnames. This behavior was reverted in v1.14.1.
//
// To provide flexibility and allow users to control this behavior, the
// `LowercaseHostname` and `SetLowerHostname` functions were added.
//
// By default, hostnames are not lowercased. If you require hostnames to be
// lowercased, explicitly set this using `SetLowerHostname(true)`.
package providers
9 changes: 8 additions & 1 deletion providers/linux/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/prometheus/procfs"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)
Expand Down Expand Up @@ -176,7 +178,8 @@ func newHost(fs procFS) (*host, error) {
}

type reader struct {
errs []error
errs []error
lowerHostname bool
}

func (r *reader) addErr(err error) bool {
Expand Down Expand Up @@ -233,6 +236,10 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}

if providers.LowercaseHostname() {
v = strings.ToLower(v)
}
h.info.Hostname = v
}

Expand Down
17 changes: 17 additions & 0 deletions providers/lowerhostname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package providers

Check failure on line 1 in providers/lowerhostname.go

View workflow job for this annotation

GitHub Actions / check

is missing the license header

import "sync/atomic"

var lowerHostname atomic.Bool

// LowercaseHostname returns if the hostname should be lowercased or not.
func LowercaseHostname() bool {
return lowerHostname.Load()
}

// SetLowerHostname instruct the host provider to lowercase the hostname.
// The LowercaseHostname and SetLowerHostname exist as a fix to allow the user
// to choose or not this behaviour introduced on v1.11.0 and reverted on v1.14.1.
func SetLowerHostname(lower bool) {
lowerHostname.Store(lower)
}
6 changes: 6 additions & 0 deletions providers/shared/fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"net"
"os"
"strings"

"github.com/elastic/go-sysinfo/providers"
)

// FQDNWithContext attempts to lookup the host's fully-qualified domain name and returns it.
Expand Down Expand Up @@ -67,6 +69,10 @@ func fqdn(ctx context.Context, hostname string) (string, error) {
if cname != "" {
cname = strings.TrimSuffix(cname, ".")

if providers.LowercaseHostname() {
return strings.ToLower(cname), nil
}

// 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
Expand Down
23 changes: 19 additions & 4 deletions providers/shared/fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ import (
"time"

"github.com/stretchr/testify/require"

"github.com/elastic/go-sysinfo/providers"
)

func TestFQDN(t *testing.T) {
lowercaseHostname := providers.LowercaseHostname()
defer func() {
providers.SetLowerHostname(lowercaseHostname)
}()

tests := map[string]struct {
osHostname string
expectedFQDN string
expectedErrRegex string
timeout time.Duration
lowercaseHostname bool
osHostname string
expectedFQDN string
expectedErrRegex string
timeout time.Duration
}{
// This test case depends on network, particularly DNS,
// being available. If it starts to fail often enough
Expand All @@ -59,6 +67,12 @@ func TestFQDN(t *testing.T) {
expectedFQDN: "eLaSTic.co",
expectedErrRegex: "",
},
"long_mixed_case_hostname_lowercaseHostname": {
lowercaseHostname: true,
osHostname: "eLaSTic.co",
expectedFQDN: "elastic.co",
expectedErrRegex: "",
},
"nonexistent_timeout": {
osHostname: "foobarbaz",
expectedFQDN: "",
Expand All @@ -77,6 +91,7 @@ func TestFQDN(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

providers.SetLowerHostname(test.lowercaseHostname)
actualFQDN, err := fqdn(ctx, test.osHostname)
require.Equal(t, test.expectedFQDN, actualFQDN)

Expand Down
7 changes: 6 additions & 1 deletion providers/windows/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (

stdwindows "golang.org/x/sys/windows"

windows "github.com/elastic/go-windows"
"github.com/elastic/go-windows"

"github.com/elastic/go-sysinfo/internal/registry"
"github.com/elastic/go-sysinfo/providers"
"github.com/elastic/go-sysinfo/providers/shared"
"github.com/elastic/go-sysinfo/types"
)
Expand Down Expand Up @@ -161,6 +162,10 @@ func (r *reader) hostname(h *host) {
if r.addErr(err) {
return
}

if providers.LowercaseHostname() {
v = strings.ToLower(v)
}
h.info.Hostname = v
}

Expand Down

0 comments on commit 0b70157

Please sign in to comment.