2021-01-07 22:58:09 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// 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"
|
|
|
|
|
|
|
|
"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"
|
2021-05-12 20:20:00 +00:00
|
|
|
"k8s.io/utils/pointer"
|
2020-09-21 19:30:53 +00:00
|
|
|
|
2021-02-16 19:00:08 +00:00
|
|
|
auth1alpha1 "go.pinniped.dev/generated/latest/apis/concierge/authentication/v1alpha1"
|
|
|
|
loginv1alpha1 "go.pinniped.dev/generated/latest/apis/concierge/login/v1alpha1"
|
2021-06-22 15:23:19 +00:00
|
|
|
"go.pinniped.dev/test/testlib"
|
2020-07-23 15:05:21 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 19:34:33 +00:00
|
|
|
// TCRs are non-mutating and safe to run in parallel with serial tests, see main_test.go.
|
|
|
|
func TestUnsuccessfulCredentialRequest_Parallel(t *testing.T) {
|
2021-06-22 15:23:19 +00:00
|
|
|
env := testlib.IntegrationEnv(t).WithCapability(testlib.AnonymousAuthenticationSupported)
|
2021-01-12 20:55:31 +00:00
|
|
|
|
2021-03-05 01:25:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2020-09-22 15:02:32 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
response, err := testlib.CreateTokenCredentialRequest(ctx, t,
|
2021-03-10 22:17:20 +00:00
|
|
|
loginv1alpha1.TokenCredentialRequestSpec{
|
|
|
|
Token: env.TestUser.Token,
|
|
|
|
Authenticator: corev1.TypedLocalObjectReference{
|
|
|
|
APIGroup: &auth1alpha1.SchemeGroupVersion.Group,
|
|
|
|
Kind: "WebhookAuthenticator",
|
|
|
|
Name: "some-webhook-that-does-not-exist",
|
|
|
|
},
|
|
|
|
},
|
2021-03-10 18:30:06 +00:00
|
|
|
)
|
2020-09-22 15:02:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
|
|
|
require.NotNil(t, response.Status.Message)
|
|
|
|
require.Equal(t, "authentication failed", *response.Status.Message)
|
|
|
|
}
|
|
|
|
|
2022-02-16 14:20:28 +00:00
|
|
|
// TestSuccessfulCredentialRequest_Browser cannot run in parallel because runPinnipedLoginOIDC uses a fixed port
|
2021-10-20 11:59:24 +00:00
|
|
|
// for its localhost listener via --listen-port=env.CLIUpstreamOIDC.CallbackURL.Port() per oidcLoginCommand.
|
|
|
|
// Since ports are global to the process, tests using oidcLoginCommand must be run serially.
|
2022-02-16 14:20:28 +00:00
|
|
|
func TestSuccessfulCredentialRequest_Browser(t *testing.T) {
|
2021-06-22 15:23:19 +00:00
|
|
|
env := testlib.IntegrationEnv(t).WithCapability(testlib.ClusterSigningKeyIsAvailable)
|
2020-09-24 22:51:43 +00:00
|
|
|
|
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
|
2020-12-15 18:23:52 +00:00
|
|
|
authenticator func(context.Context, *testing.T) corev1.TypedLocalObjectReference
|
2020-12-08 01:39:51 +00:00
|
|
|
token func(t *testing.T) (token string, username string, groups []string)
|
|
|
|
}{
|
|
|
|
{
|
2020-12-15 18:23:52 +00:00
|
|
|
name: "webhook",
|
2021-06-22 15:23:19 +00:00
|
|
|
authenticator: testlib.CreateTestWebhookAuthenticator,
|
2020-12-08 01:39:51 +00:00
|
|
|
token: func(t *testing.T) (string, string, []string) {
|
2021-06-22 15:23:19 +00:00
|
|
|
return testlib.IntegrationEnv(t).TestUser.Token, env.TestUser.ExpectedUsername, env.TestUser.ExpectedGroups
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-12-15 18:23:52 +00:00
|
|
|
name: "jwt authenticator",
|
2021-06-22 15:23:19 +00:00
|
|
|
authenticator: testlib.CreateTestJWTAuthenticatorForCLIUpstream,
|
2020-12-08 01:39:51 +00:00
|
|
|
token: func(t *testing.T) (string, string, []string) {
|
2021-06-22 15:23:19 +00:00
|
|
|
pinnipedExe := testlib.PinnipedCLIPath(t)
|
2020-12-15 18:23:52 +00:00
|
|
|
credOutput, _ := runPinnipedLoginOIDC(ctx, t, pinnipedExe)
|
2020-12-08 01:39:51 +00:00
|
|
|
token := credOutput.Status.Token
|
|
|
|
|
2020-12-16 01:16:08 +00:00
|
|
|
// By default, the JWTAuthenticator expects the username to be in the "username" claim and the
|
2020-12-08 01:39:51 +00:00
|
|
|
// groups to be in the "groups" claim.
|
2020-12-16 02:09:44 +00:00
|
|
|
// However, we are configuring Pinniped in the `CreateTestJWTAuthenticatorForCLIUpstream` method above
|
|
|
|
// to read the username from the "sub" claim of the token instead.
|
2020-12-08 01:39:51 +00:00
|
|
|
username, groups := getJWTSubAndGroupsClaims(t, token)
|
|
|
|
|
2020-12-16 01:16:08 +00:00
|
|
|
return token, username, groups
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
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) {
|
2020-12-15 18:23:52 +00:00
|
|
|
authenticator := test.authenticator(ctx, t)
|
2020-12-08 01:39:51 +00:00
|
|
|
token, username, groups := test.token(t)
|
|
|
|
|
|
|
|
var response *loginv1alpha1.TokenCredentialRequest
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
|
2020-12-08 01:39:51 +00:00
|
|
|
var err error
|
2021-06-22 15:23:19 +00:00
|
|
|
response, err = testlib.CreateTokenCredentialRequest(ctx, t,
|
2021-03-10 18:30:06 +00:00
|
|
|
loginv1alpha1.TokenCredentialRequestSpec{Token: token, Authenticator: authenticator},
|
|
|
|
)
|
2021-06-16 22:51:23 +00:00
|
|
|
requireEventually.NoError(err, "the request should never fail at the HTTP level")
|
|
|
|
requireEventually.NotNil(response)
|
|
|
|
requireEventually.NotNil(response.Status.Credential, "the response should contain a credential")
|
|
|
|
requireEventually.Emptyf(response.Status.Message, "value is: %q", safeDerefStringPtr(response.Status.Message))
|
|
|
|
requireEventually.NotNil(response.Status.Credential)
|
|
|
|
requireEventually.Empty(response.Spec)
|
|
|
|
requireEventually.Empty(response.Status.Credential.Token)
|
|
|
|
requireEventually.NotEmpty(response.Status.Credential.ClientCertificateData)
|
|
|
|
requireEventually.Equal(username, getCommonName(t, response.Status.Credential.ClientCertificateData))
|
|
|
|
requireEventually.ElementsMatch(groups, getOrganizations(t, response.Status.Credential.ClientCertificateData))
|
|
|
|
requireEventually.NotEmpty(response.Status.Credential.ClientKeyData)
|
|
|
|
requireEventually.NotNil(response.Status.Credential.ExpirationTimestamp)
|
|
|
|
requireEventually.InDelta(5*time.Minute, time.Until(response.Status.Credential.ExpirationTimestamp.Time), float64(time.Minute))
|
|
|
|
}, 10*time.Second, 500*time.Millisecond)
|
2020-12-08 01:39:51 +00:00
|
|
|
|
|
|
|
// Create a client using the certificate from the CredentialRequest.
|
2021-06-22 15:23:19 +00:00
|
|
|
clientWithCertFromCredentialRequest := testlib.NewClientsetWithCertAndKey(
|
2020-12-08 01:39:51 +00:00
|
|
|
t,
|
|
|
|
response.Status.Credential.ClientCertificateData,
|
|
|
|
response.Status.Credential.ClientKeyData,
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Run(
|
|
|
|
"access as user",
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.AccessAsUserTest(ctx, username, clientWithCertFromCredentialRequest),
|
2020-12-08 01:39:51 +00:00
|
|
|
)
|
|
|
|
for _, group := range groups {
|
|
|
|
group := group
|
|
|
|
t.Run(
|
|
|
|
"access as group "+group,
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.AccessAsGroupTest(ctx, group, clientWithCertFromCredentialRequest),
|
2020-12-08 01:39:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2020-09-10 15:30:15 +00:00
|
|
|
}
|
2020-07-23 23:01:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 19:34:33 +00:00
|
|
|
// TCRs are non-mutating and safe to run in parallel with serial tests, see main_test.go.
|
|
|
|
func TestFailedCredentialRequestWhenTheRequestIsValidButTheTokenDoesNotAuthenticateTheUser_Parallel(t *testing.T) {
|
2021-06-22 15:23:19 +00:00
|
|
|
_ = testlib.IntegrationEnv(t).WithCapability(testlib.ClusterSigningKeyIsAvailable)
|
2020-08-25 01:07:34 +00:00
|
|
|
|
2021-02-05 01:02:59 +00:00
|
|
|
// Create a testWebhook so we have a legitimate authenticator to pass to the
|
|
|
|
// TokenCredentialRequest API.
|
2021-03-05 01:25:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2021-02-05 01:02:59 +00:00
|
|
|
defer cancel()
|
2021-06-22 15:23:19 +00:00
|
|
|
testWebhook := testlib.CreateTestWebhookAuthenticator(ctx, t)
|
2021-02-05 01:02:59 +00:00
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
response, err := testlib.CreateTokenCredentialRequest(context.Background(), t,
|
2021-03-10 18:30:06 +00:00
|
|
|
loginv1alpha1.TokenCredentialRequestSpec{Token: "not a good token", Authenticator: testWebhook},
|
|
|
|
)
|
2020-07-23 23:01:55 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Empty(t, response.Spec)
|
|
|
|
require.Nil(t, response.Status.Credential)
|
2021-05-12 20:20:00 +00:00
|
|
|
require.Equal(t, pointer.StringPtr("authentication failed"), response.Status.Message)
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 19:34:33 +00:00
|
|
|
// TCRs are non-mutating and safe to run in parallel with serial tests, see main_test.go.
|
|
|
|
func TestCredentialRequest_ShouldFailWhenRequestDoesNotIncludeToken_Parallel(t *testing.T) {
|
2021-06-22 15:23:19 +00:00
|
|
|
_ = testlib.IntegrationEnv(t).WithCapability(testlib.ClusterSigningKeyIsAvailable)
|
2020-08-25 01:07:34 +00:00
|
|
|
|
2021-02-05 01:02:59 +00:00
|
|
|
// Create a testWebhook so we have a legitimate authenticator to pass to the
|
|
|
|
// TokenCredentialRequest API.
|
2021-03-05 01:25:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2021-02-05 01:02:59 +00:00
|
|
|
defer cancel()
|
2021-06-22 15:23:19 +00:00
|
|
|
testWebhook := testlib.CreateTestWebhookAuthenticator(ctx, t)
|
2021-02-05 01:02:59 +00:00
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
response, err := testlib.CreateTokenCredentialRequest(context.Background(), t,
|
2021-03-10 18:30:06 +00:00
|
|
|
loginv1alpha1.TokenCredentialRequestSpec{Token: "", Authenticator: testWebhook},
|
|
|
|
)
|
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-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
|
|
|
|
}
|