Skip to content

Commit 7c782c7

Browse files
author
Vadim Rutkovsky
committed
certrotation: set not-before/not-after annotations
This ensures every secret managed by this controller has valid annotation set. Instead of analyzing potentially sensitive secret on customer cluster we should be able to tell if the certificate rotation didn't happen and certificate expired by looking into annotations
1 parent fc703a7 commit 7c782c7

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

pkg/operator/certrotation/annotations.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import (
66
)
77

88
const (
9+
// CertificateNotBeforeAnnotation contains the certificate expiration date in RFC3339 format.
10+
CertificateNotBeforeAnnotation = "auth.openshift.io/certificate-not-before"
11+
// CertificateNotAfterAnnotation contains the certificate expiration date in RFC3339 format.
12+
CertificateNotAfterAnnotation = "auth.openshift.io/certificate-not-after"
13+
// CertificateIssuer contains the common name of the certificate that signed another certificate.
14+
CertificateIssuer = "auth.openshift.io/certificate-issuer"
15+
// CertificateHostnames contains the hostnames used by a signer.
16+
CertificateHostnames = "auth.openshift.io/certificate-hostnames"
17+
// AutoRegenerateAfterOfflineExpiryAnnotation contains a link to PR and an e2e test name which verifies
18+
// that TLS artifact is correctly regenerated after it has expired
919
AutoRegenerateAfterOfflineExpiryAnnotation string = "certificates.openshift.io/auto-regenerate-after-offline-expiry"
1020
)
1121

@@ -17,6 +27,10 @@ type AdditionalAnnotations struct {
1727
// AutoRegenerateAfterOfflineExpiry contains a link to PR and an e2e test name which verifies
1828
// that TLS artifact is correctly regenerated after it has expired
1929
AutoRegenerateAfterOfflineExpiry string
30+
// NotBefore contains certificate the certificate creation date in RFC3339 format.
31+
NotBefore string
32+
// NotAfter contains certificate the certificate validity date in RFC3339 format.
33+
NotAfter string
2034
}
2135

2236
func (a AdditionalAnnotations) EnsureTLSMetadataUpdate(meta *metav1.ObjectMeta) bool {
@@ -36,6 +50,14 @@ func (a AdditionalAnnotations) EnsureTLSMetadataUpdate(meta *metav1.ObjectMeta)
3650
meta.Annotations[AutoRegenerateAfterOfflineExpiryAnnotation] = a.AutoRegenerateAfterOfflineExpiry
3751
modified = true
3852
}
53+
if len(a.NotBefore) > 0 && meta.Annotations[CertificateNotBeforeAnnotation] != a.NotBefore {
54+
meta.Annotations[CertificateNotBeforeAnnotation] = a.NotBefore
55+
modified = true
56+
}
57+
if len(a.NotAfter) > 0 && meta.Annotations[CertificateNotAfterAnnotation] != a.NotAfter {
58+
meta.Annotations[CertificateNotAfterAnnotation] = a.NotAfter
59+
modified = true
60+
}
3961
return modified
4062
}
4163

pkg/operator/certrotation/client_cert_rotation_controller.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ import (
1515
)
1616

1717
const (
18-
// CertificateNotBeforeAnnotation contains the certificate expiration date in RFC3339 format.
19-
CertificateNotBeforeAnnotation = "auth.openshift.io/certificate-not-before"
20-
// CertificateNotAfterAnnotation contains the certificate expiration date in RFC3339 format.
21-
CertificateNotAfterAnnotation = "auth.openshift.io/certificate-not-after"
22-
// CertificateIssuer contains the common name of the certificate that signed another certificate.
23-
CertificateIssuer = "auth.openshift.io/certificate-issuer"
24-
// CertificateHostnames contains the hostnames used by a signer.
25-
CertificateHostnames = "auth.openshift.io/certificate-hostnames"
2618
// RunOnceContextKey is a context value key that can be used to call the controller Sync() and make it only run the syncWorker once and report error.
2719
RunOnceContextKey = "cert-rotation-controller.openshift.io/run-once"
2820
)

pkg/operator/certrotation/signer.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (c RotatedSigningCASecret) EnsureSigningCertKeyPair(ctx context.Context) (*
9090
reason = "secret doesn't exist"
9191
}
9292
c.EventRecorder.Eventf("SignerUpdateRequired", "%q in %q requires a new signing cert/key pair: %v", c.Name, c.Namespace, reason)
93-
if err := setSigningCertKeyPairSecret(signingCertKeyPairSecret, c.Validity); err != nil {
93+
if err := setSigningCertKeyPairSecret(signingCertKeyPairSecret, c.Validity, c.AdditionalAnnotations); err != nil {
9494
return nil, false, err
9595
}
9696

@@ -194,7 +194,7 @@ func getValidityFromAnnotations(annotations map[string]string) (notBefore time.T
194194
}
195195

196196
// setSigningCertKeyPairSecret creates a new signing cert/key pair and sets them in the secret
197-
func setSigningCertKeyPairSecret(signingCertKeyPairSecret *corev1.Secret, validity time.Duration) error {
197+
func setSigningCertKeyPairSecret(signingCertKeyPairSecret *corev1.Secret, validity time.Duration, annotations AdditionalAnnotations) error {
198198
signerName := fmt.Sprintf("%s_%s@%d", signingCertKeyPairSecret.Namespace, signingCertKeyPairSecret.Name, time.Now().Unix())
199199
ca, err := crypto.MakeSelfSignedCAConfigForDuration(signerName, validity)
200200
if err != nil {
@@ -215,9 +215,11 @@ func setSigningCertKeyPairSecret(signingCertKeyPairSecret *corev1.Secret, validi
215215
}
216216
signingCertKeyPairSecret.Data["tls.crt"] = certBytes.Bytes()
217217
signingCertKeyPairSecret.Data["tls.key"] = keyBytes.Bytes()
218-
signingCertKeyPairSecret.Annotations[CertificateNotAfterAnnotation] = ca.Certs[0].NotAfter.Format(time.RFC3339)
219-
signingCertKeyPairSecret.Annotations[CertificateNotBeforeAnnotation] = ca.Certs[0].NotBefore.Format(time.RFC3339)
218+
annotations.NotBefore = ca.Certs[0].NotBefore.Format(time.RFC3339)
219+
annotations.NotAfter = ca.Certs[0].NotAfter.Format(time.RFC3339)
220220
signingCertKeyPairSecret.Annotations[CertificateIssuer] = ca.Certs[0].Issuer.CommonName
221221

222+
_ = annotations.EnsureTLSMetadataUpdate(&signingCertKeyPairSecret.ObjectMeta)
223+
222224
return nil
223225
}

pkg/operator/certrotation/target.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ func setTargetCertKeyPairSecret(targetCertKeyPairSecret *corev1.Secret, validity
251251
if err != nil {
252252
return err
253253
}
254-
targetCertKeyPairSecret.Annotations[CertificateNotAfterAnnotation] = certKeyPair.Certs[0].NotAfter.Format(time.RFC3339)
255-
targetCertKeyPairSecret.Annotations[CertificateNotBeforeAnnotation] = certKeyPair.Certs[0].NotBefore.Format(time.RFC3339)
254+
annotations.NotBefore = certKeyPair.Certs[0].NotBefore.Format(time.RFC3339)
255+
annotations.NotAfter = certKeyPair.Certs[0].NotAfter.Format(time.RFC3339)
256256
targetCertKeyPairSecret.Annotations[CertificateIssuer] = certKeyPair.Certs[0].Issuer.CommonName
257257

258258
_ = annotations.EnsureTLSMetadataUpdate(&targetCertKeyPairSecret.ObjectMeta)

0 commit comments

Comments
 (0)