@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"os"
24
24
"path/filepath"
25
+ "sync"
25
26
26
27
"github.com/pkg/errors"
27
28
"k8s.io/client-go/util/keyutil"
@@ -32,6 +33,12 @@ import (
32
33
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
33
34
)
34
35
36
+ var (
37
+ // certPeriodValidation is used to store if period validation was done for a certificate
38
+ certPeriodValidationMutex sync.Mutex
39
+ certPeriodValidation = map [string ]struct {}{}
40
+ )
41
+
35
42
// CreatePKIAssets will create and write to disk all PKI assets necessary to establish the control plane.
36
43
// If the PKI assets already exists in the target folder, they are used only if evaluated equal; otherwise an error is returned.
37
44
func CreatePKIAssets (cfg * kubeadmapi.InitConfiguration ) error {
@@ -166,6 +173,8 @@ func LoadCertificateAuthority(pkiDir string, baseName string) (*x509.Certificate
166
173
if err != nil {
167
174
return nil , nil , errors .Wrapf (err , "failure loading %s certificate authority" , baseName )
168
175
}
176
+ // Validate period
177
+ CheckCertificatePeriodValidity (baseName , caCert )
169
178
170
179
// Make sure the loaded CA cert actually is a CA
171
180
if ! caCert .IsCA {
@@ -189,6 +198,8 @@ func writeCertificateAuthorityFilesIfNotExist(pkiDir string, baseName string, ca
189
198
if err != nil {
190
199
return errors .Wrapf (err , "failure loading %s certificate" , baseName )
191
200
}
201
+ // Validate period
202
+ CheckCertificatePeriodValidity (baseName , caCert )
192
203
193
204
// Check if the existing cert is a CA
194
205
if ! caCert .IsCA {
@@ -223,6 +234,8 @@ func writeCertificateFilesIfNotExist(pkiDir string, baseName string, signingCert
223
234
if err != nil {
224
235
return errors .Wrapf (err , "failure loading %s certificate" , baseName )
225
236
}
237
+ // Validate period
238
+ CheckCertificatePeriodValidity (baseName , signedCert )
226
239
227
240
// Check if the existing cert is signed by the given CA
228
241
if err := signedCert .CheckSignatureFrom (signingCert ); err != nil {
@@ -364,6 +377,8 @@ func validateCACert(l certKeyLocation) error {
364
377
if err != nil {
365
378
return errors .Wrapf (err , "failure loading certificate for %s" , l .uxName )
366
379
}
380
+ // Validate period
381
+ CheckCertificatePeriodValidity (l .uxName , caCert )
367
382
368
383
// Check if cert is a CA
369
384
if ! caCert .IsCA {
@@ -394,6 +409,8 @@ func validateSignedCert(l certKeyLocation) error {
394
409
if err != nil {
395
410
return errors .Wrapf (err , "failure loading certificate authority for %s" , l .uxName )
396
411
}
412
+ // Validate period
413
+ CheckCertificatePeriodValidity (l .uxName , caCert )
397
414
398
415
return validateSignedCertWithCA (l , caCert )
399
416
}
@@ -405,6 +422,8 @@ func validateSignedCertWithCA(l certKeyLocation, caCert *x509.Certificate) error
405
422
if err != nil {
406
423
return errors .Wrapf (err , "failure loading certificate for %s" , l .uxName )
407
424
}
425
+ // Validate period
426
+ CheckCertificatePeriodValidity (l .uxName , signedCert )
408
427
409
428
// Check if the cert is signed by the CA
410
429
if err := signedCert .CheckSignatureFrom (caCert ); err != nil {
@@ -438,3 +457,21 @@ func validateCertificateWithConfig(cert *x509.Certificate, baseName string, cfg
438
457
}
439
458
return nil
440
459
}
460
+
461
+ // CheckCertificatePeriodValidity takes a certificate and prints a warning if its period
462
+ // is not valid related to the current time. It does so only if the certificate was not validated already
463
+ // by keeping track with a cache.
464
+ func CheckCertificatePeriodValidity (baseName string , cert * x509.Certificate ) {
465
+ certPeriodValidationMutex .Lock ()
466
+ if _ , exists := certPeriodValidation [baseName ]; exists {
467
+ certPeriodValidationMutex .Unlock ()
468
+ return
469
+ }
470
+ certPeriodValidation [baseName ] = struct {}{}
471
+ certPeriodValidationMutex .Unlock ()
472
+
473
+ klog .V (5 ).Infof ("validating certificate period for %s certificate" , baseName )
474
+ if err := pkiutil .ValidateCertPeriod (cert , 0 ); err != nil {
475
+ klog .Warningf ("WARNING: could not validate bounds for certificate %s: %v" , baseName , err )
476
+ }
477
+ }
0 commit comments