Skip to content

Commit

Permalink
Dial by UUID for label based ssh (#16257) (#16345)
Browse files Browse the repository at this point in the history
Switches label based ssh to always use the node UUID instead of its
address. This prevents issues that occurred when dialing by address
if the public_addr isn't set correctly.

Closes #3214
  • Loading branch information
rosstimothy committed Sep 13, 2022
1 parent 285da95 commit 8bbb83e
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions lib/client/api.go
Expand Up @@ -1273,37 +1273,31 @@ func (tc *TeleportClient) getTargetNodes(ctx context.Context, proxy *ProxyClient
)
defer span.End()

var (
err error
nodes []types.Server
retval = make([]string, 0)
)
if tc.Labels != nil && len(tc.Labels) > 0 {
nodes, err = proxy.FindServersByLabels(ctx, tc.Namespace, tc.Labels)
if err != nil {
return nil, trace.Wrap(err)
}
for i := 0; i < len(nodes); i++ {
addr := nodes[i].GetAddr()
if addr == "" {
// address is empty, try dialing by UUID instead.
addr = fmt.Sprintf("%s:0", nodes[i].GetName())
}
retval = append(retval, addr)
}
}
if len(nodes) == 0 {
// use the target node that was explicitly provided if valid
if len(tc.Labels) == 0 {
// detect the common error when users use host:port address format
_, port, err := net.SplitHostPort(tc.Host)
// client has used host:port notation
if err == nil {
return nil, trace.BadParameter(
"please use ssh subcommand with '--port=%v' flag instead of semicolon",
port)
return nil, trace.BadParameter("please use ssh subcommand with '--port=%v' flag instead of semicolon", port)
}

addr := net.JoinHostPort(tc.Host, strconv.Itoa(tc.HostPort))
retval = append(retval, addr)
return []string{addr}, nil
}

// find the nodes matching the labels that were provided
nodes, err := proxy.FindServersByLabels(ctx, tc.Namespace, tc.Labels)
if err != nil {
return nil, trace.Wrap(err)
}

retval := make([]string, 0, len(nodes))
for i := 0; i < len(nodes); i++ {
// always dial nodes by UUID
retval = append(retval, fmt.Sprintf("%s:0", nodes[i].GetName()))
}

return retval, nil
}

Expand Down

0 comments on commit 8bbb83e

Please sign in to comment.