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
|
|
|
|
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"
|
2020-11-10 15:22:16 +00:00
|
|
|
"go.pinniped.dev/internal/multierror"
|
2020-10-08 02:18:34 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
2020-11-10 15:22:16 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-10-08 02:18:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2020-12-17 19:34:49 +00:00
|
|
|
SetProviders(federationDomains ...*provider.FederationDomainIssuer)
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 22:27:09 +00:00
|
|
|
type federationDomainWatcherController struct {
|
2020-12-17 19:34:49 +00:00
|
|
|
providerSetter ProvidersSetter
|
|
|
|
clock clock.Clock
|
|
|
|
client pinnipedclientset.Interface
|
|
|
|
federationDomainInformer configinformers.FederationDomainInformer
|
2020-10-08 02:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 22:27:09 +00:00
|
|
|
// NewFederationDomainWatcherController creates a controllerlib.Controller that watches
|
|
|
|
// FederationDomain objects and notifies a callback object of the collection of provider configs.
|
|
|
|
func NewFederationDomainWatcherController(
|
2020-10-08 17:27:45 +00:00
|
|
|
providerSetter ProvidersSetter,
|
|
|
|
clock clock.Clock,
|
|
|
|
client pinnipedclientset.Interface,
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomainInformer configinformers.FederationDomainInformer,
|
2020-10-08 02:18:34 +00:00
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
) controllerlib.Controller {
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
2020-12-16 22:27:09 +00:00
|
|
|
Name: "FederationDomainWatcherController",
|
|
|
|
Syncer: &federationDomainWatcherController{
|
2020-12-17 19:34:49 +00:00
|
|
|
providerSetter: providerSetter,
|
|
|
|
clock: clock,
|
|
|
|
client: client,
|
|
|
|
federationDomainInformer: federationDomainInformer,
|
2020-10-08 02:18:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomainInformer,
|
2020-10-02 17:22:18 +00:00
|
|
|
pinnipedcontroller.MatchAnythingFilter(pinnipedcontroller.SingletonQueue()),
|
2020-10-08 02:18:34 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync implements controllerlib.Syncer.
|
2020-12-16 22:27:09 +00:00
|
|
|
func (c *federationDomainWatcherController) Sync(ctx controllerlib.Context) error {
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomains, err := c.federationDomainInformer.Lister().List(labels.Everything())
|
2020-10-08 02:18:34 +00:00
|
|
|
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-12-16 22:27:09 +00:00
|
|
|
// multiple FederationDomains 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-12-17 19:34:49 +00:00
|
|
|
for _, federationDomain := range federationDomains {
|
|
|
|
issuerURL, err := url.Parse(federationDomain.Spec.Issuer)
|
2020-10-23 23:25:44 +00:00
|
|
|
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-12-17 19:34:49 +00:00
|
|
|
if federationDomain.Spec.TLS != nil {
|
|
|
|
setOfSecretNames[federationDomain.Spec.TLS.SecretName] = true
|
2020-11-02 22:55:29 +00:00
|
|
|
}
|
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-12-17 19:34:49 +00:00
|
|
|
federationDomainIssuers := make([]*provider.FederationDomainIssuer, 0)
|
|
|
|
for _, federationDomain := range federationDomains {
|
|
|
|
issuerURL, urlParseErr := url.Parse(federationDomain.Spec.Issuer)
|
2020-10-23 23:25:44 +00:00
|
|
|
|
|
|
|
// 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,
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomain.Namespace,
|
|
|
|
federationDomain.Name,
|
2020-12-16 22:27:09 +00:00
|
|
|
configv1alpha1.DuplicateFederationDomainStatusCondition,
|
2020-12-17 19:34:49 +00:00
|
|
|
"Duplicate issuer: "+federationDomain.Spec.Issuer,
|
2020-10-23 23:25:44 +00:00
|
|
|
); 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,
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomain.Namespace,
|
|
|
|
federationDomain.Name,
|
2020-12-16 22:27:09 +00:00
|
|
|
configv1alpha1.SameIssuerHostMustUseSameSecretFederationDomainStatusCondition,
|
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-12-17 19:34:49 +00:00
|
|
|
federationDomainIssuer, err := provider.NewFederationDomainIssuer(federationDomain.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,
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomain.Namespace,
|
|
|
|
federationDomain.Name,
|
2020-12-16 22:27:09 +00:00
|
|
|
configv1alpha1.InvalidFederationDomainStatusCondition,
|
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,
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomain.Namespace,
|
|
|
|
federationDomain.Name,
|
2020-12-16 22:27:09 +00:00
|
|
|
configv1alpha1.SuccessFederationDomainStatusCondition,
|
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-12-17 19:34:49 +00:00
|
|
|
|
|
|
|
federationDomainIssuers = append(federationDomainIssuers, federationDomainIssuer)
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
c.providerSetter.SetProviders(federationDomainIssuers...)
|
2020-10-08 17:27:45 +00:00
|
|
|
|
2020-10-08 18:28:21 +00:00
|
|
|
return errs.ErrOrNil()
|
2020-10-08 17:27:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 22:27:09 +00:00
|
|
|
func (c *federationDomainWatcherController) updateStatus(
|
2020-10-08 17:27:45 +00:00
|
|
|
ctx context.Context,
|
|
|
|
namespace, name string,
|
2020-12-16 22:27:09 +00:00
|
|
|
status configv1alpha1.FederationDomainStatusCondition,
|
2020-10-08 17:27:45 +00:00
|
|
|
message string,
|
|
|
|
) error {
|
|
|
|
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomain, err := c.client.ConfigV1alpha1().FederationDomains(namespace).Get(ctx, name, metav1.GetOptions{})
|
2020-10-08 17:27:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
if federationDomain.Status.Status == status && federationDomain.Status.Message == message {
|
2020-10-08 17:27:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug(
|
2020-10-08 17:27:45 +00:00
|
|
|
"attempting status update",
|
2020-12-17 19:34:49 +00:00
|
|
|
"federationdomain",
|
2020-10-08 17:27:45 +00:00
|
|
|
klog.KRef(namespace, name),
|
|
|
|
"status",
|
|
|
|
status,
|
|
|
|
"message",
|
|
|
|
message,
|
2020-10-08 02:18:34 +00:00
|
|
|
)
|
2020-12-17 19:34:49 +00:00
|
|
|
federationDomain.Status.Status = status
|
|
|
|
federationDomain.Status.Message = message
|
|
|
|
federationDomain.Status.LastUpdateTime = timePtr(metav1.NewTime(c.clock.Now()))
|
|
|
|
_, err = c.client.ConfigV1alpha1().FederationDomains(namespace).Update(ctx, federationDomain, 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 }
|