Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/openshift/microshift/pkg/config"
"github.com/openshift/microshift/pkg/controllers"
"github.com/openshift/microshift/pkg/util"

ctrl "k8s.io/kubernetes/pkg/controlplane"
)

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

func loadCA(cfg *config.MicroshiftConfig) error {
return util.LoadRootCA(cfg.DataDir+"/certs/ca-bundle", "ca-bundle.crt", "ca-bundle.key")
}

func initCerts(cfg *config.MicroshiftConfig) error {
_, svcNet, err := net.ParseCIDR(cfg.Cluster.ServiceCIDR)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -64,6 +65,15 @@ func RunMicroshift(cfg *config.MicroshiftConfig, flags *pflag.FlagSet) error {
// TODO: change to only initialize what is strictly necessary for the selected role(s)
if _, err := os.Stat(filepath.Join(cfg.DataDir, "certs")); errors.Is(err, os.ErrNotExist) {
initAll(cfg)
} else {
err = loadCA(cfg)
if err != nil {
err := os.RemoveAll(filepath.Join(cfg.DataDir, "certs"))
if err != nil {
klog.ErrorS(err, "removing old certs directory")
}
util.Must(initAll(cfg))
}
}

m := servicemanager.NewServiceManager()
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"time"

"github.com/pkg/errors"
"k8s.io/klog/v2"
)

var (
Expand Down Expand Up @@ -72,6 +73,36 @@ func GenCA(common string, svcName []string, duration time.Duration) (*rsa.Privat
return key, ca, err
}

func LoadRootCA(dir, certFilename, keyFilename string) error {

key, err := ioutil.ReadFile(filepath.Join(dir, keyFilename))
if err != nil {
return errors.Wrap(err, "error reading CA key")
}

if rootKey, err = PemToPrivateKey(key); err != nil {
return errors.Wrap(err, "parsing CA key from PEM")
}

certPath := filepath.Join(dir, certFilename)
cert, err := ioutil.ReadFile(certPath)
if err != nil {
return errors.Wrap(err, "reading CA certificate")
}

if rootCA, err = PemToCertificate(cert); err != nil {
return errors.Wrap(err, "parsing CA certificate")
}

now := time.Now()

if now.After(rootCA.NotAfter) {
klog.ErrorS(nil, "CA has expired: current time %s is after %s", now.Format(time.RFC3339), rootCA.NotAfter.Format(time.RFC3339))
}

return nil
}

func StoreRootCA(common, dir, certFilename, keyFilename string, svcName []string) error {
if rootCA == nil || rootKey == nil {
var err error
Expand Down Expand Up @@ -186,6 +217,15 @@ func (cfg *CertCfg) GenerateSelfSignedCertificate() (*rsa.PrivateKey, *x509.Cert

// GenerateSignedCertificate generate a key and cert defined by CertCfg and signed by CA.
func (cfg *CertCfg) GenerateSignedCertificate(caKey *rsa.PrivateKey, caCert *x509.Certificate) (*rsa.PrivateKey, *x509.Certificate, error) {

if caCert == nil {
return nil, nil, errors.New("Unable to GenerateSignedCertificate with (nil) caCert")
}

if caKey == nil {
return nil, nil, errors.New("Unable to GenerateSignedCertificate with (nil) caKey")
}

// create a private key
key, err := PrivateKey()
if err != nil {
Expand Down