2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-14 14:01:38 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2020-09-11 00:10:27 +00:00
|
|
|
"strings"
|
2020-07-23 15:05:21 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-18 22:15:04 +00:00
|
|
|
"go.pinniped.dev/generated/1.19/apis/login/v1alpha1"
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/test/library"
|
2020-09-21 18:40:11 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-07-23 15:05:21 +00:00
|
|
|
)
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestSuccessfulCredentialRequest(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-25 01:07:34 +00:00
|
|
|
library.SkipUnlessClusterHasCapability(t, library.ClusterSigningKeyIsAvailable)
|
2020-09-11 00:10:27 +00:00
|
|
|
testUsername := library.GetEnv(t, "PINNIPED_TEST_USER_USERNAME")
|
|
|
|
expectedTestUserGroups := strings.Split(
|
|
|
|
strings.ReplaceAll(library.GetEnv(t, "PINNIPED_TEST_USER_GROUPS"), " ", ""), ",",
|
|
|
|
)
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2020-08-26 01:22:53 +00:00
|
|
|
response, err := makeRequest(t, validCredentialRequestSpecWithRealToken(t))
|
2020-07-23 15:05:21 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-24 22:57:29 +00:00
|
|
|
|
2020-09-11 20:08:54 +00:00
|
|
|
require.NotNil(t, response.Status.Credential)
|
2020-07-23 23:01:55 +00:00
|
|
|
require.Empty(t, response.Status.Message)
|
2020-07-23 15:05:21 +00:00
|
|
|
require.Empty(t, response.Spec)
|
2020-07-24 15:40:08 +00:00
|
|
|
require.Empty(t, response.Status.Credential.Token)
|
|
|
|
require.NotEmpty(t, response.Status.Credential.ClientCertificateData)
|
2020-09-11 00:10:27 +00:00
|
|
|
require.Equal(t, testUsername, getCommonName(t, response.Status.Credential.ClientCertificateData))
|
|
|
|
require.ElementsMatch(t, expectedTestUserGroups, getOrganizations(t, response.Status.Credential.ClientCertificateData))
|
2020-07-24 15:40:08 +00:00
|
|
|
require.NotEmpty(t, response.Status.Credential.ClientKeyData)
|
2020-07-30 21:41:12 +00:00
|
|
|
require.NotNil(t, response.Status.Credential.ExpirationTimestamp)
|
2020-08-11 20:09:31 +00:00
|
|
|
require.InDelta(t, time.Until(response.Status.Credential.ExpirationTimestamp.Time), 1*time.Hour, float64(3*time.Minute))
|
2020-07-24 15:40:08 +00:00
|
|
|
|
2020-07-24 22:57:29 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-03 22:29:55 +00:00
|
|
|
// Create a client using the admin kubeconfig.
|
2020-07-24 22:57:29 +00:00
|
|
|
adminClient := library.NewClientset(t)
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
// Create a client using the certificate from the CredentialRequest.
|
|
|
|
clientWithCertFromCredentialRequest := library.NewClientsetWithCertAndKey(
|
2020-08-03 22:29:55 +00:00
|
|
|
t,
|
2020-08-12 21:29:46 +00:00
|
|
|
response.Status.Credential.ClientCertificateData,
|
|
|
|
response.Status.Credential.ClientKeyData,
|
2020-08-03 22:29:55 +00:00
|
|
|
)
|
|
|
|
|
2020-09-15 15:00:38 +00:00
|
|
|
t.Run(
|
|
|
|
"access as user",
|
|
|
|
accessAsUserTest(ctx, adminClient, testUsername, clientWithCertFromCredentialRequest),
|
|
|
|
)
|
2020-09-11 00:10:27 +00:00
|
|
|
for _, group := range expectedTestUserGroups {
|
2020-09-11 01:34:18 +00:00
|
|
|
group := group
|
2020-09-15 15:00:38 +00:00
|
|
|
t.Run(
|
|
|
|
"access as group "+group,
|
|
|
|
accessAsGroupTest(ctx, adminClient, group, clientWithCertFromCredentialRequest),
|
|
|
|
)
|
2020-09-10 15:30:15 +00:00
|
|
|
}
|
2020-07-23 23:01:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestFailedCredentialRequestWhenTheRequestIsValidButTheTokenDoesNotAuthenticateTheUser(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-25 01:07:34 +00:00
|
|
|
library.SkipUnlessClusterHasCapability(t, library.ClusterSigningKeyIsAvailable)
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
response, err := makeRequest(t, v1alpha1.TokenCredentialRequestSpec{Token: "not a good token"})
|
2020-07-23 23:01:55 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
2020-08-14 13:18:31 +00:00
|
|
|
require.Equal(t, stringPtr("authentication failed"), response.Status.Message)
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestCredentialRequest_ShouldFailWhenRequestDoesNotIncludeToken(t *testing.T) {
|
2020-08-07 01:44:14 +00:00
|
|
|
library.SkipUnlessIntegration(t)
|
2020-08-25 01:07:34 +00:00
|
|
|
library.SkipUnlessClusterHasCapability(t, library.ClusterSigningKeyIsAvailable)
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
response, err := makeRequest(t, v1alpha1.TokenCredentialRequestSpec{Token: ""})
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
statusError, isStatus := err.(*errors.StatusError)
|
|
|
|
require.True(t, isStatus)
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(statusError.ErrStatus.Details.Causes))
|
|
|
|
cause := statusError.ErrStatus.Details.Causes[0]
|
|
|
|
require.Equal(t, metav1.CauseType("FieldValueRequired"), cause.Type)
|
|
|
|
require.Equal(t, "Required value: token must be supplied", cause.Message)
|
|
|
|
require.Equal(t, "spec.token.value", cause.Field)
|
2020-07-24 15:40:08 +00:00
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
2020-08-14 13:18:31 +00:00
|
|
|
|
2020-08-26 01:22:53 +00:00
|
|
|
func TestCredentialRequest_OtherwiseValidRequestWithRealTokenShouldFailWhenTheClusterIsNotCapable(t *testing.T) {
|
|
|
|
library.SkipUnlessIntegration(t)
|
|
|
|
library.SkipWhenClusterHasCapability(t, library.ClusterSigningKeyIsAvailable)
|
|
|
|
|
|
|
|
response, err := makeRequest(t, validCredentialRequestSpecWithRealToken(t))
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
|
|
|
require.Equal(t, stringPtr("authentication failed"), response.Status.Message)
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
func makeRequest(t *testing.T, spec v1alpha1.TokenCredentialRequestSpec) (*v1alpha1.TokenCredentialRequest, error) {
|
2020-08-26 01:22:53 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
client := library.NewAnonymousPinnipedClientset(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
ns := library.GetEnv(t, "PINNIPED_NAMESPACE")
|
|
|
|
return client.LoginV1alpha1().TokenCredentialRequests(ns).Create(ctx, &v1alpha1.TokenCredentialRequest{
|
2020-08-26 01:22:53 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{},
|
|
|
|
Spec: spec,
|
|
|
|
}, metav1.CreateOptions{})
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
func validCredentialRequestSpecWithRealToken(t *testing.T) v1alpha1.TokenCredentialRequestSpec {
|
|
|
|
return v1alpha1.TokenCredentialRequestSpec{Token: library.GetEnv(t, "PINNIPED_TEST_USER_TOKEN")}
|
2020-08-26 01:22:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 13:18:31 +00:00
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
2020-08-14 14:01:38 +00:00
|
|
|
|
|
|
|
func getCommonName(t *testing.T, certPEM string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pemBlock, _ := pem.Decode([]byte(certPEM))
|
|
|
|
cert, err := x509.ParseCertificate(pemBlock.Bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return cert.Subject.CommonName
|
|
|
|
}
|
2020-09-10 15:30:15 +00:00
|
|
|
|
2020-09-11 00:10:27 +00:00
|
|
|
func getOrganizations(t *testing.T, certPEM string) []string {
|
2020-09-10 15:30:15 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
pemBlock, _ := pem.Decode([]byte(certPEM))
|
|
|
|
cert, err := x509.ParseCertificate(pemBlock.Bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-11 00:10:27 +00:00
|
|
|
return cert.Subject.Organization
|
2020-09-10 15:30:15 +00:00
|
|
|
}
|