2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-09 17:04:05 +00:00
|
|
|
|
|
|
|
package apicerts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
"github.com/suzerain-io/pinniped/internal/certauthority"
|
|
|
|
pinnipedcontroller "github.com/suzerain-io/pinniped/internal/controller"
|
2020-08-28 15:59:09 +00:00
|
|
|
"github.com/suzerain-io/pinniped/internal/controllerlib"
|
2020-08-09 17:04:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
//nolint: gosec
|
|
|
|
certsSecretName = "api-serving-cert"
|
|
|
|
caCertificateSecretKey = "caCertificate"
|
|
|
|
tlsPrivateKeySecretKey = "tlsPrivateKey"
|
|
|
|
tlsCertificateChainSecretKey = "tlsCertificateChain"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certsManagerController struct {
|
2020-09-08 23:36:49 +00:00
|
|
|
namespace string
|
|
|
|
k8sClient kubernetes.Interface
|
|
|
|
secretInformer corev1informers.SecretInformer
|
2020-08-20 19:17:18 +00:00
|
|
|
|
2020-08-27 19:59:47 +00:00
|
|
|
// certDuration is the lifetime of both the serving certificate and its CA
|
|
|
|
// certificate that this controller will use when issuing the certificates.
|
2020-08-20 19:17:18 +00:00
|
|
|
certDuration time.Duration
|
2020-09-08 23:36:49 +00:00
|
|
|
|
|
|
|
generatedCACommonName string
|
|
|
|
serviceNameForGeneratedCertCommonName string
|
2020-08-09 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 23:36:49 +00:00
|
|
|
func NewCertsManagerController(namespace string,
|
2020-08-09 17:04:05 +00:00
|
|
|
k8sClient kubernetes.Interface,
|
|
|
|
secretInformer corev1informers.SecretInformer,
|
2020-08-20 17:54:15 +00:00
|
|
|
withInformer pinnipedcontroller.WithInformerOptionFunc,
|
|
|
|
withInitialEvent pinnipedcontroller.WithInitialEventOptionFunc,
|
2020-08-20 19:17:18 +00:00
|
|
|
certDuration time.Duration,
|
2020-09-08 23:36:49 +00:00
|
|
|
generatedCACommonName string,
|
|
|
|
serviceNameForGeneratedCertCommonName string,
|
2020-08-28 15:59:09 +00:00
|
|
|
) controllerlib.Controller {
|
|
|
|
return controllerlib.New(
|
|
|
|
controllerlib.Config{
|
2020-08-09 17:04:05 +00:00
|
|
|
Name: "certs-manager-controller",
|
|
|
|
Syncer: &certsManagerController{
|
2020-09-08 23:36:49 +00:00
|
|
|
namespace: namespace,
|
|
|
|
k8sClient: k8sClient,
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
certDuration: certDuration,
|
|
|
|
generatedCACommonName: generatedCACommonName,
|
|
|
|
serviceNameForGeneratedCertCommonName: serviceNameForGeneratedCertCommonName,
|
2020-08-09 17:04:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
2020-08-20 17:54:15 +00:00
|
|
|
pinnipedcontroller.NameAndNamespaceExactMatchFilterFactory(certsSecretName, namespace),
|
2020-08-28 15:59:09 +00:00
|
|
|
controllerlib.InformerOption{},
|
2020-08-09 17:04:05 +00:00
|
|
|
),
|
|
|
|
// Be sure to run once even if the Secret that the informer is watching doesn't exist.
|
2020-08-28 15:59:09 +00:00
|
|
|
withInitialEvent(controllerlib.Key{
|
2020-08-09 17:04:05 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Name: certsSecretName,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:59:09 +00:00
|
|
|
func (c *certsManagerController) Sync(ctx controllerlib.Context) error {
|
2020-08-09 17:04:05 +00:00
|
|
|
// Try to get the secret from the informer cache.
|
|
|
|
_, err := c.secretInformer.Lister().Secrets(c.namespace).Get(certsSecretName)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if err != nil && !notFound {
|
|
|
|
return fmt.Errorf("failed to get %s/%s secret: %w", c.namespace, certsSecretName, err)
|
|
|
|
}
|
|
|
|
if !notFound {
|
|
|
|
// The secret already exists, so nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CA.
|
2020-09-08 23:36:49 +00:00
|
|
|
aggregatedAPIServerCA, err := certauthority.New(pkix.Name{CommonName: c.generatedCACommonName}, c.certDuration)
|
2020-08-09 17:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not initialize CA: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the CA from above, create a TLS server cert for the aggregated API server to use.
|
2020-09-08 23:36:49 +00:00
|
|
|
serviceEndpoint := c.serviceNameForGeneratedCertCommonName + "." + c.namespace + ".svc"
|
2020-08-09 17:04:05 +00:00
|
|
|
aggregatedAPIServerTLSCert, err := aggregatedAPIServerCA.Issue(
|
2020-08-12 16:01:06 +00:00
|
|
|
pkix.Name{CommonName: serviceEndpoint},
|
|
|
|
[]string{serviceEndpoint},
|
2020-08-20 19:17:18 +00:00
|
|
|
c.certDuration,
|
2020-08-09 17:04:05 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not issue serving certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the CA's public key bundle and the serving certs to a secret.
|
2020-08-11 02:34:45 +00:00
|
|
|
tlsCertChainPEM, tlsPrivateKeyPEM, err := certauthority.ToPEM(aggregatedAPIServerTLSCert)
|
2020-08-09 17:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not PEM encode serving certificate: %w", err)
|
|
|
|
}
|
|
|
|
secret := corev1.Secret{
|
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: certsSecretName,
|
|
|
|
Namespace: c.namespace,
|
|
|
|
},
|
|
|
|
StringData: map[string]string{
|
|
|
|
caCertificateSecretKey: string(aggregatedAPIServerCA.Bundle()),
|
|
|
|
tlsPrivateKeySecretKey: string(tlsPrivateKeyPEM),
|
|
|
|
tlsCertificateChainSecretKey: string(tlsCertChainPEM),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = c.k8sClient.CoreV1().Secrets(c.namespace).Create(ctx.Context, &secret, metav1.CreateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create secret: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-08 23:36:49 +00:00
|
|
|
klog.Info("certsManagerController Sync successfully created secret")
|
2020-08-09 17:04:05 +00:00
|
|
|
return nil
|
|
|
|
}
|