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

[v10] Set SNI when tsh login --format kubernetes is invoked #19432

Merged
merged 5 commits into from
Dec 16, 2022
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
10 changes: 9 additions & 1 deletion tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -1482,14 +1482,22 @@ func onLogin(cf *CLIConf) error {
return trace.Wrap(err)
}
key.TrustedCA = auth.AuthoritiesToTrustedCerts(authorities)

// If we're in multiplexed mode get SNI name for kube from single multiplexed proxy addr
kubeTLSServerName := ""
if tc.TLSRoutingEnabled {
log.Debug("Using Proxy SNI for kube TLS server name")
kubeHost, _ := tc.KubeProxyHostPort()
kubeTLSServerName = client.GetKubeTLSServerName(kubeHost)
}
filesWritten, err := identityfile.Write(identityfile.WriteConfig{
OutputPath: cf.IdentityFileOut,
Key: key,
Format: cf.IdentityFormat,
KubeProxyAddr: tc.KubeClusterAddr(),
OverwriteDestination: cf.IdentityOverwrite,
KubeStoreAllCAs: tc.LoadAllCAs,
KubeTLSServerName: kubeTLSServerName,
KubeClusterName: tc.KubernetesCluster,
})
if err != nil {
return trace.Wrap(err)
Expand Down
104 changes: 89 additions & 15 deletions tool/tsh/tsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,10 @@ func findMOTD(t *testing.T, sc *bufio.Scanner, motd string) {
}

// TestLoginIdentityOut makes sure that "tsh login --out <ident>" command
// writes identity credentials to the specified path.
// writes identity credentials to the specified path. It also supports
// specifying the output format via `--format=<format>`.
func TestLoginIdentityOut(t *testing.T) {
const kubeClusterName = "kubeTest"
tmpHomePath := t.TempDir()

connector := mockConnector(t)
Expand All @@ -417,30 +419,102 @@ func TestLoginIdentityOut(t *testing.T) {
alice.SetRoles([]string{"access"})

authProcess, proxyProcess := makeTestServers(t, withBootstrap(connector, alice))

authServer := authProcess.GetAuthServer()
require.NotNil(t, authServer)

proxyAddr, err := proxyProcess.ProxyWebAddr()
require.NoError(t, err)

identPath := filepath.Join(t.TempDir(), "ident")

err = Run(context.Background(), []string{
"login",
"--insecure",
"--debug",
"--auth", connector.GetName(),
"--proxy", proxyAddr.String(),
"--out", identPath,
}, setHomePath(tmpHomePath), func(cf *CLIConf) error {
cf.mockSSOLogin = mockSSOLogin(t, authServer, alice)
return nil
kubeService, err := types.NewServer(kubeClusterName, types.KindKubeService, types.ServerSpecV2{
KubernetesClusters: []*types.KubernetesCluster{
{
Name: kubeClusterName,
},
},
})
require.NoError(t, err)
_, err = authServer.UpsertKubeServiceV2(context.Background(), kubeService)
require.NoError(t, err)

cases := []struct {
name string
extraArgs []string
validationFunc func(t *testing.T, identityPath string)
requiresTLSRouting bool
}{
{
name: "write indentity out",
validationFunc: func(t *testing.T, identityPath string) {
_, err := client.KeyFromIdentityFile(identityPath)
require.NoError(t, err)
},
},
{
name: "write identity in kubeconfig format with tls routing enabled",
extraArgs: []string{"--format", "kubernetes", "--kube-cluster", kubeClusterName},
validationFunc: func(t *testing.T, identityPath string) {
cfg, err := kubeconfig.Load(identityPath)
require.NoError(t, err)
kubeCtx := cfg.Contexts[cfg.CurrentContext]
require.NotNil(t, kubeCtx)
cluster := cfg.Clusters[kubeCtx.Cluster]
require.NotNil(t, cluster)
require.NotEmpty(t, cluster.TLSServerName)
},
requiresTLSRouting: true,
},
{
name: "write identity in kubeconfig format with tls routing disabled",
extraArgs: []string{"--format", "kubernetes", "--kube-cluster", kubeClusterName},
validationFunc: func(t *testing.T, identityPath string) {
cfg, err := kubeconfig.Load(identityPath)
require.NoError(t, err)
kubeCtx := cfg.Contexts[cfg.CurrentContext]
require.NotNil(t, kubeCtx)
cluster := cfg.Clusters[kubeCtx.Cluster]
require.NotNil(t, cluster)
require.Empty(t, cluster.TLSServerName)
},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
identPath := filepath.Join(t.TempDir(), "ident")
if tt.requiresTLSRouting {
switchProxyListenerMode(t, authServer, types.ProxyListenerMode_Multiplex)
}
err = Run(context.Background(), append([]string{
"login",
"--insecure",
"--debug",
"--auth", connector.GetName(),
"--proxy", proxyAddr.String(),
"--out", identPath,
}, tt.extraArgs...), setHomePath(tmpHomePath), func(cf *CLIConf) error {
cf.mockSSOLogin = mockSSOLogin(t, authServer, alice)
return nil
})
require.NoError(t, err)
tt.validationFunc(t, identPath)
})
}
}

_, err = client.KeyFromIdentityFile(identPath)
// switchProxyListenerMode switches the proxy listener mode to the specified mode
// and schedules a reversion to the previous value once the sub-test completes.
func switchProxyListenerMode(t *testing.T, authServer *auth.Server, mode types.ProxyListenerMode) {
networkCfg, err := authServer.GetClusterNetworkingConfig(context.Background())
require.NoError(t, err)
prevValue := networkCfg.GetProxyListenerMode()
networkCfg.SetProxyListenerMode(mode)
err = authServer.SetClusterNetworkingConfig(context.Background(), networkCfg)
require.NoError(t, err)

t.Cleanup(func() {
networkCfg.SetProxyListenerMode(prevValue)
err = authServer.SetClusterNetworkingConfig(context.Background(), networkCfg)
require.NoError(t, err)
})
}

func TestRelogin(t *testing.T) {
Expand Down