From 2f5c3e86f9ca81814b5ba5ef0c5f79e3b5cfc0bf Mon Sep 17 00:00:00 2001 From: rosstimothy <39066650+rosstimothy@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:52:40 +0000 Subject: [PATCH] Display more user friendly error for invalid os logins in Web UI (#47420) Host resolution no longer happens prior to connections in the Web UI and all dial attempts are by UUID. When an invalid login is attempted the error message constructed only contained the info provided to the dial request: a login and a Host UUID. To make this error more user friendly access denied errors are now augmented with the hostname if the user has permissions to that host and the error occurs only due to an invalid login. Closes https://github.com/gravitational/teleport/issues/23719 --- lib/web/apiserver_test.go | 4 ++++ lib/web/terminal.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/web/apiserver_test.go b/lib/web/apiserver_test.go index 2c77ee51a0e3a..f038cf502625c 100644 --- a/lib/web/apiserver_test.go +++ b/lib/web/apiserver_test.go @@ -7451,6 +7451,10 @@ type authProviderMock struct { server types.ServerV2 } +func (mock authProviderMock) ListUnifiedResources(ctx context.Context, req *authproto.ListUnifiedResourcesRequest) (*authproto.ListUnifiedResourcesResponse, error) { + return nil, nil +} + func (mock authProviderMock) GetNodes(ctx context.Context, n string) ([]types.Server, error) { return []types.Server{&mock.server}, nil } diff --git a/lib/web/terminal.go b/lib/web/terminal.go index 2117a79a639ac..1f24dcd4a0835 100644 --- a/lib/web/terminal.go +++ b/lib/web/terminal.go @@ -100,6 +100,7 @@ type AuthProvider interface { IsMFARequired(ctx context.Context, req *authproto.IsMFARequiredRequest) (*authproto.IsMFARequiredResponse, error) GenerateUserSingleUseCerts(ctx context.Context) (authproto.AuthService_GenerateUserSingleUseCertsClient, error) MaintainSessionPresence(ctx context.Context) (authproto.AuthService_MaintainSessionPresenceClient, error) + ListUnifiedResources(ctx context.Context, req *authproto.ListUnifiedResourcesRequest) (*authproto.ListUnifiedResourcesResponse, error) } // NewTerminal creates a web-based terminal based on WebSockets and returns a @@ -885,6 +886,21 @@ func (t *sshBaseHandler) connectToNode(ctx context.Context, ws terminal.WSConn, // The close error is ignored instead of using [trace.NewAggregate] because // aggregate errors do not allow error inspection with things like [trace.IsAccessDenied]. _ = conn.Close() + + // Since connection attempts are made via UUID and not hostname, any access denied errors + // will not contain the resolved host address. To provide an easier troubleshooting experience + // for users, attempt to resolve the hostname of the server and augment the error message with it. + if trace.IsAccessDenied(err) { + if resp, err := t.userAuthClient.ListUnifiedResources(ctx, &authproto.ListUnifiedResourcesRequest{ + SortBy: types.SortBy{Field: types.ResourceKind}, + Kinds: []string{types.KindNode}, + Limit: 1, + PredicateExpression: fmt.Sprintf(`resource.metadata.name == "%s"`, t.sessionData.ServerID), + }); err == nil && len(resp.Resources) > 0 { + return nil, trace.AccessDenied("access denied to %q connecting to %v", sshConfig.User, resp.Resources[0].GetNode().GetHostname()) + } + } + return nil, trace.Wrap(err) }