From 4eb30022b22fc43e711098111a503ff11d7939b0 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 28 Nov 2016 10:38:54 -0800 Subject: [PATCH 1/2] Use net.JoinHostPort instead of fmt.Sprintf Using fmt.Sprintf breaks IPv6 addresses. --- client/driver/env/env.go | 5 +++-- command/agent/config.go | 18 +++++++++--------- command/server_members.go | 3 ++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 2f9fc5c8cf8..d241db4bf15 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -2,6 +2,7 @@ package env import ( "fmt" + "net" "os" "strconv" "strings" @@ -153,8 +154,8 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { if forwardedPort, ok := t.PortMap[label]; ok { value = forwardedPort } - t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) - IPPort := fmt.Sprintf("%s:%d", network.IP, value) + t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = strconv.Itoa(value) + IPPort := net.JoinHostPort(network.IP, strconv.Itoa(value)) t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort } diff --git a/command/agent/config.go b/command/agent/config.go index dcfce9bf0d1..b8d99ce09c2 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -529,7 +529,7 @@ func (c *Config) Listener(proto, addr string, port int) (net.Listener, error) { Err: &net.AddrError{Err: "invalid port", Addr: fmt.Sprint(port)}, } } - return net.Listen(proto, fmt.Sprintf("%s:%d", addr, port)) + return net.Listen(proto, net.JoinHostPort(addr, strconv.Itoa(port))) } // Merge merges two configurations. @@ -677,9 +677,9 @@ func (c *Config) normalizeAddrs() error { c.Addresses.RPC = normalizeBind(c.Addresses.RPC, c.BindAddr) c.Addresses.Serf = normalizeBind(c.Addresses.Serf, c.BindAddr) c.normalizedAddrs = &Addresses{ - HTTP: fmt.Sprintf("%s:%d", c.Addresses.HTTP, c.Ports.HTTP), - RPC: fmt.Sprintf("%s:%d", c.Addresses.RPC, c.Ports.RPC), - Serf: fmt.Sprintf("%s:%d", c.Addresses.Serf, c.Ports.Serf), + HTTP: net.JoinHostPort(c.Addresses.HTTP, strconv.Itoa(c.Ports.HTTP)), + RPC: net.JoinHostPort(c.Addresses.RPC, strconv.Itoa(c.Ports.RPC)), + Serf: net.JoinHostPort(c.Addresses.Serf, strconv.Itoa(c.Ports.Serf)), } addr, err := normalizeAdvertise(c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode) @@ -735,7 +735,7 @@ func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string } // missing port, append the default - return fmt.Sprintf("%s:%d", addr, defport), nil + return net.JoinHostPort(addr, strconv.Itoa(defport)), nil } return addr, nil } @@ -749,11 +749,11 @@ func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string // Return the first unicast address for _, ip := range ips { if ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() { - return fmt.Sprintf("%s:%d", ip, defport), nil + return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil } if ip.IsLoopback() && dev { // loopback is fine for dev mode - return fmt.Sprintf("%s:%d", ip, defport), nil + return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil } } @@ -772,11 +772,11 @@ func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string // Return the first unicast address for _, ip := range ips { if ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() { - return fmt.Sprintf("%s:%d", ip, defport), nil + return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil } if ip.IsLoopback() && dev { // loopback is fine for dev mode - return fmt.Sprintf("%s:%d", ip, defport), nil + return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil } } return "", fmt.Errorf("No valid advertise addresses, please set `advertise` manually") diff --git a/command/server_members.go b/command/server_members.go index 378be3a6e00..7a752e13b78 100644 --- a/command/server_members.go +++ b/command/server_members.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "net" "sort" "strings" @@ -107,7 +108,7 @@ func standardOutput(mem []*api.AgentMember, leaders map[string]string) []string regLeader, ok := leaders[reg] isLeader := false if ok { - if regLeader == fmt.Sprintf("%s:%s", member.Addr, member.Tags["port"]) { + if regLeader == net.JoinHostPort(member.Addr, member.Tags["port"]) { isLeader = true } From 7dea7b528049b29e39ef8d2035391aa8333299c1 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 28 Nov 2016 10:57:51 -0800 Subject: [PATCH 2/2] Add changelog entry for #2036 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52ce7f971aa..9b585692d25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.5.1 (Unreleased) + +BUG FIXES: + * agent/config: Fix use of IPv6 addresses [GH-2036] + ## 0.5.0 (November 16, 2016) __BACKWARDS INCOMPATIBILITIES:__