2023-05-10 21:12:38 +00:00
|
|
|
// Copyright 2020-2023 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
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
package credentialrequest
|
2020-07-23 15:05:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-07-24 16:52:38 +00:00
|
|
|
"fmt"
|
2020-07-23 15:05:21 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-12-10 22:22:36 +00:00
|
|
|
"github.com/go-logr/logr"
|
2020-07-27 13:08:39 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-08-06 22:14:30 +00:00
|
|
|
"github.com/sclevine/spec"
|
2020-07-23 15:05:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-23 16:50:23 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2020-07-23 15:05:21 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2021-02-05 15:55:19 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2020-07-24 15:21:36 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
2020-07-23 15:05:21 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
2020-08-06 22:14:30 +00:00
|
|
|
"k8s.io/klog/v2"
|
2023-07-28 14:16:02 +00:00
|
|
|
"k8s.io/utils/ptr"
|
2020-07-23 15:05:21 +00:00
|
|
|
|
2021-02-16 19:00:08 +00:00
|
|
|
loginapi "go.pinniped.dev/generated/latest/apis/concierge/login"
|
2021-03-10 18:30:06 +00:00
|
|
|
"go.pinniped.dev/internal/issuer"
|
2020-09-21 16:37:54 +00:00
|
|
|
"go.pinniped.dev/internal/mocks/credentialrequestmocks"
|
2021-03-13 00:09:16 +00:00
|
|
|
"go.pinniped.dev/internal/mocks/issuermocks"
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/testutil"
|
2020-07-23 15:05:21 +00:00
|
|
|
)
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
func TestNew(t *testing.T) {
|
2021-02-05 15:55:19 +00:00
|
|
|
r := NewREST(nil, nil, schema.GroupResource{Group: "bears", Resource: "panda"})
|
2020-09-21 16:37:54 +00:00
|
|
|
require.NotNil(t, r)
|
2021-02-09 16:57:34 +00:00
|
|
|
require.False(t, r.NamespaceScoped())
|
2020-11-13 18:09:22 +00:00
|
|
|
require.Equal(t, []string{"pinniped"}, r.Categories())
|
2023-05-10 21:12:38 +00:00
|
|
|
require.Equal(t, "tokencredentialrequest", r.GetSingularName())
|
2020-09-21 16:37:54 +00:00
|
|
|
require.IsType(t, &loginapi.TokenCredentialRequest{}, r.New())
|
2021-02-05 15:55:19 +00:00
|
|
|
require.IsType(t, &loginapi.TokenCredentialRequestList{}, r.NewList())
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// check the simple invariants of our no-op list
|
|
|
|
list, err := r.List(ctx, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, list)
|
|
|
|
require.IsType(t, &loginapi.TokenCredentialRequestList{}, list)
|
|
|
|
require.Equal(t, "0", list.(*loginapi.TokenCredentialRequestList).ResourceVersion)
|
|
|
|
require.NotNil(t, list.(*loginapi.TokenCredentialRequestList).Items)
|
|
|
|
require.Len(t, list.(*loginapi.TokenCredentialRequestList).Items, 0)
|
|
|
|
|
|
|
|
// make sure we can turn lists into tables if needed
|
|
|
|
table, err := r.ConvertToTable(ctx, list, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, table)
|
|
|
|
require.Equal(t, "0", table.ResourceVersion)
|
|
|
|
require.Nil(t, table.Rows)
|
|
|
|
|
|
|
|
// exercise group resource - force error by passing a runtime.Object that does not have an embedded object meta
|
|
|
|
_, err = r.ConvertToTable(ctx, &metav1.APIGroup{}, nil)
|
|
|
|
require.Error(t, err, "the resource panda.bears does not support being converted to a Table")
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 22:14:30 +00:00
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
spec.Run(t, "create", func(t *testing.T, when spec.G, it spec.S) {
|
|
|
|
var r *require.Assertions
|
|
|
|
var ctrl *gomock.Controller
|
|
|
|
var logger *testutil.TranscriptLogger
|
|
|
|
|
|
|
|
it.Before(func() {
|
|
|
|
r = require.New(t)
|
|
|
|
ctrl = gomock.NewController(t)
|
2022-08-24 21:45:55 +00:00
|
|
|
logger = testutil.NewTranscriptLogger(t) //nolint:staticcheck // old test with lots of log statements
|
2022-04-16 02:43:53 +00:00
|
|
|
klog.SetLogger(logr.New(logger)) // this is unfortunately a global logger, so can't run these tests in parallel :(
|
2020-08-06 22:14:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it.After(func() {
|
2021-12-10 22:22:36 +00:00
|
|
|
klog.ClearLogger()
|
2020-08-06 22:14:30 +00:00
|
|
|
ctrl.Finish()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateSucceedsWhenGivenATokenAndTheWebhookAuthenticatesTheToken", func() {
|
2020-09-21 16:37:54 +00:00
|
|
|
req := validCredentialRequest()
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).
|
|
|
|
Return(&user.DefaultInfo{
|
|
|
|
Name: "test-user",
|
|
|
|
Groups: []string{"test-group-1", "test-group-2"},
|
|
|
|
}, nil)
|
2020-09-16 19:57:18 +00:00
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
clientCertIssuer := issuermocks.NewMockClientCertIssuer(ctrl)
|
|
|
|
clientCertIssuer.EXPECT().IssueClientCertPEM(
|
|
|
|
"test-user",
|
|
|
|
[]string{"test-group-1", "test-group-2"},
|
2020-11-13 16:43:23 +00:00
|
|
|
5*time.Minute,
|
2020-09-16 19:57:18 +00:00
|
|
|
).Return([]byte("test-cert"), []byte("test-key"), nil)
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
storage := NewREST(requestAuthenticator, clientCertIssuer, schema.GroupResource{})
|
2020-09-16 19:57:18 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
2020-09-16 19:57:18 +00:00
|
|
|
|
|
|
|
r.NoError(err)
|
|
|
|
r.IsType(&loginapi.TokenCredentialRequest{}, response)
|
|
|
|
|
|
|
|
expires := response.(*loginapi.TokenCredentialRequest).Status.Credential.ExpirationTimestamp
|
|
|
|
r.NotNil(expires)
|
2020-11-13 16:43:23 +00:00
|
|
|
r.InDelta(time.Now().Add(5*time.Minute).Unix(), expires.Unix(), 5)
|
2020-09-16 19:57:18 +00:00
|
|
|
response.(*loginapi.TokenCredentialRequest).Status.Credential.ExpirationTimestamp = metav1.Time{}
|
|
|
|
|
|
|
|
r.Equal(response, &loginapi.TokenCredentialRequest{
|
|
|
|
Status: loginapi.TokenCredentialRequestStatus{
|
|
|
|
Credential: &loginapi.ClusterCredential{
|
|
|
|
ExpirationTimestamp: metav1.Time{},
|
|
|
|
ClientCertificateData: "test-cert",
|
|
|
|
ClientKeyData: "test-key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-05-03 18:06:49 +00:00
|
|
|
requireOneLogStatement(r, logger, `"success" userID:,hasExtra:false,authenticated:true`)
|
2020-09-16 19:57:18 +00:00
|
|
|
})
|
|
|
|
|
2020-08-06 22:14:30 +00:00
|
|
|
it("CreateFailsWithValidTokenWhenCertIssuerFails", func() {
|
2020-09-21 16:37:54 +00:00
|
|
|
req := validCredentialRequest()
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).
|
|
|
|
Return(&user.DefaultInfo{
|
|
|
|
Name: "test-user",
|
|
|
|
Groups: []string{"test-group-1", "test-group-2"},
|
|
|
|
}, nil)
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
clientCertIssuer := issuermocks.NewMockClientCertIssuer(ctrl)
|
|
|
|
clientCertIssuer.EXPECT().
|
|
|
|
IssueClientCertPEM(gomock.Any(), gomock.Any(), gomock.Any()).
|
2020-08-06 22:14:30 +00:00
|
|
|
Return(nil, nil, fmt.Errorf("some certificate authority error"))
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
storage := NewREST(requestAuthenticator, clientCertIssuer, schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
2020-08-06 22:14:30 +00:00
|
|
|
requireSuccessfulResponseWithAuthenticationFailureMessage(t, err, response)
|
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:cert issuer,msg:some certificate authority error`)
|
|
|
|
})
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
it("CreateSucceedsWithAnUnauthenticatedStatusWhenGivenATokenAndTheWebhookReturnsNilUser", func() {
|
|
|
|
req := validCredentialRequest()
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).Return(nil, nil)
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(requestAuthenticator, nil, schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
2020-08-06 22:14:30 +00:00
|
|
|
|
|
|
|
requireSuccessfulResponseWithAuthenticationFailureMessage(t, err, response)
|
2021-05-03 18:06:49 +00:00
|
|
|
requireOneLogStatement(r, logger, `"success" userID:<none>,hasExtra:false,authenticated:false`)
|
2020-08-06 22:14:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateSucceedsWithAnUnauthenticatedStatusWhenWebhookFails", func() {
|
2020-09-21 16:37:54 +00:00
|
|
|
req := validCredentialRequest()
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).
|
|
|
|
Return(nil, errors.New("some webhook error"))
|
2020-09-11 20:08:54 +00:00
|
|
|
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(requestAuthenticator, nil, schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
2020-08-06 22:14:30 +00:00
|
|
|
|
|
|
|
requireSuccessfulResponseWithAuthenticationFailureMessage(t, err, response)
|
2020-12-08 01:39:51 +00:00
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:token authentication,msg:some webhook error`)
|
2020-08-06 22:14:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateSucceedsWithAnUnauthenticatedStatusWhenWebhookReturnsAnEmptyUsername", func() {
|
2020-09-21 16:37:54 +00:00
|
|
|
req := validCredentialRequest()
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).
|
|
|
|
Return(&user.DefaultInfo{Name: ""}, nil)
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(requestAuthenticator, nil, schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requireSuccessfulResponseWithAuthenticationFailureMessage(t, err, response)
|
2021-05-03 18:06:49 +00:00
|
|
|
requireOneLogStatement(r, logger, `"success" userID:,hasExtra:false,authenticated:false`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateSucceedsWithAnUnauthenticatedStatusWhenWebhookReturnsAUserWithUID", func() {
|
|
|
|
req := validCredentialRequest()
|
|
|
|
|
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).
|
|
|
|
Return(&user.DefaultInfo{
|
|
|
|
Name: "test-user",
|
|
|
|
UID: "test-uid",
|
|
|
|
Groups: []string{"test-group-1", "test-group-2"},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
storage := NewREST(requestAuthenticator, nil, schema.GroupResource{})
|
|
|
|
|
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
|
|
|
|
|
|
|
requireSuccessfulResponseWithAuthenticationFailureMessage(t, err, response)
|
|
|
|
requireOneLogStatement(r, logger, `"success" userID:test-uid,hasExtra:false,authenticated:false`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateSucceedsWithAnUnauthenticatedStatusWhenWebhookReturnsAUserWithExtra", func() {
|
|
|
|
req := validCredentialRequest()
|
|
|
|
|
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req).
|
|
|
|
Return(&user.DefaultInfo{
|
|
|
|
Name: "test-user",
|
|
|
|
Groups: []string{"test-group-1", "test-group-2"},
|
|
|
|
Extra: map[string][]string{"test-key": {"test-val-1", "test-val-2"}},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
storage := NewREST(requestAuthenticator, nil, schema.GroupResource{})
|
|
|
|
|
|
|
|
response, err := callCreate(context.Background(), storage, req)
|
|
|
|
|
|
|
|
requireSuccessfulResponseWithAuthenticationFailureMessage(t, err, response)
|
|
|
|
requireOneLogStatement(r, logger, `"success" userID:,hasExtra:true,authenticated:false`)
|
2020-08-06 22:14:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateFailsWhenGivenTheWrongInputType", func() {
|
2020-08-14 14:11:14 +00:00
|
|
|
notACredentialRequest := runtime.Unknown{}
|
2021-02-05 15:55:19 +00:00
|
|
|
response, err := NewREST(nil, nil, schema.GroupResource{}).Create(
|
2020-08-06 22:14:30 +00:00
|
|
|
genericapirequest.NewContext(),
|
2020-08-14 14:11:14 +00:00
|
|
|
¬ACredentialRequest,
|
2020-08-06 22:14:30 +00:00
|
|
|
rest.ValidateAllObjectFunc,
|
|
|
|
&metav1.CreateOptions{})
|
|
|
|
|
2020-09-21 16:37:54 +00:00
|
|
|
requireAPIError(t, response, err, apierrors.IsBadRequest, "not a TokenCredentialRequest")
|
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:request validation,msg:not a TokenCredentialRequest`)
|
2020-08-06 22:14:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateFailsWhenTokenValueIsEmptyInRequest", func() {
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(nil, nil, schema.GroupResource{})
|
2020-09-18 22:15:04 +00:00
|
|
|
response, err := callCreate(context.Background(), storage, credentialRequest(loginapi.TokenCredentialRequestSpec{
|
|
|
|
Token: "",
|
2020-08-06 22:14:30 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
requireAPIError(t, response, err, apierrors.IsInvalid,
|
2020-08-20 17:54:15 +00:00
|
|
|
`.pinniped.dev "request name" is invalid: spec.token.value: Required value: token must be supplied`)
|
2020-08-06 22:14:30 +00:00
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:request validation,msg:token must be supplied`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateFailsWhenValidationFails", func() {
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(nil, nil, schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
response, err := storage.Create(
|
|
|
|
context.Background(),
|
2020-08-14 14:11:14 +00:00
|
|
|
validCredentialRequest(),
|
2020-08-06 22:14:30 +00:00
|
|
|
func(ctx context.Context, obj runtime.Object) error {
|
|
|
|
return fmt.Errorf("some validation error")
|
|
|
|
},
|
|
|
|
&metav1.CreateOptions{})
|
|
|
|
r.Nil(response)
|
|
|
|
r.EqualError(err, "some validation error")
|
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:validation webhook,msg:some validation error`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateDoesNotAllowValidationFunctionToMutateRequest", func() {
|
2020-09-21 16:37:54 +00:00
|
|
|
req := validCredentialRequest()
|
|
|
|
|
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req.DeepCopy()).
|
|
|
|
Return(&user.DefaultInfo{Name: "test-user"}, nil)
|
|
|
|
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(requestAuthenticator, successfulIssuer(ctrl), schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
response, err := storage.Create(
|
|
|
|
context.Background(),
|
2020-09-21 16:37:54 +00:00
|
|
|
req,
|
2020-08-06 22:14:30 +00:00
|
|
|
func(ctx context.Context, obj runtime.Object) error {
|
2020-09-18 22:15:04 +00:00
|
|
|
credentialRequest, _ := obj.(*loginapi.TokenCredentialRequest)
|
|
|
|
credentialRequest.Spec.Token = "foobaz"
|
2020-08-06 22:14:30 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
&metav1.CreateOptions{})
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotEmpty(response)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateDoesNotAllowValidationFunctionToSeeTheActualRequestToken", func() {
|
2020-09-21 16:37:54 +00:00
|
|
|
req := validCredentialRequest()
|
|
|
|
|
|
|
|
requestAuthenticator := credentialrequestmocks.NewMockTokenCredentialRequestAuthenticator(ctrl)
|
|
|
|
requestAuthenticator.EXPECT().AuthenticateTokenCredentialRequest(gomock.Any(), req.DeepCopy()).
|
|
|
|
Return(&user.DefaultInfo{Name: "test-user"}, nil)
|
2020-08-06 22:14:30 +00:00
|
|
|
|
2021-02-05 15:55:19 +00:00
|
|
|
storage := NewREST(requestAuthenticator, successfulIssuer(ctrl), schema.GroupResource{})
|
2020-08-06 22:14:30 +00:00
|
|
|
validationFunctionWasCalled := false
|
|
|
|
var validationFunctionSawTokenValue string
|
|
|
|
response, err := storage.Create(
|
|
|
|
context.Background(),
|
2020-09-21 16:37:54 +00:00
|
|
|
req,
|
2020-08-06 22:14:30 +00:00
|
|
|
func(ctx context.Context, obj runtime.Object) error {
|
2020-09-18 22:15:04 +00:00
|
|
|
credentialRequest, _ := obj.(*loginapi.TokenCredentialRequest)
|
2020-08-06 22:14:30 +00:00
|
|
|
validationFunctionWasCalled = true
|
2020-09-18 22:15:04 +00:00
|
|
|
validationFunctionSawTokenValue = credentialRequest.Spec.Token
|
2020-08-06 22:14:30 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
&metav1.CreateOptions{})
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotEmpty(response)
|
|
|
|
r.True(validationFunctionWasCalled)
|
|
|
|
r.Empty(validationFunctionSawTokenValue)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("CreateFailsWhenRequestOptionsDryRunIsNotEmpty", func() {
|
2021-02-05 15:55:19 +00:00
|
|
|
response, err := NewREST(nil, nil, schema.GroupResource{}).Create(
|
2020-08-06 22:14:30 +00:00
|
|
|
genericapirequest.NewContext(),
|
2020-08-14 14:11:14 +00:00
|
|
|
validCredentialRequest(),
|
2020-08-06 22:14:30 +00:00
|
|
|
rest.ValidateAllObjectFunc,
|
|
|
|
&metav1.CreateOptions{
|
|
|
|
DryRun: []string{"some dry run flag"},
|
|
|
|
})
|
|
|
|
|
|
|
|
requireAPIError(t, response, err, apierrors.IsInvalid,
|
2020-08-20 17:54:15 +00:00
|
|
|
`.pinniped.dev "request name" is invalid: dryRun: Unsupported value: []string{"some dry run flag"}`)
|
2020-08-06 22:14:30 +00:00
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:request validation,msg:dryRun not supported`)
|
|
|
|
})
|
2021-02-19 18:21:10 +00:00
|
|
|
|
|
|
|
it("CreateFailsWhenNamespaceIsNotEmpty", func() {
|
|
|
|
response, err := NewREST(nil, nil, schema.GroupResource{}).Create(
|
|
|
|
genericapirequest.WithNamespace(genericapirequest.NewContext(), "some-ns"),
|
|
|
|
validCredentialRequest(),
|
|
|
|
rest.ValidateAllObjectFunc,
|
|
|
|
&metav1.CreateOptions{})
|
|
|
|
|
|
|
|
requireAPIError(t, response, err, apierrors.IsBadRequest, `namespace is not allowed on TokenCredentialRequest: some-ns`)
|
|
|
|
requireOneLogStatement(r, logger, `"failure" failureType:request validation,msg:namespace is not allowed`)
|
|
|
|
})
|
2020-08-06 22:14:30 +00:00
|
|
|
}, spec.Sequential())
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireOneLogStatement(r *require.Assertions, logger *testutil.TranscriptLogger, messageContains string) {
|
2020-08-19 18:21:07 +00:00
|
|
|
transcript := logger.Transcript()
|
|
|
|
r.Len(transcript, 1)
|
|
|
|
r.Equal("info", transcript[0].Level)
|
|
|
|
r.Contains(transcript[0].Message, messageContains)
|
2020-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 19:57:18 +00:00
|
|
|
func callCreate(ctx context.Context, storage *REST, obj runtime.Object) (runtime.Object, error) {
|
2020-07-23 15:05:21 +00:00
|
|
|
return storage.Create(
|
|
|
|
ctx,
|
2020-09-16 19:57:18 +00:00
|
|
|
obj,
|
2020-07-23 15:05:21 +00:00
|
|
|
rest.ValidateAllObjectFunc,
|
|
|
|
&metav1.CreateOptions{
|
|
|
|
DryRun: []string{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
func validCredentialRequest() *loginapi.TokenCredentialRequest {
|
2020-08-14 14:11:14 +00:00
|
|
|
return validCredentialRequestWithToken("some token")
|
2020-07-23 23:01:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
func validCredentialRequestWithToken(token string) *loginapi.TokenCredentialRequest {
|
|
|
|
return credentialRequest(loginapi.TokenCredentialRequestSpec{Token: token})
|
2020-07-23 15:05:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:15:04 +00:00
|
|
|
func credentialRequest(spec loginapi.TokenCredentialRequestSpec) *loginapi.TokenCredentialRequest {
|
|
|
|
return &loginapi.TokenCredentialRequest{
|
2020-07-23 15:05:21 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "request name",
|
|
|
|
},
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:50:23 +00:00
|
|
|
func requireAPIError(t *testing.T, response runtime.Object, err error, expectedErrorTypeChecker func(err error) bool, expectedErrorMessage string) {
|
|
|
|
t.Helper()
|
|
|
|
require.Nil(t, response)
|
|
|
|
require.True(t, expectedErrorTypeChecker(err))
|
|
|
|
var status apierrors.APIStatus
|
|
|
|
errors.As(err, &status)
|
|
|
|
require.Contains(t, status.Status().Message, expectedErrorMessage)
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:00:29 +00:00
|
|
|
func requireSuccessfulResponseWithAuthenticationFailureMessage(t *testing.T, err error, response runtime.Object) {
|
2020-08-06 22:14:30 +00:00
|
|
|
t.Helper()
|
2020-07-24 18:00:29 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-18 22:15:04 +00:00
|
|
|
require.Equal(t, response, &loginapi.TokenCredentialRequest{
|
|
|
|
Status: loginapi.TokenCredentialRequestStatus{
|
2020-07-24 18:00:29 +00:00
|
|
|
Credential: nil,
|
2023-07-28 14:16:02 +00:00
|
|
|
Message: ptr.To("authentication failed"),
|
2020-07-24 18:00:29 +00:00
|
|
|
},
|
|
|
|
})
|
2020-07-24 15:21:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
func successfulIssuer(ctrl *gomock.Controller) issuer.ClientCertIssuer {
|
|
|
|
clientCertIssuer := issuermocks.NewMockClientCertIssuer(ctrl)
|
|
|
|
clientCertIssuer.EXPECT().
|
|
|
|
IssueClientCertPEM(gomock.Any(), gomock.Any(), gomock.Any()).
|
2020-07-27 13:08:39 +00:00
|
|
|
Return([]byte("test-cert"), []byte("test-key"), nil)
|
2021-03-13 00:09:16 +00:00
|
|
|
return clientCertIssuer
|
2020-07-27 13:08:39 +00:00
|
|
|
}
|