-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit Kubernetes connections (#12275)
- Loading branch information
Showing
7 changed files
with
915 additions
and
738 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,7 @@ func TestKube(t *testing.T) { | |
t.Run("TrustedClustersSNI", suite.bind(testKubeTrustedClustersSNI)) | ||
t.Run("Disconnect", suite.bind(testKubeDisconnect)) | ||
t.Run("Join", suite.bind(testKubeJoin)) | ||
t.Run("ConnectionLimit", suite.bind(testKubeConnectionLimit)) | ||
} | ||
|
||
// TestKubeExec tests kubernetes Exec command set | ||
|
@@ -1611,3 +1612,82 @@ func testKubeJoin(t *testing.T, suite *KubeSuite) { | |
require.Contains(t, participantOutput, []byte("echo hi")) | ||
require.Contains(t, out.String(), []byte("echo hi2")) | ||
} | ||
|
||
// testKubeConnectionLimit checks that the `max_kubernetes_connections` role option is enforced. | ||
func testKubeConnectionLimit(t *testing.T, suite *KubeSuite) { | ||
teleport := NewInstance(InstanceConfig{ | ||
ClusterName: Site, | ||
HostID: HostID, | ||
NodeName: Host, | ||
Priv: suite.priv, | ||
Pub: suite.pub, | ||
log: suite.log, | ||
}) | ||
|
||
const maxConnections = 10 | ||
hostUsername := suite.me.Username | ||
kubeGroups := []string{testImpersonationGroup} | ||
kubeUsers := []string{"[email protected]"} | ||
role, err := types.NewRoleV3("kubemaster", types.RoleSpecV5{ | ||
Allow: types.RoleConditions{ | ||
Logins: []string{hostUsername}, | ||
KubeGroups: kubeGroups, | ||
KubeUsers: kubeUsers, | ||
}, | ||
Options: types.RoleOptions{ | ||
MaxKubernetesConnections: maxConnections, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
teleport.AddUserWithRole(hostUsername, role) | ||
|
||
err = teleport.Start() | ||
require.NoError(t, err) | ||
t.Cleanup(func() { require.NoError(t, teleport.StopAll()) }) | ||
|
||
// set up kube configuration using proxy | ||
proxyClient, proxyClientConfig, err := kubeProxyClient(kubeProxyConfig{ | ||
t: teleport, | ||
username: hostUsername, | ||
kubeUsers: kubeUsers, | ||
kubeGroups: kubeGroups, | ||
}) | ||
require.NoError(t, err) | ||
|
||
ctx := context.Background() | ||
// try get request to fetch available pods | ||
pod, err := proxyClient.CoreV1().Pods(testNamespace).Get(ctx, testPod, metav1.GetOptions{}) | ||
require.NoError(t, err) | ||
|
||
openExec := func() error { | ||
// interactive command, allocate pty | ||
term := NewTerminal(250) | ||
out := &bytes.Buffer{} | ||
|
||
return kubeExec(proxyClientConfig, kubeExecArgs{ | ||
podName: pod.Name, | ||
podNamespace: pod.Namespace, | ||
container: pod.Spec.Containers[0].Name, | ||
command: []string{"/bin/sh", "-c", "sleep 300"}, | ||
stdout: out, | ||
tty: true, | ||
stdin: term, | ||
}) | ||
} | ||
|
||
// Create and maintain the maximum amount of open connections | ||
for i := 0; i < maxConnections; i++ { | ||
go openExec() | ||
} | ||
|
||
// Wait for the connections to open and check for any errors | ||
require.Eventually(t, func() bool { | ||
trackers, err := teleport.Process.GetAuthServer().GetActiveSessionTrackers(ctx) | ||
require.NoError(t, err) | ||
return len(trackers) == maxConnections | ||
}, time.Second*30, time.Second) | ||
|
||
// Open one more connection. It should fail due to the limit. | ||
err = openExec() | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters