Skip to content

Commit 8a5dd08

Browse files
fix: don't return localhost as FQDN in macos (#693)
1 parent 68d6431 commit 8a5dd08

File tree

4 files changed

+140
-21
lines changed

4 files changed

+140
-21
lines changed

pkg/sysinfo/hostname/hostname.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ package hostname
55
import (
66
"errors"
77
"fmt"
8-
"net"
98
"os"
10-
"strings"
119
"sync"
1210

1311
"github.com/newrelic/infrastructure-agent/pkg/log"
@@ -107,24 +105,6 @@ func newInternalResolver(overrideFull string) *fallbackResolver {
107105
}
108106
}
109107

110-
// Looks up for the Fully Qualified Domain Name
111-
func getFqdnHostname(osHost string) (string, error) {
112-
ips, err := net.LookupIP(osHost)
113-
if err != nil {
114-
return "", err
115-
}
116-
117-
for _, ip := range ips {
118-
hosts, err := net.LookupAddr(ip.String())
119-
if err != nil || len(hosts) == 0 {
120-
return "", err
121-
}
122-
trace.Hostname("found FQDN hosts: %s", strings.Join(hosts, ", "))
123-
return strings.TrimSuffix(hosts[0], "."), nil
124-
}
125-
return "", errors.New("can't lookup FQDN")
126-
}
127-
128108
// Implementation of the HostnameResolver interface that provides fallback capabilities
129109
// in case the full name resolving fails, returning the last successful value.
130110
// If the Full name resolution is "localhost" (known problem in some wrong FQDN configurations)

pkg/sysinfo/hostname/hostname_darwin.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// Copyright 2020 New Relic Corporation. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
3+
// +build darwin
4+
35
package hostname
46

5-
import "github.com/newrelic/infrastructure-agent/pkg/helpers"
7+
import (
8+
"errors"
9+
"github.com/newrelic/infrastructure-agent/pkg/helpers"
10+
"github.com/newrelic/infrastructure-agent/pkg/trace"
11+
"net"
12+
"strings"
13+
)
614

715
// attempts to determine the hostname, gracefully falling back until we
816
// run out of options
@@ -22,3 +30,30 @@ func internalHostname() (hn string, err error) {
2230
// return whatever we did get including the error
2331
return
2432
}
33+
34+
type ipLookupper func(host string) ([]net.IP, error)
35+
type addrLookupper func(addr string) (names []string, err error)
36+
37+
var lookupIp ipLookupper = net.LookupIP
38+
var lookupAddr addrLookupper = net.LookupAddr
39+
40+
// Looks up for the Fully Qualified Domain Name. Do not take into account localhost as FQDN
41+
func getFqdnHostname(osHost string) (string, error) {
42+
ips, err := lookupIp(osHost)
43+
if err != nil {
44+
return "", err
45+
}
46+
47+
for _, ip := range ips {
48+
hosts, err := lookupAddr(ip.String())
49+
if err != nil || len(hosts) == 0 {
50+
return "", err
51+
}
52+
if hosts[0] == "localhost" {
53+
continue
54+
}
55+
trace.Hostname("found FQDN hosts: %s", strings.Join(hosts, ", "))
56+
return strings.TrimSuffix(hosts[0], "."), nil
57+
}
58+
return "", errors.New("can't lookup FQDN")
59+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2020 New Relic Corporation. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build darwin
4+
5+
package hostname
6+
7+
import (
8+
"errors"
9+
"github.com/stretchr/testify/assert"
10+
"net"
11+
"testing"
12+
)
13+
14+
func ipLookUpperMock(ips []net.IP, err error) func(host string) ([]net.IP, error) {
15+
return func(_ string) ([]net.IP, error) {
16+
return ips, err
17+
}
18+
}
19+
20+
func addrLookUpperMock(names map[string][]string, err error) func(addr string) (names []string, err error) {
21+
return func(lookUpIp string) ([]string, error) {
22+
return names[lookUpIp], err
23+
}
24+
}
25+
26+
func TestGetFqdnHostname_DontReturnLocalhost(t *testing.T) {
27+
28+
tests := []struct {
29+
name string
30+
ips []net.IP
31+
fqdns map[string][]string
32+
ipLookupError error
33+
addrLookupError error
34+
expectedHostname string
35+
expectedErr error
36+
}{
37+
{
38+
name: "no ips should return error",
39+
expectedErr: errors.New("can't lookup FQDN"),
40+
expectedHostname: "",
41+
},
42+
{
43+
name: "only localhost should return error",
44+
ips: []net.IP{net.ParseIP("127.0.0.1")},
45+
fqdns: map[string][]string{"127.0.0.1": {"localhost"}},
46+
expectedErr: errors.New("can't lookup FQDN"),
47+
expectedHostname: "",
48+
},
49+
{
50+
name: "first valid fqdn should be returned",
51+
ips: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.1.1"), net.ParseIP("10.0.0.1")},
52+
fqdns: map[string][]string{
53+
"127.0.0.1": {"localhost"},
54+
"192.168.1.1": {"test.local"},
55+
"10.0.0.1": {"another.test.local"},
56+
},
57+
expectedHostname: "test.local",
58+
},
59+
}
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
lookupIp = ipLookUpperMock(tt.ips, tt.ipLookupError)
64+
lookupAddr = addrLookUpperMock(tt.fqdns, tt.addrLookupError)
65+
66+
hostname, err := getFqdnHostname("some")
67+
68+
assert.Equal(t, tt.expectedErr, err)
69+
assert.Equal(t, tt.expectedHostname, hostname)
70+
})
71+
}
72+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 New Relic Corporation. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux windows
4+
5+
package hostname
6+
7+
import (
8+
"errors"
9+
"github.com/newrelic/infrastructure-agent/pkg/trace"
10+
"net"
11+
"strings"
12+
)
13+
14+
// Looks up for the Fully Qualified Domain Name.
15+
// `localhost` should not be returned as FQDN, but until deciding how it affects we will maintain this version
16+
// for linux and windows for backwards compatibility
17+
func getFqdnHostname(osHost string) (string, error) {
18+
ips, err := net.LookupIP(osHost)
19+
if err != nil {
20+
return "", err
21+
}
22+
23+
for _, ip := range ips {
24+
hosts, err := net.LookupAddr(ip.String())
25+
if err != nil || len(hosts) == 0 {
26+
return "", err
27+
}
28+
trace.Hostname("found FQDN hosts: %s", strings.Join(hosts, ", "))
29+
return strings.TrimSuffix(hosts[0], "."), nil
30+
}
31+
return "", errors.New("can't lookup FQDN")
32+
}

0 commit comments

Comments
 (0)