Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GenerateNewCA not write a file. call WriteCertAndKey after if it… #152

Merged
merged 3 commits into from
Sep 12, 2024
Merged
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
15 changes: 8 additions & 7 deletions pkg/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@ func GenerateAllCerts(outDir string, privateCACert *x509.Certificate, privateCAK

if privateCACert == nil && privateCAKey == nil {
// If privateCACert and privateCAKey are both nil, we will generate a new one
privateCACertBytes, privateCAKeyBytes, err := GenerateNewCA(path.Join(outDir, "ca", "private_ca"))
var privateCACertDER []byte
privateCACertDER, privateCAKey, err = GenerateNewCA()
if err != nil {
return fmt.Errorf("error creating private ca pair: %w", err)
}
privateCACert, err = ParsePemCertificate(privateCACertBytes)
privateCACertBytes, _, err := WriteCertAndKey(privateCACertDER, privateCAKey, path.Join(outDir, "ca", "private_ca"))
if err != nil {
return fmt.Errorf("error parsing generated private_ca.crt: %w", err)
return fmt.Errorf("error writing private ca: %w", err)
}
privateCAKey, err = ParsePemKey(privateCAKeyBytes)
privateCACert, err = ParsePemCertificate(privateCACertBytes)
if err != nil {
return fmt.Errorf("error parsing generated private_ca.key: %w", err)
return fmt.Errorf("error parsing generated private_ca.crt: %w", err)
}
} else if privateCACert == nil || privateCAKey == nil {
// If only one of them is nil, we can't continue
Expand Down Expand Up @@ -222,7 +223,7 @@ func WriteCertAndKey(certDER []byte, certKey *rsa.PrivateKey, certKeyBase string
}

// GenerateNewCA generates a new CA
func GenerateNewCA(certKeyBase string) ([]byte, []byte, error) {
func GenerateNewCA() ([]byte, *rsa.PrivateKey, error) {
// Generate a new RSA private key
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
Expand Down Expand Up @@ -256,7 +257,7 @@ func GenerateNewCA(certKeyBase string) ([]byte, []byte, error) {
return nil, nil, err
}

return WriteCertAndKey(certDER, privateKey, certKeyBase)
return certDER, privateKey, nil
}

// GenerateCASignedCert generates a new key/cert signed by the given CA
Expand Down