Skip to content

Commit 0ff1775

Browse files
Use net.JoinHostPort for host:port formatting to handle IPv6 addresses (#22359)
* refactor: use net.JoinHostPort for address formatting This change improves reliability and correctness when handling IPv6 addresses * added changelog * Update .changelog/22359.txt Co-authored-by: Sreeram Narayanan <[email protected]> * Update .changelog/22359.txt Co-authored-by: Sreeram Narayanan <[email protected]> --------- Co-authored-by: Sreeram Narayanan <[email protected]>
1 parent 8655438 commit 0ff1775

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

.changelog/22359.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
connect: Use net.JoinHostPort for host:port formatting to handle IPv6.
3+
```

agent/structs/config_entry_gateways.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package structs
66
import (
77
"encoding/json"
88
"fmt"
9+
"net"
910
"regexp"
1011
"sort"
12+
"strconv"
1113
"strings"
1214

1315
"github.com/miekg/dns"
@@ -691,7 +693,8 @@ func (g *GatewayService) Addresses(defaultHosts []string) []string {
691693
// ensuring we trim any trailing DNS . characters from the domain name as we
692694
// go
693695
for _, h := range hosts {
694-
addresses = append(addresses, fmt.Sprintf("%s:%d", strings.TrimRight(h, "."), g.Port))
696+
host := strings.TrimRight(h, ".")
697+
addresses = append(addresses, net.JoinHostPort(host, strconv.Itoa(g.Port)))
695698
}
696699
return addresses
697700
}

agent/xds/routes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net"
1010
"sort"
11+
"strconv"
1112
"strings"
1213
"time"
1314

@@ -615,7 +616,7 @@ func generateUpstreamIngressDomains(listenerKey proxycfg.IngressListenerKey, u s
615616
continue
616617
}
617618

618-
domainWithPort := fmt.Sprintf("%s:%d", h, listenerKey.Port)
619+
domainWithPort := net.JoinHostPort(h, strconv.Itoa(listenerKey.Port))
619620

620621
// Do not add a duplicate domain if a hostname with port is already in the
621622
// set

command/connect/envoy/envoy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ func (c *cmd) generateConfig() ([]byte, error) {
762762
}
763763

764764
if c.envoyReadyBindAddress != "" && c.envoyReadyBindPort != 0 {
765-
bsCfg.ReadyBindAddr = fmt.Sprintf("%s:%d", c.envoyReadyBindAddress, c.envoyReadyBindPort)
765+
bsCfg.ReadyBindAddr = net.JoinHostPort(c.envoyReadyBindAddress, strconv.Itoa(c.envoyReadyBindPort))
766766
}
767767

768768
if !c.disableCentralConfig {

command/connect/envoy/flags.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type ServiceAddressValue struct {
2424

2525
func (s *ServiceAddressValue) String() string {
2626
if s == nil || (s.value.Port == 0 && s.value.Address == "") {
27-
return fmt.Sprintf(":%d", defaultGatewayPort)
27+
return net.JoinHostPort("", strconv.Itoa(defaultGatewayPort))
2828
}
29-
return fmt.Sprintf("%v:%d", s.value.Address, s.value.Port)
29+
return net.JoinHostPort(s.value.Address, strconv.Itoa(s.value.Port))
3030
}
3131

3232
func (s *ServiceAddressValue) Value() api.ServiceAddress {
@@ -85,7 +85,8 @@ type ServiceAddressMapValue struct {
8585
func (s *ServiceAddressMapValue) String() string {
8686
buf := new(strings.Builder)
8787
for k, v := range s.value {
88-
buf.WriteString(fmt.Sprintf("%v=%v:%d,", k, v.Address, v.Port))
88+
addr := net.JoinHostPort(v.Address, strconv.Itoa(v.Port))
89+
buf.WriteString(fmt.Sprintf("%v=%v,", k, addr))
8990
}
9091
return buf.String()
9192
}

command/connect/proxy/proxy.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@ func (c *cmd) configWatcher(client *api.Client) (proxyImpl.ConfigWatcher, error)
323323

324324
addr := config.LocalBindSocketPath
325325
if addr == "" {
326-
addr = fmt.Sprintf(
327-
"%s:%d",
328-
config.LocalBindAddress, config.LocalBindPort)
326+
addr = net.JoinHostPort(config.LocalBindAddress, strconv.Itoa(config.LocalBindPort))
329327
}
330328

331329
c.UI.Info(fmt.Sprintf(

connect/proxy/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package proxy
55

66
import (
77
"fmt"
8+
"net"
9+
"strconv"
810
"time"
911

1012
"github.com/mitchellh/mapstructure"
@@ -122,9 +124,7 @@ func (uc *UpstreamConfig) applyDefaults() {
122124
func (uc *UpstreamConfig) String() string {
123125
addr := uc.LocalBindSocketPath
124126
if addr == "" {
125-
addr = fmt.Sprintf(
126-
"%s:%d",
127-
uc.LocalBindAddress, uc.LocalBindPort)
127+
addr = net.JoinHostPort(uc.LocalBindAddress, strconv.Itoa(uc.LocalBindPort))
128128
}
129129
return fmt.Sprintf("%s->%s:%s/%s/%s", addr,
130130
uc.DestinationType, uc.DestinationPartition, uc.DestinationNamespace, uc.DestinationName)

0 commit comments

Comments
 (0)