@@ -36,6 +36,7 @@ import (
3636 "time"
3737
3838 "github.com/pkg/errors"
39+ "k8s.io/klog/v2"
3940)
4041
4142var (
@@ -72,6 +73,36 @@ func GenCA(common string, svcName []string, duration time.Duration) (*rsa.Privat
7273 return key , ca , err
7374}
7475
76+ func LoadRootCA (dir , certFilename , keyFilename string ) error {
77+
78+ key , err := ioutil .ReadFile (filepath .Join (dir , keyFilename ))
79+ if err != nil {
80+ return errors .Wrap (err , "error reading CA key" )
81+ }
82+
83+ if rootKey , err = PemToPrivateKey (key ); err != nil {
84+ return errors .Wrap (err , "parsing CA key from PEM" )
85+ }
86+
87+ certPath := filepath .Join (dir , certFilename )
88+ cert , err := ioutil .ReadFile (certPath )
89+ if err != nil {
90+ return errors .Wrap (err , "reading CA certificate" )
91+ }
92+
93+ if rootCA , err = PemToCertificate (cert ); err != nil {
94+ return errors .Wrap (err , "parsing CA certificate" )
95+ }
96+
97+ now := time .Now ()
98+
99+ if now .After (rootCA .NotAfter ) {
100+ klog .ErrorS (nil , "CA has expired: current time %s is after %s" , now .Format (time .RFC3339 ), rootCA .NotAfter .Format (time .RFC3339 ))
101+ }
102+
103+ return nil
104+ }
105+
75106func StoreRootCA (common , dir , certFilename , keyFilename string , svcName []string ) error {
76107 if rootCA == nil || rootKey == nil {
77108 var err error
@@ -186,6 +217,15 @@ func (cfg *CertCfg) GenerateSelfSignedCertificate() (*rsa.PrivateKey, *x509.Cert
186217
187218// GenerateSignedCertificate generate a key and cert defined by CertCfg and signed by CA.
188219func (cfg * CertCfg ) GenerateSignedCertificate (caKey * rsa.PrivateKey , caCert * x509.Certificate ) (* rsa.PrivateKey , * x509.Certificate , error ) {
220+
221+ if caCert == nil {
222+ return nil , nil , errors .New ("Unable to GenerateSignedCertificate with (nil) caCert" )
223+ }
224+
225+ if caKey == nil {
226+ return nil , nil , errors .New ("Unable to GenerateSignedCertificate with (nil) caKey" )
227+ }
228+
189229 // create a private key
190230 key , err := PrivateKey ()
191231 if err != nil {
0 commit comments