2020-10-06 00:28:19 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-27 00:03:26 +00:00
|
|
|
"crypto/tls"
|
2020-10-06 00:28:19 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-10-27 00:03:26 +00:00
|
|
|
"strings"
|
2020-10-06 00:28:19 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-08 17:27:45 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeinformers "k8s.io/client-go/informers"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2020-10-06 00:28:19 +00:00
|
|
|
"k8s.io/client-go/pkg/version"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
"k8s.io/component-base/logs"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedclientset "go.pinniped.dev/generated/1.19/client/clientset/versioned"
|
|
|
|
pinnipedinformers "go.pinniped.dev/generated/1.19/client/informers/externalversions"
|
2020-10-15 19:40:56 +00:00
|
|
|
"go.pinniped.dev/internal/config/supervisor"
|
2020-10-06 00:28:19 +00:00
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
|
|
|
"go.pinniped.dev/internal/downward"
|
2020-10-17 00:51:40 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/jwks"
|
2020-10-27 00:03:26 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
2020-10-08 18:28:21 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider/manager"
|
2020-10-06 00:28:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
singletonWorker = 1
|
|
|
|
defaultResyncInterval = 3 * time.Minute
|
|
|
|
)
|
|
|
|
|
2020-10-08 02:18:34 +00:00
|
|
|
func start(ctx context.Context, l net.Listener, handler http.Handler) {
|
|
|
|
server := http.Server{Handler: handler}
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- server.Serve(l)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
klog.InfoS("server exited", "err", err)
|
|
|
|
case <-ctx.Done():
|
|
|
|
klog.InfoS("server context cancelled", "err", ctx.Err())
|
|
|
|
if err := server.Shutdown(context.Background()); err != nil {
|
|
|
|
klog.InfoS("server shutdown failed", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForSignal() os.Signal {
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt)
|
|
|
|
return <-signalCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func startControllers(
|
|
|
|
ctx context.Context,
|
2020-10-15 19:40:56 +00:00
|
|
|
cfg *supervisor.Config,
|
2020-10-17 00:51:40 +00:00
|
|
|
issuerManager *manager.Manager,
|
|
|
|
dynamicJWKSProvider jwks.DynamicJWKSProvider,
|
2020-10-27 00:03:26 +00:00
|
|
|
dynamicTLSCertProvider provider.DynamicTLSCertProvider,
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeClient kubernetes.Interface,
|
2020-10-08 17:27:45 +00:00
|
|
|
pinnipedClient pinnipedclientset.Interface,
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeInformers kubeinformers.SharedInformerFactory,
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedInformers pinnipedinformers.SharedInformerFactory,
|
2020-10-06 00:28:19 +00:00
|
|
|
) {
|
|
|
|
// Create controller manager.
|
|
|
|
controllerManager := controllerlib.
|
|
|
|
NewManager().
|
|
|
|
WithController(
|
2020-10-08 02:18:34 +00:00
|
|
|
supervisorconfig.NewOIDCProviderConfigWatcherController(
|
2020-10-17 00:51:40 +00:00
|
|
|
issuerManager,
|
2020-10-08 17:27:45 +00:00
|
|
|
clock.RealClock{},
|
|
|
|
pinnipedClient,
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedInformers.Config().V1alpha1().OIDCProviderConfigs(),
|
2020-10-06 00:28:19 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2020-10-14 13:47:34 +00:00
|
|
|
).
|
|
|
|
WithController(
|
2020-10-17 00:51:40 +00:00
|
|
|
supervisorconfig.NewJWKSWriterController(
|
2020-10-15 19:40:56 +00:00
|
|
|
cfg.Labels,
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeClient,
|
|
|
|
pinnipedClient,
|
|
|
|
kubeInformers.Core().V1().Secrets(),
|
|
|
|
pinnipedInformers.Config().V1alpha1().OIDCProviderConfigs(),
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2020-10-17 00:51:40 +00:00
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
supervisorconfig.NewJWKSObserverController(
|
|
|
|
dynamicJWKSProvider,
|
|
|
|
kubeInformers.Core().V1().Secrets(),
|
|
|
|
pinnipedInformers.Config().V1alpha1().OIDCProviderConfigs(),
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2020-10-27 00:03:26 +00:00
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
supervisorconfig.NewTLSCertObserverController(
|
|
|
|
dynamicTLSCertProvider,
|
2020-10-28 18:56:50 +00:00
|
|
|
cfg.NamesConfig.DefaultTLSCertificateSecret,
|
2020-10-27 00:03:26 +00:00
|
|
|
kubeInformers.Core().V1().Secrets(),
|
|
|
|
pinnipedInformers.Config().V1alpha1().OIDCProviderConfigs(),
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2020-10-06 00:28:19 +00:00
|
|
|
)
|
|
|
|
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeInformers.Start(ctx.Done())
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedInformers.Start(ctx.Done())
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2020-10-21 18:51:31 +00:00
|
|
|
// Wait until the caches are synced before returning.
|
|
|
|
kubeInformers.WaitForCacheSync(ctx.Done())
|
|
|
|
pinnipedInformers.WaitForCacheSync(ctx.Done())
|
|
|
|
|
2020-10-06 00:28:19 +00:00
|
|
|
go controllerManager.Start(ctx)
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:47:34 +00:00
|
|
|
func newClients() (kubernetes.Interface, pinnipedclientset.Interface, error) {
|
2020-10-06 00:28:19 +00:00
|
|
|
kubeConfig, err := restclient.InClusterConfig()
|
|
|
|
if err != nil {
|
2020-10-14 13:47:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not load in-cluster configuration: %w", err)
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the core Kubernetes API.
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("could not create kube client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the Pinniped API.
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedClient, err := pinnipedclientset.NewForConfig(kubeConfig)
|
2020-10-06 00:28:19 +00:00
|
|
|
if err != nil {
|
2020-10-14 13:47:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not create pinniped client: %w", err)
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 13:47:34 +00:00
|
|
|
return kubeClient, pinnipedClient, nil
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
func run(serverInstallationNamespace string, cfg *supervisor.Config) error {
|
2020-10-06 00:28:19 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeClient, pinnipedClient, err := newClients()
|
2020-10-06 00:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create k8s client: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(
|
|
|
|
kubeClient,
|
|
|
|
defaultResyncInterval,
|
|
|
|
kubeinformers.WithNamespace(serverInstallationNamespace),
|
|
|
|
)
|
|
|
|
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedInformers := pinnipedinformers.NewSharedInformerFactoryWithOptions(
|
|
|
|
pinnipedClient,
|
2020-10-06 00:28:19 +00:00
|
|
|
defaultResyncInterval,
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedinformers.WithNamespace(serverInstallationNamespace),
|
2020-10-06 00:28:19 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 22:24:48 +00:00
|
|
|
// Serve the /healthz endpoint and make all other paths result in 404.
|
|
|
|
healthMux := http.NewServeMux()
|
|
|
|
healthMux.Handle("/healthz", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
_, _ = writer.Write([]byte("ok"))
|
|
|
|
}))
|
|
|
|
|
2020-10-17 00:51:40 +00:00
|
|
|
dynamicJWKSProvider := jwks.NewDynamicJWKSProvider()
|
2020-10-27 00:03:26 +00:00
|
|
|
dynamicTLSCertProvider := provider.NewDynamicTLSCertProvider()
|
|
|
|
|
2020-10-21 22:24:48 +00:00
|
|
|
// OIDC endpoints will be served by the oidProvidersManager, and any non-OIDC paths will fallback to the healthMux.
|
|
|
|
oidProvidersManager := manager.NewManager(healthMux, dynamicJWKSProvider)
|
2020-10-27 00:03:26 +00:00
|
|
|
|
|
|
|
startControllers(
|
|
|
|
ctx,
|
|
|
|
cfg,
|
|
|
|
oidProvidersManager,
|
|
|
|
dynamicJWKSProvider,
|
|
|
|
dynamicTLSCertProvider,
|
|
|
|
kubeClient,
|
|
|
|
pinnipedClient,
|
|
|
|
kubeInformers,
|
|
|
|
pinnipedInformers,
|
|
|
|
)
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
//nolint: gosec // Intentionally binding to all network interfaces.
|
2020-10-27 00:03:26 +00:00
|
|
|
httpListener, err := net.Listen("tcp", ":80")
|
2020-10-06 00:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create listener: %w", err)
|
|
|
|
}
|
2020-10-27 23:33:08 +00:00
|
|
|
defer func() { _ = httpListener.Close() }()
|
2020-10-27 00:03:26 +00:00
|
|
|
start(ctx, httpListener, oidProvidersManager)
|
2020-10-21 18:51:31 +00:00
|
|
|
|
2020-10-27 00:03:26 +00:00
|
|
|
//nolint: gosec // Intentionally binding to all network interfaces.
|
|
|
|
httpsListener, err := tls.Listen("tcp", ":443", &tls.Config{
|
2020-10-27 21:57:25 +00:00
|
|
|
MinVersion: tls.VersionTLS12, // Allow v1.2 because clients like the default `curl` on MacOS don't support 1.3 yet.
|
2020-10-27 00:03:26 +00:00
|
|
|
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2020-10-27 21:57:25 +00:00
|
|
|
cert := dynamicTLSCertProvider.GetTLSCert(strings.ToLower(info.ServerName))
|
2020-10-27 23:33:08 +00:00
|
|
|
defaultCert := dynamicTLSCertProvider.GetDefaultTLSCert()
|
|
|
|
klog.InfoS("GetCertificate called for port 443",
|
|
|
|
"info.ServerName", info.ServerName,
|
|
|
|
"foundSNICert", cert != nil,
|
|
|
|
"foundDefaultCert", defaultCert != nil,
|
|
|
|
)
|
|
|
|
if cert == nil {
|
|
|
|
cert = defaultCert
|
|
|
|
}
|
2020-10-27 21:57:25 +00:00
|
|
|
return cert, nil
|
2020-10-27 00:03:26 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create listener: %w", err)
|
|
|
|
}
|
2020-10-27 23:33:08 +00:00
|
|
|
defer func() { _ = httpsListener.Close() }()
|
2020-10-27 00:03:26 +00:00
|
|
|
start(ctx, httpsListener, oidProvidersManager)
|
|
|
|
|
|
|
|
klog.InfoS("supervisor is ready",
|
|
|
|
"httpAddress", httpListener.Addr().String(),
|
|
|
|
"httpsAddress", httpsListener.Addr().String(),
|
|
|
|
)
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
gotSignal := waitForSignal()
|
|
|
|
klog.InfoS("supervisor exiting", "signal", gotSignal)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logs.InitLogs()
|
|
|
|
defer logs.FlushLogs()
|
|
|
|
|
|
|
|
klog.Infof("Running %s at %#v", rest.DefaultKubernetesUserAgent(), version.Get())
|
|
|
|
klog.Infof("Command-line arguments were: %s %s %s", os.Args[0], os.Args[1], os.Args[2])
|
|
|
|
|
|
|
|
// Discover in which namespace we are installed.
|
|
|
|
podInfo, err := downward.Load(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
klog.Fatal(fmt.Errorf("could not read pod metadata: %w", err))
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
// Read the server config file.
|
|
|
|
cfg, err := supervisor.FromPath(os.Args[2])
|
|
|
|
if err != nil {
|
|
|
|
klog.Fatal(fmt.Errorf("could not load config: %w", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := run(podInfo.Namespace, cfg); err != nil {
|
2020-10-06 00:28:19 +00:00
|
|
|
klog.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|