2021-01-07 22:58:09 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-10-06 00:28:19 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-07-26 16:18:43 +00:00
|
|
|
// Package server defines the entrypoint for the Pinniped Supervisor server.
|
|
|
|
package server
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-14 15:36:45 +00:00
|
|
|
"crypto/rand"
|
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"
|
2021-08-29 00:38:50 +00:00
|
|
|
"sync"
|
2021-08-28 15:23:11 +00:00
|
|
|
"syscall"
|
2020-10-06 00:28:19 +00:00
|
|
|
"time"
|
|
|
|
|
2020-12-11 16:11:49 +00:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
2020-12-17 12:41:53 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-10-08 17:27:45 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
2021-11-30 17:55:19 +00:00
|
|
|
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
|
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"
|
|
|
|
"k8s.io/component-base/logs"
|
|
|
|
"k8s.io/klog/v2"
|
2020-11-11 23:10:06 +00:00
|
|
|
"k8s.io/klog/v2/klogr"
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-02-16 19:00:08 +00:00
|
|
|
configv1alpha1 "go.pinniped.dev/generated/latest/apis/supervisor/config/v1alpha1"
|
|
|
|
pinnipedclientset "go.pinniped.dev/generated/latest/client/supervisor/clientset/versioned"
|
|
|
|
pinnipedinformers "go.pinniped.dev/generated/latest/client/supervisor/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"
|
2021-07-15 18:32:15 +00:00
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig/activedirectoryupstreamwatcher"
|
2020-12-12 00:05:08 +00:00
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig/generator"
|
2021-05-12 21:00:39 +00:00
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig/ldapupstreamwatcher"
|
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig/oidcupstreamwatcher"
|
2020-12-11 23:21:34 +00:00
|
|
|
"go.pinniped.dev/internal/controller/supervisorstorage"
|
2021-08-29 00:38:50 +00:00
|
|
|
"go.pinniped.dev/internal/controllerinit"
|
2020-10-06 00:28:19 +00:00
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2021-10-20 11:59:24 +00:00
|
|
|
"go.pinniped.dev/internal/crypto/ptls"
|
2021-01-05 22:07:33 +00:00
|
|
|
"go.pinniped.dev/internal/deploymentref"
|
2020-10-06 00:28:19 +00:00
|
|
|
"go.pinniped.dev/internal/downward"
|
2021-01-13 01:27:41 +00:00
|
|
|
"go.pinniped.dev/internal/groupsuffix"
|
2021-01-05 22:07:33 +00:00
|
|
|
"go.pinniped.dev/internal/kubeclient"
|
2021-08-18 04:14:38 +00:00
|
|
|
"go.pinniped.dev/internal/leaderelection"
|
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-11-10 13:48:42 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-12-17 12:41:53 +00:00
|
|
|
"go.pinniped.dev/internal/secret"
|
2020-10-06 00:28:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
singletonWorker = 1
|
|
|
|
defaultResyncInterval = 3 * time.Minute
|
|
|
|
)
|
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
func startServer(ctx context.Context, shutdown *sync.WaitGroup, l net.Listener, handler http.Handler) {
|
2021-11-30 17:55:19 +00:00
|
|
|
server := http.Server{Handler: genericapifilters.WithWarningRecorder(handler)}
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
shutdown.Add(1)
|
2020-10-06 00:28:19 +00:00
|
|
|
go func() {
|
2021-08-29 00:38:50 +00:00
|
|
|
defer shutdown.Done()
|
|
|
|
|
|
|
|
err := server.Serve(l)
|
|
|
|
plog.Debug("server exited", "err", err)
|
2020-10-06 00:28:19 +00:00
|
|
|
}()
|
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
shutdown.Add(1)
|
2020-10-06 00:28:19 +00:00
|
|
|
go func() {
|
2021-08-29 00:38:50 +00:00
|
|
|
defer shutdown.Done()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
plog.Debug("server context cancelled", "err", ctx.Err())
|
|
|
|
|
|
|
|
// allow up to a minute grace period for active connections to return to idle
|
|
|
|
connectionsCtx, connectionsCancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer connectionsCancel()
|
|
|
|
|
|
|
|
if err := server.Shutdown(connectionsCtx); err != nil {
|
|
|
|
plog.Debug("server shutdown failed", "err", err)
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
func signalCtx() context.Context {
|
2020-10-06 00:28:19 +00:00
|
|
|
signalCh := make(chan os.Signal, 1)
|
2021-08-28 15:23:11 +00:00
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
2021-08-29 00:38:50 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s := <-signalCh
|
|
|
|
plog.Debug("saw signal", "signal", s)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ctx
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:44:01 +00:00
|
|
|
//nolint:funlen
|
2021-08-29 00:38:50 +00:00
|
|
|
func prepareControllers(
|
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-11-11 23:10:06 +00:00
|
|
|
dynamicUpstreamIDPProvider provider.DynamicUpstreamIDPProvider,
|
2020-12-11 16:11:49 +00:00
|
|
|
secretCache *secret.Cache,
|
|
|
|
supervisorDeployment *appsv1.Deployment,
|
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,
|
2021-08-29 00:38:50 +00:00
|
|
|
leaderElector controllerinit.RunnerWrapper,
|
|
|
|
) controllerinit.RunnerBuilder {
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer := pinnipedInformers.Config().V1alpha1().FederationDomains()
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer := kubeInformers.Core().V1().Secrets()
|
|
|
|
|
2020-10-06 00:28:19 +00:00
|
|
|
// Create controller manager.
|
|
|
|
controllerManager := controllerlib.
|
|
|
|
NewManager().
|
2020-12-11 23:21:34 +00:00
|
|
|
WithController(
|
|
|
|
supervisorstorage.GarbageCollectorController(
|
2021-10-22 21:32:26 +00:00
|
|
|
dynamicUpstreamIDPProvider,
|
2020-12-11 23:21:34 +00:00
|
|
|
clock.RealClock{},
|
|
|
|
kubeClient,
|
2020-12-17 19:34:49 +00:00
|
|
|
secretInformer,
|
2020-12-11 23:21:34 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
2020-10-06 00:28:19 +00:00
|
|
|
WithController(
|
2020-12-16 22:27:09 +00:00
|
|
|
supervisorconfig.NewFederationDomainWatcherController(
|
2020-10-17 00:51:40 +00:00
|
|
|
issuerManager,
|
2020-10-08 17:27:45 +00:00
|
|
|
clock.RealClock{},
|
|
|
|
pinnipedClient,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
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,
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
2020-10-14 13:47:34 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2020-10-17 00:51:40 +00:00
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
supervisorconfig.NewJWKSObserverController(
|
|
|
|
dynamicJWKSProvider,
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
2020-10-17 00:51:40 +00:00
|
|
|
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-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
2020-10-27 00:03:26 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2020-11-11 23:10:06 +00:00
|
|
|
).
|
2020-12-11 16:11:49 +00:00
|
|
|
WithController(
|
2020-12-12 00:05:08 +00:00
|
|
|
generator.NewSupervisorSecretsController(
|
2020-12-11 16:11:49 +00:00
|
|
|
supervisorDeployment,
|
2020-12-14 20:53:12 +00:00
|
|
|
cfg.Labels,
|
2020-12-11 16:11:49 +00:00
|
|
|
kubeClient,
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-11 16:11:49 +00:00
|
|
|
func(secret []byte) {
|
|
|
|
plog.Debug("setting csrf cookie secret")
|
|
|
|
secretCache.SetCSRFCookieEncoderHashKey(secret)
|
|
|
|
},
|
2020-12-15 00:08:48 +00:00
|
|
|
controllerlib.WithInformer,
|
2020-12-15 12:58:33 +00:00
|
|
|
controllerlib.WithInitialEvent,
|
2020-12-11 16:11:49 +00:00
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
2020-11-11 23:10:06 +00:00
|
|
|
WithController(
|
2020-12-16 22:27:09 +00:00
|
|
|
generator.NewFederationDomainSecretsController(
|
2020-12-15 01:38:01 +00:00
|
|
|
generator.NewSymmetricSecretHelper(
|
2020-12-14 15:36:45 +00:00
|
|
|
"pinniped-oidc-provider-hmac-key-",
|
|
|
|
cfg.Labels,
|
|
|
|
rand.Reader,
|
2020-12-15 14:13:01 +00:00
|
|
|
generator.SecretUsageTokenSigningKey,
|
2020-12-16 22:27:09 +00:00
|
|
|
func(federationDomainIssuer string, symmetricKey []byte) {
|
|
|
|
plog.Debug("setting hmac secret", "issuer", federationDomainIssuer)
|
|
|
|
secretCache.SetTokenHMACKey(federationDomainIssuer, symmetricKey)
|
2020-12-14 15:36:45 +00:00
|
|
|
},
|
|
|
|
),
|
2021-02-11 02:46:03 +00:00
|
|
|
func(fd *configv1alpha1.FederationDomainStatus) *corev1.LocalObjectReference {
|
|
|
|
return &fd.Secrets.TokenSigningKey
|
2020-12-17 12:41:53 +00:00
|
|
|
},
|
2020-12-14 15:36:45 +00:00
|
|
|
kubeClient,
|
2020-12-15 14:13:01 +00:00
|
|
|
pinnipedClient,
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
2020-12-14 15:36:45 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
2020-12-16 22:27:09 +00:00
|
|
|
generator.NewFederationDomainSecretsController(
|
2020-12-15 01:38:01 +00:00
|
|
|
generator.NewSymmetricSecretHelper(
|
2020-12-14 15:36:45 +00:00
|
|
|
"pinniped-oidc-provider-upstream-state-signature-key-",
|
|
|
|
cfg.Labels,
|
|
|
|
rand.Reader,
|
2020-12-15 14:13:01 +00:00
|
|
|
generator.SecretUsageStateSigningKey,
|
2020-12-16 22:27:09 +00:00
|
|
|
func(federationDomainIssuer string, symmetricKey []byte) {
|
|
|
|
plog.Debug("setting state signature key", "issuer", federationDomainIssuer)
|
|
|
|
secretCache.SetStateEncoderHashKey(federationDomainIssuer, symmetricKey)
|
2020-12-14 15:36:45 +00:00
|
|
|
},
|
|
|
|
),
|
2021-02-11 02:46:03 +00:00
|
|
|
func(fd *configv1alpha1.FederationDomainStatus) *corev1.LocalObjectReference {
|
|
|
|
return &fd.Secrets.StateSigningKey
|
2020-12-17 12:41:53 +00:00
|
|
|
},
|
2020-12-14 15:36:45 +00:00
|
|
|
kubeClient,
|
2020-12-15 14:13:01 +00:00
|
|
|
pinnipedClient,
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
2020-12-14 15:36:45 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
2020-12-16 22:27:09 +00:00
|
|
|
generator.NewFederationDomainSecretsController(
|
2020-12-15 01:38:01 +00:00
|
|
|
generator.NewSymmetricSecretHelper(
|
2020-12-14 15:36:45 +00:00
|
|
|
"pinniped-oidc-provider-upstream-state-encryption-key-",
|
|
|
|
cfg.Labels,
|
|
|
|
rand.Reader,
|
2020-12-15 14:13:01 +00:00
|
|
|
generator.SecretUsageStateEncryptionKey,
|
2020-12-16 22:27:09 +00:00
|
|
|
func(federationDomainIssuer string, symmetricKey []byte) {
|
|
|
|
plog.Debug("setting state encryption key", "issuer", federationDomainIssuer)
|
|
|
|
secretCache.SetStateEncoderBlockKey(federationDomainIssuer, symmetricKey)
|
2020-12-14 15:36:45 +00:00
|
|
|
},
|
|
|
|
),
|
2021-02-11 02:46:03 +00:00
|
|
|
func(fd *configv1alpha1.FederationDomainStatus) *corev1.LocalObjectReference {
|
|
|
|
return &fd.Secrets.StateEncryptionKey
|
2020-12-17 12:41:53 +00:00
|
|
|
},
|
2020-12-14 15:36:45 +00:00
|
|
|
kubeClient,
|
2020-12-15 14:13:01 +00:00
|
|
|
pinnipedClient,
|
2020-12-14 15:36:45 +00:00
|
|
|
secretInformer,
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer,
|
2020-12-14 15:36:45 +00:00
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
2021-05-12 21:00:39 +00:00
|
|
|
oidcupstreamwatcher.New(
|
2020-11-11 23:10:06 +00:00
|
|
|
dynamicUpstreamIDPProvider,
|
|
|
|
pinnipedClient,
|
2020-12-16 22:27:09 +00:00
|
|
|
pinnipedInformers.IDP().V1alpha1().OIDCIdentityProviders(),
|
2020-12-17 21:49:53 +00:00
|
|
|
secretInformer,
|
2020-12-18 23:41:07 +00:00
|
|
|
klogr.New(),
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
2021-04-10 01:49:43 +00:00
|
|
|
singletonWorker).
|
|
|
|
WithController(
|
2021-05-12 21:00:39 +00:00
|
|
|
ldapupstreamwatcher.New(
|
2021-04-10 01:49:43 +00:00
|
|
|
dynamicUpstreamIDPProvider,
|
|
|
|
pinnipedClient,
|
|
|
|
pinnipedInformers.IDP().V1alpha1().LDAPIdentityProviders(),
|
|
|
|
secretInformer,
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
2021-07-02 22:30:27 +00:00
|
|
|
singletonWorker).
|
|
|
|
WithController(
|
|
|
|
activedirectoryupstreamwatcher.New(
|
|
|
|
dynamicUpstreamIDPProvider,
|
|
|
|
pinnipedClient,
|
|
|
|
pinnipedInformers.IDP().V1alpha1().ActiveDirectoryIdentityProviders(),
|
|
|
|
secretInformer,
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
2020-11-11 23:10:06 +00:00
|
|
|
singletonWorker)
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
return controllerinit.Prepare(controllerManager.Start, leaderElector, kubeInformers, pinnipedInformers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startControllers(ctx context.Context, shutdown *sync.WaitGroup, buildControllers controllerinit.RunnerBuilder) error {
|
|
|
|
runControllers, err := buildControllers(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create run controller func: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer shutdown.Done()
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
runControllers(ctx)
|
|
|
|
}()
|
2020-10-21 18:51:31 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
return nil
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
func runSupervisor(podInfo *downward.PodInfo, cfg *supervisor.Config) error {
|
2020-12-11 16:11:49 +00:00
|
|
|
serverInstallationNamespace := podInfo.Namespace
|
|
|
|
|
2021-01-05 22:07:33 +00:00
|
|
|
dref, supervisorDeployment, err := deploymentref.New(podInfo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create deployment ref: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-18 04:14:38 +00:00
|
|
|
opts := []kubeclient.Option{
|
2021-01-13 01:27:41 +00:00
|
|
|
dref,
|
|
|
|
kubeclient.WithMiddleware(groupsuffix.New(*cfg.APIGroupSuffix)),
|
2021-08-18 04:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, leaderElector, err := leaderelection.New(
|
|
|
|
podInfo,
|
|
|
|
supervisorDeployment,
|
|
|
|
opts...,
|
2021-01-13 01:27:41 +00:00
|
|
|
)
|
2020-10-06 00:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create k8s client: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-18 04:14:38 +00:00
|
|
|
clientWithoutLeaderElection, err := kubeclient.New(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create k8s client without leader election: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(
|
2021-01-05 22:07:33 +00:00
|
|
|
client.Kubernetes,
|
2020-10-14 13:47:34 +00:00
|
|
|
defaultResyncInterval,
|
|
|
|
kubeinformers.WithNamespace(serverInstallationNamespace),
|
|
|
|
)
|
|
|
|
|
2020-10-07 14:53:05 +00:00
|
|
|
pinnipedInformers := pinnipedinformers.NewSharedInformerFactoryWithOptions(
|
2021-01-05 22:07:33 +00:00
|
|
|
client.PinnipedSupervisor,
|
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-11-05 01:06:47 +00:00
|
|
|
dynamicUpstreamIDPProvider := provider.NewDynamicUpstreamIDPProvider()
|
2020-12-11 16:11:49 +00:00
|
|
|
secretCache := secret.Cache{}
|
2020-10-27 00:03:26 +00:00
|
|
|
|
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.
|
2020-12-03 01:39:45 +00:00
|
|
|
oidProvidersManager := manager.NewManager(
|
|
|
|
healthMux,
|
|
|
|
dynamicJWKSProvider,
|
|
|
|
dynamicUpstreamIDPProvider,
|
2020-12-11 16:11:49 +00:00
|
|
|
&secretCache,
|
2021-08-18 04:14:38 +00:00
|
|
|
clientWithoutLeaderElection.Kubernetes.CoreV1().Secrets(serverInstallationNamespace), // writes to kube storage are allowed for non-leaders
|
2020-12-03 01:39:45 +00:00
|
|
|
)
|
2020-10-27 00:03:26 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
buildControllersFunc := prepareControllers(
|
2020-10-27 00:03:26 +00:00
|
|
|
cfg,
|
|
|
|
oidProvidersManager,
|
|
|
|
dynamicJWKSProvider,
|
|
|
|
dynamicTLSCertProvider,
|
2020-11-11 23:10:06 +00:00
|
|
|
dynamicUpstreamIDPProvider,
|
2020-12-11 16:11:49 +00:00
|
|
|
&secretCache,
|
|
|
|
supervisorDeployment,
|
2021-01-05 22:07:33 +00:00
|
|
|
client.Kubernetes,
|
|
|
|
client.PinnipedSupervisor,
|
2020-10-27 00:03:26 +00:00
|
|
|
kubeInformers,
|
|
|
|
pinnipedInformers,
|
2021-08-18 04:14:38 +00:00
|
|
|
leaderElector,
|
2020-10-27 00:03:26 +00:00
|
|
|
)
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
ctx := signalCtx()
|
|
|
|
shutdown := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
if err := startControllers(ctx, shutdown, buildControllersFunc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 00:28:19 +00:00
|
|
|
//nolint: gosec // Intentionally binding to all network interfaces.
|
2020-11-02 16:57:05 +00:00
|
|
|
httpListener, err := net.Listen("tcp", ":8080")
|
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() }()
|
2021-08-29 00:38:50 +00:00
|
|
|
startServer(ctx, shutdown, httpListener, oidProvidersManager)
|
2020-10-21 18:51:31 +00:00
|
|
|
|
2021-10-20 11:59:24 +00:00
|
|
|
c := ptls.Default(nil)
|
|
|
|
c.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
cert := dynamicTLSCertProvider.GetTLSCert(strings.ToLower(info.ServerName))
|
|
|
|
defaultCert := dynamicTLSCertProvider.GetDefaultTLSCert()
|
|
|
|
plog.Debug("GetCertificate called for port 8443",
|
|
|
|
"info.ServerName", info.ServerName,
|
|
|
|
"foundSNICert", cert != nil,
|
|
|
|
"foundDefaultCert", defaultCert != nil,
|
|
|
|
)
|
|
|
|
if cert == nil {
|
|
|
|
cert = defaultCert
|
|
|
|
}
|
|
|
|
return cert, nil
|
|
|
|
}
|
2020-10-27 00:03:26 +00:00
|
|
|
//nolint: gosec // Intentionally binding to all network interfaces.
|
2021-10-20 11:59:24 +00:00
|
|
|
httpsListener, err := tls.Listen("tcp", ":8443", c)
|
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() }()
|
2021-08-29 00:38:50 +00:00
|
|
|
startServer(ctx, shutdown, httpsListener, oidProvidersManager)
|
2020-10-27 00:03:26 +00:00
|
|
|
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("supervisor is ready",
|
2020-10-27 00:03:26 +00:00
|
|
|
"httpAddress", httpListener.Addr().String(),
|
|
|
|
"httpsAddress", httpsListener.Addr().String(),
|
|
|
|
)
|
2021-08-29 00:38:50 +00:00
|
|
|
defer plog.Debug("supervisor exiting")
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
shutdown.Wait()
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
func main() error { // return an error instead of klog.Fatal to allow defer statements to run
|
2020-10-06 00:28:19 +00:00
|
|
|
logs.InitLogs()
|
|
|
|
defer logs.FlushLogs()
|
2020-11-10 13:48:42 +00:00
|
|
|
plog.RemoveKlogGlobalFlags() // move this whenever the below code gets refactored to use cobra
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
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 {
|
2021-08-29 00:38:50 +00:00
|
|
|
return fmt.Errorf("could not read pod metadata: %w", err)
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
// Read the server config file.
|
|
|
|
cfg, err := supervisor.FromPath(os.Args[2])
|
|
|
|
if err != nil {
|
2021-08-29 00:38:50 +00:00
|
|
|
return fmt.Errorf("could not load config: %w", err)
|
2020-10-15 19:40:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
return runSupervisor(podInfo, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Main() {
|
|
|
|
if err := main(); err != nil {
|
2020-10-06 00:28:19 +00:00
|
|
|
klog.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|