2022-01-14 18:49:22 +00:00
|
|
|
// Copyright 2020-2022 the Pinniped contributors. All Rights Reserved.
|
2020-12-08 01:39:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package cachecleaner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-08 16:08:53 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-12-08 01:39:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
2021-02-16 19:00:08 +00:00
|
|
|
authv1alpha "go.pinniped.dev/generated/latest/apis/concierge/authentication/v1alpha1"
|
|
|
|
pinnipedfake "go.pinniped.dev/generated/latest/client/concierge/clientset/versioned/fake"
|
|
|
|
pinnipedinformers "go.pinniped.dev/generated/latest/client/concierge/informers/externalversions"
|
2020-12-08 01:39:51 +00:00
|
|
|
"go.pinniped.dev/internal/controller/authenticator/authncache"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2020-12-08 16:08:53 +00:00
|
|
|
"go.pinniped.dev/internal/mocks/mocktokenauthenticatorcloser"
|
2020-12-08 01:39:51 +00:00
|
|
|
"go.pinniped.dev/internal/testutil/testlogger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestController(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testWebhookKey1 := authncache.Key{
|
2021-02-09 23:16:22 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
|
|
|
Kind: "WebhookAuthenticator",
|
|
|
|
Name: "test-webhook-name-one",
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
|
|
|
testWebhookKey2 := authncache.Key{
|
2021-02-09 23:16:22 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
|
|
|
Kind: "WebhookAuthenticator",
|
|
|
|
Name: "test-webhook-name-two",
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
|
|
|
testJWTAuthenticatorKey1 := authncache.Key{
|
2021-02-09 23:16:22 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
|
|
|
Kind: "JWTAuthenticator",
|
|
|
|
Name: "test-jwt-authenticator-name-one",
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
|
|
|
testJWTAuthenticatorKey2 := authncache.Key{
|
2021-02-09 23:16:22 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
|
|
|
Kind: "JWTAuthenticator",
|
|
|
|
Name: "test-jwt-authenticator-name-two",
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
|
|
|
testKeyUnknownType := authncache.Key{
|
2021-02-09 23:16:22 +00:00
|
|
|
APIGroup: "authentication.concierge.pinniped.dev",
|
|
|
|
Kind: "SomeOtherAuthenticator",
|
|
|
|
Name: "test-name-one",
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
objects []runtime.Object
|
2020-12-08 16:08:53 +00:00
|
|
|
initialCache func(t *testing.T, cache *authncache.Cache)
|
2020-12-08 01:39:51 +00:00
|
|
|
wantErr string
|
|
|
|
wantLogs []string
|
|
|
|
wantCacheKeys []authncache.Key
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no change",
|
2020-12-08 16:08:53 +00:00
|
|
|
initialCache: func(t *testing.T, cache *authncache.Cache) {
|
|
|
|
cache.Store(testWebhookKey1, nil)
|
|
|
|
cache.Store(testJWTAuthenticatorKey1, nil)
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
objects: []runtime.Object{
|
|
|
|
&authv1alpha.WebhookAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testWebhookKey1.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&authv1alpha.JWTAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testJWTAuthenticatorKey1.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantCacheKeys: []authncache.Key{testWebhookKey1, testJWTAuthenticatorKey1},
|
|
|
|
},
|
|
|
|
{
|
2020-12-08 16:08:53 +00:00
|
|
|
name: "authenticators not yet added",
|
2020-12-08 01:39:51 +00:00
|
|
|
objects: []runtime.Object{
|
|
|
|
&authv1alpha.WebhookAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testWebhookKey1.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&authv1alpha.WebhookAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testWebhookKey2.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&authv1alpha.JWTAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testJWTAuthenticatorKey1.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&authv1alpha.JWTAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testJWTAuthenticatorKey2.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantCacheKeys: []authncache.Key{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful cleanup",
|
2020-12-08 16:08:53 +00:00
|
|
|
initialCache: func(t *testing.T, cache *authncache.Cache) {
|
|
|
|
cache.Store(testWebhookKey1, nil)
|
|
|
|
cache.Store(testWebhookKey2, nil)
|
|
|
|
cache.Store(testJWTAuthenticatorKey1, newClosableCacheValue(t, 0))
|
|
|
|
cache.Store(testJWTAuthenticatorKey2, newClosableCacheValue(t, 1))
|
|
|
|
cache.Store(testKeyUnknownType, nil)
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
objects: []runtime.Object{
|
|
|
|
&authv1alpha.WebhookAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testWebhookKey1.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&authv1alpha.JWTAuthenticator{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: testJWTAuthenticatorKey1.Name,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantLogs: []string{
|
2021-02-09 23:16:22 +00:00
|
|
|
`cachecleaner-controller "level"=0 "msg"="deleting authenticator from cache" "authenticator"={"name":"test-jwt-authenticator-name-two"} "kind"="JWTAuthenticator"`,
|
|
|
|
`cachecleaner-controller "level"=0 "msg"="deleting authenticator from cache" "authenticator"={"name":"test-webhook-name-two"} "kind"="WebhookAuthenticator"`,
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
wantCacheKeys: []authncache.Key{testWebhookKey1, testJWTAuthenticatorKey1, testKeyUnknownType},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// When we have t.Parallel() here, this test blocks pretty consistently...y tho?
|
|
|
|
|
|
|
|
fakeClient := pinnipedfake.NewSimpleClientset(tt.objects...)
|
|
|
|
informers := pinnipedinformers.NewSharedInformerFactory(fakeClient, 0)
|
2021-02-09 20:51:35 +00:00
|
|
|
cache := authncache.New()
|
2020-12-08 16:08:53 +00:00
|
|
|
if tt.initialCache != nil {
|
|
|
|
tt.initialCache(t, cache)
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|
2022-08-24 21:45:55 +00:00
|
|
|
testLog := testlogger.NewLegacy(t) //nolint:staticcheck // old test with lots of log statements
|
2020-12-08 01:39:51 +00:00
|
|
|
|
|
|
|
webhooks := informers.Authentication().V1alpha1().WebhookAuthenticators()
|
|
|
|
jwtAuthenticators := informers.Authentication().V1alpha1().JWTAuthenticators()
|
2021-12-10 22:22:36 +00:00
|
|
|
controller := New(cache, webhooks, jwtAuthenticators, testLog.Logger)
|
2020-12-08 01:39:51 +00:00
|
|
|
|
2021-03-05 01:25:43 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-12-08 01:39:51 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
informers.Start(ctx.Done())
|
|
|
|
informers.WaitForCacheSync(ctx.Done())
|
|
|
|
controllerlib.TestRunSynchronously(t, controller)
|
|
|
|
|
|
|
|
syncCtx := controllerlib.Context{
|
|
|
|
Context: ctx,
|
|
|
|
Key: controllerlib.Key{
|
2021-02-09 23:16:22 +00:00
|
|
|
Name: "test-webhook-name-one",
|
2020-12-08 01:39:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := controllerlib.TestSync(t, controller, syncCtx); tt.wantErr != "" {
|
|
|
|
require.EqualError(t, err, tt.wantErr)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.ElementsMatch(t, tt.wantLogs, testLog.Lines())
|
|
|
|
require.ElementsMatch(t, tt.wantCacheKeys, cache.Keys())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:08:53 +00:00
|
|
|
func newClosableCacheValue(t *testing.T, wantCloses int) authncache.Value {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
t.Cleanup(ctrl.Finish)
|
|
|
|
tac := mocktokenauthenticatorcloser.NewMockTokenAuthenticatorCloser(ctrl)
|
|
|
|
tac.EXPECT().Close().Times(wantCloses)
|
|
|
|
return tac
|
2020-12-08 01:39:51 +00:00
|
|
|
}
|