2020-10-08 02:18:34 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package supervisorconfig
|
|
|
|
|
|
|
|
import (
|
2020-10-08 17:27:45 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-10-23 23:25:44 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-10-08 18:28:21 +00:00
|
|
|
|
|
|
|
"go.pinniped.dev/internal/multierror"
|
2020-10-08 02:18:34 +00:00
|
|
|
|
2020-10-08 17:27:45 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-10-08 02:18:34 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2020-10-08 17:27:45 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
|
|
|
"k8s.io/client-go/util/retry"
|
2020-10-08 02:18:34 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-10-30 20:09:14 +00:00
|
|
|
configv1alpha1 "go.pinniped.dev/generated/1.19/apis/supervisor/config/v1alpha1"
|
|
|
|
pinnipedclientset "go.pinniped.dev/generated/1.19/client/supervisor/clientset/versioned"
|
|
|
|
configinformers "go.pinniped.dev/generated/1.19/client/supervisor/informers/externalversions/config/v1alpha1"
|
2020-10-08 02:18:34 +00:00
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProvidersSetter can be notified of all known valid providers with its SetIssuer function.
|
|
|
|
// If there are no longer any valid issuers, then it can be called with no arguments.
|
|
|
|
// Implementations of this type should be thread-safe to support calls from multiple goroutines.
|
|
|
|
type ProvidersSetter interface {
|
|
|
|
SetProviders(oidcProviders ...*provider.OIDCProvider)
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:24:55 +00:00
|
|
|
type oidcProviderWatcherController struct {
|
2020-10-08 02:18:34 +00:00
|
|
|
providerSetter ProvidersSetter
|
2020-10-08 17:27:45 +00:00
|
|
|
clock clock.Clock
|
|
|
|
client pinnipedclientset.Interface
|
2020-11-02 22:24:55 +00:00
|
|
|
opcInformer configinformers.OIDCProviderInformer
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 22:24:55 +00:00
|
|
|
// NewOIDCProviderWatcherController creates a controllerlib.Controller that watches
|
|
|
|
// OIDCProvider objects and notifies a callback object of the collection of provider configs.
|
|
|
|
func NewOIDCProviderWatcherController(
|
2020-10-08 17:27:45 +00:00
|
|
|
providerSetter ProvidersSetter,
|
|
|
|
clock clock.Clock,
|
|
|
|
client pinnipedclientset.Interface,
|
2020-11-02 22:24:55 +00:00
|
|
|
opcInformer configinformers.OIDCProviderInformer,
|
2020-10-08 02:18:34 +00:00
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
2020-11-02 22:24:55 +00:00
|
|
|
Name: "OIDCProviderWatcherController",
|
|
|
|
Syncer: &oidcProviderWatcherController{
|
2020-10-08 17:27:45 +00:00
|
|
|
providerSetter: providerSetter,
|
|
|
|
clock: clock,
|
|
|
|
client: client,
|
2020-10-08 02:18:34 +00:00
|
|
|
opcInformer: opcInformer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
opcInformer,
|
2020-10-17 00:51:40 +00:00
|
|
|
pinnipedcontroller.MatchAnythingFilter(),
|
2020-10-08 02:18:34 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync implements controllerlib.Syncer.
|
2020-11-02 22:24:55 +00:00
|
|
|
func (c *oidcProviderWatcherController) Sync(ctx controllerlib.Context) error {
|
2020-10-08 02:18:34 +00:00
|
|
|
all, err := c.opcInformer.Lister().List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-23 23:25:44 +00:00
|
|
|
// Make a map of issuer strings -> count of how many times we saw that issuer string.
|
|
|
|
// This will help us complain when there are duplicate issuer strings.
|
|
|
|
// Also make a helper function for forming keys into this map.
|
2020-10-08 17:27:45 +00:00
|
|
|
issuerCounts := make(map[string]int)
|
2020-10-23 23:25:44 +00:00
|
|
|
issuerURLToIssuerKey := func(issuerURL *url.URL) string {
|
|
|
|
return fmt.Sprintf("%s://%s%s", issuerURL.Scheme, strings.ToLower(issuerURL.Host), issuerURL.Path)
|
|
|
|
}
|
|
|
|
|
2020-10-27 00:03:26 +00:00
|
|
|
// Make a map of issuer hostnames -> set of unique secret names. This will help us complain when
|
2020-11-02 22:24:55 +00:00
|
|
|
// multiple OIDCProviders have the same issuer hostname (excluding port) but specify
|
2020-10-23 23:25:44 +00:00
|
|
|
// different TLS serving Secrets. Doesn't make sense to have the one address use more than one
|
2020-10-27 00:03:26 +00:00
|
|
|
// TLS cert. Ignore ports because SNI information on the incoming requests is not going to include
|
|
|
|
// port numbers. Also make a helper function for forming keys into this map.
|
2020-10-23 23:25:44 +00:00
|
|
|
uniqueSecretNamesPerIssuerAddress := make(map[string]map[string]bool)
|
2020-10-27 00:03:26 +00:00
|
|
|
issuerURLToHostnameKey := lowercaseHostWithoutPort
|
2020-10-23 23:25:44 +00:00
|
|
|
|
2020-10-08 17:27:45 +00:00
|
|
|
for _, opc := range all {
|
2020-10-23 23:25:44 +00:00
|
|
|
issuerURL, err := url.Parse(opc.Spec.Issuer)
|
|
|
|
if err != nil {
|
|
|
|
continue // Skip url parse errors because they will be validated again below.
|
|
|
|
}
|
|
|
|
|
|
|
|
issuerCounts[issuerURLToIssuerKey(issuerURL)]++
|
|
|
|
|
2020-10-27 00:03:26 +00:00
|
|
|
setOfSecretNames := uniqueSecretNamesPerIssuerAddress[issuerURLToHostnameKey(issuerURL)]
|
2020-10-23 23:25:44 +00:00
|
|
|
if setOfSecretNames == nil {
|
|
|
|
setOfSecretNames = make(map[string]bool)
|
2020-10-27 00:03:26 +00:00
|
|
|
uniqueSecretNamesPerIssuerAddress[issuerURLToHostnameKey(issuerURL)] = setOfSecretNames
|
2020-10-23 23:25:44 +00:00
|
|
|
}
|
2020-11-02 22:55:29 +00:00
|
|
|
if opc.Spec.TLS != nil {
|
|
|
|
setOfSecretNames[opc.Spec.TLS.SecretName] = true
|
|
|
|
}
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
errs := multierror.New()
|
2020-10-08 17:27:45 +00:00
|
|
|
|
2020-10-08 02:18:34 +00:00
|
|
|
oidcProviders := make([]*provider.OIDCProvider, 0)
|
|
|
|
for _, opc := range all {
|
2020-10-23 23:25:44 +00:00
|
|
|
issuerURL, urlParseErr := url.Parse(opc.Spec.Issuer)
|
|
|
|
|
|
|
|
// Skip url parse errors because they will be validated below.
|
|
|
|
if urlParseErr == nil {
|
|
|
|
if issuerCount := issuerCounts[issuerURLToIssuerKey(issuerURL)]; issuerCount > 1 {
|
|
|
|
if err := c.updateStatus(
|
|
|
|
ctx.Context,
|
|
|
|
opc.Namespace,
|
|
|
|
opc.Name,
|
2020-11-02 22:24:55 +00:00
|
|
|
configv1alpha1.DuplicateOIDCProviderStatusCondition,
|
2020-10-23 23:25:44 +00:00
|
|
|
"Duplicate issuer: "+opc.Spec.Issuer,
|
|
|
|
); err != nil {
|
|
|
|
errs.Add(fmt.Errorf("could not update status: %w", err))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip url parse errors because they will be validated below.
|
2020-10-27 00:03:26 +00:00
|
|
|
if urlParseErr == nil && len(uniqueSecretNamesPerIssuerAddress[issuerURLToHostnameKey(issuerURL)]) > 1 {
|
2020-10-08 17:27:45 +00:00
|
|
|
if err := c.updateStatus(
|
|
|
|
ctx.Context,
|
|
|
|
opc.Namespace,
|
|
|
|
opc.Name,
|
2020-11-02 22:24:55 +00:00
|
|
|
configv1alpha1.SameIssuerHostMustUseSameSecretOIDCProviderStatusCondition,
|
2020-10-27 00:03:26 +00:00
|
|
|
"Issuers with the same DNS hostname (address not including port) must use the same secretName: "+issuerURLToHostnameKey(issuerURL),
|
2020-10-08 17:27:45 +00:00
|
|
|
); err != nil {
|
2020-10-08 18:28:21 +00:00
|
|
|
errs.Add(fmt.Errorf("could not update status: %w", err))
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-23 23:25:44 +00:00
|
|
|
oidcProvider, err := provider.NewOIDCProvider(opc.Spec.Issuer) // This validates the Issuer URL.
|
2020-10-08 02:18:34 +00:00
|
|
|
if err != nil {
|
2020-10-08 17:27:45 +00:00
|
|
|
if err := c.updateStatus(
|
|
|
|
ctx.Context,
|
|
|
|
opc.Namespace,
|
|
|
|
opc.Name,
|
2020-11-02 22:24:55 +00:00
|
|
|
configv1alpha1.InvalidOIDCProviderStatusCondition,
|
2020-10-08 18:28:21 +00:00
|
|
|
"Invalid: "+err.Error(),
|
2020-10-08 17:27:45 +00:00
|
|
|
); err != nil {
|
2020-10-08 18:28:21 +00:00
|
|
|
errs.Add(fmt.Errorf("could not update status: %w", err))
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
2020-10-08 02:18:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-10-08 17:27:45 +00:00
|
|
|
|
|
|
|
if err := c.updateStatus(
|
|
|
|
ctx.Context,
|
|
|
|
opc.Namespace,
|
|
|
|
opc.Name,
|
2020-11-02 22:24:55 +00:00
|
|
|
configv1alpha1.SuccessOIDCProviderStatusCondition,
|
2020-10-08 17:27:45 +00:00
|
|
|
"Provider successfully created",
|
|
|
|
); err != nil {
|
2020-10-08 18:28:21 +00:00
|
|
|
errs.Add(fmt.Errorf("could not update status: %w", err))
|
2020-10-09 14:39:17 +00:00
|
|
|
continue
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
2020-10-09 14:39:17 +00:00
|
|
|
oidcProviders = append(oidcProviders, oidcProvider)
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.providerSetter.SetProviders(oidcProviders...)
|
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
return errs.ErrOrNil()
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 22:24:55 +00:00
|
|
|
func (c *oidcProviderWatcherController) updateStatus(
|
2020-10-08 17:27:45 +00:00
|
|
|
ctx context.Context,
|
|
|
|
namespace, name string,
|
2020-11-02 22:24:55 +00:00
|
|
|
status configv1alpha1.OIDCProviderStatusCondition,
|
2020-10-08 17:27:45 +00:00
|
|
|
message string,
|
|
|
|
) error {
|
|
|
|
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
2020-11-02 22:24:55 +00:00
|
|
|
opc, err := c.client.ConfigV1alpha1().OIDCProviders(namespace).Get(ctx, name, metav1.GetOptions{})
|
2020-10-08 17:27:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opc.Status.Status == status && opc.Status.Message == message {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-08 02:18:34 +00:00
|
|
|
klog.InfoS(
|
2020-10-08 17:27:45 +00:00
|
|
|
"attempting status update",
|
|
|
|
"openidproviderconfig",
|
|
|
|
klog.KRef(namespace, name),
|
|
|
|
"status",
|
|
|
|
status,
|
|
|
|
"message",
|
|
|
|
message,
|
2020-10-08 02:18:34 +00:00
|
|
|
)
|
2020-10-08 17:27:45 +00:00
|
|
|
opc.Status.Status = status
|
|
|
|
opc.Status.Message = message
|
2020-10-09 15:54:50 +00:00
|
|
|
opc.Status.LastUpdateTime = timePtr(metav1.NewTime(c.clock.Now()))
|
2020-11-02 22:24:55 +00:00
|
|
|
_, err = c.client.ConfigV1alpha1().OIDCProviders(namespace).Update(ctx, opc, metav1.UpdateOptions{})
|
2020-10-08 17:27:45 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2020-10-09 15:54:50 +00:00
|
|
|
|
|
|
|
func timePtr(t metav1.Time) *metav1.Time { return &t }
|