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
7 changes: 5 additions & 2 deletions pkg/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,11 @@ func certSetup(cfg *config.MicroshiftConfig) (*certchains.CertificateChains, err
return nil, err
}

if err := util.GenKeys(filepath.Join(microshiftDataDir, "/resources/kube-apiserver/secrets/service-account-key"),
"service-account.crt", "service-account.key"); err != nil {
saKeyDir := filepath.Join(microshiftDataDir, "/resources/kube-apiserver/secrets/service-account-key")
if err := util.EnsureKeyPair(
filepath.Join(saKeyDir, "service-account.pub"),
filepath.Join(saKeyDir, "service-account.key"),
); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/kube-apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (s *KubeAPIServer) configure(cfg *config.MicroshiftConfig) error {
},
},
ServiceAccountPublicKeyFiles: []string{
microshiftDataDir + "/resources/kube-apiserver/secrets/service-account-key/service-account.crt",
microshiftDataDir + "/resources/kube-apiserver/secrets/service-account-key/service-account.pub",
},
ServicesSubnet: cfg.Cluster.ServiceCIDR,
ServicesNodePortRange: cfg.Cluster.ServiceNodePortRange,
Expand Down
40 changes: 35 additions & 5 deletions pkg/util/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/pem"
"fmt"
"io/ioutil"
"path/filepath"
"time"

"github.com/pkg/errors"
Expand All @@ -41,8 +40,16 @@ const (
ValidityTenYears = 10 * ValidityOneYear
)

func EnsureKeyPair(pubKeyPath, privKeyPath string) error {
if _, err := getKeyPair(pubKeyPath, privKeyPath); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a log if the error is not nil? Is it relevant enough even though its regenerated afterwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If getKeyPair is not successful, it will return all errors internally. I'd say it's not necessary.

return nil
}

return GenKeys(pubKeyPath, privKeyPath)
}

// GenKeys generates and save rsa keys
func GenKeys(dir, pubFilename, keyFilename string) error {
func GenKeys(pubPath, keyPath string) error {
rsaKey, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return errors.Wrap(err, "error generating RSA private key")
Expand All @@ -58,9 +65,6 @@ func GenKeys(dir, pubFilename, keyFilename string) error {
return err
}

keyPath := filepath.Join(dir, keyFilename)
pubPath := filepath.Join(dir, pubFilename)

if err := keyutil.WriteKey(keyPath, keyPEM); err != nil {
return fmt.Errorf("failed to write the private key to %s: %v", keyPath, err)
}
Expand All @@ -84,3 +88,29 @@ func PublicKeyToPem(key *rsa.PublicKey) ([]byte, error) {
)
return keyinPem, nil
}

func getKeyPair(pubKeyPath, privKeyPath string) (*rsa.PrivateKey, error) {
pubKeys, err := keyutil.PublicKeysFromFile(pubKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to read public key: %w", err)
}
if len(pubKeys) > 1 {
return nil, fmt.Errorf("too many pub keys in file %s", pubKeyPath)
}

privKey, err := keyutil.PrivateKeyFromFile(privKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to read private key: %w", err)
}

rsaPrivKey, ok := privKey.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("only RSA private keys are currently supported")
}

if !rsaPrivKey.PublicKey.Equal(pubKeys[0].(*rsa.PublicKey)) {
return nil, fmt.Errorf("public and private keys don't match")
}

return rsaPrivKey, nil
}