Default to private IP advertise address in non-dev mode
This commit is contained in:
parent
3c5c49bedb
commit
562deb6262
|
@ -727,13 +727,13 @@ func (c *Config) normalizeAddrs() error {
|
||||||
Serf: net.JoinHostPort(c.Addresses.Serf, strconv.Itoa(c.Ports.Serf)),
|
Serf: net.JoinHostPort(c.Addresses.Serf, strconv.Itoa(c.Ports.Serf)),
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err = normalizeAdvertise(c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP)
|
addr, err = normalizeAdvertise(c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to parse HTTP advertise address: %v", err)
|
return fmt.Errorf("Failed to parse HTTP advertise address: %v", err)
|
||||||
}
|
}
|
||||||
c.AdvertiseAddrs.HTTP = addr
|
c.AdvertiseAddrs.HTTP = addr
|
||||||
|
|
||||||
addr, err = normalizeAdvertise(c.AdvertiseAddrs.RPC, c.Addresses.RPC, c.Ports.RPC)
|
addr, err = normalizeAdvertise(c.AdvertiseAddrs.RPC, c.Addresses.RPC, c.Ports.RPC, c.DevMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to parse RPC advertise address: %v", err)
|
return fmt.Errorf("Failed to parse RPC advertise address: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -741,7 +741,7 @@ func (c *Config) normalizeAddrs() error {
|
||||||
|
|
||||||
// Skip serf if server is disabled
|
// Skip serf if server is disabled
|
||||||
if c.Server != nil && c.Server.Enabled {
|
if c.Server != nil && c.Server.Enabled {
|
||||||
addr, err = normalizeAdvertise(c.AdvertiseAddrs.Serf, c.Addresses.Serf, c.Ports.Serf)
|
addr, err = normalizeAdvertise(c.AdvertiseAddrs.Serf, c.Addresses.Serf, c.Ports.Serf, c.DevMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to parse Serf advertise address: %v", err)
|
return fmt.Errorf("Failed to parse Serf advertise address: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -793,7 +793,7 @@ func normalizeBind(addr, bind string) (string, error) {
|
||||||
// is resolved and returned with the port.
|
// is resolved and returned with the port.
|
||||||
//
|
//
|
||||||
// Loopback is only considered a valid advertise address in dev mode.
|
// Loopback is only considered a valid advertise address in dev mode.
|
||||||
func normalizeAdvertise(addr string, bind string, defport int) (string, error) {
|
func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string, error) {
|
||||||
addr, err := parseSingleIPTemplate(addr)
|
addr, err := parseSingleIPTemplate(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Error parsing advertise address template: %v", err)
|
return "", fmt.Errorf("Error parsing advertise address template: %v", err)
|
||||||
|
@ -813,8 +813,29 @@ func normalizeAdvertise(addr string, bind string, defport int) (string, error) {
|
||||||
return net.JoinHostPort(host, port), nil
|
return net.JoinHostPort(host, port), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to bind address, as it has been resolved before.
|
// Fallback to bind address first, and then try resolving the local hostname
|
||||||
return net.JoinHostPort(bind, strconv.Itoa(defport)), nil
|
ips, err := net.LookupIP(bind)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Error resolving bind address %q: %v", bind, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the first unicast address
|
||||||
|
for _, ip := range ips {
|
||||||
|
if ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() {
|
||||||
|
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
|
||||||
|
}
|
||||||
|
if !ip.IsLoopback() || (ip.IsLoopback() && dev) {
|
||||||
|
// loopback is fine for dev mode
|
||||||
|
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, default to the private IP address
|
||||||
|
addr, err = parseSingleIPTemplate("{{ GetPrivateIP }}")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Unable to parse default advertise address: %v", err)
|
||||||
|
}
|
||||||
|
return net.JoinHostPort(addr, strconv.Itoa(defport)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isMissingPort returns true if an error is a "missing port" error from
|
// isMissingPort returns true if an error is a "missing port" error from
|
||||||
|
|
|
@ -522,6 +522,7 @@ func TestConfig_Listener(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig_normalizeAddrs(t *testing.T) {
|
func TestConfig_normalizeAddrs(t *testing.T) {
|
||||||
|
// allow to advertise 127.0.0.1 if dev-mode is enabled
|
||||||
c := &Config{
|
c := &Config{
|
||||||
BindAddr: "127.0.0.1",
|
BindAddr: "127.0.0.1",
|
||||||
Ports: &Ports{
|
Ports: &Ports{
|
||||||
|
@ -567,6 +568,35 @@ func TestConfig_normalizeAddrs(t *testing.T) {
|
||||||
t.Fatalf("expected unset Serf advertise address, got %s", c.AdvertiseAddrs.Serf)
|
t.Fatalf("expected unset Serf advertise address, got %s", c.AdvertiseAddrs.Serf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// default to non-localhost address in non-dev mode
|
||||||
|
c = &Config{
|
||||||
|
BindAddr: "127.0.0.1",
|
||||||
|
Ports: &Ports{
|
||||||
|
HTTP: 4646,
|
||||||
|
RPC: 4647,
|
||||||
|
Serf: 4648,
|
||||||
|
},
|
||||||
|
Addresses: &Addresses{},
|
||||||
|
AdvertiseAddrs: &AdvertiseAddrs{},
|
||||||
|
DevMode: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.normalizeAddrs(); err != nil {
|
||||||
|
t.Fatalf("unable to normalize addresses: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.AdvertiseAddrs.HTTP == "127.0.0.1:4646" {
|
||||||
|
t.Fatalf("expected non-localhost HTTP advertise address, got %s", c.AdvertiseAddrs.HTTP)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.AdvertiseAddrs.RPC == "127.0.0.1:4647" {
|
||||||
|
t.Fatalf("expected non-localhost RPC advertise address, got %s", c.AdvertiseAddrs.RPC)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.AdvertiseAddrs.Serf == "127.0.0.1:4648" {
|
||||||
|
t.Fatalf("expected non-localhost Serf advertise address, got %s", c.AdvertiseAddrs.Serf)
|
||||||
|
}
|
||||||
|
|
||||||
c = &Config{
|
c = &Config{
|
||||||
BindAddr: "169.254.1.5",
|
BindAddr: "169.254.1.5",
|
||||||
Ports: &Ports{
|
Ports: &Ports{
|
||||||
|
|
Loading…
Reference in a new issue