[lxc-devel] [lxd/master] Network: Use net.JoinHostPort rather than manual fmt.Sprintf
tomponline on Github
lxc-bot at linuxcontainers.org
Tue Aug 25 11:31:15 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 764 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200825/d1940945/attachment-0001.bin>
-------------- next part --------------
From f2445d96042831054d7a5243d51ad6a8a988fa5b Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 25 Aug 2020 12:23:31 +0100
Subject: [PATCH 1/6] lxd/main/init/interactive: Use net.JoinHostPort rather
than manual fmt.Sprintf
Which also adds IPv6 support for listeners as this wasn't handled.
Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
lxd/main_init_interactive.go | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lxd/main_init_interactive.go b/lxd/main_init_interactive.go
index ad3b42133f..f6cfe3b33f 100644
--- a/lxd/main_init_interactive.go
+++ b/lxd/main_init_interactive.go
@@ -675,7 +675,7 @@ they otherwise would.
}
netPort := cli.AskInt("Port to bind LXD to [default=8443]: ", 1, 65535, "8443", func(netPort int64) error {
- address := fmt.Sprintf("%s:%d", netAddr, netPort)
+ address := net.JoinHostPort(netAddr, strconv.Itoa(int(netPort)))
s, _, err := d.GetServer()
if err == nil {
@@ -693,7 +693,7 @@ they otherwise would.
listener.Close()
return nil
})
- config.Node.Config["core.https_address"] = fmt.Sprintf("%s:%d", netAddr, netPort)
+ config.Node.Config["core.https_address"] = net.JoinHostPort(netAddr, strconv.Itoa(int(netPort)))
config.Node.Config["core.trust_password"] = cli.AskPassword("Trust password for new clients: ")
if config.Node.Config["core.trust_password"] == "" {
fmt.Printf("No password set, client certificates will have to be manually trusted.")
From 9905b2849e3c7a59697a8598118c42305521fe59 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 25 Aug 2020 12:23:12 +0100
Subject: [PATCH 2/6] lxd/main/init/interactive: Error wrapping
Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
lxd/main_init_interactive.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lxd/main_init_interactive.go b/lxd/main_init_interactive.go
index f6cfe3b33f..8653bd7217 100644
--- a/lxd/main_init_interactive.go
+++ b/lxd/main_init_interactive.go
@@ -135,7 +135,7 @@ func (c *cmdInit) askClustering(config *cmdInitData, d lxd.InstanceServer) error
listener, err := net.Listen("tcp", address)
if err != nil {
- return fmt.Errorf("Can't bind address %q: %v", address, err)
+ return errors.Wrapf(err, "Can't bind address %q", address)
}
listener.Close()
From c14fd8655df0c13558b86b279e9f275ec9006846 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 25 Aug 2020 12:22:20 +0100
Subject: [PATCH 3/6] lxd/main/init/interactive: Use canonical address after
port has been added for comparison
Otherwise if default port is added, the listen address doesn't match the supplied host IP and a duplicate bind is attempted.
Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
lxd/main_init_interactive.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lxd/main_init_interactive.go b/lxd/main_init_interactive.go
index 8653bd7217..403f39741c 100644
--- a/lxd/main_init_interactive.go
+++ b/lxd/main_init_interactive.go
@@ -127,7 +127,7 @@ func (c *cmdInit) askClustering(config *cmdInitData, d lxd.InstanceServer) error
s, _, err := d.GetServer()
if err == nil {
- if s.Config["cluster.https_address"] == value || s.Config["core.https_address"] == value {
+ if s.Config["cluster.https_address"] == address || s.Config["core.https_address"] == address {
// We already own the address, just move on.
return nil
}
From ef4fad27e2475634766612696b197504b606b15a Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 25 Aug 2020 12:20:57 +0100
Subject: [PATCH 4/6] lxd/main/init/auto: Use net.JoinHostPort rather than
manual fmt.Sprintf
Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
lxd/main_init_auto.go | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lxd/main_init_auto.go b/lxd/main_init_auto.go
index 19840d0a59..56ac325e7a 100644
--- a/lxd/main_init_auto.go
+++ b/lxd/main_init_auto.go
@@ -2,6 +2,8 @@ package main
import (
"fmt"
+ "net"
+ "strconv"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -66,7 +68,7 @@ func (c *cmdInit) RunAuto(cmd *cobra.Command, args []string, d lxd.InstanceServe
// Network listening
if c.flagNetworkAddress != "" {
- config.Config["core.https_address"] = fmt.Sprintf("%s:%d", c.flagNetworkAddress, c.flagNetworkPort)
+ config.Config["core.https_address"] = net.JoinHostPort(c.flagNetworkAddress, strconv.Itoa(c.flagNetworkPort))
if c.flagTrustPassword != "" {
config.Config["core.trust_password"] = c.flagTrustPassword
From e5e7c5d9cc97d45dacb5a0a7e5d85916688415f7 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 25 Aug 2020 12:20:17 +0100
Subject: [PATCH 5/6] lxd/util/net: Updates CanonicalNetworkAddress to use
net.JoinHostPort rather than manual fmt.Sprintf
Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
lxd/util/net.go | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/lxd/util/net.go b/lxd/util/net.go
index b5a4046523..f2831cab9a 100644
--- a/lxd/util/net.go
+++ b/lxd/util/net.go
@@ -68,19 +68,17 @@ func (a *inMemoryAddr) String() string {
return ""
}
-// CanonicalNetworkAddress parses the given network address and returns a
-// string of the form "host:port", possibly filling it with the default port if
-// it's missing.
+// CanonicalNetworkAddress parses the given network address and returns a string of the form "host:port",
+// possibly filling it with the default port if it's missing.
func CanonicalNetworkAddress(address string) string {
_, _, err := net.SplitHostPort(address)
if err != nil {
ip := net.ParseIP(address)
- if ip != nil && ip.To4() == nil {
- address = fmt.Sprintf("[%s]:%s", address, shared.DefaultPort)
- } else {
- address = fmt.Sprintf("%s:%s", address, shared.DefaultPort)
+ if ip != nil {
+ address = net.JoinHostPort(ip.String(), shared.DefaultPort)
}
}
+
return address
}
From 8418016b730b641e8fddea8a94074a0cbfcbb249 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 25 Aug 2020 12:19:43 +0100
Subject: [PATCH 6/6] lxd/device/device/utils/proxy: Use net.JoinHostPort
rather than manual fmt.Sprintf
Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
lxd/device/device_utils_proxy.go | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/lxd/device/device_utils_proxy.go b/lxd/device/device_utils_proxy.go
index 6aae2a5b87..29d53eeb7a 100644
--- a/lxd/device/device_utils_proxy.go
+++ b/lxd/device/device_utils_proxy.go
@@ -3,6 +3,7 @@ package device
import (
"fmt"
"net"
+ "strconv"
"strings"
deviceConfig "github.com/lxc/lxd/lxd/device/config"
@@ -53,13 +54,7 @@ func ProxyParseAddr(addr string) (*deviceConfig.ProxyAddress, error) {
}
for i := int64(0); i < portRange; i++ {
- var newAddr string
- if strings.Contains(address, ":") {
- // IPv6 addresses need to be enclosed in square brackets.
- newAddr = fmt.Sprintf("[%s]:%d", address, portFirst+i)
- } else {
- newAddr = fmt.Sprintf("%s:%d", address, portFirst+i)
- }
+ newAddr := net.JoinHostPort(address, strconv.Itoa(int(portFirst+i)))
newProxyAddr.Addr = append(newProxyAddr.Addr, newAddr)
}
}
More information about the lxc-devel
mailing list