Skip to content

Commit

Permalink
fix(provider): do not blindly use first IP for SSH (#704)
Browse files Browse the repository at this point in the history
For SSH access, try in order:
- IPv4 address of the interface with IPv4 Gateway (if there is one)
- IPv6 address of the interface with IPv6 Gateway (if there is one)
- fallback to the first interface with IPv4 address

Signed-off-by: Oto Petřík <oto.petrik@gmail.com>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
  • Loading branch information
otopetrik and bpg authored Nov 10, 2023
1 parent d2b8ce1 commit a586d03
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
23 changes: 22 additions & 1 deletion fwprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,34 @@ func (r *apiResolver) Resolve(ctx context.Context, nodeName string) (ssh.Proxmox

nodeAddress := ""

// try IPv4 address on the interface with IPv4 gateway
for _, d := range networkDevices {
if d.Address != nil {
if d.Gateway != nil && d.Address != nil {
nodeAddress = *d.Address
break
}
}

if nodeAddress == "" {
// fallback 1: try IPv6 address on the interface with IPv6 gateway
for _, d := range networkDevices {
if d.Gateway6 != nil && d.Address6 != nil {
nodeAddress = *d.Address6
break
}
}
}

if nodeAddress == "" {
// fallback 2: use first interface with any IPv4 address
for _, d := range networkDevices {
if d.Address != nil {
nodeAddress = *d.Address
break
}
}
}

if nodeAddress == "" {
return ssh.ProxmoxNode{}, fmt.Errorf("failed to determine the IP address of node \"%s\"", nc.NodeName)
}
Expand Down
23 changes: 22 additions & 1 deletion proxmoxtf/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,34 @@ func (r *apiResolver) Resolve(ctx context.Context, nodeName string) (ssh.Proxmox

nodeAddress := ""

// try IPv4 address on the interface with IPv4 gateway
for _, d := range networkDevices {
if d.Address != nil {
if d.Gateway != nil && d.Address != nil {
nodeAddress = *d.Address
break
}
}

if nodeAddress == "" {
// fallback 1: try IPv6 address on the interface with IPv6 gateway
for _, d := range networkDevices {
if d.Gateway6 != nil && d.Address6 != nil {
nodeAddress = *d.Address6
break
}
}
}

if nodeAddress == "" {
// fallback 2: use first interface with any IPv4 address
for _, d := range networkDevices {
if d.Address != nil {
nodeAddress = *d.Address
break
}
}
}

if nodeAddress == "" {
return ssh.ProxmoxNode{}, fmt.Errorf("failed to determine the IP address of node \"%s\"", nc.NodeName)
}
Expand Down

0 comments on commit a586d03

Please sign in to comment.