2022-05-11 14:58:14 +00:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-12 14:07:25 +00:00
|
|
|
"crypto/rsa"
|
2022-05-11 14:58:14 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-07-06 08:00:36 +00:00
|
|
|
"github.com/kyverno/kyverno/api/kyverno"
|
2022-05-11 14:58:14 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CertRenewalInterval is the renewal interval for rootCA
|
2022-10-13 09:46:05 +00:00
|
|
|
CertRenewalInterval = 12 * time.Hour
|
2022-05-12 14:07:25 +00:00
|
|
|
// CAValidityDuration is the valid duration for CA certificates
|
2022-10-13 09:46:05 +00:00
|
|
|
CAValidityDuration = 365 * 24 * time.Hour
|
2022-05-12 14:07:25 +00:00
|
|
|
// TLSValidityDuration is the valid duration for TLS certificates
|
2022-10-13 09:46:05 +00:00
|
|
|
TLSValidityDuration = 150 * 24 * time.Hour
|
2023-07-31 13:17:51 +00:00
|
|
|
rootCAKey = "rootCA.crt"
|
2022-05-11 14:58:14 +00:00
|
|
|
)
|
|
|
|
|
2022-10-14 04:23:42 +00:00
|
|
|
type CertValidator interface {
|
|
|
|
// ValidateCert checks the certificates validity
|
2023-06-21 13:35:43 +00:00
|
|
|
ValidateCert(context.Context) (bool, error)
|
2022-10-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 09:46:05 +00:00
|
|
|
type CertRenewer interface {
|
|
|
|
// RenewCA renews the CA certificate if needed
|
2022-12-21 20:30:45 +00:00
|
|
|
RenewCA(context.Context) error
|
2022-10-13 09:46:05 +00:00
|
|
|
// RenewTLS renews the TLS certificate if needed
|
2022-12-21 20:30:45 +00:00
|
|
|
RenewTLS(context.Context) error
|
2022-10-13 09:46:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 11:52:57 +00:00
|
|
|
type client interface {
|
|
|
|
Get(context.Context, string, metav1.GetOptions) (*corev1.Secret, error)
|
|
|
|
Create(context.Context, *corev1.Secret, metav1.CreateOptions) (*corev1.Secret, error)
|
|
|
|
Update(context.Context, *corev1.Secret, metav1.UpdateOptions) (*corev1.Secret, error)
|
|
|
|
Delete(context.Context, string, metav1.DeleteOptions) error
|
|
|
|
}
|
|
|
|
|
2022-10-13 09:46:05 +00:00
|
|
|
// certRenewer creates rootCA and pem pair to register
|
2022-05-11 14:58:14 +00:00
|
|
|
// webhook configurations and webhook server
|
|
|
|
// renews RootCA at the given interval
|
2022-10-13 09:46:05 +00:00
|
|
|
type certRenewer struct {
|
2023-08-24 11:52:57 +00:00
|
|
|
client client
|
2022-05-12 14:07:25 +00:00
|
|
|
certRenewalInterval time.Duration
|
|
|
|
caValidityDuration time.Duration
|
|
|
|
tlsValidityDuration time.Duration
|
2022-05-11 14:58:14 +00:00
|
|
|
|
2022-10-13 09:46:05 +00:00
|
|
|
// server is an IP address or domain name where Kyverno controller runs. Only required if out-of-cluster.
|
2023-08-25 11:24:52 +00:00
|
|
|
server string
|
|
|
|
commonName string
|
|
|
|
dnsNames []string
|
|
|
|
namespace string
|
|
|
|
caSecret string
|
|
|
|
pairSecret string
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCertRenewer returns an instance of CertRenewer
|
2022-12-07 06:08:37 +00:00
|
|
|
func NewCertRenewer(
|
2023-08-24 11:52:57 +00:00
|
|
|
client client,
|
2022-12-07 06:08:37 +00:00
|
|
|
certRenewalInterval,
|
|
|
|
caValidityDuration,
|
|
|
|
tlsValidityDuration time.Duration,
|
|
|
|
server string,
|
2023-08-25 11:24:52 +00:00
|
|
|
commonName string,
|
|
|
|
dnsNames []string,
|
|
|
|
namespace string,
|
|
|
|
caSecret string,
|
|
|
|
pairSecret string,
|
2022-12-07 06:08:37 +00:00
|
|
|
) *certRenewer {
|
2022-10-13 09:46:05 +00:00
|
|
|
return &certRenewer{
|
2022-05-12 14:07:25 +00:00
|
|
|
client: client,
|
|
|
|
certRenewalInterval: certRenewalInterval,
|
|
|
|
caValidityDuration: caValidityDuration,
|
|
|
|
tlsValidityDuration: tlsValidityDuration,
|
2022-10-13 09:46:05 +00:00
|
|
|
server: server,
|
2023-08-25 11:24:52 +00:00
|
|
|
commonName: commonName,
|
|
|
|
dnsNames: dnsNames,
|
|
|
|
namespace: namespace,
|
|
|
|
caSecret: caSecret,
|
|
|
|
pairSecret: pairSecret,
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 09:46:05 +00:00
|
|
|
// RenewCA renews the CA certificate if needed
|
2022-12-21 20:30:45 +00:00
|
|
|
func (c *certRenewer) RenewCA(ctx context.Context) error {
|
2023-06-21 13:35:43 +00:00
|
|
|
secret, key, certs, err := c.decodeCASecret(ctx)
|
2022-05-11 14:58:14 +00:00
|
|
|
if err != nil && !apierrors.IsNotFound(err) {
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Error(err, "failed to read CA")
|
2022-05-11 14:58:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
now := time.Now()
|
|
|
|
certs = removeExpiredCertificates(now, certs...)
|
|
|
|
if !allCertificatesExpired(now.Add(5*c.certRenewalInterval), certs...) {
|
|
|
|
logger.V(4).Info("CA certificate does not need to be renewed")
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-14 04:23:42 +00:00
|
|
|
if !isSecretManagedByKyverno(secret) {
|
2022-05-12 14:07:25 +00:00
|
|
|
err := fmt.Errorf("tls is not valid but certificates are not managed by kyverno, we can't renew them")
|
|
|
|
logger.Error(err, "tls is not valid but certificates are not managed by kyverno, we can't renew them")
|
|
|
|
return err
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
2023-02-22 09:04:17 +00:00
|
|
|
if secret != nil && secret.Type != corev1.SecretTypeTLS {
|
|
|
|
logger.Info("CA secret type is not TLS, we're going to delete it and regenrate one")
|
|
|
|
err := c.client.Delete(ctx, secret.Name, metav1.DeleteOptions{})
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to delete CA secret")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
caKey, caCert, err := generateCA(key, c.caValidityDuration)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to generate CA")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
certs = append(certs, caCert)
|
2022-12-21 20:30:45 +00:00
|
|
|
if err := c.writeCASecret(ctx, caKey, certs...); err != nil {
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Error(err, "failed to write CA")
|
|
|
|
return err
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Info("CA was renewed")
|
|
|
|
return nil
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 14:07:25 +00:00
|
|
|
// RenewTLS renews the TLS certificate if needed
|
2022-12-21 20:30:45 +00:00
|
|
|
func (c *certRenewer) RenewTLS(ctx context.Context) error {
|
2023-06-21 13:35:43 +00:00
|
|
|
_, caKey, caCerts, err := c.decodeCASecret(ctx)
|
2022-05-11 14:58:14 +00:00
|
|
|
if err != nil {
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Error(err, "failed to read CA")
|
2022-05-11 14:58:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-06-21 13:35:43 +00:00
|
|
|
secret, _, cert, err := c.decodeTLSSecret(ctx)
|
2022-05-12 14:07:25 +00:00
|
|
|
if err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to read TLS")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
if cert != nil && !allCertificatesExpired(now.Add(5*c.certRenewalInterval), cert) {
|
|
|
|
logger.V(4).Info("TLS certificate does not need to be renewed")
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-14 04:23:42 +00:00
|
|
|
if !isSecretManagedByKyverno(secret) {
|
2022-05-12 14:07:25 +00:00
|
|
|
err := fmt.Errorf("tls is not valid but certificates are not managed by kyverno, we can't renew them")
|
|
|
|
logger.Error(err, "tls is not valid but certificates are not managed by kyverno, we can't renew them")
|
2022-05-11 14:58:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-22 09:04:17 +00:00
|
|
|
if secret != nil && secret.Type != corev1.SecretTypeTLS {
|
|
|
|
logger.Info("TLS secret type is not TLS, we're going to delete it and regenrate one")
|
|
|
|
err := c.client.Delete(ctx, secret.Name, metav1.DeleteOptions{})
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to delete TLS secret")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-08-25 11:24:52 +00:00
|
|
|
tlsKey, tlsCert, err := generateTLS(c.server, caCerts[len(caCerts)-1], caKey, c.tlsValidityDuration, c.commonName, c.dnsNames)
|
2022-05-12 14:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "failed to generate TLS")
|
|
|
|
return err
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
2022-12-21 20:30:45 +00:00
|
|
|
if err := c.writeTLSSecret(ctx, tlsKey, tlsCert); err != nil {
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Error(err, "failed to write TLS")
|
|
|
|
return err
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Info("TLS was renewed")
|
2022-05-11 14:58:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 14:07:25 +00:00
|
|
|
// ValidateCert validates the CA Cert
|
2023-06-21 13:35:43 +00:00
|
|
|
func (c *certRenewer) ValidateCert(ctx context.Context) (bool, error) {
|
|
|
|
_, _, caCerts, err := c.decodeCASecret(ctx)
|
2022-05-12 14:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-06-21 13:35:43 +00:00
|
|
|
_, _, cert, err := c.decodeTLSSecret(ctx)
|
2022-05-12 14:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return validateCert(time.Now(), cert, caCerts...), nil
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:35:43 +00:00
|
|
|
func (c *certRenewer) getSecret(ctx context.Context, name string) (*corev1.Secret, error) {
|
|
|
|
if s, err := c.client.Get(ctx, name, metav1.GetOptions{}); err != nil {
|
2022-05-11 14:58:14 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:35:43 +00:00
|
|
|
func (c *certRenewer) decodeSecret(ctx context.Context, name string) (*corev1.Secret, *rsa.PrivateKey, []*x509.Certificate, error) {
|
|
|
|
secret, err := c.getSecret(ctx, name)
|
2022-05-12 14:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
var certBytes, keyBytes []byte
|
|
|
|
if secret != nil {
|
|
|
|
keyBytes = secret.Data[corev1.TLSPrivateKeyKey]
|
|
|
|
certBytes = secret.Data[corev1.TLSCertKey]
|
|
|
|
if len(certBytes) == 0 {
|
2022-10-13 09:46:05 +00:00
|
|
|
certBytes = secret.Data[rootCAKey]
|
2022-05-12 14:07:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var key *rsa.PrivateKey
|
|
|
|
if keyBytes != nil {
|
|
|
|
usedkey, err := pemToPrivateKey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
key = usedkey
|
|
|
|
}
|
|
|
|
return secret, key, pemToCertificates(certBytes), nil
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:35:43 +00:00
|
|
|
func (c *certRenewer) decodeCASecret(ctx context.Context) (*corev1.Secret, *rsa.PrivateKey, []*x509.Certificate, error) {
|
2023-08-25 11:24:52 +00:00
|
|
|
return c.decodeSecret(ctx, c.caSecret)
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 13:35:43 +00:00
|
|
|
func (c *certRenewer) decodeTLSSecret(ctx context.Context) (*corev1.Secret, *rsa.PrivateKey, *x509.Certificate, error) {
|
2023-08-25 11:24:52 +00:00
|
|
|
secret, key, certs, err := c.decodeSecret(ctx, c.pairSecret)
|
2022-05-12 14:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
if len(certs) == 0 {
|
|
|
|
return secret, key, nil, nil
|
|
|
|
} else if len(certs) == 1 {
|
|
|
|
return secret, key, certs[0], nil
|
2022-05-11 14:58:14 +00:00
|
|
|
} else {
|
2022-05-12 14:07:25 +00:00
|
|
|
return nil, nil, nil, err
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:30:45 +00:00
|
|
|
func (c *certRenewer) writeSecret(ctx context.Context, name string, key *rsa.PrivateKey, certs ...*x509.Certificate) error {
|
2023-08-25 11:24:52 +00:00
|
|
|
logger := logger.WithValues("name", name, "namespace", c.namespace)
|
2023-06-21 13:35:43 +00:00
|
|
|
secret, err := c.getSecret(ctx, name)
|
2022-05-11 14:58:14 +00:00
|
|
|
if err != nil && !apierrors.IsNotFound(err) {
|
|
|
|
logger.Error(err, "failed to get CA secret")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
secret = &corev1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2022-05-12 14:07:25 +00:00
|
|
|
Name: name,
|
2023-08-25 11:24:52 +00:00
|
|
|
Namespace: c.namespace,
|
2022-05-11 14:58:14 +00:00
|
|
|
Labels: map[string]string{
|
2023-07-31 13:17:51 +00:00
|
|
|
kyverno.LabelCertManagedBy: kyverno.ValueKyvernoApp,
|
2022-05-11 14:58:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Type: corev1.SecretTypeTLS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
secret.Type = corev1.SecretTypeTLS
|
|
|
|
secret.Data = map[string][]byte{
|
2022-05-12 14:07:25 +00:00
|
|
|
corev1.TLSCertKey: certificateToPem(certs...),
|
|
|
|
corev1.TLSPrivateKeyKey: privateKeyToPem(key),
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
if secret.ResourceVersion == "" {
|
2022-12-21 20:30:45 +00:00
|
|
|
if _, err := c.client.Create(ctx, secret, metav1.CreateOptions{}); err != nil {
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Error(err, "failed to update secret")
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
logger.Info("secret created")
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-21 20:30:45 +00:00
|
|
|
if _, err := c.client.Update(ctx, secret, metav1.UpdateOptions{}); err != nil {
|
2022-05-12 14:07:25 +00:00
|
|
|
logger.Error(err, "failed to update secret")
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
logger.Info("secret updated")
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 14:07:25 +00:00
|
|
|
return nil
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 14:07:25 +00:00
|
|
|
// writeCASecret stores the CA cert in secret
|
2022-12-21 20:30:45 +00:00
|
|
|
func (c *certRenewer) writeCASecret(ctx context.Context, key *rsa.PrivateKey, certs ...*x509.Certificate) error {
|
2023-08-25 11:24:52 +00:00
|
|
|
return c.writeSecret(ctx, c.caSecret, key, certs...)
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 14:07:25 +00:00
|
|
|
// writeTLSSecret Writes the pair of TLS certificate and key to the specified secret.
|
2022-12-21 20:30:45 +00:00
|
|
|
func (c *certRenewer) writeTLSSecret(ctx context.Context, key *rsa.PrivateKey, cert *x509.Certificate) error {
|
2023-08-25 11:24:52 +00:00
|
|
|
return c.writeSecret(ctx, c.pairSecret, key, cert)
|
2022-05-11 14:58:14 +00:00
|
|
|
}
|