2023-06-01 00:41:36 +00:00
|
|
|
// Copyright 2020-2023 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-10-06 18:59:03 +00:00
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
package testlib
|
2020-09-15 15:00:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-21 18:40:11 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-09-15 15:00:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-02-23 01:23:11 +00:00
|
|
|
authorizationv1 "k8s.io/api/authorization/v1"
|
2020-09-15 15:00:38 +00:00
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
2020-09-22 16:50:00 +00:00
|
|
|
const (
|
|
|
|
accessRetryInterval = 250 * time.Millisecond
|
2023-06-01 00:41:36 +00:00
|
|
|
accessRetryTimeout = 2 * time.Minute
|
2020-09-22 16:50:00 +00:00
|
|
|
)
|
|
|
|
|
2020-11-02 16:42:46 +00:00
|
|
|
// AccessAsUserTest runs a generic test in which a clientUnderTest operating with username
|
2020-09-15 15:00:38 +00:00
|
|
|
// testUsername tries to auth to the kube API (i.e., list namespaces).
|
|
|
|
//
|
|
|
|
// Use this function if you want to simply validate that a user can auth to the kube API after
|
|
|
|
// performing a Pinniped credential exchange.
|
2020-10-06 18:59:03 +00:00
|
|
|
func AccessAsUserTest(
|
2020-09-15 15:00:38 +00:00
|
|
|
ctx context.Context,
|
|
|
|
testUsername string,
|
|
|
|
clientUnderTest kubernetes.Interface,
|
|
|
|
) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2021-01-13 01:27:41 +00:00
|
|
|
addTestClusterUserCanViewEverythingRoleBinding(t, testUsername)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
|
|
|
// Use the client which is authenticated as the test user to list namespaces
|
2021-06-16 22:51:23 +00:00
|
|
|
RequireEventually(t, func(requireEventually *require.Assertions) {
|
|
|
|
resp, err := clientUnderTest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
|
|
|
|
requireEventually.NoError(err)
|
|
|
|
requireEventually.NotNil(resp)
|
|
|
|
requireEventually.NotEmpty(resp.Items)
|
|
|
|
}, accessRetryTimeout, accessRetryInterval, "user never had access to list namespaces")
|
2020-09-15 15:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:59:03 +00:00
|
|
|
func AccessAsUserWithKubectlTest(
|
2020-09-21 18:40:11 +00:00
|
|
|
testKubeConfigYAML string,
|
|
|
|
testUsername string,
|
|
|
|
expectedNamespace string,
|
|
|
|
) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2021-01-13 01:27:41 +00:00
|
|
|
addTestClusterUserCanViewEverythingRoleBinding(t, testUsername)
|
2020-09-21 18:40:11 +00:00
|
|
|
|
|
|
|
// Use the given kubeconfig with kubectl to list namespaces as the test user
|
2021-06-16 22:51:23 +00:00
|
|
|
RequireEventually(t, func(requireEventually *require.Assertions) {
|
|
|
|
kubectlCommandOutput, err := runKubectlGetNamespaces(t, testKubeConfigYAML)
|
|
|
|
requireEventually.NoError(err)
|
|
|
|
requireEventually.Containsf(kubectlCommandOutput, expectedNamespace, "actual output: %q", kubectlCommandOutput)
|
|
|
|
}, accessRetryTimeout, accessRetryInterval, "user never had access to list namespaces via kubectl")
|
2020-09-21 18:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:42:46 +00:00
|
|
|
// AccessAsGroupTest runs a generic test in which a clientUnderTest with membership in group
|
2020-09-15 15:00:38 +00:00
|
|
|
// testGroup tries to auth to the kube API (i.e., list namespaces).
|
|
|
|
//
|
|
|
|
// Use this function if you want to simply validate that a user can auth to the kube API (via
|
|
|
|
// a group membership) after performing a Pinniped credential exchange.
|
2020-10-06 18:59:03 +00:00
|
|
|
func AccessAsGroupTest(
|
2020-09-15 15:00:38 +00:00
|
|
|
ctx context.Context,
|
|
|
|
testGroup string,
|
|
|
|
clientUnderTest kubernetes.Interface,
|
|
|
|
) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2021-01-13 01:27:41 +00:00
|
|
|
addTestClusterGroupCanViewEverythingRoleBinding(t, testGroup)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
|
|
|
// Use the client which is authenticated as the test user to list namespaces
|
2021-06-16 22:51:23 +00:00
|
|
|
RequireEventually(t, func(requireEventually *require.Assertions) {
|
|
|
|
listNamespaceResponse, err := clientUnderTest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
|
|
|
|
requireEventually.NoError(err)
|
|
|
|
requireEventually.NotNil(listNamespaceResponse)
|
|
|
|
requireEventually.NotEmpty(listNamespaceResponse.Items)
|
|
|
|
}, accessRetryTimeout, accessRetryInterval, "user never had access to list namespaces")
|
2020-09-15 15:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 18:40:11 +00:00
|
|
|
|
2020-10-06 18:59:03 +00:00
|
|
|
func AccessAsGroupWithKubectlTest(
|
2020-09-21 18:40:11 +00:00
|
|
|
testKubeConfigYAML string,
|
|
|
|
testGroup string,
|
|
|
|
expectedNamespace string,
|
|
|
|
) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2021-01-13 01:27:41 +00:00
|
|
|
addTestClusterGroupCanViewEverythingRoleBinding(t, testGroup)
|
2020-09-21 18:40:11 +00:00
|
|
|
|
|
|
|
// Use the given kubeconfig with kubectl to list namespaces as the test user
|
2021-06-16 22:51:23 +00:00
|
|
|
RequireEventually(t, func(requireEventually *require.Assertions) {
|
|
|
|
kubectlCommandOutput, err := runKubectlGetNamespaces(t, testKubeConfigYAML)
|
|
|
|
requireEventually.NoError(err)
|
|
|
|
requireEventually.Containsf(kubectlCommandOutput, expectedNamespace, "actual output: %q", kubectlCommandOutput)
|
|
|
|
}, accessRetryTimeout, accessRetryInterval, "user never had access to list namespaces")
|
2020-09-21 18:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
func addTestClusterUserCanViewEverythingRoleBinding(t *testing.T, testUsername string) {
|
2020-09-24 17:53:45 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
CreateTestClusterRoleBinding(t,
|
|
|
|
rbacv1.Subject{
|
2020-09-21 18:40:11 +00:00
|
|
|
Kind: rbacv1.UserKind,
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: testUsername,
|
2021-01-13 01:27:41 +00:00
|
|
|
},
|
|
|
|
rbacv1.RoleRef{
|
2020-09-21 18:40:11 +00:00
|
|
|
Kind: "ClusterRole",
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: "view",
|
|
|
|
},
|
2021-01-13 01:27:41 +00:00
|
|
|
)
|
2021-02-23 01:23:11 +00:00
|
|
|
WaitForUserToHaveAccess(t, testUsername, []string{}, &authorizationv1.ResourceAttributes{
|
|
|
|
Verb: "get",
|
|
|
|
Group: "",
|
|
|
|
Version: "v1",
|
|
|
|
Resource: "namespaces",
|
|
|
|
})
|
2020-09-21 18:40:11 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
func addTestClusterGroupCanViewEverythingRoleBinding(t *testing.T, testGroup string) {
|
2020-09-24 17:53:45 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
CreateTestClusterRoleBinding(t,
|
|
|
|
rbacv1.Subject{
|
2020-09-21 18:40:11 +00:00
|
|
|
Kind: rbacv1.GroupKind,
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: testGroup,
|
2021-01-13 01:27:41 +00:00
|
|
|
},
|
|
|
|
rbacv1.RoleRef{
|
2020-09-21 18:40:11 +00:00
|
|
|
Kind: "ClusterRole",
|
|
|
|
APIGroup: rbacv1.GroupName,
|
|
|
|
Name: "view",
|
|
|
|
},
|
2021-01-13 01:27:41 +00:00
|
|
|
)
|
2021-02-23 01:23:11 +00:00
|
|
|
WaitForUserToHaveAccess(t, "", []string{testGroup}, &authorizationv1.ResourceAttributes{
|
|
|
|
Verb: "get",
|
|
|
|
Group: "",
|
|
|
|
Version: "v1",
|
|
|
|
Resource: "namespaces",
|
|
|
|
})
|
2020-09-21 18:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runKubectlGetNamespaces(t *testing.T, kubeConfigYAML string) (string, error) {
|
2020-09-24 17:53:45 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-09-21 18:40:11 +00:00
|
|
|
f := writeStringToTempFile(t, "pinniped-generated-kubeconfig-*", kubeConfigYAML)
|
|
|
|
|
2022-08-24 21:45:55 +00:00
|
|
|
//nolint:gosec // It's okay that we are passing f.Name() to an exec command here. It was created above.
|
2020-09-21 18:40:11 +00:00
|
|
|
output, err := exec.Command(
|
|
|
|
"kubectl", "get", "namespace", "--kubeconfig", f.Name(),
|
|
|
|
).CombinedOutput()
|
|
|
|
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeStringToTempFile(t *testing.T, filename string, kubeConfigYAML string) *os.File {
|
|
|
|
t.Helper()
|
2022-08-24 21:45:55 +00:00
|
|
|
f, err := os.CreateTemp("", filename)
|
2020-09-21 18:40:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
deferMe := func() {
|
|
|
|
err := os.Remove(f.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
t.Cleanup(deferMe)
|
|
|
|
_, err = f.WriteString(kubeConfigYAML)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = f.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return f
|
|
|
|
}
|