@@ -3,6 +3,7 @@ package csr
3
3
import (
4
4
"context"
5
5
"crypto/tls"
6
+ "crypto/x509"
6
7
"crypto/x509/pkix"
7
8
"fmt"
8
9
"math/rand"
@@ -166,7 +167,7 @@ func (c *clientCertificateController) sync(ctx context.Context, syncCtx factory.
166
167
167
168
// reconcile pending csr if exists
168
169
if len (c .csrName ) > 0 {
169
- newSecretConfig , err := c .syncCSR (secret )
170
+ newSecretConfig , leaf , err := c .syncCSR (secret )
170
171
if err != nil {
171
172
c .reset ()
172
173
return err
@@ -179,6 +180,12 @@ func (c *clientCertificateController) sync(ctx context.Context, syncCtx factory.
179
180
newSecretConfig [k ] = v
180
181
}
181
182
secret .Data = newSecretConfig
183
+
184
+ // Update not-before/not-after annotations
185
+ c .AdditionalAnnotations .NotBefore = leaf .NotBefore .Format (time .RFC3339 )
186
+ c .AdditionalAnnotations .NotAfter = leaf .NotAfter .Format (time .RFC3339 )
187
+ _ = c .AdditionalAnnotations .EnsureTLSMetadataUpdate (& secret .ObjectMeta )
188
+
182
189
// save the changes into secret
183
190
if err := c .saveSecret (secret ); err != nil {
184
191
return err
@@ -231,10 +238,10 @@ func (c *clientCertificateController) sync(ctx context.Context, syncCtx factory.
231
238
return nil
232
239
}
233
240
234
- func (c * clientCertificateController ) syncCSR (secret * corev1.Secret ) (map [string ][]byte , error ) {
241
+ func (c * clientCertificateController ) syncCSR (secret * corev1.Secret ) (map [string ][]byte , * x509. Certificate , error ) {
235
242
// skip if there is no ongoing csr
236
243
if len (c .csrName ) == 0 {
237
- return nil , fmt .Errorf ("no ongoing csr" )
244
+ return nil , nil , fmt .Errorf ("no ongoing csr" )
238
245
}
239
246
240
247
// skip if csr no longer exists
@@ -244,38 +251,44 @@ func (c *clientCertificateController) syncCSR(secret *corev1.Secret) (map[string
244
251
// fallback to fetching csr from hub apiserver in case it is not cached by informer yet
245
252
csr , err = c .hubCSRClient .Get (context .Background (), c .csrName , metav1.GetOptions {})
246
253
if errors .IsNotFound (err ) {
247
- return nil , fmt .Errorf ("unable to get csr %q. It might have already been deleted." , c .csrName )
254
+ return nil , nil , fmt .Errorf ("unable to get csr %q. It might have already been deleted." , c .csrName )
248
255
}
249
256
case err != nil :
250
- return nil , err
257
+ return nil , nil , err
251
258
}
252
259
253
260
// skip if csr is not approved yet
254
261
if ! isCSRApproved (csr ) {
255
- return nil , nil
262
+ return nil , nil , nil
256
263
}
257
264
258
265
// skip if csr has no certificate in its status yet
259
266
if len (csr .Status .Certificate ) == 0 {
260
- return nil , nil
267
+ return nil , nil , nil
261
268
}
262
269
263
270
klog .V (4 ).Infof ("Sync csr %v" , c .csrName )
264
271
// check if cert in csr status matches with the corresponding private key
265
272
if c .keyData == nil {
266
- return nil , fmt .Errorf ("No private key found for certificate in csr: %s" , c .csrName )
273
+ return nil , nil , fmt .Errorf ("No private key found for certificate in csr: %s" , c .csrName )
267
274
}
268
275
_ , err = tls .X509KeyPair (csr .Status .Certificate , c .keyData )
269
276
if err != nil {
270
- return nil , fmt .Errorf ("Private key does not match with the certificate in csr: %s" , c .csrName )
277
+ return nil , nil , fmt .Errorf ("Private key does not match with the certificate in csr: %s" , c .csrName )
278
+ }
279
+ parsed , err := x509 .ParseCertificate (csr .Status .Certificate )
280
+ if err != nil {
281
+ return nil , nil , fmt .Errorf ("failed to parse the certificate in csr %s: %v" , c .csrName , err )
282
+ }
283
+ if parsed == nil {
284
+ return nil , nil , fmt .Errorf ("Empty leaf certificate in csr: %s" , c .csrName )
271
285
}
272
286
273
287
data := map [string ][]byte {
274
288
TLSCertFile : csr .Status .Certificate ,
275
289
TLSKeyFile : c .keyData ,
276
290
}
277
-
278
- return data , nil
291
+ return data , parsed , nil
279
292
}
280
293
281
294
func (c * clientCertificateController ) createCSR (ctx context.Context ) (string , error ) {
0 commit comments