2021-02-12 01:22:47 +00:00
|
|
|
// Copyright 2021 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package impersonatorconfig
|
|
|
|
|
|
|
|
import (
|
2021-02-16 23:57:02 +00:00
|
|
|
"context"
|
2021-02-12 01:22:47 +00:00
|
|
|
"crypto/tls"
|
2021-02-25 00:03:17 +00:00
|
|
|
"crypto/x509"
|
2021-03-03 00:51:35 +00:00
|
|
|
"encoding/base64"
|
2021-02-25 00:03:17 +00:00
|
|
|
"encoding/pem"
|
2021-02-12 01:22:47 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-02-26 23:01:38 +00:00
|
|
|
"strings"
|
2021-02-12 01:22:47 +00:00
|
|
|
"time"
|
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
"github.com/go-logr/logr"
|
2021-02-16 23:57:02 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2021-05-26 20:03:04 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/equality"
|
2021-02-12 01:22:47 +00:00
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
2021-02-16 23:57:02 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2021-03-02 22:48:58 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
2021-03-10 18:30:06 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/errors"
|
2021-05-20 17:26:07 +00:00
|
|
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
2021-02-16 23:57:02 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2021-03-18 14:28:16 +00:00
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
2021-03-10 18:30:06 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2021-05-19 16:40:32 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
2021-02-12 01:22:47 +00:00
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2021-05-26 17:42:50 +00:00
|
|
|
"k8s.io/klog/v2"
|
2021-02-12 01:22:47 +00:00
|
|
|
|
2021-03-02 22:48:58 +00:00
|
|
|
"go.pinniped.dev/generated/latest/apis/concierge/config/v1alpha1"
|
|
|
|
pinnipedclientset "go.pinniped.dev/generated/latest/client/concierge/clientset/versioned"
|
2021-05-17 22:08:05 +00:00
|
|
|
conciergeconfiginformers "go.pinniped.dev/generated/latest/client/concierge/informers/externalversions/config/v1alpha1"
|
2021-02-12 01:22:47 +00:00
|
|
|
"go.pinniped.dev/internal/certauthority"
|
|
|
|
"go.pinniped.dev/internal/clusterhost"
|
|
|
|
"go.pinniped.dev/internal/concierge/impersonator"
|
2021-03-10 18:30:06 +00:00
|
|
|
"go.pinniped.dev/internal/constable"
|
2021-02-12 01:22:47 +00:00
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
2021-03-10 18:30:06 +00:00
|
|
|
"go.pinniped.dev/internal/controller/apicerts"
|
2021-03-02 22:48:58 +00:00
|
|
|
"go.pinniped.dev/internal/controller/issuerconfig"
|
2021-02-12 01:22:47 +00:00
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2021-03-10 18:30:06 +00:00
|
|
|
"go.pinniped.dev/internal/dynamiccert"
|
2021-05-25 22:44:25 +00:00
|
|
|
"go.pinniped.dev/internal/endpointaddr"
|
2021-02-12 01:22:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-13 00:09:16 +00:00
|
|
|
impersonationProxyPort = 8444
|
|
|
|
defaultHTTPSPort = 443
|
|
|
|
approximatelyOneHundredYears = 100 * 365 * 24 * time.Hour
|
|
|
|
caCommonName = "Pinniped Impersonation Proxy CA"
|
|
|
|
caCrtKey = "ca.crt"
|
|
|
|
caKeyKey = "ca.key"
|
|
|
|
appLabelKey = "app"
|
2021-02-12 01:22:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type impersonatorConfigController struct {
|
|
|
|
namespace string
|
2021-03-02 22:48:58 +00:00
|
|
|
credentialIssuerResourceName string
|
2021-02-12 01:22:47 +00:00
|
|
|
generatedLoadBalancerServiceName string
|
2021-05-20 00:00:28 +00:00
|
|
|
generatedClusterIPServiceName string
|
2021-02-24 18:56:24 +00:00
|
|
|
tlsSecretName string
|
2021-03-02 01:02:08 +00:00
|
|
|
caSecretName string
|
2021-03-10 18:30:06 +00:00
|
|
|
impersonationSignerSecretName string
|
2021-03-02 22:48:58 +00:00
|
|
|
|
|
|
|
k8sClient kubernetes.Interface
|
|
|
|
pinnipedAPIClient pinnipedclientset.Interface
|
|
|
|
|
2021-05-17 22:08:05 +00:00
|
|
|
credIssuerInformer conciergeconfiginformers.CredentialIssuerInformer
|
2021-03-02 22:48:58 +00:00
|
|
|
servicesInformer corev1informers.ServiceInformer
|
|
|
|
secretsInformer corev1informers.SecretInformer
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
labels map[string]string
|
|
|
|
clock clock.Clock
|
|
|
|
impersonationSigningCertProvider dynamiccert.Provider
|
|
|
|
impersonatorFunc impersonator.FactoryFunc
|
2021-02-12 01:22:47 +00:00
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
hasControlPlaneNodes *bool
|
|
|
|
serverStopCh chan struct{}
|
|
|
|
errorCh chan error
|
2021-03-15 16:24:07 +00:00
|
|
|
tlsServingCertDynamicCertProvider dynamiccert.Private
|
2021-05-26 17:42:50 +00:00
|
|
|
infoLog logr.Logger
|
|
|
|
debugLog logr.Logger
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImpersonatorConfigController(
|
|
|
|
namespace string,
|
2021-03-02 22:48:58 +00:00
|
|
|
credentialIssuerResourceName string,
|
2021-02-12 01:22:47 +00:00
|
|
|
k8sClient kubernetes.Interface,
|
2021-03-02 22:48:58 +00:00
|
|
|
pinnipedAPIClient pinnipedclientset.Interface,
|
2021-05-17 22:08:05 +00:00
|
|
|
credentialIssuerInformer conciergeconfiginformers.CredentialIssuerInformer,
|
2021-02-18 01:22:13 +00:00
|
|
|
servicesInformer corev1informers.ServiceInformer,
|
2021-02-24 18:56:24 +00:00
|
|
|
secretsInformer corev1informers.SecretInformer,
|
2021-02-12 01:22:47 +00:00
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
generatedLoadBalancerServiceName string,
|
2021-05-20 00:00:28 +00:00
|
|
|
generatedClusterIPServiceName string,
|
2021-02-24 18:56:24 +00:00
|
|
|
tlsSecretName string,
|
2021-03-02 01:02:08 +00:00
|
|
|
caSecretName string,
|
2021-02-16 23:57:02 +00:00
|
|
|
labels map[string]string,
|
2021-03-02 22:48:58 +00:00
|
|
|
clock clock.Clock,
|
2021-03-10 18:30:06 +00:00
|
|
|
impersonatorFunc impersonator.FactoryFunc,
|
|
|
|
impersonationSignerSecretName string,
|
|
|
|
impersonationSigningCertProvider dynamiccert.Provider,
|
2021-05-26 17:42:50 +00:00
|
|
|
log logr.Logger,
|
2021-02-12 01:22:47 +00:00
|
|
|
) controllerlib.Controller {
|
2021-03-10 18:30:06 +00:00
|
|
|
secretNames := sets.NewString(tlsSecretName, caSecretName, impersonationSignerSecretName)
|
2021-05-26 17:42:50 +00:00
|
|
|
log = log.WithName("impersonator-config-controller")
|
2021-02-12 01:22:47 +00:00
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
|
|
|
Name: "impersonator-config-controller",
|
|
|
|
Syncer: &impersonatorConfigController{
|
2021-03-10 18:30:06 +00:00
|
|
|
namespace: namespace,
|
|
|
|
credentialIssuerResourceName: credentialIssuerResourceName,
|
|
|
|
generatedLoadBalancerServiceName: generatedLoadBalancerServiceName,
|
2021-05-20 00:00:28 +00:00
|
|
|
generatedClusterIPServiceName: generatedClusterIPServiceName,
|
2021-03-10 18:30:06 +00:00
|
|
|
tlsSecretName: tlsSecretName,
|
|
|
|
caSecretName: caSecretName,
|
|
|
|
impersonationSignerSecretName: impersonationSignerSecretName,
|
|
|
|
k8sClient: k8sClient,
|
|
|
|
pinnipedAPIClient: pinnipedAPIClient,
|
2021-05-17 22:08:05 +00:00
|
|
|
credIssuerInformer: credentialIssuerInformer,
|
2021-03-10 18:30:06 +00:00
|
|
|
servicesInformer: servicesInformer,
|
|
|
|
secretsInformer: secretsInformer,
|
|
|
|
labels: labels,
|
|
|
|
clock: clock,
|
|
|
|
impersonationSigningCertProvider: impersonationSigningCertProvider,
|
|
|
|
impersonatorFunc: impersonatorFunc,
|
2021-03-15 16:24:07 +00:00
|
|
|
tlsServingCertDynamicCertProvider: dynamiccert.NewServingCert("impersonation-proxy-serving-cert"),
|
2021-05-26 21:47:02 +00:00
|
|
|
infoLog: log.V(2),
|
|
|
|
debugLog: log.V(4),
|
2021-02-12 01:22:47 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-17 22:08:05 +00:00
|
|
|
withInformer(credentialIssuerInformer,
|
|
|
|
pinnipedcontroller.SimpleFilterWithSingletonQueue(func(obj metav1.Object) bool {
|
|
|
|
return obj.GetName() == credentialIssuerResourceName
|
|
|
|
}),
|
2021-02-12 01:22:47 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
2021-02-18 01:22:13 +00:00
|
|
|
withInformer(
|
|
|
|
servicesInformer,
|
2021-05-26 17:54:40 +00:00
|
|
|
pinnipedcontroller.SimpleFilterWithSingletonQueue(func(obj metav1.Object) bool {
|
|
|
|
return obj.GetNamespace() == namespace && obj.GetName() == generatedLoadBalancerServiceName
|
|
|
|
}),
|
2021-02-18 01:22:13 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
2021-02-24 18:56:24 +00:00
|
|
|
withInformer(
|
|
|
|
secretsInformer,
|
2021-05-26 17:54:40 +00:00
|
|
|
pinnipedcontroller.SimpleFilterWithSingletonQueue(func(obj metav1.Object) bool {
|
2021-03-10 18:30:06 +00:00
|
|
|
return obj.GetNamespace() == namespace && secretNames.Has(obj.GetName())
|
2021-05-26 17:54:40 +00:00
|
|
|
}),
|
2021-02-24 18:56:24 +00:00
|
|
|
controllerlib.InformerOption{},
|
|
|
|
),
|
2021-02-12 01:22:47 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-02 01:02:08 +00:00
|
|
|
func (c *impersonatorConfigController) Sync(syncCtx controllerlib.Context) error {
|
2021-05-26 17:42:50 +00:00
|
|
|
c.debugLog.Info("starting impersonatorConfigController Sync")
|
2021-02-12 01:22:47 +00:00
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
// Load the CredentialIssuer that we'll update with status.
|
|
|
|
credIssuer, err := c.credIssuerInformer.Lister().Get(c.credentialIssuerResourceName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get CredentialIssuer to update: %w", err)
|
|
|
|
}
|
2021-03-02 22:48:58 +00:00
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
strategy, err := c.doSync(syncCtx, credIssuer)
|
2021-03-02 22:48:58 +00:00
|
|
|
if err != nil {
|
|
|
|
strategy = &v1alpha1.CredentialIssuerStrategy{
|
|
|
|
Type: v1alpha1.ImpersonationProxyStrategyType,
|
|
|
|
Status: v1alpha1.ErrorStrategyStatus,
|
2021-05-27 16:13:10 +00:00
|
|
|
Reason: strategyReasonForError(err),
|
2021-03-02 22:48:58 +00:00
|
|
|
Message: err.Error(),
|
|
|
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
|
|
|
}
|
2021-03-10 18:30:06 +00:00
|
|
|
// The impersonator is not ready, so clear the signer CA from the dynamic provider.
|
|
|
|
c.clearSignerCA()
|
2021-03-02 22:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
err = utilerrors.NewAggregate([]error{err, issuerconfig.Update(
|
|
|
|
syncCtx.Context,
|
|
|
|
c.pinnipedAPIClient,
|
|
|
|
credIssuer,
|
|
|
|
*strategy,
|
|
|
|
)})
|
2021-03-02 22:48:58 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2021-05-26 17:42:50 +00:00
|
|
|
c.debugLog.Info("successfully finished impersonatorConfigController Sync")
|
2021-03-02 22:48:58 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-27 16:13:10 +00:00
|
|
|
// strategyReasonForError returns the proper v1alpha1.StrategyReason for a sync error. Some errors are occasionally
|
|
|
|
// expected because there are multiple pods running, in these cases we should report a Pending reason and we'll
|
|
|
|
// recover on a following sync.
|
|
|
|
func strategyReasonForError(err error) v1alpha1.StrategyReason {
|
|
|
|
switch {
|
|
|
|
case k8serrors.IsConflict(err), k8serrors.IsAlreadyExists(err):
|
|
|
|
return v1alpha1.PendingStrategyReason
|
|
|
|
default:
|
|
|
|
return v1alpha1.ErrorDuringSetupStrategyReason
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
type certNameInfo struct {
|
|
|
|
// ready will be true when the certificate name information is known.
|
|
|
|
// ready will be false when it is pending because we are waiting for a load balancer to get assigned an ip/hostname.
|
|
|
|
// When false, the other fields in this struct should not be considered meaningful and may be zero values.
|
|
|
|
ready bool
|
|
|
|
|
|
|
|
// The IP address or hostname which was selected to be used as the name in the cert.
|
|
|
|
// Either selectedIP or selectedHostname will be set, but not both.
|
2021-05-26 00:01:42 +00:00
|
|
|
selectedIPs []net.IP
|
2021-03-03 17:22:35 +00:00
|
|
|
selectedHostname string
|
|
|
|
|
|
|
|
// The name of the endpoint to which a client should connect to talk to the impersonator.
|
|
|
|
// This may be a hostname or an IP, and may include a port number.
|
|
|
|
clientEndpoint string
|
|
|
|
}
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
func (c *impersonatorConfigController) doSync(syncCtx controllerlib.Context, credIssuer *v1alpha1.CredentialIssuer) (*v1alpha1.CredentialIssuerStrategy, error) {
|
2021-03-10 18:30:06 +00:00
|
|
|
ctx := syncCtx.Context
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
impersonationSpec, err := c.loadImpersonationProxyConfiguration(credIssuer)
|
2021-03-02 01:02:08 +00:00
|
|
|
if err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a live API call to avoid the cost of having an informer watch all node changes on the cluster,
|
|
|
|
// since there could be lots and we don't especially care about node changes.
|
|
|
|
// Once we have concluded that there is or is not a visible control plane, then cache that decision
|
|
|
|
// to avoid listing nodes very often.
|
|
|
|
if c.hasControlPlaneNodes == nil {
|
2021-03-02 01:02:08 +00:00
|
|
|
hasControlPlaneNodes, err := clusterhost.New(c.k8sClient).HasControlPlaneNodes(ctx)
|
2021-02-12 01:22:47 +00:00
|
|
|
if err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
c.hasControlPlaneNodes = &hasControlPlaneNodes
|
2021-05-26 17:42:50 +00:00
|
|
|
c.debugLog.Info("queried for control plane nodes", "foundControlPlaneNodes", hasControlPlaneNodes)
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
if c.shouldHaveImpersonator(impersonationSpec) {
|
2021-03-10 18:30:06 +00:00
|
|
|
if err = c.ensureImpersonatorIsStarted(syncCtx); err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-10 18:30:06 +00:00
|
|
|
if err = c.ensureImpersonatorIsStopped(true); err != nil {
|
2021-05-26 20:03:04 +00:00
|
|
|
return nil, err
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
if c.shouldHaveLoadBalancer(impersonationSpec) {
|
|
|
|
if err = c.ensureLoadBalancerIsStarted(ctx, impersonationSpec); err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-16 23:57:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-02 01:02:08 +00:00
|
|
|
if err = c.ensureLoadBalancerIsStopped(ctx); err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-16 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 01:22:47 +00:00
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
if c.shouldHaveClusterIPService(impersonationSpec) {
|
|
|
|
if err = c.ensureClusterIPServiceIsStarted(ctx, impersonationSpec); err != nil {
|
2021-05-20 00:00:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-20 18:57:07 +00:00
|
|
|
} else {
|
|
|
|
if err = c.ensureClusterIPServiceIsStopped(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 00:00:28 +00:00
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
nameInfo, err := c.findDesiredTLSCertificateName(impersonationSpec)
|
2021-03-03 17:22:35 +00:00
|
|
|
if err != nil {
|
2021-03-10 18:30:06 +00:00
|
|
|
// Unexpected error while determining the name that should go into the certs, so clear any existing certs.
|
2021-03-11 21:20:25 +00:00
|
|
|
c.tlsServingCertDynamicCertProvider.UnsetCertKeyContent()
|
2021-03-03 17:22:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-03 00:51:35 +00:00
|
|
|
var impersonationCA *certauthority.CA
|
2021-05-20 17:26:07 +00:00
|
|
|
if c.shouldHaveTLSSecret(impersonationSpec) {
|
2021-03-02 01:02:08 +00:00
|
|
|
if impersonationCA, err = c.ensureCASecretIsCreated(ctx); err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
2021-03-03 17:22:35 +00:00
|
|
|
if err = c.ensureTLSSecret(ctx, nameInfo, impersonationCA); err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
} else if err = c.ensureTLSSecretIsRemoved(ctx); err != nil {
|
2021-03-02 22:48:58 +00:00
|
|
|
return nil, err
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
credentialIssuerStrategyResult := c.doSyncResult(nameInfo, impersonationSpec, impersonationCA)
|
2021-03-10 18:30:06 +00:00
|
|
|
|
|
|
|
if err = c.loadSignerCA(credentialIssuerStrategyResult.Status); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return credentialIssuerStrategyResult, nil
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
func (c *impersonatorConfigController) loadImpersonationProxyConfiguration(credIssuer *v1alpha1.CredentialIssuer) (*v1alpha1.ImpersonationProxySpec, error) {
|
2021-05-19 16:40:32 +00:00
|
|
|
// Make a copy of the spec since we got this object from informer cache.
|
|
|
|
spec := credIssuer.Spec.DeepCopy().ImpersonationProxy
|
|
|
|
if spec == nil {
|
|
|
|
return nil, fmt.Errorf("could not load CredentialIssuer: spec.impersonationProxy is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default service type to LoadBalancer (this is normally already done via CRD defaulting).
|
|
|
|
if spec.Service.Type == "" {
|
|
|
|
spec.Service.Type = v1alpha1.ImpersonationProxyServiceTypeLoadBalancer
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateCredentialIssuerSpec(spec); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not load CredentialIssuer spec.impersonationProxy: %w", err)
|
2021-05-17 22:08:05 +00:00
|
|
|
}
|
2021-05-26 21:47:02 +00:00
|
|
|
c.debugLog.Info("read impersonation proxy config", "credentialIssuer", c.credentialIssuerResourceName)
|
2021-05-19 16:40:32 +00:00
|
|
|
return spec, nil
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) shouldHaveImpersonator(config *v1alpha1.ImpersonationProxySpec) bool {
|
|
|
|
return c.enabledByAutoMode(config) || config.Mode == v1alpha1.ImpersonationProxyModeEnabled
|
2021-03-02 22:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) enabledByAutoMode(config *v1alpha1.ImpersonationProxySpec) bool {
|
|
|
|
return config.Mode == v1alpha1.ImpersonationProxyModeAuto && !*c.hasControlPlaneNodes
|
2021-03-02 22:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) disabledByAutoMode(config *v1alpha1.ImpersonationProxySpec) bool {
|
|
|
|
return config.Mode == v1alpha1.ImpersonationProxyModeAuto && *c.hasControlPlaneNodes
|
2021-03-02 22:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) disabledExplicitly(config *v1alpha1.ImpersonationProxySpec) bool {
|
|
|
|
return config.Mode == v1alpha1.ImpersonationProxyModeDisabled
|
2021-02-18 01:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) shouldHaveLoadBalancer(config *v1alpha1.ImpersonationProxySpec) bool {
|
|
|
|
return c.shouldHaveImpersonator(config) && config.Service.Type == v1alpha1.ImpersonationProxyServiceTypeLoadBalancer
|
2021-02-18 01:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 00:00:28 +00:00
|
|
|
func (c *impersonatorConfigController) shouldHaveClusterIPService(config *v1alpha1.ImpersonationProxySpec) bool {
|
|
|
|
return c.shouldHaveImpersonator(config) && config.Service.Type == v1alpha1.ImpersonationProxyServiceTypeClusterIP
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) shouldHaveTLSSecret(config *v1alpha1.ImpersonationProxySpec) bool {
|
2021-02-26 01:03:34 +00:00
|
|
|
return c.shouldHaveImpersonator(config)
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:57:46 +00:00
|
|
|
func (c *impersonatorConfigController) serviceExists(serviceName string) (bool, error) {
|
|
|
|
_, err := c.servicesInformer.Lister().Services(c.namespace).Get(serviceName)
|
2021-03-02 01:02:08 +00:00
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if notFound {
|
|
|
|
return false, nil
|
2021-02-26 01:03:34 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2021-02-26 01:03:34 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true, nil
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 01:02:08 +00:00
|
|
|
func (c *impersonatorConfigController) tlsSecretExists() (bool, *v1.Secret, error) {
|
|
|
|
secret, err := c.secretsInformer.Lister().Secrets(c.namespace).Get(c.tlsSecretName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if notFound {
|
|
|
|
return false, nil, nil
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
return true, secret, nil
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
func (c *impersonatorConfigController) ensureImpersonatorIsStarted(syncCtx controllerlib.Context) error {
|
|
|
|
if c.serverStopCh != nil {
|
|
|
|
// The server was already started, but it could have died in the background, so make a non-blocking
|
|
|
|
// check to see if it has sent any errors on the errorCh.
|
|
|
|
select {
|
|
|
|
case runningErr := <-c.errorCh:
|
|
|
|
if runningErr == nil {
|
|
|
|
// The server sent a nil error, meaning that it shutdown without reporting any particular
|
|
|
|
// error for some reason. We would still like to report this as an error for logging purposes.
|
|
|
|
runningErr = constable.Error("unexpected shutdown of proxy server")
|
|
|
|
}
|
|
|
|
// The server has stopped, so finish shutting it down.
|
|
|
|
// If that fails too, return both errors for logging purposes.
|
|
|
|
// By returning an error, the sync function will be called again
|
2021-03-10 19:24:42 +00:00
|
|
|
// and we'll have a chance to restart the server.
|
2021-03-10 18:30:06 +00:00
|
|
|
close(c.errorCh) // We don't want ensureImpersonatorIsStopped to block on reading this channel.
|
|
|
|
stoppingErr := c.ensureImpersonatorIsStopped(false)
|
|
|
|
return errors.NewAggregate([]error{runningErr, stoppingErr})
|
|
|
|
default:
|
|
|
|
// Seems like it is still running, so nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-12 01:22:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("starting impersonation proxy", "port", impersonationProxyPort)
|
2021-03-10 18:30:06 +00:00
|
|
|
startImpersonatorFunc, err := c.impersonatorFunc(
|
|
|
|
impersonationProxyPort,
|
|
|
|
c.tlsServingCertDynamicCertProvider,
|
2021-03-11 21:20:25 +00:00
|
|
|
c.impersonationSigningCertProvider,
|
2021-03-10 18:30:06 +00:00
|
|
|
)
|
2021-02-12 01:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
c.serverStopCh = make(chan struct{})
|
2021-03-11 15:13:07 +00:00
|
|
|
// use a buffered channel so that startImpersonatorFunc can send
|
|
|
|
// on it without coordinating with the main controller go routine
|
|
|
|
c.errorCh = make(chan error, 1)
|
2021-02-12 01:22:47 +00:00
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
// startImpersonatorFunc will block until the server shuts down (or fails to start), so run it in the background.
|
2021-02-12 01:22:47 +00:00
|
|
|
go func() {
|
2021-03-18 14:28:16 +00:00
|
|
|
defer utilruntime.HandleCrash()
|
|
|
|
|
2021-03-11 15:13:07 +00:00
|
|
|
// The server has stopped, so enqueue ourselves for another sync,
|
|
|
|
// so we can try to start the server again as quickly as possible.
|
|
|
|
defer syncCtx.Queue.AddRateLimited(syncCtx.Key)
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
// Forward any errors returned by startImpersonatorFunc on the errorCh.
|
2021-03-11 15:13:07 +00:00
|
|
|
c.errorCh <- startImpersonatorFunc(c.serverStopCh)
|
2021-02-12 01:22:47 +00:00
|
|
|
}()
|
2021-03-10 18:30:06 +00:00
|
|
|
|
2021-02-12 01:22:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-16 23:57:02 +00:00
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
func (c *impersonatorConfigController) ensureImpersonatorIsStopped(shouldCloseErrChan bool) error {
|
|
|
|
if c.serverStopCh == nil {
|
|
|
|
return nil
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
2021-03-10 18:30:06 +00:00
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("stopping impersonation proxy", "port", impersonationProxyPort)
|
2021-03-10 18:30:06 +00:00
|
|
|
close(c.serverStopCh)
|
|
|
|
stopErr := <-c.errorCh
|
|
|
|
|
|
|
|
if shouldCloseErrChan {
|
|
|
|
close(c.errorCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.serverStopCh = nil
|
|
|
|
c.errorCh = nil
|
|
|
|
|
|
|
|
return stopErr
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 23:54:59 +00:00
|
|
|
func (c *impersonatorConfigController) ensureLoadBalancerIsStarted(ctx context.Context, config *v1alpha1.ImpersonationProxySpec) error {
|
2021-03-02 01:02:08 +00:00
|
|
|
appNameLabel := c.labels[appLabelKey]
|
2021-02-16 23:57:02 +00:00
|
|
|
loadBalancer := v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
2021-03-02 01:02:08 +00:00
|
|
|
Type: v1.ServiceTypeLoadBalancer,
|
2021-02-16 23:57:02 +00:00
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
2021-03-10 18:30:06 +00:00
|
|
|
TargetPort: intstr.FromInt(impersonationProxyPort),
|
2021-03-02 01:02:08 +00:00
|
|
|
Port: defaultHTTPSPort,
|
2021-02-16 23:57:02 +00:00
|
|
|
Protocol: v1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
},
|
2021-05-18 23:54:59 +00:00
|
|
|
LoadBalancerIP: config.Service.LoadBalancerIP,
|
|
|
|
Selector: map[string]string{appLabelKey: appNameLabel},
|
2021-02-16 23:57:02 +00:00
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2021-05-18 23:54:59 +00:00
|
|
|
Name: c.generatedLoadBalancerServiceName,
|
|
|
|
Namespace: c.namespace,
|
|
|
|
Labels: c.labels,
|
|
|
|
Annotations: config.Service.Annotations,
|
2021-02-16 23:57:02 +00:00
|
|
|
},
|
|
|
|
}
|
2021-05-26 20:03:04 +00:00
|
|
|
return c.createOrUpdateService(ctx, &loadBalancer)
|
2021-02-16 23:57:02 +00:00
|
|
|
}
|
2021-02-24 18:56:24 +00:00
|
|
|
|
2021-02-25 00:03:17 +00:00
|
|
|
func (c *impersonatorConfigController) ensureLoadBalancerIsStopped(ctx context.Context) error {
|
2021-05-21 16:57:46 +00:00
|
|
|
running, err := c.serviceExists(c.generatedLoadBalancerServiceName)
|
2021-02-24 18:56:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-25 00:03:17 +00:00
|
|
|
if !running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("deleting load balancer for impersonation proxy",
|
|
|
|
"service", klog.KRef(c.namespace, c.generatedLoadBalancerServiceName),
|
|
|
|
)
|
2021-03-02 01:02:08 +00:00
|
|
|
return c.k8sClient.CoreV1().Services(c.namespace).Delete(ctx, c.generatedLoadBalancerServiceName, metav1.DeleteOptions{})
|
|
|
|
}
|
|
|
|
|
2021-05-20 00:00:28 +00:00
|
|
|
func (c *impersonatorConfigController) ensureClusterIPServiceIsStarted(ctx context.Context, config *v1alpha1.ImpersonationProxySpec) error {
|
|
|
|
appNameLabel := c.labels[appLabelKey]
|
|
|
|
clusterIP := v1.Service{
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Type: v1.ServiceTypeClusterIP,
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
TargetPort: intstr.FromInt(impersonationProxyPort),
|
|
|
|
Port: defaultHTTPSPort,
|
|
|
|
Protocol: v1.ProtocolTCP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Selector: map[string]string{appLabelKey: appNameLabel},
|
|
|
|
},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: c.generatedClusterIPServiceName,
|
|
|
|
Namespace: c.namespace,
|
|
|
|
Labels: c.labels,
|
|
|
|
Annotations: config.Service.Annotations,
|
|
|
|
},
|
|
|
|
}
|
2021-05-26 20:03:04 +00:00
|
|
|
return c.createOrUpdateService(ctx, &clusterIP)
|
2021-05-21 16:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *impersonatorConfigController) ensureClusterIPServiceIsStopped(ctx context.Context) error {
|
|
|
|
running, err := c.serviceExists(c.generatedClusterIPServiceName)
|
2021-05-20 23:21:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-21 16:57:46 +00:00
|
|
|
if !running {
|
2021-05-20 18:57:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-21 16:57:46 +00:00
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("deleting cluster ip for impersonation proxy",
|
|
|
|
"service", klog.KRef(c.namespace, c.generatedClusterIPServiceName),
|
|
|
|
)
|
2021-05-21 16:57:46 +00:00
|
|
|
return c.k8sClient.CoreV1().Services(c.namespace).Delete(ctx, c.generatedClusterIPServiceName, metav1.DeleteOptions{})
|
2021-05-20 00:00:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 20:03:04 +00:00
|
|
|
func (c *impersonatorConfigController) createOrUpdateService(ctx context.Context, service *v1.Service) error {
|
|
|
|
log := c.infoLog.WithValues("serviceType", service.Spec.Type, "service", klog.KObj(service))
|
|
|
|
existing, err := c.servicesInformer.Lister().Services(c.namespace).Get(service.Name)
|
|
|
|
if k8serrors.IsNotFound(err) {
|
|
|
|
log.Info("creating service for impersonation proxy")
|
|
|
|
_, err := c.k8sClient.CoreV1().Services(c.namespace).Create(ctx, service, metav1.CreateOptions{})
|
|
|
|
return err
|
|
|
|
}
|
2021-05-20 18:57:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-26 20:03:04 +00:00
|
|
|
|
|
|
|
// Update only the specific fields that are meaningfully part of our desired state.
|
|
|
|
updated := existing.DeepCopy()
|
|
|
|
updated.ObjectMeta.Labels = service.ObjectMeta.Labels
|
|
|
|
updated.ObjectMeta.Annotations = service.ObjectMeta.Annotations
|
|
|
|
updated.Spec.LoadBalancerIP = service.Spec.LoadBalancerIP
|
|
|
|
updated.Spec.Type = service.Spec.Type
|
|
|
|
updated.Spec.Selector = service.Spec.Selector
|
|
|
|
updated.Spec.Ports = service.Spec.Ports
|
|
|
|
|
|
|
|
// If our updates didn't change anything, we're done.
|
|
|
|
if equality.Semantic.DeepEqual(existing, updated) {
|
2021-05-20 18:57:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 20:03:04 +00:00
|
|
|
|
|
|
|
// Otherwise apply the updates.
|
|
|
|
c.infoLog.Info("updating service for impersonation proxy")
|
|
|
|
_, err = c.k8sClient.CoreV1().Services(c.namespace).Update(ctx, updated, metav1.UpdateOptions{})
|
2021-05-21 16:57:46 +00:00
|
|
|
return err
|
2021-05-20 18:57:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
func (c *impersonatorConfigController) ensureTLSSecret(ctx context.Context, nameInfo *certNameInfo, ca *certauthority.CA) error {
|
2021-03-02 01:02:08 +00:00
|
|
|
secretFromInformer, err := c.secretsInformer.Lister().Secrets(c.namespace).Get(c.tlsSecretName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if !notFound && err != nil {
|
2021-03-03 17:22:35 +00:00
|
|
|
return err
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 01:02:08 +00:00
|
|
|
if !notFound {
|
2021-03-03 17:22:35 +00:00
|
|
|
secretWasDeleted, err := c.deleteTLSSecretWhenCertificateDoesNotMatchDesiredState(ctx, nameInfo, ca, secretFromInformer)
|
2021-03-02 01:02:08 +00:00
|
|
|
if err != nil {
|
2021-03-03 17:22:35 +00:00
|
|
|
return err
|
2021-03-02 01:02:08 +00:00
|
|
|
}
|
|
|
|
// If it was deleted by the above call, then set it to nil. This allows us to avoid waiting
|
|
|
|
// for the informer cache to update before deciding to proceed to create the new Secret below.
|
|
|
|
if secretWasDeleted {
|
|
|
|
secretFromInformer = nil
|
|
|
|
}
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
return c.ensureTLSSecretIsCreatedAndLoaded(ctx, nameInfo, secretFromInformer, ca)
|
2021-03-02 01:02:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
func (c *impersonatorConfigController) deleteTLSSecretWhenCertificateDoesNotMatchDesiredState(ctx context.Context, nameInfo *certNameInfo, ca *certauthority.CA, secret *v1.Secret) (bool, error) {
|
2021-02-25 00:03:17 +00:00
|
|
|
certPEM := secret.Data[v1.TLSCertKey]
|
|
|
|
block, _ := pem.Decode(certPEM)
|
|
|
|
if block == nil {
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("found missing or not PEM-encoded data in TLS Secret",
|
2021-03-02 01:02:08 +00:00
|
|
|
"invalidCertPEM", string(certPEM),
|
2021-05-26 17:42:50 +00:00
|
|
|
"secret", klog.KObj(secret),
|
|
|
|
)
|
2021-02-26 18:58:56 +00:00
|
|
|
deleteErr := c.ensureTLSSecretIsRemoved(ctx)
|
|
|
|
if deleteErr != nil {
|
2021-03-02 01:02:08 +00:00
|
|
|
return false, fmt.Errorf("found missing or not PEM-encoded data in TLS Secret, but got error while deleting it: %w", deleteErr)
|
2021-02-26 18:58:56 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true, nil
|
2021-02-26 18:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actualCertFromSecret, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Error(err, "found missing or not PEM-encoded data in TLS Secret",
|
2021-03-02 01:02:08 +00:00
|
|
|
"invalidCertPEM", string(certPEM),
|
2021-05-26 17:42:50 +00:00
|
|
|
"secret", klog.KObj(secret),
|
|
|
|
)
|
2021-03-02 01:02:08 +00:00
|
|
|
if err = c.ensureTLSSecretIsRemoved(ctx); err != nil {
|
|
|
|
return false, fmt.Errorf("PEM data represented an invalid cert, but got error while deleting it: %w", err)
|
2021-02-26 18:58:56 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true, nil
|
2021-02-26 18:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyPEM := secret.Data[v1.TLSPrivateKeyKey]
|
|
|
|
_, err = tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
if err != nil {
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Error(err, "found invalid private key PEM data in TLS Secret",
|
|
|
|
"invalidCertPEM", string(certPEM),
|
|
|
|
"secret", klog.KObj(secret),
|
|
|
|
)
|
2021-03-02 01:02:08 +00:00
|
|
|
if err = c.ensureTLSSecretIsRemoved(ctx); err != nil {
|
|
|
|
return false, fmt.Errorf("cert had an invalid private key, but got error while deleting it: %w", err)
|
2021-02-26 18:58:56 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := x509.VerifyOptions{Roots: ca.Pool()}
|
|
|
|
if _, err = actualCertFromSecret.Verify(opts); err != nil {
|
|
|
|
// The TLS cert was not signed by the current CA. Since they are mismatched, delete the TLS cert
|
|
|
|
// so we can recreate it using the current CA.
|
|
|
|
if err = c.ensureTLSSecretIsRemoved(ctx); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
if !nameInfo.ready {
|
2021-02-25 00:03:17 +00:00
|
|
|
// We currently have a secret but we are waiting for a load balancer to be assigned an ingress, so
|
|
|
|
// our current secret must be old/unwanted.
|
2021-03-02 01:02:08 +00:00
|
|
|
if err = c.ensureTLSSecretIsRemoved(ctx); err != nil {
|
|
|
|
return false, err
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true, nil
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 18:27:19 +00:00
|
|
|
actualIPs := actualCertFromSecret.IPAddresses
|
|
|
|
actualHostnames := actualCertFromSecret.DNSNames
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("checking TLS certificate names",
|
2021-05-26 00:01:42 +00:00
|
|
|
"desiredIPs", nameInfo.selectedIPs,
|
2021-03-03 17:22:35 +00:00
|
|
|
"desiredHostname", nameInfo.selectedHostname,
|
2021-02-25 18:27:19 +00:00
|
|
|
"actualIPs", actualIPs,
|
|
|
|
"actualHostnames", actualHostnames,
|
2021-05-26 17:42:50 +00:00
|
|
|
"secret", klog.KObj(secret),
|
|
|
|
)
|
2021-02-25 18:27:19 +00:00
|
|
|
|
2021-05-26 00:01:42 +00:00
|
|
|
if certHostnameAndIPMatchDesiredState(nameInfo.selectedIPs, actualIPs, nameInfo.selectedHostname, actualHostnames) {
|
2021-02-26 18:58:56 +00:00
|
|
|
// The cert already matches the desired state, so there is no need to delete/recreate it.
|
2021-03-02 01:02:08 +00:00
|
|
|
return false, nil
|
2021-02-25 18:27:19 +00:00
|
|
|
}
|
2021-02-26 18:58:56 +00:00
|
|
|
|
2021-03-02 01:02:08 +00:00
|
|
|
if err = c.ensureTLSSecretIsRemoved(ctx); err != nil {
|
|
|
|
return false, err
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true, nil
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:01:42 +00:00
|
|
|
func certHostnameAndIPMatchDesiredState(desiredIPs []net.IP, actualIPs []net.IP, desiredHostname string, actualHostnames []string) bool {
|
|
|
|
if len(desiredIPs) > 0 && len(actualIPs) > 0 && len(actualIPs) == len(desiredIPs) && len(actualHostnames) == 0 {
|
|
|
|
for i := range desiredIPs {
|
|
|
|
if !actualIPs[i].Equal(desiredIPs[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 01:02:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if desiredHostname != "" && len(actualHostnames) == 1 && desiredHostname == actualHostnames[0] && len(actualIPs) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
func (c *impersonatorConfigController) ensureTLSSecretIsCreatedAndLoaded(ctx context.Context, nameInfo *certNameInfo, secret *v1.Secret, ca *certauthority.CA) error {
|
2021-02-25 00:03:17 +00:00
|
|
|
if secret != nil {
|
|
|
|
err := c.loadTLSCertFromSecret(secret)
|
|
|
|
if err != nil {
|
2021-03-03 17:22:35 +00:00
|
|
|
return err
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
2021-03-03 17:22:35 +00:00
|
|
|
return nil
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
2021-02-25 00:03:17 +00:00
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
if !nameInfo.ready {
|
|
|
|
return nil
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:01:42 +00:00
|
|
|
newTLSSecret, err := c.createNewTLSSecret(ctx, ca, nameInfo.selectedIPs, nameInfo.selectedHostname)
|
2021-02-25 00:03:17 +00:00
|
|
|
if err != nil {
|
2021-03-03 17:22:35 +00:00
|
|
|
return err
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.loadTLSCertFromSecret(newTLSSecret)
|
|
|
|
if err != nil {
|
2021-03-03 17:22:35 +00:00
|
|
|
return err
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
return nil
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 01:02:08 +00:00
|
|
|
func (c *impersonatorConfigController) ensureCASecretIsCreated(ctx context.Context) (*certauthority.CA, error) {
|
|
|
|
caSecret, err := c.secretsInformer.Lister().Secrets(c.namespace).Get(c.caSecretName)
|
|
|
|
if err != nil && !k8serrors.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var impersonationCA *certauthority.CA
|
|
|
|
if k8serrors.IsNotFound(err) {
|
|
|
|
impersonationCA, err = c.createCASecret(ctx)
|
|
|
|
} else {
|
|
|
|
crtBytes := caSecret.Data[caCrtKey]
|
|
|
|
keyBytes := caSecret.Data[caKeyKey]
|
|
|
|
impersonationCA, err = certauthority.Load(string(crtBytes), string(keyBytes))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return impersonationCA, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *impersonatorConfigController) createCASecret(ctx context.Context) (*certauthority.CA, error) {
|
2021-03-13 00:09:16 +00:00
|
|
|
impersonationCA, err := certauthority.New(caCommonName, approximatelyOneHundredYears)
|
2021-03-02 01:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create impersonation CA: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caPrivateKeyPEM, err := impersonationCA.PrivateKeyToPEM()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secret := v1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: c.caSecretName,
|
|
|
|
Namespace: c.namespace,
|
|
|
|
Labels: c.labels,
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
caCrtKey: impersonationCA.Bundle(),
|
|
|
|
caKeyKey: caPrivateKeyPEM,
|
|
|
|
},
|
|
|
|
Type: v1.SecretTypeOpaque,
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("creating CA certificates for impersonation proxy",
|
|
|
|
"secret", klog.KObj(&secret),
|
|
|
|
)
|
2021-03-02 01:02:08 +00:00
|
|
|
if _, err = c.k8sClient.CoreV1().Secrets(c.namespace).Create(ctx, &secret, metav1.CreateOptions{}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return impersonationCA, nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) findDesiredTLSCertificateName(config *v1alpha1.ImpersonationProxySpec) (*certNameInfo, error) {
|
|
|
|
if config.ExternalEndpoint != "" {
|
2021-03-10 18:30:06 +00:00
|
|
|
return c.findTLSCertificateNameFromEndpointConfig(config), nil
|
2021-05-20 18:57:07 +00:00
|
|
|
} else if config.Service.Type == v1alpha1.ImpersonationProxyServiceTypeClusterIP {
|
|
|
|
return c.findTLSCertificateNameFromClusterIPService()
|
|
|
|
}
|
2021-02-25 18:27:19 +00:00
|
|
|
return c.findTLSCertificateNameFromLoadBalancer()
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) findTLSCertificateNameFromEndpointConfig(config *v1alpha1.ImpersonationProxySpec) *certNameInfo {
|
2021-05-25 22:44:25 +00:00
|
|
|
addr, _ := endpointaddr.Parse(config.ExternalEndpoint, 443)
|
|
|
|
endpoint := strings.TrimSuffix(addr.Endpoint(), ":443")
|
|
|
|
|
|
|
|
if ip := net.ParseIP(addr.Host); ip != nil {
|
2021-05-26 00:06:26 +00:00
|
|
|
return &certNameInfo{ready: true, selectedIPs: []net.IP{ip}, clientEndpoint: endpoint}
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
2021-05-25 22:44:25 +00:00
|
|
|
return &certNameInfo{ready: true, selectedHostname: addr.Host, clientEndpoint: endpoint}
|
2021-02-25 18:27:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
func (c *impersonatorConfigController) findTLSCertificateNameFromLoadBalancer() (*certNameInfo, error) {
|
2021-02-25 18:27:19 +00:00
|
|
|
lb, err := c.servicesInformer.Lister().Services(c.namespace).Get(c.generatedLoadBalancerServiceName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if notFound {
|
2021-03-02 01:02:08 +00:00
|
|
|
// We aren't ready and will try again later in this case.
|
2021-03-03 17:22:35 +00:00
|
|
|
return &certNameInfo{ready: false}, nil
|
2021-02-25 18:27:19 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-03-03 17:22:35 +00:00
|
|
|
return nil, err
|
2021-02-25 18:27:19 +00:00
|
|
|
}
|
|
|
|
ingresses := lb.Status.LoadBalancer.Ingress
|
2021-02-25 19:40:14 +00:00
|
|
|
if len(ingresses) == 0 || (ingresses[0].Hostname == "" && ingresses[0].IP == "") {
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("load balancer for impersonation proxy does not have an ingress yet, so skipping tls cert generation while we wait",
|
|
|
|
"service", klog.KObj(lb),
|
|
|
|
)
|
2021-03-03 17:22:35 +00:00
|
|
|
return &certNameInfo{ready: false}, nil
|
2021-02-25 18:27:19 +00:00
|
|
|
}
|
2021-02-26 01:03:34 +00:00
|
|
|
for _, ingress := range ingresses {
|
|
|
|
hostname := ingress.Hostname
|
|
|
|
if hostname != "" {
|
2021-03-03 17:22:35 +00:00
|
|
|
return &certNameInfo{ready: true, selectedHostname: hostname, clientEndpoint: hostname}, nil
|
2021-02-26 01:03:34 +00:00
|
|
|
}
|
2021-02-25 19:40:14 +00:00
|
|
|
}
|
2021-02-26 01:03:34 +00:00
|
|
|
for _, ingress := range ingresses {
|
|
|
|
ip := ingress.IP
|
|
|
|
parsedIP := net.ParseIP(ip)
|
|
|
|
if parsedIP != nil {
|
2021-05-26 00:01:42 +00:00
|
|
|
return &certNameInfo{ready: true, selectedIPs: []net.IP{parsedIP}, clientEndpoint: ip}, nil
|
2021-02-26 01:03:34 +00:00
|
|
|
}
|
2021-02-25 19:40:14 +00:00
|
|
|
}
|
2021-02-26 01:03:34 +00:00
|
|
|
|
2021-03-03 17:22:35 +00:00
|
|
|
return nil, fmt.Errorf("could not find valid IP addresses or hostnames from load balancer %s/%s", c.namespace, lb.Name)
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 18:57:07 +00:00
|
|
|
func (c *impersonatorConfigController) findTLSCertificateNameFromClusterIPService() (*certNameInfo, error) {
|
|
|
|
clusterIP, err := c.servicesInformer.Lister().Services(c.namespace).Get(c.generatedClusterIPServiceName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if notFound {
|
|
|
|
// We aren't ready and will try again later in this case.
|
|
|
|
return &certNameInfo{ready: false}, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ip := clusterIP.Spec.ClusterIP
|
2021-05-26 00:01:42 +00:00
|
|
|
ips := clusterIP.Spec.ClusterIPs
|
2021-05-20 18:57:07 +00:00
|
|
|
if ip != "" {
|
2021-05-26 00:01:42 +00:00
|
|
|
// clusterIP will always exist when clusterIPs does, but not vice versa
|
|
|
|
var parsedIPs []net.IP
|
|
|
|
if len(ips) > 0 {
|
|
|
|
for _, ipFromIPs := range ips {
|
|
|
|
parsedIPs = append(parsedIPs, net.ParseIP(ipFromIPs))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parsedIPs = []net.IP{net.ParseIP(ip)}
|
|
|
|
}
|
|
|
|
return &certNameInfo{ready: true, selectedIPs: parsedIPs, clientEndpoint: ip}, nil
|
2021-05-20 18:57:07 +00:00
|
|
|
}
|
|
|
|
return &certNameInfo{ready: false}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:01:42 +00:00
|
|
|
func (c *impersonatorConfigController) createNewTLSSecret(ctx context.Context, ca *certauthority.CA, ips []net.IP, hostname string) (*v1.Secret, error) {
|
2021-03-02 01:02:08 +00:00
|
|
|
var hostnames []string
|
|
|
|
if hostname != "" {
|
|
|
|
hostnames = []string{hostname}
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:09:16 +00:00
|
|
|
impersonationCert, err := ca.IssueServerCert(hostnames, ips, approximatelyOneHundredYears)
|
2021-02-24 18:56:24 +00:00
|
|
|
if err != nil {
|
2021-02-25 00:03:17 +00:00
|
|
|
return nil, fmt.Errorf("could not create impersonation cert: %w", err)
|
2021-02-24 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:58:56 +00:00
|
|
|
certPEM, keyPEM, err := certauthority.ToPEM(impersonationCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 18:56:24 +00:00
|
|
|
|
2021-02-25 00:03:17 +00:00
|
|
|
newTLSSecret := &v1.Secret{
|
2021-02-24 18:56:24 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: c.tlsSecretName,
|
|
|
|
Namespace: c.namespace,
|
|
|
|
Labels: c.labels,
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
v1.TLSPrivateKeyKey: keyPEM,
|
|
|
|
v1.TLSCertKey: certPEM,
|
|
|
|
},
|
2021-03-02 01:02:08 +00:00
|
|
|
Type: v1.SecretTypeTLS,
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
2021-02-26 18:58:56 +00:00
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("creating TLS certificates for impersonation proxy",
|
2021-02-25 18:27:19 +00:00
|
|
|
"ips", ips,
|
|
|
|
"hostnames", hostnames,
|
2021-05-26 17:42:50 +00:00
|
|
|
"secret", klog.KObj(newTLSSecret),
|
|
|
|
)
|
2021-03-02 01:02:08 +00:00
|
|
|
return c.k8sClient.CoreV1().Secrets(c.namespace).Create(ctx, newTLSSecret, metav1.CreateOptions{})
|
2021-02-25 00:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *impersonatorConfigController) loadTLSCertFromSecret(tlsSecret *v1.Secret) error {
|
|
|
|
certPEM := tlsSecret.Data[v1.TLSCertKey]
|
|
|
|
keyPEM := tlsSecret.Data[v1.TLSPrivateKeyKey]
|
2021-03-11 21:20:25 +00:00
|
|
|
|
|
|
|
if err := c.tlsServingCertDynamicCertProvider.SetCertKeyContent(certPEM, keyPEM); err != nil {
|
|
|
|
c.tlsServingCertDynamicCertProvider.UnsetCertKeyContent()
|
2021-02-25 00:03:17 +00:00
|
|
|
return fmt.Errorf("could not parse TLS cert PEM data from Secret: %w", err)
|
|
|
|
}
|
2021-03-11 21:20:25 +00:00
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("loading TLS certificates for impersonation proxy",
|
2021-03-02 01:02:08 +00:00
|
|
|
"certPEM", string(certPEM),
|
2021-05-26 17:42:50 +00:00
|
|
|
"secret", klog.KObj(tlsSecret),
|
|
|
|
)
|
2021-03-11 21:20:25 +00:00
|
|
|
|
2021-02-24 18:56:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *impersonatorConfigController) ensureTLSSecretIsRemoved(ctx context.Context) error {
|
|
|
|
tlsSecretExists, _, err := c.tlsSecretExists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !tlsSecretExists {
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("deleting TLS certificates for impersonation proxy",
|
|
|
|
"secret", klog.KRef(c.namespace, c.tlsSecretName),
|
|
|
|
)
|
2021-02-24 18:56:24 +00:00
|
|
|
err = c.k8sClient.CoreV1().Secrets(c.namespace).Delete(ctx, c.tlsSecretName, metav1.DeleteOptions{})
|
2021-03-30 21:43:04 +00:00
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if notFound {
|
|
|
|
// its okay if we tried to delete and we got a not found error. This probably means
|
|
|
|
// another instance of the concierge got here first so there's nothing to delete.
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-24 18:56:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-11 21:20:25 +00:00
|
|
|
c.tlsServingCertDynamicCertProvider.UnsetCertKeyContent()
|
2021-03-10 18:30:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *impersonatorConfigController) loadSignerCA(status v1alpha1.StrategyStatus) error {
|
|
|
|
// Clear it when the impersonator is not completely ready.
|
|
|
|
if status != v1alpha1.SuccessStrategyStatus {
|
|
|
|
c.clearSignerCA()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
signingCertSecret, err := c.secretsInformer.Lister().Secrets(c.namespace).Get(c.impersonationSignerSecretName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load the impersonator's credential signing secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certPEM := signingCertSecret.Data[apicerts.CACertificateSecretKey]
|
|
|
|
keyPEM := signingCertSecret.Data[apicerts.CACertificatePrivateKeySecretKey]
|
2021-03-11 21:20:25 +00:00
|
|
|
|
|
|
|
if err := c.impersonationSigningCertProvider.SetCertKeyContent(certPEM, keyPEM); err != nil {
|
2021-03-10 18:30:06 +00:00
|
|
|
return fmt.Errorf("could not load the impersonator's credential signing secret: %w", err)
|
|
|
|
}
|
2021-02-25 00:03:17 +00:00
|
|
|
|
2021-05-26 17:42:50 +00:00
|
|
|
c.infoLog.Info("loading credential signing certificate for impersonation proxy",
|
2021-03-10 18:30:06 +00:00
|
|
|
"certPEM", string(certPEM),
|
2021-05-26 17:42:50 +00:00
|
|
|
"secret", klog.KObj(signingCertSecret),
|
|
|
|
)
|
2021-03-11 21:20:25 +00:00
|
|
|
|
2021-02-24 18:56:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
func (c *impersonatorConfigController) clearSignerCA() {
|
2021-05-26 21:47:02 +00:00
|
|
|
c.debugLog.Info("clearing credential signing certificate for impersonation proxy")
|
2021-03-11 21:20:25 +00:00
|
|
|
c.impersonationSigningCertProvider.UnsetCertKeyContent()
|
2021-03-10 18:30:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
func (c *impersonatorConfigController) doSyncResult(nameInfo *certNameInfo, config *v1alpha1.ImpersonationProxySpec, ca *certauthority.CA) *v1alpha1.CredentialIssuerStrategy {
|
2021-03-02 22:48:58 +00:00
|
|
|
switch {
|
|
|
|
case c.disabledExplicitly(config):
|
|
|
|
return &v1alpha1.CredentialIssuerStrategy{
|
|
|
|
Type: v1alpha1.ImpersonationProxyStrategyType,
|
|
|
|
Status: v1alpha1.ErrorStrategyStatus,
|
|
|
|
Reason: v1alpha1.DisabledStrategyReason,
|
|
|
|
Message: "impersonation proxy was explicitly disabled by configuration",
|
|
|
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
|
|
|
}
|
|
|
|
case c.disabledByAutoMode(config):
|
|
|
|
return &v1alpha1.CredentialIssuerStrategy{
|
|
|
|
Type: v1alpha1.ImpersonationProxyStrategyType,
|
|
|
|
Status: v1alpha1.ErrorStrategyStatus,
|
|
|
|
Reason: v1alpha1.DisabledStrategyReason,
|
|
|
|
Message: "automatically determined that impersonation proxy should be disabled",
|
|
|
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
|
|
|
}
|
2021-03-03 17:22:35 +00:00
|
|
|
case !nameInfo.ready:
|
|
|
|
return &v1alpha1.CredentialIssuerStrategy{
|
|
|
|
Type: v1alpha1.ImpersonationProxyStrategyType,
|
|
|
|
Status: v1alpha1.ErrorStrategyStatus,
|
|
|
|
Reason: v1alpha1.PendingStrategyReason,
|
|
|
|
Message: "waiting for load balancer Service to be assigned IP or hostname",
|
|
|
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
2021-03-03 00:51:35 +00:00
|
|
|
}
|
2021-03-03 17:22:35 +00:00
|
|
|
default:
|
2021-03-02 22:48:58 +00:00
|
|
|
return &v1alpha1.CredentialIssuerStrategy{
|
|
|
|
Type: v1alpha1.ImpersonationProxyStrategyType,
|
|
|
|
Status: v1alpha1.SuccessStrategyStatus,
|
|
|
|
Reason: v1alpha1.ListeningStrategyReason,
|
|
|
|
Message: "impersonation proxy is ready to accept client connections",
|
|
|
|
LastUpdateTime: metav1.NewTime(c.clock.Now()),
|
2021-03-03 00:51:35 +00:00
|
|
|
Frontend: &v1alpha1.CredentialIssuerFrontend{
|
|
|
|
Type: v1alpha1.ImpersonationProxyFrontendType,
|
|
|
|
ImpersonationProxyInfo: &v1alpha1.ImpersonationProxyInfo{
|
2021-03-03 17:22:35 +00:00
|
|
|
Endpoint: "https://" + nameInfo.clientEndpoint,
|
2021-03-03 00:51:35 +00:00
|
|
|
CertificateAuthorityData: base64.StdEncoding.EncodeToString(ca.Bundle()),
|
|
|
|
},
|
|
|
|
},
|
2021-03-02 22:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 19:16:27 +00:00
|
|
|
|
2021-05-19 16:40:32 +00:00
|
|
|
func validateCredentialIssuerSpec(spec *v1alpha1.ImpersonationProxySpec) error {
|
|
|
|
// Validate that the mode is one of our known values.
|
|
|
|
switch spec.Mode {
|
2021-05-18 19:16:27 +00:00
|
|
|
case v1alpha1.ImpersonationProxyModeDisabled:
|
2021-05-19 16:40:32 +00:00
|
|
|
case v1alpha1.ImpersonationProxyModeAuto:
|
2021-05-18 19:16:27 +00:00
|
|
|
case v1alpha1.ImpersonationProxyModeEnabled:
|
|
|
|
default:
|
2021-05-19 16:40:32 +00:00
|
|
|
return fmt.Errorf("invalid proxy mode %q (expected auto, disabled, or enabled)", spec.Mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If disabled, ignore all other fields and consider the configuration valid.
|
|
|
|
if spec.Mode == v1alpha1.ImpersonationProxyModeDisabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that the service type is one of our known values.
|
|
|
|
switch spec.Service.Type {
|
|
|
|
case v1alpha1.ImpersonationProxyServiceTypeNone:
|
|
|
|
case v1alpha1.ImpersonationProxyServiceTypeLoadBalancer:
|
|
|
|
case v1alpha1.ImpersonationProxyServiceTypeClusterIP:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid service type %q (expected None, LoadBalancer, or ClusterIP)", spec.Service.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If specified, validate that the LoadBalancerIP is a valid IPv4 or IPv6 address.
|
|
|
|
if ip := spec.Service.LoadBalancerIP; ip != "" && len(validation.IsValidIP(ip)) > 0 {
|
|
|
|
return fmt.Errorf("invalid LoadBalancerIP %q", spec.Service.LoadBalancerIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If service is type "None", a non-empty external endpoint must be specified.
|
|
|
|
if spec.ExternalEndpoint == "" && spec.Service.Type == v1alpha1.ImpersonationProxyServiceTypeNone {
|
|
|
|
return fmt.Errorf("externalEndpoint must be set when service.type is None")
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:44:25 +00:00
|
|
|
if spec.ExternalEndpoint != "" {
|
|
|
|
if _, err := endpointaddr.Parse(spec.ExternalEndpoint, 443); err != nil {
|
|
|
|
return fmt.Errorf("invalid ExternalEndpoint %q: %w", spec.ExternalEndpoint, err)
|
|
|
|
}
|
2021-05-18 19:16:27 +00:00
|
|
|
}
|
2021-05-19 16:40:32 +00:00
|
|
|
|
2021-05-18 19:16:27 +00:00
|
|
|
return nil
|
|
|
|
}
|