Skip to content

Commit 91216c7

Browse files
author
Vadim Rutkovsky
committed
Set not-before/not-after annotations for secrets created from CSRs
1 parent 7c782c7 commit 91216c7

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

pkg/operator/csr/cert_controller.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package csr
33
import (
44
"context"
55
"crypto/tls"
6+
"crypto/x509"
67
"crypto/x509/pkix"
8+
"encoding/pem"
79
"fmt"
810
"math/rand"
911
"time"
@@ -166,7 +168,7 @@ func (c *clientCertificateController) sync(ctx context.Context, syncCtx factory.
166168

167169
// reconcile pending csr if exists
168170
if len(c.csrName) > 0 {
169-
newSecretConfig, err := c.syncCSR(secret)
171+
newSecretConfig, leaf, err := c.syncCSR(secret)
170172
if err != nil {
171173
c.reset()
172174
return err
@@ -179,6 +181,12 @@ func (c *clientCertificateController) sync(ctx context.Context, syncCtx factory.
179181
newSecretConfig[k] = v
180182
}
181183
secret.Data = newSecretConfig
184+
185+
// Update not-before/not-after annotations
186+
c.AdditionalAnnotations.NotBefore = leaf.NotBefore.Format(time.RFC3339)
187+
c.AdditionalAnnotations.NotAfter = leaf.NotAfter.Format(time.RFC3339)
188+
_ = c.AdditionalAnnotations.EnsureTLSMetadataUpdate(&secret.ObjectMeta)
189+
182190
// save the changes into secret
183191
if err := c.saveSecret(secret); err != nil {
184192
return err
@@ -231,10 +239,10 @@ func (c *clientCertificateController) sync(ctx context.Context, syncCtx factory.
231239
return nil
232240
}
233241

234-
func (c *clientCertificateController) syncCSR(secret *corev1.Secret) (map[string][]byte, error) {
242+
func (c *clientCertificateController) syncCSR(secret *corev1.Secret) (map[string][]byte, *x509.Certificate, error) {
235243
// skip if there is no ongoing csr
236244
if len(c.csrName) == 0 {
237-
return nil, fmt.Errorf("no ongoing csr")
245+
return nil, nil, fmt.Errorf("no ongoing csr")
238246
}
239247

240248
// skip if csr no longer exists
@@ -244,38 +252,48 @@ func (c *clientCertificateController) syncCSR(secret *corev1.Secret) (map[string
244252
// fallback to fetching csr from hub apiserver in case it is not cached by informer yet
245253
csr, err = c.hubCSRClient.Get(context.Background(), c.csrName, metav1.GetOptions{})
246254
if errors.IsNotFound(err) {
247-
return nil, fmt.Errorf("unable to get csr %q. It might have already been deleted.", c.csrName)
255+
return nil, nil, fmt.Errorf("unable to get csr %q. It might have already been deleted.", c.csrName)
248256
}
249257
case err != nil:
250-
return nil, err
258+
return nil, nil, err
251259
}
252260

253261
// skip if csr is not approved yet
254262
if !isCSRApproved(csr) {
255-
return nil, nil
263+
return nil, nil, nil
256264
}
257265

258266
// skip if csr has no certificate in its status yet
259267
if len(csr.Status.Certificate) == 0 {
260-
return nil, nil
268+
return nil, nil, nil
261269
}
262270

263271
klog.V(4).Infof("Sync csr %v", c.csrName)
264272
// check if cert in csr status matches with the corresponding private key
265273
if c.keyData == nil {
266-
return nil, fmt.Errorf("No private key found for certificate in csr: %s", c.csrName)
274+
return nil, nil, fmt.Errorf("No private key found for certificate in csr: %s", c.csrName)
267275
}
268276
_, err = tls.X509KeyPair(csr.Status.Certificate, c.keyData)
269277
if err != nil {
270-
return nil, fmt.Errorf("Private key does not match with the certificate in csr: %s", c.csrName)
278+
return nil, nil, fmt.Errorf("Private key does not match with the certificate in csr: %s", c.csrName)
279+
}
280+
// verify that the recieved data is a valid x509 certificate
281+
var block *pem.Block
282+
block, _ = pem.Decode(csr.Status.Certificate)
283+
if block == nil || block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
284+
return nil, nil, fmt.Errorf("invalid first block found for certificate in csr: %s", c.csrName)
271285
}
286+
certBytes := block.Bytes
287+
parsedCert, err := x509.ParseCertificate(certBytes)
272288

289+
if err != nil {
290+
return nil, nil, fmt.Errorf("failed to parse the certificate in csr %s: %v", c.csrName, err)
291+
}
273292
data := map[string][]byte{
274293
TLSCertFile: csr.Status.Certificate,
275294
TLSKeyFile: c.keyData,
276295
}
277-
278-
return data, nil
296+
return data, parsedCert, nil
279297
}
280298

281299
func (c *clientCertificateController) createCSR(ctx context.Context) (string, error) {

0 commit comments

Comments
 (0)