@@ -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 {
@@ -365,6 +378,8 @@ func validateCACert(l certKeyLocation) error {
365
378
if err != nil {
366
379
return errors .Wrapf (err , "failure loading certificate for %s" , l .uxName )
367
380
}
381
+ // Validate period
382
+ CheckCertificatePeriodValidity (l .uxName , caCert )
368
383
369
384
// Check if cert is a CA
370
385
if ! caCert .IsCA {
@@ -395,6 +410,8 @@ func validateSignedCert(l certKeyLocation) error {
395
410
if err != nil {
396
411
return errors .Wrapf (err , "failure loading certificate authority for %s" , l .uxName )
397
412
}
413
+ // Validate period
414
+ CheckCertificatePeriodValidity (l .uxName , caCert )
398
415
399
416
return validateSignedCertWithCA (l , caCert )
400
417
}
@@ -406,6 +423,8 @@ func validateSignedCertWithCA(l certKeyLocation, caCert *x509.Certificate) error
406
423
if err != nil {
407
424
return errors .Wrapf (err , "failure loading certificate for %s" , l .uxName )
408
425
}
426
+ // Validate period
427
+ CheckCertificatePeriodValidity (l .uxName , signedCert )
409
428
410
429
// Check if the cert is signed by the CA
411
430
if err := signedCert .CheckSignatureFrom (caCert ); err != nil {
@@ -439,3 +458,21 @@ func validateCertificateWithConfig(cert *x509.Certificate, baseName string, cfg
439
458
}
440
459
return nil
441
460
}
461
+
462
+ // CheckCertificatePeriodValidity takes a certificate and prints a warning if its period
463
+ // is not valid related to the current time. It does so only if the certificate was not validated already
464
+ // by keeping track with a cache.
465
+ func CheckCertificatePeriodValidity (baseName string , cert * x509.Certificate ) {
466
+ certPeriodValidationMutex .Lock ()
467
+ if _ , exists := certPeriodValidation [baseName ]; exists {
468
+ certPeriodValidationMutex .Unlock ()
469
+ return
470
+ }
471
+ certPeriodValidation [baseName ] = struct {}{}
472
+ certPeriodValidationMutex .Unlock ()
473
+
474
+ klog .V (5 ).Infof ("validating certificate period for %s certificate" , baseName )
475
+ if err := pkiutil .ValidateCertPeriod (cert , 0 ); err != nil {
476
+ klog .Warningf ("WARNING: could not validate bounds for certificate %s: %v" , baseName , err )
477
+ }
478
+ }
0 commit comments