Skip to content

Commit

Permalink
Add new command to generate a new signing certificate for the controller
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Gaiduk <[email protected]>
  • Loading branch information
europaul committed Jan 18, 2024
1 parent dbe8542 commit 94cac32
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/lf-edge/eden/pkg/defaults"
"github.com/lf-edge/eden/pkg/eden"
"github.com/lf-edge/eden/pkg/openevec"
"github.com/lf-edge/eden/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -47,3 +48,29 @@ func newCertsCmd(cfg *openevec.EdenSetupArgs) *cobra.Command {

return certsCmd
}

func newGenSigningCertCmd() *cobra.Command {
var certPath string

var certsCmd = &cobra.Command{
Use: "gen-signing-cert",
Short: "generate new signing certificate for controller",
Long: `Generate a new signing certificate for the controller using the same signing key`,
Run: func(cmd *cobra.Command, args []string) {
if err := utils.GenServerCertFromPrevCertAndKey(certPath); err != nil {
log.Errorf("cannot generate signing cert: %s", err)
} else {
log.Info("GenServerCertEllipticFromPrevCertAndKey done")
}
},
}

edenHome, err := utils.DefaultEdenDir()
if err != nil {
log.Fatal(err)
}

certsCmd.Flags().StringVarP(&certPath, "out", "o", filepath.Join(edenHome, defaults.DefaultCertsDist, "signing-new.pem"), "certificate output path")

return certsCmd
}
1 change: 1 addition & 0 deletions cmd/edenUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newUtilsCmd(configName, verbosity *string) *cobra.Command {
newDownloaderCmd(cfg),
newOciImageCmd(),
newCertsCmd(cfg),
newGenSigningCertCmd(),
newGcpCmd(cfg),
newSdInfoEveCmd(),
newDebugCmd(cfg),
Expand Down
63 changes: 63 additions & 0 deletions pkg/utils/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"time"

"crypto/ecdsa"
Expand Down Expand Up @@ -106,6 +107,68 @@ func GenServerCertElliptic(cert *x509.Certificate, key *rsa.PrivateKey, serial *

}

// GenServerCertFromPrevCertAndKey generate new signing certificate for the controller using the same signing key and saves it to give path
func GenServerCertFromPrevCertAndKey(writePath string) error {
edenHome, err := DefaultEdenDir()
if err != nil {
return err
}

// Read root cert
rootCert, err := ParseCertificate(filepath.Join(edenHome, defaults.DefaultCertsDist, "root-certificate.pem"))
if err != nil {
return err
}

// Read root key
rootKey, err := ParsePrivateKey(filepath.Join(edenHome, defaults.DefaultCertsDist, "root-certificate-key.pem"))
if err != nil {
return err
}

// Read server cert
oldServerCert, err := ParseCertificate(filepath.Join(edenHome, defaults.DefaultCertsDist, "signing.pem"))
if err != nil {
return err
}

// Read ecdsa server key
serverKeyBytes, err := os.ReadFile(filepath.Join(edenHome, defaults.DefaultCertsDist, "signing-key.pem"))
if err != nil {
return err
}
var serverKey *ecdsa.PrivateKey
for block, rest := pem.Decode(serverKeyBytes); block != nil; block, rest = pem.Decode(rest) {
if block.Type == "EC PRIVATE KEY" {
serverKey, err = x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return err
}
break
}
}

// keep all the same except for dates
serverTemplate := *oldServerCert
serverTemplate.NotBefore = time.Now().Add(-10 * time.Second)
serverTemplate.NotAfter = time.Now().AddDate(10, 0, 0)

// create new certificate and write it to file
serverCert := genCertECDSA(&serverTemplate, rootCert, &serverKey.PublicKey, rootKey)
certOut, err := os.Create(writePath)
if err != nil {
return err
}
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: serverCert.Raw}); err != nil {
return err
}
if err := certOut.Close(); err != nil {
return err
}

return nil
}

// WriteToFiles write cert and key
func WriteToFiles(crt *x509.Certificate, key interface{}, certFile string, keyFile string) (err error) {
certOut, err := os.Create(certFile)
Expand Down

0 comments on commit 94cac32

Please sign in to comment.