Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix race in TestSSH/ssh_jump_host_access #24195

Merged
merged 2 commits into from
Apr 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"runtime"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -2847,8 +2848,9 @@ func (tc *TeleportClient) generateClientConfig(ctx context.Context) (*clientConf
// cluster. If it's the root cluster proxy, tc.SiteName could
// be pointing at a leaf cluster and we don't want to override
// that.
if clusterGuesser.clusterName != rootClusterName {
return clusterGuesser.clusterName
cluster := clusterGuesser.clusterName()
if cluster != rootClusterName {
return cluster
}
return tc.SiteName
}
Expand Down Expand Up @@ -3061,7 +3063,7 @@ func (tc *TeleportClient) rootClusterName() (string, error) {
// the proxy host certificate. It then passes that name to signersForCluster to
// get the SSH certificates for that cluster.
type proxyClusterGuesser struct {
clusterName string
cluster atomic.Pointer[string]

nextHostKeyCallback ssh.HostKeyCallback
signersForCluster func(context.Context, string) ([]ssh.Signer, error)
Expand All @@ -3079,19 +3081,33 @@ func (g *proxyClusterGuesser) hostKeyCallback(hostname string, remote net.Addr,
if !ok {
return trace.BadParameter("remote proxy did not present a host certificate")
}
g.clusterName = cert.Permissions.Extensions[utils.CertExtensionAuthority]
if g.clusterName == "" {

clusterName, ok := cert.Permissions.Extensions[utils.CertExtensionAuthority]
if ok {
g.cluster.CompareAndSwap(nil, &clusterName)
}

if clusterName == "" {
log.Debugf("Target SSH server %q does not have a cluster name embedded in their certificate; will use all available client certificates to authenticate", hostname)
}

if g.nextHostKeyCallback != nil {
return g.nextHostKeyCallback(hostname, remote, key)
}
return nil
}

func (g *proxyClusterGuesser) clusterName() string {
name := g.cluster.Load()
if name != nil {
return *name
}
return ""
}

func (g *proxyClusterGuesser) authMethod(ctx context.Context) ssh.AuthMethod {
return ssh.PublicKeysCallback(func() ([]ssh.Signer, error) {
return g.signersForCluster(ctx, g.clusterName)
return g.signersForCluster(ctx, g.clusterName())
})
}

Expand Down
15 changes: 14 additions & 1 deletion lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3857,12 +3857,25 @@ func (process *TeleportProcess) initProxyEndpoint(conn *Connector) error {
}

tlscfg := serverTLSConfig.Clone()
setupTLSConfigClientCAsForCluster(tlscfg, accessPoint, clusterName)
tlscfg.ClientAuth = tls.RequireAndVerifyClientCert
if lib.IsInsecureDevMode() {
tlscfg.InsecureSkipVerify = true
tlscfg.ClientAuth = tls.RequireAnyClientCert
}
tlscfg.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
tlsClone := tlscfg.Clone()

// Build the client CA pool containing the cluster's user CA in
// order to be able to validate certificates provided by users.
var err error
tlsClone.ClientCAs, _, err = auth.DefaultClientCertPool(accessPoint, clusterName)
if err != nil {
return nil, trace.Wrap(err)
}

return tlsClone, nil
}

creds, err := auth.NewTransportCredentials(auth.TransportCredentialsConfig{
TransportCredentials: credentials.NewTLS(tlscfg),
UserGetter: authMiddleware,
Expand Down