2022-01-08 01:19:13 +00:00
|
|
|
// Copyright 2021-2022 the Pinniped contributors. All Rights Reserved.
|
2021-04-10 01:49:43 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-05-12 21:00:39 +00:00
|
|
|
// Package ldapupstreamwatcher implements a controller which watches LDAPIdentityProviders.
|
|
|
|
package ldapupstreamwatcher
|
2021-04-10 01:49:43 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-12 20:53:21 +00:00
|
|
|
"context"
|
2021-04-10 01:49:43 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-04-12 20:53:21 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/equality"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-04-10 01:49:43 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
2021-04-12 20:53:21 +00:00
|
|
|
"k8s.io/klog/v2/klogr"
|
2021-04-10 01:49:43 +00:00
|
|
|
|
|
|
|
"go.pinniped.dev/generated/latest/apis/supervisor/idp/v1alpha1"
|
|
|
|
pinnipedclientset "go.pinniped.dev/generated/latest/client/supervisor/clientset/versioned"
|
|
|
|
idpinformers "go.pinniped.dev/generated/latest/client/supervisor/informers/externalversions/idp/v1alpha1"
|
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
2021-05-12 21:00:39 +00:00
|
|
|
"go.pinniped.dev/internal/controller/conditionsutil"
|
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig/upstreamwatchers"
|
2021-04-10 01:49:43 +00:00
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
|
|
|
"go.pinniped.dev/internal/upstreamldap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-07-19 21:18:29 +00:00
|
|
|
ldapControllerName = "ldap-upstream-observer"
|
2021-04-10 01:49:43 +00:00
|
|
|
)
|
|
|
|
|
2021-07-19 20:54:07 +00:00
|
|
|
type ldapUpstreamGenericLDAPImpl struct {
|
|
|
|
ldapIdentityProvider v1alpha1.LDAPIdentityProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPImpl) Spec() upstreamwatchers.UpstreamGenericLDAPSpec {
|
|
|
|
return &ldapUpstreamGenericLDAPSpec{g.ldapIdentityProvider}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPImpl) Namespace() string {
|
|
|
|
return g.ldapIdentityProvider.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPImpl) Name() string {
|
|
|
|
return g.ldapIdentityProvider.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPImpl) Generation() int64 {
|
|
|
|
return g.ldapIdentityProvider.Generation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPImpl) Status() upstreamwatchers.UpstreamGenericLDAPStatus {
|
|
|
|
return &ldapUpstreamGenericLDAPStatus{g.ldapIdentityProvider}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ldapUpstreamGenericLDAPSpec struct {
|
|
|
|
ldapIdentityProvider v1alpha1.LDAPIdentityProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ldapUpstreamGenericLDAPSpec) Host() string {
|
|
|
|
return s.ldapIdentityProvider.Spec.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ldapUpstreamGenericLDAPSpec) TLSSpec() *v1alpha1.TLSSpec {
|
|
|
|
return s.ldapIdentityProvider.Spec.TLS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ldapUpstreamGenericLDAPSpec) BindSecretName() string {
|
|
|
|
return s.ldapIdentityProvider.Spec.Bind.SecretName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ldapUpstreamGenericLDAPSpec) UserSearch() upstreamwatchers.UpstreamGenericLDAPUserSearch {
|
|
|
|
return &ldapUpstreamGenericLDAPUserSearch{s.ldapIdentityProvider.Spec.UserSearch}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ldapUpstreamGenericLDAPSpec) GroupSearch() upstreamwatchers.UpstreamGenericLDAPGroupSearch {
|
|
|
|
return &ldapUpstreamGenericLDAPGroupSearch{s.ldapIdentityProvider.Spec.GroupSearch}
|
|
|
|
}
|
|
|
|
|
2021-07-21 20:24:54 +00:00
|
|
|
func (s *ldapUpstreamGenericLDAPSpec) DetectAndSetSearchBase(_ context.Context, config *upstreamldap.ProviderConfig) *v1alpha1.Condition {
|
|
|
|
config.GroupSearch.Base = s.ldapIdentityProvider.Spec.GroupSearch.Base
|
|
|
|
config.UserSearch.Base = s.ldapIdentityProvider.Spec.UserSearch.Base
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:54:07 +00:00
|
|
|
type ldapUpstreamGenericLDAPUserSearch struct {
|
|
|
|
userSearch v1alpha1.LDAPIdentityProviderUserSearch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ldapUpstreamGenericLDAPUserSearch) Base() string {
|
|
|
|
return u.userSearch.Base
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ldapUpstreamGenericLDAPUserSearch) Filter() string {
|
|
|
|
return u.userSearch.Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ldapUpstreamGenericLDAPUserSearch) UsernameAttribute() string {
|
|
|
|
return u.userSearch.Attributes.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ldapUpstreamGenericLDAPUserSearch) UIDAttribute() string {
|
|
|
|
return u.userSearch.Attributes.UID
|
|
|
|
}
|
|
|
|
|
|
|
|
type ldapUpstreamGenericLDAPGroupSearch struct {
|
|
|
|
groupSearch v1alpha1.LDAPIdentityProviderGroupSearch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPGroupSearch) Base() string {
|
|
|
|
return g.groupSearch.Base
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPGroupSearch) Filter() string {
|
|
|
|
return g.groupSearch.Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *ldapUpstreamGenericLDAPGroupSearch) GroupNameAttribute() string {
|
|
|
|
return g.groupSearch.Attributes.GroupName
|
|
|
|
}
|
|
|
|
|
|
|
|
type ldapUpstreamGenericLDAPStatus struct {
|
|
|
|
ldapIdentityProvider v1alpha1.LDAPIdentityProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ldapUpstreamGenericLDAPStatus) Conditions() []v1alpha1.Condition {
|
|
|
|
return s.ldapIdentityProvider.Status.Conditions
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:49:43 +00:00
|
|
|
// UpstreamLDAPIdentityProviderICache is a thread safe cache that holds a list of validated upstream LDAP IDP configurations.
|
|
|
|
type UpstreamLDAPIdentityProviderICache interface {
|
|
|
|
SetLDAPIdentityProviders([]provider.UpstreamLDAPIdentityProviderI)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ldapWatcherController struct {
|
|
|
|
cache UpstreamLDAPIdentityProviderICache
|
2022-01-14 18:06:00 +00:00
|
|
|
validatedSettingsCache upstreamwatchers.ValidatedSettingsCacheI
|
2021-04-12 18:23:08 +00:00
|
|
|
ldapDialer upstreamldap.LDAPDialer
|
2021-04-10 01:49:43 +00:00
|
|
|
client pinnipedclientset.Interface
|
|
|
|
ldapIdentityProviderInformer idpinformers.LDAPIdentityProviderInformer
|
|
|
|
secretInformer corev1informers.SecretInformer
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:00:39 +00:00
|
|
|
// New instantiates a new controllerlib.Controller which will populate the provided UpstreamLDAPIdentityProviderICache.
|
|
|
|
func New(
|
2021-04-27 19:43:09 +00:00
|
|
|
idpCache UpstreamLDAPIdentityProviderICache,
|
|
|
|
client pinnipedclientset.Interface,
|
|
|
|
ldapIdentityProviderInformer idpinformers.LDAPIdentityProviderInformer,
|
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
2021-05-13 22:22:36 +00:00
|
|
|
return newInternal(
|
|
|
|
idpCache,
|
2022-01-08 01:19:13 +00:00
|
|
|
// start with an empty cache
|
|
|
|
upstreamwatchers.NewValidatedSettingsCache(),
|
2021-05-13 22:22:36 +00:00
|
|
|
// nil means to use a real production dialer when creating objects to add to the cache
|
|
|
|
nil,
|
|
|
|
client,
|
|
|
|
ldapIdentityProviderInformer,
|
|
|
|
secretInformer,
|
|
|
|
withInformer,
|
|
|
|
)
|
2021-04-27 19:43:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 22:22:36 +00:00
|
|
|
// For test dependency injection purposes.
|
2021-04-27 19:43:09 +00:00
|
|
|
func newInternal(
|
2021-04-10 01:49:43 +00:00
|
|
|
idpCache UpstreamLDAPIdentityProviderICache,
|
2022-01-14 18:06:00 +00:00
|
|
|
validatedSettingsCache upstreamwatchers.ValidatedSettingsCacheI,
|
2021-04-12 18:23:08 +00:00
|
|
|
ldapDialer upstreamldap.LDAPDialer,
|
2021-04-10 01:49:43 +00:00
|
|
|
client pinnipedclientset.Interface,
|
|
|
|
ldapIdentityProviderInformer idpinformers.LDAPIdentityProviderInformer,
|
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
|
|
|
c := ldapWatcherController{
|
|
|
|
cache: idpCache,
|
2022-01-14 18:06:00 +00:00
|
|
|
validatedSettingsCache: validatedSettingsCache,
|
2021-04-12 18:23:08 +00:00
|
|
|
ldapDialer: ldapDialer,
|
2021-04-10 01:49:43 +00:00
|
|
|
client: client,
|
|
|
|
ldapIdentityProviderInformer: ldapIdentityProviderInformer,
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
}
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{Name: ldapControllerName, Syncer: &c},
|
|
|
|
withInformer(
|
|
|
|
ldapIdentityProviderInformer,
|
|
|
|
pinnipedcontroller.MatchAnythingFilter(pinnipedcontroller.SingletonQueue()),
|
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
2021-07-19 21:18:29 +00:00
|
|
|
pinnipedcontroller.MatchAnySecretOfTypeFilter(upstreamwatchers.LDAPBindAccountSecretType, pinnipedcontroller.SingletonQueue()),
|
2021-04-10 01:49:43 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync implements controllerlib.Syncer.
|
|
|
|
func (c *ldapWatcherController) Sync(ctx controllerlib.Context) error {
|
|
|
|
actualUpstreams, err := c.ldapIdentityProviderInformer.Lister().List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list LDAPIdentityProviders: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requeue := false
|
|
|
|
validatedUpstreams := make([]provider.UpstreamLDAPIdentityProviderI, 0, len(actualUpstreams))
|
|
|
|
for _, upstream := range actualUpstreams {
|
2021-05-13 22:22:36 +00:00
|
|
|
valid, requestedRequeue := c.validateUpstream(ctx.Context, upstream)
|
|
|
|
if valid != nil {
|
2021-04-10 01:49:43 +00:00
|
|
|
validatedUpstreams = append(validatedUpstreams, valid)
|
|
|
|
}
|
2021-05-13 22:22:36 +00:00
|
|
|
if requestedRequeue {
|
|
|
|
requeue = true
|
|
|
|
}
|
2021-04-10 01:49:43 +00:00
|
|
|
}
|
2021-05-13 22:22:36 +00:00
|
|
|
|
2021-04-10 01:49:43 +00:00
|
|
|
c.cache.SetLDAPIdentityProviders(validatedUpstreams)
|
2021-05-13 22:22:36 +00:00
|
|
|
|
2021-04-10 01:49:43 +00:00
|
|
|
if requeue {
|
|
|
|
return controllerlib.ErrSyntheticRequeue
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:22:36 +00:00
|
|
|
func (c *ldapWatcherController) validateUpstream(ctx context.Context, upstream *v1alpha1.LDAPIdentityProvider) (p provider.UpstreamLDAPIdentityProviderI, requeue bool) {
|
2021-04-12 18:23:08 +00:00
|
|
|
spec := upstream.Spec
|
2021-04-14 00:16:57 +00:00
|
|
|
|
2021-04-15 17:25:35 +00:00
|
|
|
config := &upstreamldap.ProviderConfig{
|
2021-10-08 22:48:21 +00:00
|
|
|
Name: upstream.Name,
|
|
|
|
ResourceUID: upstream.UID,
|
|
|
|
Host: spec.Host,
|
2021-04-15 17:25:35 +00:00
|
|
|
UserSearch: upstreamldap.UserSearchConfig{
|
2021-04-12 18:23:08 +00:00
|
|
|
Base: spec.UserSearch.Base,
|
|
|
|
Filter: spec.UserSearch.Filter,
|
|
|
|
UsernameAttribute: spec.UserSearch.Attributes.Username,
|
2021-04-27 19:43:09 +00:00
|
|
|
UIDAttribute: spec.UserSearch.Attributes.UID,
|
2021-04-12 18:23:08 +00:00
|
|
|
},
|
2021-05-17 18:10:26 +00:00
|
|
|
GroupSearch: upstreamldap.GroupSearchConfig{
|
|
|
|
Base: spec.GroupSearch.Base,
|
|
|
|
Filter: spec.GroupSearch.Filter,
|
|
|
|
GroupNameAttribute: spec.GroupSearch.Attributes.GroupName,
|
2022-02-01 16:31:29 +00:00
|
|
|
SkipGroupRefresh: spec.GroupSearch.SkipGroupRefresh,
|
2021-05-17 18:10:26 +00:00
|
|
|
},
|
2021-04-12 18:23:08 +00:00
|
|
|
Dialer: c.ldapDialer,
|
|
|
|
}
|
2021-04-15 21:44:43 +00:00
|
|
|
|
2022-01-14 18:06:00 +00:00
|
|
|
conditions := upstreamwatchers.ValidateGenericLDAP(ctx, &ldapUpstreamGenericLDAPImpl{*upstream}, c.secretInformer, c.validatedSettingsCache, config)
|
2021-04-14 00:16:57 +00:00
|
|
|
|
2021-07-19 20:54:07 +00:00
|
|
|
c.updateStatus(ctx, upstream, conditions.Conditions())
|
2021-04-12 20:53:21 +00:00
|
|
|
|
2021-07-19 20:54:07 +00:00
|
|
|
return upstreamwatchers.EvaluateConditions(conditions, config)
|
2021-04-12 20:53:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 22:22:36 +00:00
|
|
|
func (c *ldapWatcherController) updateStatus(ctx context.Context, upstream *v1alpha1.LDAPIdentityProvider, conditions []*v1alpha1.Condition) {
|
2021-04-12 20:53:21 +00:00
|
|
|
log := klogr.New().WithValues("namespace", upstream.Namespace, "name", upstream.Name)
|
|
|
|
updated := upstream.DeepCopy()
|
|
|
|
|
2021-05-12 21:00:39 +00:00
|
|
|
hadErrorCondition := conditionsutil.Merge(conditions, upstream.Generation, &updated.Status.Conditions, log)
|
2021-04-12 20:53:21 +00:00
|
|
|
|
|
|
|
updated.Status.Phase = v1alpha1.LDAPPhaseReady
|
|
|
|
if hadErrorCondition {
|
|
|
|
updated.Status.Phase = v1alpha1.LDAPPhaseError
|
|
|
|
}
|
|
|
|
|
|
|
|
if equality.Semantic.DeepEqual(upstream, updated) {
|
2021-05-13 22:22:36 +00:00
|
|
|
return // nothing to update
|
2021-04-12 20:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.client.
|
|
|
|
IDPV1alpha1().
|
|
|
|
LDAPIdentityProviders(upstream.Namespace).
|
|
|
|
UpdateStatus(ctx, updated, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "failed to update status")
|
2021-04-12 18:23:08 +00:00
|
|
|
}
|
2021-04-10 01:49:43 +00:00
|
|
|
}
|