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-07-23 15:05:21 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-22 16:50:00 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-07-23 15:05:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-12-08 01:39:51 +00:00
|
|
|
jwtpkg "gopkg.in/square/go-jose.v2/jwt"
|
2020-09-22 00:55:04 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-09-21 18:40:11 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-09-21 19:30:53 +00:00
|
|
|
|
2020-10-30 16:03:25 +00:00
|
|
|
auth1alpha1 "go.pinniped.dev/generated/1.19/apis/concierge/authentication/v1alpha1"
|
2020-10-30 14:34:43 +00:00
|
|
|
loginv1alpha1 "go.pinniped.dev/generated/1.19/apis/concierge/login/v1alpha1"
|
2020-09-21 19:30:53 +00:00
|
|
|
"go.pinniped.dev/test/library"
|
2020-07-23 15:05:21 +00:00
|
|
|
)
|
|
|
|
|
2020-09-22 15:02:32 +00:00
|
|
|
func TestUnsuccessfulCredentialRequest(t *testing.T) {
|
|
|
|
library.SkipUnlessIntegration(t)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
response, err := makeRequest(ctx, t, validCredentialRequestSpecWithRealToken(t, corev1.TypedLocalObjectReference{
|
2020-10-30 16:03:25 +00:00
|
|
|
APIGroup: &auth1alpha1.SchemeGroupVersion.Group,
|
2020-10-30 16:39:26 +00:00
|
|
|
Kind: "WebhookAuthenticator",
|
2020-09-22 15:02:32 +00:00
|
|
|
Name: "some-webhook-that-does-not-exist",
|
|
|
|
}))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
|
|
|
require.NotNil(t, response.Status.Message)
|
|
|
|
require.Equal(t, "authentication failed", *response.Status.Message)
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func TestSuccessfulCredentialRequest(t *testing.T) {
|
2020-09-24 22:51:43 +00:00
|
|
|
env := library.IntegrationEnv(t).WithCapability(library.ClusterSigningKeyIsAvailable)
|
|
|
|
|
2020-09-24 17:20:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute)
|
2020-09-22 00:55:04 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-12-08 01:39:51 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
authenticator func(t *testing.T) corev1.TypedLocalObjectReference
|
|
|
|
token func(t *testing.T) (token string, username string, groups []string)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "webhook",
|
|
|
|
authenticator: func(t *testing.T) corev1.TypedLocalObjectReference {
|
|
|
|
return library.CreateTestWebhookAuthenticator(ctx, t)
|
|
|
|
},
|
|
|
|
token: func(t *testing.T) (string, string, []string) {
|
|
|
|
return library.IntegrationEnv(t).TestUser.Token, env.TestUser.ExpectedUsername, env.TestUser.ExpectedGroups
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jwt authenticator",
|
|
|
|
authenticator: func(t *testing.T) corev1.TypedLocalObjectReference {
|
|
|
|
return library.CreateTestJWTAuthenticator(ctx, t)
|
|
|
|
},
|
|
|
|
token: func(t *testing.T) (string, string, []string) {
|
2020-12-15 18:19:42 +00:00
|
|
|
pinnipedExe := library.PinnipedCLIPath(t)
|
2020-12-08 01:39:51 +00:00
|
|
|
credOutput, _ := runPinniedLoginOIDC(ctx, t, pinnipedExe)
|
|
|
|
token := credOutput.Status.Token
|
|
|
|
|
|
|
|
// By default, the JWTAuthenticator expects the username to be in the "sub" claim and the
|
|
|
|
// groups to be in the "groups" claim.
|
|
|
|
username, groups := getJWTSubAndGroupsClaims(t, token)
|
|
|
|
|
|
|
|
return credOutput.Status.Token, username, groups
|
|
|
|
},
|
|
|
|
},
|
2020-09-22 16:50:00 +00:00
|
|
|
}
|
2020-12-08 01:39:51 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
authenticator := test.authenticator(t)
|
|
|
|
token, username, groups := test.token(t)
|
|
|
|
|
|
|
|
var response *loginv1alpha1.TokenCredentialRequest
|
|
|
|
successfulResponse := func() bool {
|
|
|
|
var err error
|
|
|
|
response, err = makeRequest(ctx, t, loginv1alpha1.TokenCredentialRequestSpec{
|
|
|
|
Token: token,
|
|
|
|
Authenticator: authenticator,
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "the request should never fail at the HTTP level")
|
|
|
|
return response.Status.Credential != nil
|
|
|
|
}
|
|
|
|
assert.Eventually(t, successfulResponse, 10*time.Second, 500*time.Millisecond)
|
|
|
|
require.NotNil(t, response)
|
|
|
|
require.Emptyf(t, response.Status.Message, "value is: %q", safeDerefStringPtr(response.Status.Message))
|
|
|
|
require.NotNil(t, response.Status.Credential)
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Empty(t, response.Status.Credential.Token)
|
|
|
|
require.NotEmpty(t, response.Status.Credential.ClientCertificateData)
|
|
|
|
require.Equal(t, username, getCommonName(t, response.Status.Credential.ClientCertificateData))
|
|
|
|
require.ElementsMatch(t, groups, getOrganizations(t, response.Status.Credential.ClientCertificateData))
|
|
|
|
require.NotEmpty(t, response.Status.Credential.ClientKeyData)
|
|
|
|
require.NotNil(t, response.Status.Credential.ExpirationTimestamp)
|
|
|
|
require.InDelta(t, 5*time.Minute, time.Until(response.Status.Credential.ExpirationTimestamp.Time), float64(time.Minute))
|
|
|
|
|
|
|
|
// Create a client using the admin kubeconfig.
|
|
|
|
adminClient := library.NewClientset(t)
|
|
|
|
|
|
|
|
// Create a client using the certificate from the CredentialRequest.
|
|
|
|
clientWithCertFromCredentialRequest := library.NewClientsetWithCertAndKey(
|
|
|
|
t,
|
|
|
|
response.Status.Credential.ClientCertificateData,
|
|
|
|
response.Status.Credential.ClientKeyData,
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Run(
|
|
|
|
"access as user",
|
|
|
|
library.AccessAsUserTest(ctx, adminClient, username, clientWithCertFromCredentialRequest),
|
|
|
|
)
|
|
|
|
for _, group := range groups {
|
|
|
|
group := group
|
|
|
|
t.Run(
|
|
|
|
"access as group "+group,
|
|
|
|
library.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-09-24 22:51:43 +00:00
|
|
|
library.IntegrationEnv(t).WithCapability(library.ClusterSigningKeyIsAvailable)
|
2020-08-25 01:07:34 +00:00
|
|
|
|
2020-09-22 15:02:32 +00:00
|
|
|
response, err := makeRequest(context.Background(), t, loginv1alpha1.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-09-24 22:51:43 +00:00
|
|
|
library.IntegrationEnv(t).WithCapability(library.ClusterSigningKeyIsAvailable)
|
2020-08-25 01:07:34 +00:00
|
|
|
|
2020-09-22 15:02:32 +00:00
|
|
|
response, err := makeRequest(context.Background(), t, loginv1alpha1.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) {
|
2020-09-24 22:51:43 +00:00
|
|
|
library.IntegrationEnv(t).WithoutCapability(library.ClusterSigningKeyIsAvailable)
|
2020-08-26 01:22:53 +00:00
|
|
|
|
2020-09-22 00:55:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
testWebhook := library.CreateTestWebhookAuthenticator(ctx, t)
|
2020-09-22 00:55:04 +00:00
|
|
|
|
|
|
|
response, err := makeRequest(ctx, t, validCredentialRequestSpecWithRealToken(t, testWebhook))
|
2020-08-26 01:22:53 +00:00
|
|
|
|
|
|
|
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-22 15:02:32 +00:00
|
|
|
func makeRequest(ctx context.Context, t *testing.T, spec loginv1alpha1.TokenCredentialRequestSpec) (*loginv1alpha1.TokenCredentialRequest, error) {
|
2020-08-26 01:22:53 +00:00
|
|
|
t.Helper()
|
2020-09-24 22:51:43 +00:00
|
|
|
env := library.IntegrationEnv(t)
|
2020-08-26 01:22:53 +00:00
|
|
|
|
2020-10-30 20:09:14 +00:00
|
|
|
client := library.NewAnonymousConciergeClientset(t)
|
2020-08-26 01:22:53 +00:00
|
|
|
|
2020-09-22 00:55:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
2020-08-26 01:22:53 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-10-09 17:11:47 +00:00
|
|
|
return client.LoginV1alpha1().TokenCredentialRequests(env.ConciergeNamespace).Create(ctx, &loginv1alpha1.TokenCredentialRequest{
|
2020-08-26 01:22:53 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{},
|
2020-10-09 17:11:47 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: env.ConciergeNamespace},
|
2020-08-26 01:22:53 +00:00
|
|
|
Spec: spec,
|
|
|
|
}, metav1.CreateOptions{})
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
func validCredentialRequestSpecWithRealToken(t *testing.T, authenticator corev1.TypedLocalObjectReference) loginv1alpha1.TokenCredentialRequestSpec {
|
2020-09-22 15:02:32 +00:00
|
|
|
return loginv1alpha1.TokenCredentialRequestSpec{
|
2020-10-30 17:41:21 +00:00
|
|
|
Token: library.IntegrationEnv(t).TestUser.Token,
|
2020-10-30 19:02:21 +00:00
|
|
|
Authenticator: authenticator,
|
2020-09-22 00:55:04 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-12-08 01:39:51 +00:00
|
|
|
|
|
|
|
func safeDerefStringPtr(s *string) string {
|
|
|
|
if s == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return *s
|
|
|
|
}
|
|
|
|
|
|
|
|
func getJWTSubAndGroupsClaims(t *testing.T, jwt string) (string, []string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
token, err := jwtpkg.ParseSigned(jwt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var claims struct {
|
|
|
|
Sub string `json:"sub"`
|
|
|
|
Groups []string `json:"groups"`
|
|
|
|
}
|
|
|
|
err = token.UnsafeClaimsWithoutVerification(&claims)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return claims.Sub, claims.Groups
|
|
|
|
}
|