Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Backport: fix tsh login with trusted clusters #5105

Merged
merged 1 commit into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,24 @@ func (tc *TeleportClient) NewWatcher(ctx context.Context, watch services.Watch)
return proxyClient.NewWatcher(ctx, watch)
}

// WithRootClusterClient provides a functional interface for making calls against the root cluster's
// auth server.
func (tc *TeleportClient) WithRootClusterClient(ctx context.Context, do func(clt auth.ClientI) error) error {
proxyClient, err := tc.ConnectToProxy(ctx)
if err != nil {
return trace.Wrap(err)
}
defer proxyClient.Close()

clt, err := proxyClient.ConnectToRootCluster(ctx, false)
if err != nil {
return trace.Wrap(err)
}
defer clt.Close()

return trace.Wrap(do(clt))
}

// SSH connects to a node and, if 'command' is specified, executes the command on it,
// otherwise runs interactive shell
//
Expand Down
31 changes: 31 additions & 0 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ func (proxy *ProxyClient) ReissueUserCerts(ctx context.Context, params ReissuePa
return trace.Wrap(err)
}

// RootClusterName returns name of the current cluster
func (proxy *ProxyClient) RootClusterName() (string, error) {
localAgent := proxy.teleportClient.LocalAgent()
key, err := localAgent.GetKey()
if err != nil {
return "", trace.Wrap(err)
}
tlsCert, err := key.TLSCertificate()
if err != nil {
return "", trace.Wrap(err)
}
clusterName, err := tlsca.ClusterName(tlsCert.Issuer)
if err != nil {
return "", trace.Wrap(err)
}
return clusterName, nil
}

// CreateAccessRequest registers a new access request with the auth server.
func (proxy *ProxyClient) CreateAccessRequest(ctx context.Context, req services.AccessRequest) error {
site, err := proxy.ConnectToCurrentCluster(ctx, false)
Expand Down Expand Up @@ -347,6 +365,19 @@ func (proxy *ProxyClient) ConnectToCurrentCluster(ctx context.Context, quiet boo
return proxy.ConnectToCluster(ctx, cluster.Name, quiet)
}

// ConnectToRootCluster connects to the auth server of the root cluster
// cluster via proxy. It returns connected and authenticated auth server client
//
// if 'quiet' is set to true, no errors will be printed to stdout, otherwise
// any connection errors are visible to a user.
func (proxy *ProxyClient) ConnectToRootCluster(ctx context.Context, quiet bool) (auth.ClientI, error) {
clusterName, err := proxy.RootClusterName()
if err != nil {
return nil, trace.Wrap(err)
}
return proxy.ConnectToCluster(ctx, clusterName, quiet)
}

// ConnectToCluster connects to the auth server of the given cluster via proxy.
// It returns connected and authenticated auth server client
//
Expand Down
26 changes: 19 additions & 7 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,26 @@ func onLogin(cf *CLIConf) {
tc.Logout()
utils.FatalError(err)
}
for _, roleName := range roleNames {
role, err := tc.GetRole(cf.Context, roleName)
if err != nil {
tc.Logout()
utils.FatalError(err)
// load all roles from root cluster and collect relevant options.
// the normal one-off TeleportClient methods don't re-use the auth server
// connection, so we use WithRootClusterClient to speed things up.
err = tc.WithRootClusterClient(cf.Context, func(clt auth.ClientI) error {
for _, roleName := range roleNames {
role, err := clt.GetRole(roleName)
if err != nil {
return trace.Wrap(err)
}
reason = reason || role.GetOptions().RequestAccess.RequireReason()
auto = auto || role.GetOptions().RequestAccess.ShouldAutoRequest()
if prompt == "" {
prompt = role.GetOptions().RequestPrompt
}
}
reason = reason || role.GetOptions().RequestAccess.RequireReason()
auto = auto || role.GetOptions().RequestAccess.ShouldAutoRequest()
return nil
})
if err != nil {
tc.Logout()
utils.FatalError(err)
}
if reason && cf.RequestReason == "" {
tc.Logout()
Expand Down