Skip to content

Commit 6dc34b5

Browse files
authored
Revert "Revert "Reload existing CA from disk on restart (#499)" (#521)" (#524)
This reverts commit e209007.
1 parent c6758a8 commit 6dc34b5

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

pkg/cmd/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/openshift/microshift/pkg/config"
2222
"github.com/openshift/microshift/pkg/controllers"
2323
"github.com/openshift/microshift/pkg/util"
24-
2524
ctrl "k8s.io/kubernetes/pkg/controlplane"
2625
)
2726

@@ -42,6 +41,10 @@ func initAll(cfg *config.MicroshiftConfig) error {
4241
return nil
4342
}
4443

44+
func loadCA(cfg *config.MicroshiftConfig) error {
45+
return util.LoadRootCA(cfg.DataDir+"/certs/ca-bundle", "ca-bundle.crt", "ca-bundle.key")
46+
}
47+
4548
func initCerts(cfg *config.MicroshiftConfig) error {
4649
_, svcNet, err := net.ParseCIDR(cfg.Cluster.ServiceCIDR)
4750
if err != nil {

pkg/cmd/run.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/sirupsen/logrus"
2121
"github.com/spf13/cobra"
2222
"github.com/spf13/pflag"
23+
"k8s.io/klog/v2"
2324
)
2425

2526
const (
@@ -64,6 +65,15 @@ func RunMicroshift(cfg *config.MicroshiftConfig, flags *pflag.FlagSet) error {
6465
// TODO: change to only initialize what is strictly necessary for the selected role(s)
6566
if _, err := os.Stat(filepath.Join(cfg.DataDir, "certs")); errors.Is(err, os.ErrNotExist) {
6667
initAll(cfg)
68+
} else {
69+
err = loadCA(cfg)
70+
if err != nil {
71+
err := os.RemoveAll(filepath.Join(cfg.DataDir, "certs"))
72+
if err != nil {
73+
klog.ErrorS(err, "removing old certs directory")
74+
}
75+
util.Must(initAll(cfg))
76+
}
6777
}
6878

6979
m := servicemanager.NewServiceManager()

pkg/util/cert.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"time"
3737

3838
"github.com/pkg/errors"
39+
"k8s.io/klog/v2"
3940
)
4041

4142
var (
@@ -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+
75106
func 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.
188219
func (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

Comments
 (0)