80a520390b
New resource naming conventions: - Do not repeat the Kind in the name, e.g. do not call it foo-cluster-role-binding, just call it foo - Names will generally start with a prefix to identify our component, so when a user lists all objects of that kind, they can tell to which component it is related, e.g. `kubectl get configmaps` would list one named "pinniped-config" - It should be possible for an operator to make the word "pinniped" mostly disappear if they choose, by specifying the app_name in values.yaml, to the extent that is practical (but not from APIService names because those are hardcoded in golang) - Each role/clusterrole and its corresponding binding have the same name - Pinniped resource names that must be known by the server golang code are passed to the code at run time via ConfigMap, rather than hardcoded in the golang code. This also allows them to be prepended with the app_name from values.yaml while creating the ConfigMap. - Since the CLI `get-kubeconfig` command cannot guess the name of the CredentialIssuerConfig resource in advance anymore, it lists all CredentialIssuerConfig in the app's namespace and returns an error if there is not exactly one found, and then uses that one regardless of its name
154 lines
4.4 KiB
Go
154 lines
4.4 KiB
Go
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/vmware-tanzu/pinniped/test/library"
|
|
)
|
|
|
|
func TestCLI(t *testing.T) {
|
|
library.SkipUnlessIntegration(t)
|
|
library.SkipUnlessClusterHasCapability(t, library.ClusterSigningKeyIsAvailable)
|
|
token := library.GetEnv(t, "PINNIPED_TEST_USER_TOKEN")
|
|
namespaceName := library.GetEnv(t, "PINNIPED_NAMESPACE")
|
|
testUsername := library.GetEnv(t, "PINNIPED_TEST_USER_USERNAME")
|
|
expectedTestUserGroups := strings.Split(
|
|
strings.ReplaceAll(library.GetEnv(t, "PINNIPED_TEST_USER_GROUPS"), " ", ""), ",",
|
|
)
|
|
|
|
// Remove all Pinniped environment variables for the remainder of this test
|
|
// because some of their names clash with the env vars expected by our
|
|
// kubectl exec plugin. We would like this test to prove that the exec
|
|
// plugin receives all of the necessary env vars via the auto-generated
|
|
// kubeconfig from the Pinniped CLI.
|
|
initialEnvVars := make(map[string]string)
|
|
for _, e := range os.Environ() {
|
|
pair := strings.SplitN(e, "=", 2)
|
|
name := pair[0]
|
|
value := pair[1]
|
|
if strings.HasPrefix(name, "PINNIPED_") {
|
|
initialEnvVars[name] = value
|
|
err := os.Unsetenv(name)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
// Put them back for other tests to use after this one
|
|
t.Cleanup(func() {
|
|
for k, v := range initialEnvVars {
|
|
err := os.Setenv(k, v)
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
|
|
// Build pinniped CLI.
|
|
pinnipedExe, cleanupFunc := buildPinnipedCLI(t)
|
|
defer cleanupFunc()
|
|
|
|
// Run pinniped CLI to get kubeconfig.
|
|
kubeConfig := runPinnipedCLI(t, pinnipedExe, token, namespaceName)
|
|
|
|
// In addition to the client-go based testing below, also try the kubeconfig
|
|
// with kubectl once just in case it is somehow different.
|
|
runKubectlCLI(t, kubeConfig, namespaceName, testUsername)
|
|
|
|
// Create Kubernetes client with kubeconfig from pinniped CLI.
|
|
kubeClient := library.NewClientsetForKubeConfig(t, kubeConfig)
|
|
|
|
// Validate that we can auth to the API via our user.
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*3)
|
|
defer cancelFunc()
|
|
|
|
adminClient := library.NewClientset(t)
|
|
|
|
t.Run("access as user", accessAsUserTest(ctx, adminClient, testUsername, kubeClient))
|
|
for _, group := range expectedTestUserGroups {
|
|
group := group
|
|
t.Run(
|
|
"access as group "+group,
|
|
accessAsGroupTest(ctx, adminClient, group, kubeClient),
|
|
)
|
|
}
|
|
}
|
|
|
|
func buildPinnipedCLI(t *testing.T) (string, func()) {
|
|
t.Helper()
|
|
|
|
pinnipedExeDir, err := ioutil.TempDir("", "pinniped-cli-test-*")
|
|
require.NoError(t, err)
|
|
|
|
pinnipedExe := filepath.Join(pinnipedExeDir, "pinniped")
|
|
output, err := exec.Command(
|
|
"go",
|
|
"build",
|
|
"-o",
|
|
pinnipedExe,
|
|
"github.com/vmware-tanzu/pinniped/cmd/pinniped",
|
|
).CombinedOutput()
|
|
require.NoError(t, err, string(output))
|
|
|
|
return pinnipedExe, func() {
|
|
require.NoError(t, os.RemoveAll(pinnipedExeDir))
|
|
}
|
|
}
|
|
|
|
func runPinnipedCLI(t *testing.T, pinnipedExe, token, namespaceName string) string {
|
|
t.Helper()
|
|
|
|
output, err := exec.Command(
|
|
pinnipedExe,
|
|
"get-kubeconfig",
|
|
"--token", token,
|
|
"--pinniped-namespace", namespaceName,
|
|
).CombinedOutput()
|
|
require.NoError(t, err, string(output))
|
|
|
|
return string(output)
|
|
}
|
|
|
|
func runKubectlCLI(t *testing.T, kubeConfig, namespaceName, username string) string {
|
|
t.Helper()
|
|
|
|
f, err := ioutil.TempFile("", "pinniped-generated-kubeconfig-*")
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
err := os.Remove(f.Name())
|
|
require.NoError(t, err)
|
|
}()
|
|
_, err = f.WriteString(kubeConfig)
|
|
require.NoError(t, err)
|
|
err = f.Close()
|
|
require.NoError(t, err)
|
|
|
|
//nolint: gosec // It's okay that we are passing f.Name() to an exec command here. It was created above.
|
|
output, err := exec.Command(
|
|
"kubectl",
|
|
"get",
|
|
"pods",
|
|
"--kubeconfig", f.Name(),
|
|
"--namespace", namespaceName,
|
|
).CombinedOutput()
|
|
|
|
// Expect an error because this user has no RBAC permission. However, the
|
|
// error message should state that we had already authenticated as the test user.
|
|
expectedErrorMessage := `Error from server (Forbidden): pods is forbidden: User "` +
|
|
username +
|
|
`" cannot list resource "pods" in API group "" in the namespace "` +
|
|
namespaceName +
|
|
`"` + "\n"
|
|
require.EqualError(t, err, "exit status 1")
|
|
require.Equal(t, expectedErrorMessage, string(output))
|
|
|
|
return string(output)
|
|
}
|