Skip to content

Commit

Permalink
Fix direct node dial from WebUI (gravitational#20785)
Browse files Browse the repository at this point in the history
* Fix direct node dial from WebUI

* Apply suggestion.

* Revert last commit

* Add test

* Add comment

* Wait for the node registration in UT
  • Loading branch information
jakule authored Jan 30, 2023
1 parent 34e9b44 commit 21331bf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,13 @@ func (h *Handler) generateSession(ctx context.Context, clt auth.ClientI, req *Te
}

if len(resources) == 0 {
return session.Session{}, trace.NotFound("no matching servers")
// If we didn't find the resource set host and port,
// so we can try direct dial.
host, port, err = serverHostPort(req.Server)
if err != nil {
return session.Session{}, trace.Wrap(err)
}
id = host
}

matches := 0
Expand Down
69 changes: 69 additions & 0 deletions lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,75 @@ func TestTerminalRouting(t *testing.T) {
}
}

func TestTerminalNameResolution(t *testing.T) {
t.Parallel()
s := newWebSuite(t)
pack := s.authPack(t, "foo")

llama := s.addNode(t, uuid.NewString(), "llama", "127.0.0.1:0")

ctx, cancel := context.WithTimeout(context.Background(), 7*time.Second)
t.Cleanup(cancel)

// Wait for the node to be registered as the registration is asynchronous.
require.Eventuallyf(t, func() bool {
nodes, err := s.proxyClient.GetNodes(ctx, "default")
require.NoError(t, err)

return len(nodes) == 2 // one created by default and llama
}, 5*time.Second, 200*time.Millisecond, "failed to register node")

tests := []struct {
name string
target string
serverID string
serverHostname string
port int
}{
{
name: "registered node by name",
target: "llama",
serverID: llama.ID(),
serverHostname: "llama",
},
{
name: "registered node by address",
target: llama.Addr(),
serverID: llama.ID(),
serverHostname: llama.Addr(),
},
{
name: "direct dial",
target: "[email protected]",
serverID: "[email protected]",
serverHostname: "[email protected]",
},
{
name: "direct dial with port",
target: "[email protected]:1234",
serverID: "[email protected]",
serverHostname: "[email protected]",
port: 1234,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

ws, resp, err := s.makeTerminal(t, pack, withServer(tt.target))
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, ws.Close()) })

require.Equal(t, tt.serverID, resp.ServerID)
require.Equal(t, tt.serverHostname, resp.ServerHostname)
require.Equal(t, tt.port, resp.ServerHostPort)
})
}

}

func TestTerminalRequireSessionMfa(t *testing.T) {
ctx := context.Background()
env := newWebPack(t, 1)
Expand Down

0 comments on commit 21331bf

Please sign in to comment.