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 6ed87ca
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/236.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
30 changes: 30 additions & 0 deletions providers/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// Package providers
//
// # 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
34 changes: 34 additions & 0 deletions providers/lowerhostname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package providers

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 6ed87ca

Please sign in to comment.