Skip to content

Commit

Permalink
Fix IPv6 addresses formatting
Browse files Browse the repository at this point in the history
When joining an ipv6 host and a port, the host must be wrapped between brackets, otherwise the port will become part of the host.
  • Loading branch information
jooola authored and lbajolet-hashicorp committed Jun 6, 2024
1 parent 1a2b341 commit 9c1d243
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions communicator/step_connect_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"context"
"errors"
"fmt"
"golang.org/x/term"
"io"
"log"
"net"
"os"
"strings"
"time"

"golang.org/x/term"

helperssh "github.com/hashicorp/packer-plugin-sdk/communicator/ssh"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -101,8 +102,7 @@ func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, ctx context.Contex
if s.Config.SSHBastionHost != "" {
// The protocol is hardcoded for now, but may be configurable one day
bProto = "tcp"
bAddr = fmt.Sprintf(
"%s:%d", s.Config.SSHBastionHost, s.Config.SSHBastionPort)
bAddr = net.JoinHostPort(s.Config.SSHBastionHost, fmt.Sprint(s.Config.SSHBastionPort))

conf, err := sshBastionConfig(s.Config)
if err != nil {
Expand All @@ -112,7 +112,7 @@ func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, ctx context.Contex
}

if s.Config.SSHProxyHost != "" {
pAddr = fmt.Sprintf("%s:%d", s.Config.SSHProxyHost, s.Config.SSHProxyPort)
pAddr = net.JoinHostPort(s.Config.SSHProxyHost, fmt.Sprint(s.Config.SSHProxyPort))
if s.Config.SSHProxyUsername != "" {
pAuth = new(proxy.Auth)
pAuth.User = s.Config.SSHProxyUsername
Expand Down Expand Up @@ -165,7 +165,7 @@ func (s *StepConnectSSH) waitForSSH(state multistep.StateBag, ctx context.Contex

// Attempt to connect to SSH port
var connFunc func() (net.Conn, error)
address := fmt.Sprintf("%s:%d", host, port)
address := net.JoinHostPort(host, fmt.Sprint(port))
if bAddr != "" {
log.Printf("[INFO] connecting with SSH to host %s through bastion at %s",
address, bAddr)
Expand Down
3 changes: 2 additions & 1 deletion communicator/step_connect_winrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -213,7 +214,7 @@ func (s *StepConnectWinRM) waitForWinRM(state multistep.StateBag, ctx context.Co
// setNoProxy configures the $NO_PROXY env var
func setNoProxy(host string, port int) error {
current := os.Getenv("NO_PROXY")
p := fmt.Sprintf("%s:%d", host, port)
p := net.JoinHostPort(host, fmt.Sprint(port))
if current == "" {
return os.Setenv("NO_PROXY", p)
}
Expand Down
3 changes: 2 additions & 1 deletion multistep/commonsteps/step_provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package commonsteps

import (
"fmt"
"net"
"os"
"testing"

Expand Down Expand Up @@ -43,7 +44,7 @@ func TestPopulateProvisionHookData(t *testing.T) {
packerRunUUID := "1fa225b8-27d1-42d1-9117-221772213962"
httpIP := "10.0.2.2"
httpPort := 2222
httpAddr := fmt.Sprintf("%s:%d", httpIP, httpPort)
httpAddr := net.JoinHostPort(httpIP, fmt.Sprint(httpPort))

state.Put("generated_data", generatedData)
state.Put("instance_id", instanceId)
Expand Down
2 changes: 1 addition & 1 deletion net/configure_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
return ErrPortFileLocked(port)
}

l, err := lc.ListenConfig.Listen(ctx, lc.Network, fmt.Sprintf("%s:%d", lc.Addr, port))
l, err := lc.ListenConfig.Listen(ctx, lc.Network, net.JoinHostPort(lc.Addr, fmt.Sprint(port)))
if err != nil {
if err := lock.Unlock(); err != nil {
log.Fatalf("Could not unlock file lock for port %d: %v", port, err)
Expand Down
3 changes: 2 additions & 1 deletion sdk-internals/communicator/winrm/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -193,7 +194,7 @@ func (c *Communicator) getClientConfig() *winrmcp.Config {
}

func (c *Communicator) newCopyClient() (*winrmcp.Winrmcp, error) {
addr := fmt.Sprintf("%s:%d", c.endpoint.Host, c.endpoint.Port)
addr := net.JoinHostPort(c.endpoint.Host, fmt.Sprint(c.endpoint.Port))
clientConfig := c.getClientConfig()
return winrmcp.New(addr, clientConfig)
}
Expand Down

0 comments on commit 9c1d243

Please sign in to comment.