2023-08-24 14:22:42 +00:00
|
|
|
// Copyright 2020-2023 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-06-09 20:45:21 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
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"
|
2022-08-30 19:11:17 +00:00
|
|
|
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
|
2022-06-09 20:45:21 +00:00
|
|
|
genericapiserver "k8s.io/apiserver/pkg/server"
|
|
|
|
genericoptions "k8s.io/apiserver/pkg/server/options"
|
2020-10-14 13:47:34 +00:00
|
|
|
kubeinformers "k8s.io/client-go/informers"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2022-08-26 17:57:45 +00:00
|
|
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
2020-10-06 00:28:19 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2022-06-09 20:45:21 +00:00
|
|
|
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
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"
|
2022-08-26 17:57:45 +00:00
|
|
|
"go.pinniped.dev/generated/latest/client/supervisor/clientset/versioned/typed/config/v1alpha1"
|
2021-02-16 19:00:08 +00:00
|
|
|
pinnipedinformers "go.pinniped.dev/generated/latest/client/supervisor/informers/externalversions"
|
2022-08-30 19:11:17 +00:00
|
|
|
supervisoropenapi "go.pinniped.dev/generated/latest/client/supervisor/openapi"
|
2022-06-09 20:45:21 +00:00
|
|
|
"go.pinniped.dev/internal/apiserviceref"
|
2020-10-15 19:40:56 +00:00
|
|
|
"go.pinniped.dev/internal/config/supervisor"
|
2022-06-09 20:45:21 +00:00
|
|
|
"go.pinniped.dev/internal/controller/apicerts"
|
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"
|
2022-06-17 16:56:53 +00:00
|
|
|
"go.pinniped.dev/internal/controller/supervisorconfig/oidcclientwatcher"
|
2021-05-12 21:00:39 +00:00
|
|
|
"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"
|
2022-06-09 20:45:21 +00:00
|
|
|
"go.pinniped.dev/internal/dynamiccert"
|
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"
|
2023-08-28 16:54:27 +00:00
|
|
|
"go.pinniped.dev/internal/pversion"
|
2020-12-17 12:41:53 +00:00
|
|
|
"go.pinniped.dev/internal/secret"
|
2022-06-09 20:45:21 +00:00
|
|
|
"go.pinniped.dev/internal/supervisor/apiserver"
|
|
|
|
supervisorscheme "go.pinniped.dev/internal/supervisor/scheme"
|
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,
|
2022-06-09 20:45:21 +00:00
|
|
|
dynamicServingCertProvider dynamiccert.Private,
|
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,
|
2022-06-09 20:45:21 +00:00
|
|
|
aggregatorClient aggregatorclient.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,
|
2022-06-09 20:45:21 +00:00
|
|
|
podInfo *downward.PodInfo,
|
2021-08-29 00:38:50 +00:00
|
|
|
) controllerinit.RunnerBuilder {
|
2022-06-15 16:38:21 +00:00
|
|
|
const certificateName string = "pinniped-supervisor-api-tls-serving-certificate"
|
|
|
|
clientSecretSupervisorGroupData := groupsuffix.SupervisorAggregatedGroups(*cfg.APIGroupSuffix)
|
2020-12-17 21:49:53 +00:00
|
|
|
federationDomainInformer := pinnipedInformers.Config().V1alpha1().FederationDomains()
|
2022-06-17 16:56:53 +00:00
|
|
|
oidcClientInformer := pinnipedInformers.Config().V1alpha1().OIDCClients()
|
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,
|
2023-05-08 21:07:38 +00:00
|
|
|
pinnipedInformers.IDP().V1alpha1().OIDCIdentityProviders(),
|
|
|
|
pinnipedInformers.IDP().V1alpha1().LDAPIdentityProviders(),
|
|
|
|
pinnipedInformers.IDP().V1alpha1().ActiveDirectoryIdentityProviders(),
|
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,
|
2023-06-13 19:26:59 +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,
|
|
|
|
),
|
2022-06-09 20:45:21 +00:00
|
|
|
singletonWorker).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsManagerController(
|
|
|
|
podInfo.Namespace,
|
2022-06-15 16:38:21 +00:00
|
|
|
certificateName,
|
2022-06-09 20:45:21 +00:00
|
|
|
cfg.Labels,
|
|
|
|
kubeClient,
|
|
|
|
secretInformer,
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
controllerlib.WithInitialEvent,
|
2022-06-15 16:38:21 +00:00
|
|
|
365*24*time.Hour, // about one year
|
|
|
|
"Pinniped Supervisor Aggregation CA",
|
2022-06-09 20:45:21 +00:00
|
|
|
cfg.NamesConfig.APIService,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewAPIServiceUpdaterController(
|
|
|
|
podInfo.Namespace,
|
2022-06-15 16:38:21 +00:00
|
|
|
certificateName,
|
|
|
|
clientSecretSupervisorGroupData.APIServiceName(),
|
2022-06-09 20:45:21 +00:00
|
|
|
aggregatorClient,
|
|
|
|
secretInformer,
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsObserverController(
|
|
|
|
podInfo.Namespace,
|
2022-06-15 16:38:21 +00:00
|
|
|
certificateName,
|
2022-06-09 20:45:21 +00:00
|
|
|
dynamicServingCertProvider,
|
|
|
|
secretInformer,
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsExpirerController(
|
|
|
|
podInfo.Namespace,
|
2022-06-15 16:38:21 +00:00
|
|
|
certificateName,
|
2022-06-09 20:45:21 +00:00
|
|
|
kubeClient,
|
|
|
|
secretInformer,
|
|
|
|
controllerlib.WithInformer,
|
2022-06-15 16:38:21 +00:00
|
|
|
9*30*24*time.Hour, // about 9 months
|
2022-06-09 20:45:21 +00:00
|
|
|
apicerts.TLSCertificateChainSecretKey,
|
|
|
|
plog.New(),
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2022-06-17 16:56:53 +00:00
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
oidcclientwatcher.NewOIDCClientWatcherController(
|
|
|
|
pinnipedClient,
|
|
|
|
secretInformer,
|
|
|
|
oidcClientInformer,
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
2022-06-09 20:45:21 +00:00
|
|
|
)
|
2020-10-06 00:28:19 +00:00
|
|
|
|
2021-08-29 00:38:50 +00:00
|
|
|
return controllerinit.Prepare(controllerManager.Start, leaderElector, kubeInformers, pinnipedInformers)
|
|
|
|
}
|
|
|
|
|
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
|
2022-06-15 16:38:21 +00:00
|
|
|
clientSecretSupervisorGroupData := groupsuffix.SupervisorAggregatedGroups(*cfg.APIGroupSuffix)
|
2022-06-09 20:45:21 +00:00
|
|
|
|
2022-06-15 16:38:21 +00:00
|
|
|
apiServiceRef, err := apiserviceref.New(clientSecretSupervisorGroupData.APIServiceName())
|
2022-06-09 20:45:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create API service ref: %w", err)
|
|
|
|
}
|
2020-12-11 16:11:49 +00:00
|
|
|
|
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,
|
2022-06-09 20:45:21 +00:00
|
|
|
apiServiceRef,
|
2021-01-13 01:27:41 +00:00
|
|
|
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"))
|
|
|
|
}))
|
|
|
|
|
2022-06-09 20:45:21 +00:00
|
|
|
dynamicServingCertProvider := dynamiccert.NewServingCert("supervisor-serving-cert")
|
|
|
|
|
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
|
2022-07-22 22:19:19 +00:00
|
|
|
client.PinnipedSupervisor.ConfigV1alpha1().OIDCClients(serverInstallationNamespace),
|
2020-12-03 01:39:45 +00:00
|
|
|
)
|
2020-10-27 00:03:26 +00:00
|
|
|
|
2022-06-15 16:38:21 +00:00
|
|
|
// Get the "real" name of the client secret supervisor API group (i.e., the API group name with the
|
2022-06-09 20:45:21 +00:00
|
|
|
// injected suffix).
|
2022-06-15 16:38:21 +00:00
|
|
|
scheme, clientSecretGV := supervisorscheme.New(*cfg.APIGroupSuffix)
|
2022-06-09 20:45:21 +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,
|
2022-06-09 20:45:21 +00:00
|
|
|
dynamicServingCertProvider,
|
2020-12-11 16:11:49 +00:00
|
|
|
&secretCache,
|
|
|
|
supervisorDeployment,
|
2021-01-05 22:07:33 +00:00
|
|
|
client.Kubernetes,
|
|
|
|
client.PinnipedSupervisor,
|
2022-06-09 20:45:21 +00:00
|
|
|
client.Aggregation,
|
2020-10-27 00:03:26 +00:00
|
|
|
kubeInformers,
|
|
|
|
pinnipedInformers,
|
2021-08-18 04:14:38 +00:00
|
|
|
leaderElector,
|
2022-06-09 20:45:21 +00:00
|
|
|
podInfo,
|
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{}
|
|
|
|
|
2022-06-09 20:45:21 +00:00
|
|
|
// Get the aggregated API server config.
|
|
|
|
aggregatedAPIServerConfig, err := getAggregatedAPIServerConfig(
|
|
|
|
dynamicServingCertProvider,
|
|
|
|
buildControllersFunc,
|
|
|
|
*cfg.APIGroupSuffix,
|
2022-06-15 16:38:21 +00:00
|
|
|
*cfg.AggregatedAPIServerPort,
|
2022-06-09 20:45:21 +00:00
|
|
|
scheme,
|
2022-06-15 16:38:21 +00:00
|
|
|
clientSecretGV,
|
2022-08-26 17:57:45 +00:00
|
|
|
clientWithoutLeaderElection.Kubernetes.CoreV1().Secrets(serverInstallationNamespace),
|
|
|
|
client.PinnipedSupervisor.ConfigV1alpha1().OIDCClients(serverInstallationNamespace),
|
|
|
|
serverInstallationNamespace,
|
2022-06-09 20:45:21 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not configure aggregated API server: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete the aggregated API server config and make a server instance.
|
|
|
|
server, err := aggregatedAPIServerConfig.Complete().New()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create aggregated API server: %w", err)
|
2021-08-29 00:38:50 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2023-09-08 16:22:10 +00:00
|
|
|
foundServerNameCert := cert != nil
|
2021-12-15 20:48:55 +00:00
|
|
|
|
|
|
|
defaultCert := dynamicTLSCertProvider.GetDefaultTLSCert()
|
|
|
|
|
2023-09-08 16:22:10 +00:00
|
|
|
if !foundServerNameCert {
|
2021-12-15 20:48:55 +00:00
|
|
|
cert = defaultCert
|
|
|
|
}
|
|
|
|
|
2023-09-08 16:22:10 +00:00
|
|
|
// If we still don't have a cert for the request at this point, then using the bootstrapping cert,
|
|
|
|
// but in that case also set the request to fail unless it is a health check request.
|
|
|
|
usingBootstrapCert := false
|
2021-12-15 20:48:55 +00:00
|
|
|
if cert == nil {
|
2023-09-08 16:22:10 +00:00
|
|
|
usingBootstrapCert = true
|
2021-12-15 20:48:55 +00:00
|
|
|
setIsBootstrapConn(info.Context()) // make this connection only work for bootstrap requests
|
|
|
|
cert = bootstrapCert
|
|
|
|
}
|
|
|
|
|
2023-09-08 16:22:10 +00:00
|
|
|
// Emit logs visible at a higher level of logging than the default. Using Info level so the user
|
|
|
|
// can safely configure a production Supervisor to show this message if they choose.
|
|
|
|
plog.Info("choosing TLS cert for incoming request",
|
|
|
|
"requestSNIServerName", info.ServerName,
|
|
|
|
"foundCertForSNIServerNameFromFederationDomain", foundServerNameCert,
|
|
|
|
"foundDefaultCertFromSecret", defaultCert != nil,
|
|
|
|
"defaultCertSecretName", cfg.NamesConfig.DefaultTLSCertificateSecret,
|
|
|
|
"servingBootstrapHealthzCert", usingBootstrapCert,
|
|
|
|
"requestLocalAddr", info.Conn.LocalAddr().String(),
|
|
|
|
"requestRemoteAddr", info.Conn.RemoteAddr().String(),
|
|
|
|
)
|
|
|
|
|
2021-12-15 20:48:55 +00:00
|
|
|
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
|
|
|
|
2022-06-09 20:45:21 +00:00
|
|
|
// Run the server. Its post-start hook will start the controllers.
|
|
|
|
err = server.GenericAPIServer.PrepareRun().Run(ctx.Done())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-29 00:38:50 +00:00
|
|
|
shutdown.Wait()
|
2020-10-06 00:28:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:45:21 +00:00
|
|
|
func getAggregatedAPIServerConfig(
|
|
|
|
dynamicCertProvider dynamiccert.Private,
|
|
|
|
buildControllers controllerinit.RunnerBuilder,
|
|
|
|
apiGroupSuffix string,
|
|
|
|
aggregatedAPIServerPort int64,
|
|
|
|
scheme *runtime.Scheme,
|
2022-06-15 16:38:21 +00:00
|
|
|
clientSecretSupervisorGroupVersion schema.GroupVersion,
|
2022-08-26 17:57:45 +00:00
|
|
|
secrets corev1client.SecretInterface,
|
|
|
|
oidcClients v1alpha1.OIDCClientInterface,
|
|
|
|
serverInstallationNamespace string,
|
2022-06-09 20:45:21 +00:00
|
|
|
) (*apiserver.Config, error) {
|
|
|
|
codecs := serializer.NewCodecFactory(scheme)
|
|
|
|
|
|
|
|
// this is unused for now but it is a safe value that we could use in the future
|
2022-06-15 16:38:21 +00:00
|
|
|
defaultEtcdPathPrefix := fmt.Sprintf("/pinniped-supervisor-registry/%s", apiGroupSuffix)
|
2022-06-09 20:45:21 +00:00
|
|
|
|
|
|
|
recommendedOptions := genericoptions.NewRecommendedOptions(
|
|
|
|
defaultEtcdPathPrefix,
|
2022-06-15 16:38:21 +00:00
|
|
|
codecs.LegacyCodec(clientSecretSupervisorGroupVersion),
|
2022-06-09 20:45:21 +00:00
|
|
|
)
|
|
|
|
recommendedOptions.Etcd = nil // turn off etcd storage because we don't need it yet
|
|
|
|
recommendedOptions.SecureServing.ServerCert.GeneratedCert = dynamicCertProvider
|
|
|
|
|
|
|
|
// This port is configurable. It should be safe to cast because the config reader already validated it.
|
|
|
|
recommendedOptions.SecureServing.BindPort = int(aggregatedAPIServerPort)
|
|
|
|
|
|
|
|
// secure TLS for connections coming from and going to the Kube API server
|
|
|
|
// this is best effort because not all options provide the right hooks to override TLS config
|
|
|
|
// since our only client is the Kube API server, this uses the most secure TLS config
|
|
|
|
if err := ptls.SecureRecommendedOptions(recommendedOptions, kubeclient.Secure); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to secure recommended options: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverConfig := genericapiserver.NewRecommendedConfig(codecs)
|
2022-08-30 19:11:17 +00:00
|
|
|
// Add the generated openapi docs to the server config. Publishing openapi docs allows
|
|
|
|
// `kubectl explain` to work for the Supervisor's aggregated API resources.
|
|
|
|
serverConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(
|
|
|
|
supervisoropenapi.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(scheme))
|
2023-08-24 18:08:15 +00:00
|
|
|
// serverConfig.OpenAPIConfig.Info.InfoProps.Title = "Pinniped Supervisor"
|
2023-08-24 14:22:42 +00:00
|
|
|
serverConfig.OpenAPIV3Config = genericapiserver.DefaultOpenAPIV3Config(
|
|
|
|
supervisoropenapi.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(scheme))
|
2023-08-24 18:08:15 +00:00
|
|
|
// serverConfig.OpenAPIV3Config.Info.InfoProps.Title = "Pinniped Supervisor"
|
2022-06-09 20:45:21 +00:00
|
|
|
// Note that among other things, this ApplyTo() function copies
|
|
|
|
// `recommendedOptions.SecureServing.ServerCert.GeneratedCert` into
|
|
|
|
// `serverConfig.SecureServing.Cert` thus making `dynamicCertProvider`
|
|
|
|
// the cert provider for the running server. The provider will be called
|
|
|
|
// by the API machinery periodically. When the provider returns nil certs,
|
|
|
|
// the API server will return "the server is currently unable to
|
|
|
|
// handle the request" error responses for all incoming requests.
|
|
|
|
// If the provider later starts returning certs, then the API server
|
|
|
|
// will use them to handle the incoming requests successfully.
|
|
|
|
if err := recommendedOptions.ApplyTo(serverConfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to apply recommended options: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiServerConfig := &apiserver.Config{
|
|
|
|
GenericConfig: serverConfig,
|
|
|
|
ExtraConfig: apiserver.ExtraConfig{
|
|
|
|
BuildControllersPostStartHook: buildControllers,
|
|
|
|
Scheme: scheme,
|
|
|
|
NegotiatedSerializer: codecs,
|
2022-06-15 16:38:21 +00:00
|
|
|
ClientSecretSupervisorGroupVersion: clientSecretSupervisorGroupVersion,
|
2022-08-26 17:57:45 +00:00
|
|
|
Secrets: secrets,
|
|
|
|
OIDCClients: oidcClients,
|
|
|
|
Namespace: serverInstallationNamespace,
|
2022-06-09 20:45:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return apiServerConfig, 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(),
|
2023-08-28 16:54:27 +00:00
|
|
|
"version", versionInfo(pversion.Get()),
|
2022-04-16 02:43:53 +00:00
|
|
|
"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
|