2021-02-18 19:16:34 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-08-09 17:04:05 +00:00
|
|
|
|
|
|
|
package apicerts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/certauthority"
|
|
|
|
pinnipedcontroller "go.pinniped.dev/internal/controller"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2020-08-09 17:04:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-10 18:30:06 +00:00
|
|
|
CACertificateSecretKey = "caCertificate"
|
|
|
|
CACertificatePrivateKeySecretKey = "caCertificatePrivateKey"
|
|
|
|
tlsPrivateKeySecretKey = "tlsPrivateKey"
|
|
|
|
TLSCertificateChainSecretKey = "tlsCertificateChain"
|
2020-08-09 17:04:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type certsManagerController struct {
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
namespace string
|
|
|
|
certsSecretResourceName string
|
2020-10-15 17:14:23 +00:00
|
|
|
certsSecretLabels map[string]string
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
func NewCertsManagerController(
|
|
|
|
namespace string,
|
|
|
|
certsSecretResourceName string,
|
2020-10-15 17:14:23 +00:00
|
|
|
certsSecretLabels map[string]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,
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
certsSecretResourceName: certsSecretResourceName,
|
2020-10-15 17:14:23 +00:00
|
|
|
certsSecretLabels: certsSecretLabels,
|
2020-09-08 23:36:49 +00:00
|
|
|
k8sClient: k8sClient,
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
certDuration: certDuration,
|
|
|
|
generatedCACommonName: generatedCACommonName,
|
|
|
|
serviceNameForGeneratedCertCommonName: serviceNameForGeneratedCertCommonName,
|
2020-08-09 17:04:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
withInformer(
|
|
|
|
secretInformer,
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
pinnipedcontroller.NameAndNamespaceExactMatchFilterFactory(certsSecretResourceName, 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,
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
Name: certsSecretResourceName,
|
2020-08-09 17:04:05 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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.
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
_, err := c.secretInformer.Lister().Secrets(c.namespace).Get(c.certsSecretResourceName)
|
2020-08-09 17:04:05 +00:00
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if err != nil && !notFound {
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
return fmt.Errorf("failed to get %s/%s secret: %w", c.namespace, c.certsSecretResourceName, err)
|
2020-08-09 17:04:05 +00:00
|
|
|
}
|
|
|
|
if !notFound {
|
|
|
|
// The secret already exists, so nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CA.
|
2021-03-13 00:09:16 +00:00
|
|
|
ca, err := certauthority.New(c.generatedCACommonName, c.certDuration)
|
2020-08-09 17:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not initialize CA: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:30:06 +00:00
|
|
|
caPrivateKeyPEM, err := ca.PrivateKeyToPEM()
|
2020-08-09 17:04:05 +00:00
|
|
|
if err != nil {
|
2021-03-10 18:30:06 +00:00
|
|
|
return fmt.Errorf("could not get CA private key: %w", err)
|
2020-08-09 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
secret := corev1.Secret{
|
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
Name: c.certsSecretResourceName,
|
2020-08-09 17:04:05 +00:00
|
|
|
Namespace: c.namespace,
|
2020-10-15 17:14:23 +00:00
|
|
|
Labels: c.certsSecretLabels,
|
2020-08-09 17:04:05 +00:00
|
|
|
},
|
|
|
|
StringData: map[string]string{
|
2021-03-10 18:30:06 +00:00
|
|
|
CACertificateSecretKey: string(ca.Bundle()),
|
|
|
|
CACertificatePrivateKeySecretKey: string(caPrivateKeyPEM),
|
2020-08-09 17:04:05 +00:00
|
|
|
},
|
|
|
|
}
|
2021-03-10 18:30:06 +00:00
|
|
|
|
|
|
|
// Using the CA from above, create a TLS server cert if we have service name.
|
|
|
|
if len(c.serviceNameForGeneratedCertCommonName) != 0 {
|
|
|
|
serviceEndpoint := c.serviceNameForGeneratedCertCommonName + "." + c.namespace + ".svc"
|
2021-03-13 00:09:16 +00:00
|
|
|
tlsCert, err := ca.IssueServerCert([]string{serviceEndpoint}, nil, c.certDuration)
|
2021-03-10 18:30:06 +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.
|
|
|
|
tlsCertChainPEM, tlsPrivateKeyPEM, err := certauthority.ToPEM(tlsCert)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not PEM encode serving certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret.StringData[tlsPrivateKeySecretKey] = string(tlsPrivateKeyPEM)
|
|
|
|
secret.StringData[TLSCertificateChainSecretKey] = string(tlsCertChainPEM)
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:04:05 +00:00
|
|
|
_, 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
|
|
|
|
}
|