2022-01-14 18:49:22 +00:00
|
|
|
// Copyright 2020-2022 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"
|
2021-12-15 20:48:55 +00:00
|
|
|
"strconv"
|
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"
|
|
|
|
|
2021-12-15 20:48:55 +00:00
|
|
|
"github.com/joshlf/go-acl"
|
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"
|
2022-04-16 02:43:53 +00:00
|
|
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
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"
|
2021-12-10 22:22:36 +00:00
|
|
|
"k8s.io/utils/clock"
|
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-12-15 20:48:55 +00:00
|
|
|
handler = genericapifilters.WithWarningRecorder(handler)
|
|
|
|
handler = withBootstrapPaths(handler, "/healthz") // only health checks are allowed for bootstrap connections
|
|
|
|
|
|
|
|
server := http.Server{
|
2022-08-24 21:45:55 +00:00
|
|
|
Handler: handler,
|
|
|
|
ConnContext: withBootstrapConnCtx,
|
|
|
|
ReadHeaderTimeout: 10 * time.Second,
|
2021-12-15 20:48:55 +00:00
|
|
|
}
|
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,
|
2022-08-24 21:45:55 +00:00
|
|
|
plog.Logr(), //nolint:staticcheck // old controller with lots of log statements
|
2020-12-18 23:41:07 +00:00
|
|
|
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-12-15 20:48:55 +00:00
|
|
|
//nolint:funlen
|
2022-04-16 02:43:53 +00:00
|
|
|
func runSupervisor(ctx context.Context, podInfo *downward.PodInfo, cfg *supervisor.Config) error {
|
2020-12-11 16:11:49 +00:00
|
|
|
serverInstallationNamespace := podInfo.Namespace
|
|
|
|
|
2021-12-15 20:48:55 +00:00
|
|
|
dref, supervisorDeployment, supervisorPod, err := deploymentref.New(podInfo)
|
2021-01-05 22:07:33 +00:00
|
|
|
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
|
|
|
shutdown := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
if err := startControllers(ctx, shutdown, buildControllersFunc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:48:55 +00:00
|
|
|
if e := cfg.Endpoints.HTTP; e.Network != supervisor.NetworkDisabled {
|
|
|
|
finishSetupPerms := maybeSetupUnixPerms(e, supervisorPod)
|
|
|
|
|
|
|
|
httpListener, err := net.Listen(e.Network, e.Address)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create http listener with network %q and address %q: %w", e.Network, e.Address, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := finishSetupPerms(); err != nil {
|
|
|
|
return fmt.Errorf("cannot setup http listener permissions for network %q and address %q: %w", e.Network, e.Address, err)
|
2021-10-20 11:59:24 +00:00
|
|
|
}
|
2021-12-15 20:48:55 +00:00
|
|
|
|
|
|
|
defer func() { _ = httpListener.Close() }()
|
|
|
|
startServer(ctx, shutdown, httpListener, oidProvidersManager)
|
|
|
|
plog.Debug("supervisor http listener started", "address", httpListener.Addr().String())
|
2021-10-20 11:59:24 +00:00
|
|
|
}
|
2021-12-15 20:48:55 +00:00
|
|
|
|
|
|
|
if e := cfg.Endpoints.HTTPS; e.Network != supervisor.NetworkDisabled { //nolint:nestif
|
|
|
|
finishSetupPerms := maybeSetupUnixPerms(e, supervisorPod)
|
|
|
|
|
|
|
|
bootstrapCert, err := getBootstrapCert() // generate this in-memory once per process startup
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("https listener bootstrap error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := ptls.Default(nil)
|
|
|
|
c.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
cert := dynamicTLSCertProvider.GetTLSCert(strings.ToLower(info.ServerName))
|
|
|
|
|
|
|
|
defaultCert := dynamicTLSCertProvider.GetDefaultTLSCert()
|
|
|
|
|
|
|
|
if plog.Enabled(plog.LevelTrace) { // minor CPU optimization as this is generally just noise
|
|
|
|
host, port, _ := net.SplitHostPort(info.Conn.LocalAddr().String()) // error is safe to ignore here
|
|
|
|
|
|
|
|
plog.Trace("GetCertificate called",
|
|
|
|
"info.ServerName", info.ServerName,
|
|
|
|
"foundSNICert", cert != nil,
|
|
|
|
"foundDefaultCert", defaultCert != nil,
|
|
|
|
"host", host,
|
|
|
|
"port", port,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cert == nil {
|
|
|
|
cert = defaultCert
|
|
|
|
}
|
|
|
|
|
|
|
|
if cert == nil {
|
|
|
|
setIsBootstrapConn(info.Context()) // make this connection only work for bootstrap requests
|
|
|
|
cert = bootstrapCert
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
httpsListener, err := tls.Listen(e.Network, e.Address, c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create https listener with network %q and address %q: %w", e.Network, e.Address, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := finishSetupPerms(); err != nil {
|
|
|
|
return fmt.Errorf("cannot setup https listener permissions for network %q and address %q: %w", e.Network, e.Address, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = httpsListener.Close() }()
|
|
|
|
startServer(ctx, shutdown, httpsListener, oidProvidersManager)
|
|
|
|
plog.Debug("supervisor https listener started", "address", httpsListener.Addr().String())
|
2020-10-27 00:03:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 20:48:55 +00:00
|
|
|
plog.Debug("supervisor started")
|
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-12-15 20:48:55 +00:00
|
|
|
func maybeSetupUnixPerms(endpoint *supervisor.Endpoint, pod *corev1.Pod) func() error {
|
|
|
|
if endpoint.Network != supervisor.NetworkUnix {
|
|
|
|
return func() error { return nil }
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = os.Remove(endpoint.Address) // empty dir volumes persist across container crashes
|
|
|
|
|
|
|
|
return func() error {
|
|
|
|
selfUser := int64(os.Getuid())
|
|
|
|
var entries []acl.Entry
|
|
|
|
for _, container := range pod.Spec.Containers {
|
|
|
|
if container.SecurityContext == nil ||
|
|
|
|
container.SecurityContext.RunAsUser == nil ||
|
|
|
|
*container.SecurityContext.RunAsUser == selfUser {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
plog.Debug("adding write permission",
|
|
|
|
"address", endpoint.Address,
|
|
|
|
"uid", *container.SecurityContext.RunAsUser,
|
|
|
|
)
|
|
|
|
entries = append(entries, acl.Entry{
|
|
|
|
Tag: acl.TagUser,
|
|
|
|
Qualifier: strconv.FormatInt(*container.SecurityContext.RunAsUser, 10),
|
|
|
|
Perms: 2, // write permission
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return acl.Add(endpoint.Address, entries...) // allow all containers in the pod to write to the socket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
func main() error { // return an error instead of plog.Fatal to allow defer statements to run
|
|
|
|
defer plog.Setup()()
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
plog.Always("Running supervisor",
|
|
|
|
"user-agent", rest.DefaultKubernetesUserAgent(),
|
|
|
|
"version", versionInfo(version.Get()),
|
|
|
|
"arguments", os.Args,
|
|
|
|
)
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
ctx := signalCtx()
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
// Read the server config file.
|
2022-04-16 02:43:53 +00:00
|
|
|
cfg, err := supervisor.FromPath(ctx, os.Args[2])
|
2020-10-15 19:40:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-16 02:43:53 +00:00
|
|
|
return runSupervisor(ctx, podInfo, cfg)
|
2021-08-29 00:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Main() {
|
|
|
|
if err := main(); err != nil {
|
2022-04-16 02:43:53 +00:00
|
|
|
plog.Fatal(err)
|
2020-10-06 00:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-16 02:43:53 +00:00
|
|
|
|
|
|
|
type versionInfo apimachineryversion.Info // hide .String() method from plog
|