2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-14 15:41:36 +00:00
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// Package webhookcachefiller implements a controller for filling an authncache.Cache with each added/updated WebhookAuthenticator.
|
2020-09-14 15:41:36 +00:00
|
|
|
package webhookcachefiller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
k8sauthv1beta1 "k8s.io/api/authentication/v1beta1"
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
"k8s.io/apimachinery/pkg/util/net"
|
|
|
|
"k8s.io/apiserver/pkg/authentication/authenticator"
|
|
|
|
"k8s.io/apiserver/plugin/pkg/authenticator/token/webhook"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-10-30 16:03:25 +00:00
|
|
|
auth1alpha1 "go.pinniped.dev/generated/1.19/apis/concierge/authentication/v1alpha1"
|
2020-10-30 20:09:14 +00:00
|
|
|
authinformers "go.pinniped.dev/generated/1.19/client/concierge/informers/externalversions/authentication/v1alpha1"
|
2020-09-18 19:56:24 +00:00
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
2020-10-30 19:02:21 +00:00
|
|
|
"go.pinniped.dev/internal/controller/authenticator/authncache"
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2020-09-14 15:41:36 +00:00
|
|
|
)
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
// New instantiates a new controllerlib.Controller which will populate the provided authncache.Cache.
|
|
|
|
func New(cache *authncache.Cache, webhooks authinformers.WebhookAuthenticatorInformer, log logr.Logger) controllerlib.Controller {
|
2020-09-14 15:41:36 +00:00
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
|
|
|
Name: "webhookcachefiller-controller",
|
|
|
|
Syncer: &controller{
|
2020-10-30 19:02:21 +00:00
|
|
|
cache: cache,
|
|
|
|
webhooks: webhooks,
|
|
|
|
log: log.WithName("webhookcachefiller-controller"),
|
2020-09-14 15:41:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
controllerlib.WithInformer(
|
2020-10-30 19:02:21 +00:00
|
|
|
webhooks,
|
2020-10-02 17:22:18 +00:00
|
|
|
pinnipedcontroller.MatchAnythingFilter(nil), // nil parent func is fine because each event is distinct
|
2020-09-14 15:41:36 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type controller struct {
|
2020-10-30 19:02:21 +00:00
|
|
|
cache *authncache.Cache
|
|
|
|
webhooks authinformers.WebhookAuthenticatorInformer
|
|
|
|
log logr.Logger
|
2020-09-14 15:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync implements controllerlib.Syncer.
|
|
|
|
func (c *controller) Sync(ctx controllerlib.Context) error {
|
2020-10-30 19:02:21 +00:00
|
|
|
obj, err := c.webhooks.Lister().WebhookAuthenticators(ctx.Key.Namespace).Get(ctx.Key.Name)
|
2020-09-14 15:41:36 +00:00
|
|
|
if err != nil && errors.IsNotFound(err) {
|
2020-10-30 16:39:26 +00:00
|
|
|
c.log.Info("Sync() found that the WebhookAuthenticator does not exist yet or was deleted")
|
2020-09-14 15:41:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-10-30 16:39:26 +00:00
|
|
|
return fmt.Errorf("failed to get WebhookAuthenticator %s/%s: %w", ctx.Key.Namespace, ctx.Key.Name, err)
|
2020-09-14 15:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
webhookAuthenticator, err := newWebhookAuthenticator(&obj.Spec, ioutil.TempFile, clientcmd.WriteToFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to build webhook config: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:21 +00:00
|
|
|
c.cache.Store(authncache.Key{
|
2020-10-30 16:03:25 +00:00
|
|
|
APIGroup: auth1alpha1.GroupName,
|
2020-10-30 16:39:26 +00:00
|
|
|
Kind: "WebhookAuthenticator",
|
2020-09-21 16:37:54 +00:00
|
|
|
Namespace: ctx.Key.Namespace,
|
|
|
|
Name: ctx.Key.Name,
|
|
|
|
}, webhookAuthenticator)
|
2020-10-30 19:02:21 +00:00
|
|
|
c.log.WithValues("webhook", klog.KObj(obj), "endpoint", obj.Spec.Endpoint).Info("added new webhook authenticator")
|
2020-09-14 15:41:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newWebhookAuthenticator creates a webhook from the provided API server url and caBundle
|
|
|
|
// used to validate TLS connections.
|
|
|
|
func newWebhookAuthenticator(
|
2020-10-30 16:39:26 +00:00
|
|
|
spec *auth1alpha1.WebhookAuthenticatorSpec,
|
2020-09-14 15:41:36 +00:00
|
|
|
tempfileFunc func(string, string) (*os.File, error),
|
|
|
|
marshalFunc func(clientcmdapi.Config, string) error,
|
|
|
|
) (*webhook.WebhookTokenAuthenticator, error) {
|
|
|
|
temp, err := tempfileFunc("", "pinniped-webhook-kubeconfig-*")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create temporary file: %w", err)
|
|
|
|
}
|
|
|
|
defer func() { _ = os.Remove(temp.Name()) }()
|
|
|
|
|
|
|
|
cluster := &clientcmdapi.Cluster{Server: spec.Endpoint}
|
|
|
|
cluster.CertificateAuthorityData, err = getCABundle(spec.TLS)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid TLS configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeconfig := clientcmdapi.NewConfig()
|
|
|
|
kubeconfig.Clusters["anonymous-cluster"] = cluster
|
|
|
|
kubeconfig.Contexts["anonymous"] = &clientcmdapi.Context{Cluster: "anonymous-cluster"}
|
|
|
|
kubeconfig.CurrentContext = "anonymous"
|
|
|
|
|
|
|
|
if err := marshalFunc(*kubeconfig, temp.Name()); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to marshal kubeconfig: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use v1beta1 instead of v1 since v1beta1 is more prevalent in our desired
|
|
|
|
// integration points.
|
|
|
|
version := k8sauthv1beta1.SchemeGroupVersion.Version
|
|
|
|
|
|
|
|
// At the current time, we don't provide any audiences because we simply don't
|
|
|
|
// have any requirements to do so. This can be changed in the future as
|
|
|
|
// requirements change.
|
|
|
|
var implicitAuds authenticator.Audiences
|
|
|
|
|
|
|
|
// We set this to nil because we would only need this to support some of the
|
|
|
|
// custom proxy stuff used by the API server.
|
|
|
|
var customDial net.DialFunc
|
|
|
|
|
|
|
|
return webhook.New(temp.Name(), version, implicitAuds, customDial)
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:03:25 +00:00
|
|
|
func getCABundle(spec *auth1alpha1.TLSSpec) ([]byte, error) {
|
2020-09-14 15:41:36 +00:00
|
|
|
if spec == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-09-15 18:54:19 +00:00
|
|
|
return base64.StdEncoding.DecodeString(spec.CertificateAuthorityData)
|
2020-09-14 15:41:36 +00:00
|
|
|
}
|