2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-15 15:00:38 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/test/library"
|
2020-09-15 15:00:38 +00:00
|
|
|
)
|
|
|
|
|
2020-10-13 15:25:39 +00:00
|
|
|
func TestCLIGetKubeconfig(t *testing.T) {
|
2020-09-24 22:51:43 +00:00
|
|
|
env := library.IntegrationEnv(t).WithCapability(library.ClusterSigningKeyIsAvailable)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
2020-09-22 00:55:04 +00:00
|
|
|
// Create a test webhook configuration to use with the CLI.
|
2020-09-24 17:20:51 +00:00
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), 4*time.Minute)
|
2020-09-22 00:55:04 +00:00
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
idp := library.CreateTestWebhookIDP(ctx, t)
|
|
|
|
|
2020-09-15 15:00:38 +00:00
|
|
|
// Build pinniped CLI.
|
|
|
|
pinnipedExe, cleanupFunc := buildPinnipedCLI(t)
|
|
|
|
defer cleanupFunc()
|
|
|
|
|
|
|
|
// Run pinniped CLI to get kubeconfig.
|
2020-10-13 15:25:39 +00:00
|
|
|
kubeConfigYAML := runPinnipedCLIGetKubeconfig(t, pinnipedExe, env.TestUser.Token, env.ConciergeNamespace, "webhook", idp.Name)
|
2020-09-15 15:00:38 +00:00
|
|
|
|
2020-09-21 18:40:11 +00:00
|
|
|
// In addition to the client-go based testing below, also try the kubeconfig
|
|
|
|
// with kubectl to validate that it works.
|
2020-09-22 00:55:04 +00:00
|
|
|
adminClient := library.NewClientset(t)
|
2020-09-21 18:40:11 +00:00
|
|
|
t.Run(
|
|
|
|
"access as user with kubectl",
|
2020-10-09 17:11:47 +00:00
|
|
|
library.AccessAsUserWithKubectlTest(ctx, adminClient, kubeConfigYAML, env.TestUser.ExpectedUsername, env.ConciergeNamespace),
|
2020-09-21 18:40:11 +00:00
|
|
|
)
|
2020-09-24 22:51:43 +00:00
|
|
|
for _, group := range env.TestUser.ExpectedGroups {
|
2020-09-15 15:00:38 +00:00
|
|
|
group := group
|
|
|
|
t.Run(
|
2020-09-21 18:40:11 +00:00
|
|
|
"access as group "+group+" with kubectl",
|
2020-10-09 17:11:47 +00:00
|
|
|
library.AccessAsGroupWithKubectlTest(ctx, adminClient, kubeConfigYAML, group, env.ConciergeNamespace),
|
2020-09-15 15:00:38 +00:00
|
|
|
)
|
|
|
|
}
|
2020-09-21 18:40:11 +00:00
|
|
|
|
|
|
|
// Create Kubernetes client with kubeconfig from pinniped CLI.
|
|
|
|
kubeClient := library.NewClientsetForKubeConfig(t, kubeConfigYAML)
|
|
|
|
|
|
|
|
// Validate that we can auth to the API via our user.
|
2020-10-06 18:59:03 +00:00
|
|
|
t.Run("access as user with client-go", library.AccessAsUserTest(ctx, adminClient, env.TestUser.ExpectedUsername, kubeClient))
|
2020-09-24 22:51:43 +00:00
|
|
|
for _, group := range env.TestUser.ExpectedGroups {
|
2020-09-21 18:40:11 +00:00
|
|
|
group := group
|
2020-10-06 18:59:03 +00:00
|
|
|
t.Run("access as group "+group+" with client-go", library.AccessAsGroupTest(ctx, adminClient, group, kubeClient))
|
2020-09-21 18:40:11 +00:00
|
|
|
}
|
2020-09-15 15:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/cmd/pinniped",
|
2020-09-15 15:00:38 +00:00
|
|
|
).CombinedOutput()
|
|
|
|
require.NoError(t, err, string(output))
|
|
|
|
|
|
|
|
return pinnipedExe, func() {
|
|
|
|
require.NoError(t, os.RemoveAll(pinnipedExeDir))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:25:39 +00:00
|
|
|
func runPinnipedCLIGetKubeconfig(t *testing.T, pinnipedExe, token, namespaceName, idpType, idpName string) string {
|
2020-09-15 15:00:38 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
output, err := exec.Command(
|
|
|
|
pinnipedExe,
|
|
|
|
"get-kubeconfig",
|
|
|
|
"--token", token,
|
|
|
|
"--pinniped-namespace", namespaceName,
|
2020-09-22 00:55:04 +00:00
|
|
|
"--idp-type", idpType,
|
|
|
|
"--idp-name", idpName,
|
2020-09-15 15:00:38 +00:00
|
|
|
).CombinedOutput()
|
|
|
|
require.NoError(t, err, string(output))
|
|
|
|
|
|
|
|
return string(output)
|
|
|
|
}
|