2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-09 19:30:59 +00:00
|
|
|
|
|
|
|
package library
|
|
|
|
|
|
|
|
import (
|
2020-09-22 00:55:04 +00:00
|
|
|
"context"
|
2020-10-14 20:41:16 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-08-12 21:29:46 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-07-09 19:30:59 +00:00
|
|
|
"testing"
|
2020-09-22 00:55:04 +00:00
|
|
|
"time"
|
2020-07-09 19:30:59 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-22 00:55:04 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-10-15 13:09:49 +00:00
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
2020-09-22 00:55:04 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-07-09 19:30:59 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2020-08-11 17:14:57 +00:00
|
|
|
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-10-14 20:41:16 +00:00
|
|
|
configv1alpha1 "go.pinniped.dev/generated/1.19/apis/config/v1alpha1"
|
2020-09-22 00:55:04 +00:00
|
|
|
idpv1alpha1 "go.pinniped.dev/generated/1.19/apis/idp/v1alpha1"
|
2020-09-18 19:56:24 +00:00
|
|
|
pinnipedclientset "go.pinniped.dev/generated/1.19/client/clientset/versioned"
|
2020-08-27 13:12:34 +00:00
|
|
|
|
|
|
|
// Import to initialize client auth plugins - the kubeconfig that we use for
|
|
|
|
// testing may use gcloud, az, oidc, etc.
|
|
|
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
2020-07-09 19:30:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewClientConfig(t *testing.T) *rest.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-24 15:40:08 +00:00
|
|
|
return newClientConfigWithOverrides(t, &clientcmd.ConfigOverrides{})
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:29:46 +00:00
|
|
|
func NewClientset(t *testing.T) kubernetes.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return newClientsetWithConfig(t, NewClientConfig(t))
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:00:38 +00:00
|
|
|
func NewClientsetForKubeConfig(t *testing.T, kubeConfig string) kubernetes.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
kubeConfigFile, err := ioutil.TempFile("", "pinniped-cli-test-*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(kubeConfigFile.Name())
|
|
|
|
|
|
|
|
_, err = kubeConfigFile.Write([]byte(kubeConfig))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigFile.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return newClientsetWithConfig(t, restConfig)
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:29:46 +00:00
|
|
|
func NewClientsetWithCertAndKey(t *testing.T, clientCertificateData, clientKeyData string) kubernetes.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-08-24 15:52:47 +00:00
|
|
|
return newClientsetWithConfig(t, newAnonymousClientRestConfigWithCertAndKeyAdded(t, clientCertificateData, clientKeyData))
|
2020-08-12 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
func NewPinnipedClientset(t *testing.T) pinnipedclientset.Interface {
|
2020-07-24 15:40:08 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
return pinnipedclientset.NewForConfigOrDie(NewClientConfig(t))
|
2020-08-12 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
func NewAnonymousPinnipedClientset(t *testing.T) pinnipedclientset.Interface {
|
2020-08-12 21:29:46 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-08-24 15:52:47 +00:00
|
|
|
return pinnipedclientset.NewForConfigOrDie(newAnonymousClientRestConfig(t))
|
2020-08-12 21:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAggregatedClientset(t *testing.T) aggregatorclient.Interface {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return aggregatorclient.NewForConfigOrDie(NewClientConfig(t))
|
2020-07-24 15:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newClientConfigWithOverrides(t *testing.T, overrides *clientcmd.ConfigOverrides) *rest.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-09 19:30:59 +00:00
|
|
|
loader := clientcmd.NewDefaultClientConfigLoadingRules()
|
2020-07-24 15:40:08 +00:00
|
|
|
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loader, overrides)
|
2020-07-09 19:30:59 +00:00
|
|
|
config, err := clientConfig.ClientConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:29:46 +00:00
|
|
|
func newClientsetWithConfig(t *testing.T, config *rest.Config) kubernetes.Interface {
|
2020-07-24 15:40:08 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-07-27 12:52:36 +00:00
|
|
|
result, err := kubernetes.NewForConfig(config)
|
|
|
|
require.NoError(t, err, "unexpected failure from kubernetes.NewForConfig()")
|
|
|
|
return result
|
2020-07-09 19:30:59 +00:00
|
|
|
}
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-08-12 21:29:46 +00:00
|
|
|
// Returns a rest.Config without any user authentication info.
|
2020-08-24 15:52:47 +00:00
|
|
|
func newAnonymousClientRestConfig(t *testing.T) *rest.Config {
|
2020-07-23 15:05:21 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-09-28 12:57:47 +00:00
|
|
|
return rest.AnonymousClientConfig(NewClientConfig(t))
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
2020-08-11 17:14:57 +00:00
|
|
|
|
2020-08-12 21:29:46 +00:00
|
|
|
// Starting with an anonymous client config, add a cert and key to use for authentication in the API server.
|
2020-08-24 15:52:47 +00:00
|
|
|
func newAnonymousClientRestConfigWithCertAndKeyAdded(t *testing.T, clientCertificateData, clientKeyData string) *rest.Config {
|
2020-08-11 17:14:57 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-08-24 15:52:47 +00:00
|
|
|
config := newAnonymousClientRestConfig(t)
|
|
|
|
config.CertData = []byte(clientCertificateData)
|
|
|
|
config.KeyData = []byte(clientKeyData)
|
|
|
|
return config
|
2020-08-11 17:14:57 +00:00
|
|
|
}
|
2020-09-22 00:55:04 +00:00
|
|
|
|
2020-10-09 17:11:47 +00:00
|
|
|
// CreateTestWebhookIDP creates and returns a test WebhookIdentityProvider in $PINNIPED_TEST_CONCIERGE_NAMESPACE, which will be
|
2020-09-22 00:55:04 +00:00
|
|
|
// automatically deleted at the end of the current test's lifetime. It returns a corev1.TypedLocalObjectReference which
|
|
|
|
// descibes the test IDP within the test namespace.
|
|
|
|
func CreateTestWebhookIDP(ctx context.Context, t *testing.T) corev1.TypedLocalObjectReference {
|
|
|
|
t.Helper()
|
2020-09-24 22:51:43 +00:00
|
|
|
testEnv := IntegrationEnv(t)
|
2020-09-22 00:55:04 +00:00
|
|
|
|
|
|
|
client := NewPinnipedClientset(t)
|
2020-10-09 17:11:47 +00:00
|
|
|
webhooks := client.IDPV1alpha1().WebhookIdentityProviders(testEnv.ConciergeNamespace)
|
2020-09-22 00:55:04 +00:00
|
|
|
|
|
|
|
createContext, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
2020-09-22 16:23:34 +00:00
|
|
|
|
2020-09-22 00:55:04 +00:00
|
|
|
idp, err := webhooks.Create(createContext, &idpv1alpha1.WebhookIdentityProvider{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
GenerateName: "test-webhook-",
|
2020-09-22 16:23:34 +00:00
|
|
|
Labels: map[string]string{"pinniped.dev/test": ""},
|
|
|
|
Annotations: map[string]string{"pinniped.dev/testName": t.Name()},
|
2020-09-22 00:55:04 +00:00
|
|
|
},
|
2020-09-24 22:51:43 +00:00
|
|
|
Spec: testEnv.TestWebhook,
|
2020-09-22 00:55:04 +00:00
|
|
|
}, metav1.CreateOptions{})
|
|
|
|
require.NoError(t, err, "could not create test WebhookIdentityProvider")
|
|
|
|
t.Logf("created test WebhookIdentityProvider %s/%s", idp.Namespace, idp.Name)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2020-09-22 15:02:32 +00:00
|
|
|
t.Helper()
|
2020-09-22 00:55:04 +00:00
|
|
|
t.Logf("cleaning up test WebhookIdentityProvider %s/%s", idp.Namespace, idp.Name)
|
|
|
|
deleteCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := webhooks.Delete(deleteCtx, idp.Name, metav1.DeleteOptions{})
|
|
|
|
require.NoErrorf(t, err, "could not cleanup test WebhookIdentityProvider %s/%s", idp.Namespace, idp.Name)
|
|
|
|
})
|
|
|
|
|
|
|
|
return corev1.TypedLocalObjectReference{
|
|
|
|
APIGroup: &idpv1alpha1.SchemeGroupVersion.Group,
|
|
|
|
Kind: "WebhookIdentityProvider",
|
|
|
|
Name: idp.Name,
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 20:41:16 +00:00
|
|
|
|
|
|
|
// CreateTestOIDCProvider creates and returns a test OIDCProviderConfig in
|
|
|
|
// $PINNIPED_TEST_SUPERVISOR_NAMESPACE, which will be automatically deleted at the end of the
|
|
|
|
// current test's lifetime. It generates a random, valid, issuer for the OIDCProviderConfig.
|
2020-10-15 13:09:49 +00:00
|
|
|
//
|
|
|
|
// If the provided issuer is not the empty string, then it will be used for the
|
|
|
|
// OIDCProviderConfig.Spec.Issuer field. Else, a random issuer will be generated.
|
2020-10-27 21:57:25 +00:00
|
|
|
func CreateTestOIDCProvider(ctx context.Context, t *testing.T, issuer, sniCertificateSecretName string) *configv1alpha1.OIDCProviderConfig {
|
2020-10-14 20:41:16 +00:00
|
|
|
t.Helper()
|
|
|
|
testEnv := IntegrationEnv(t)
|
|
|
|
|
|
|
|
createContext, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-15 13:09:49 +00:00
|
|
|
if issuer == "" {
|
|
|
|
var err error
|
|
|
|
issuer, err = randomIssuer()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-10-14 20:41:16 +00:00
|
|
|
|
|
|
|
opcs := NewPinnipedClientset(t).ConfigV1alpha1().OIDCProviderConfigs(testEnv.SupervisorNamespace)
|
|
|
|
opc, err := opcs.Create(createContext, &configv1alpha1.OIDCProviderConfig{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
GenerateName: "test-oidc-provider-",
|
|
|
|
Labels: map[string]string{"pinniped.dev/test": ""},
|
|
|
|
Annotations: map[string]string{"pinniped.dev/testName": t.Name()},
|
|
|
|
},
|
|
|
|
Spec: configv1alpha1.OIDCProviderConfigSpec{
|
2020-10-27 21:57:25 +00:00
|
|
|
Issuer: issuer,
|
|
|
|
SNICertificateSecretName: sniCertificateSecretName,
|
2020-10-14 20:41:16 +00:00
|
|
|
},
|
|
|
|
}, metav1.CreateOptions{})
|
|
|
|
require.NoError(t, err, "could not create test OIDCProviderConfig")
|
|
|
|
t.Logf("created test OIDCProviderConfig %s/%s", opc.Namespace, opc.Name)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Helper()
|
|
|
|
t.Logf("cleaning up test OIDCProviderConfig %s/%s", opc.Namespace, opc.Name)
|
|
|
|
deleteCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := opcs.Delete(deleteCtx, opc.Name, metav1.DeleteOptions{})
|
2020-10-15 13:09:49 +00:00
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
// It's okay if it is not found, because it might have been deleted by another part of this test.
|
|
|
|
if !notFound {
|
|
|
|
require.NoErrorf(t, err, "could not cleanup test OIDCProviderConfig %s/%s", opc.Namespace, opc.Name)
|
|
|
|
}
|
2020-10-14 20:41:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return opc
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomIssuer() (string, error) {
|
|
|
|
var buf [8]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, buf[:]); err != nil {
|
2020-10-15 11:50:53 +00:00
|
|
|
return "", fmt.Errorf("could not generate random state: %w", err)
|
2020-10-14 20:41:16 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("http://test-issuer-%s.pinniped.dev", hex.EncodeToString(buf[:])), nil
|
|
|
|
}
|