From ccd96037f7a517c8ce70410c71a74c09df7eef04 Mon Sep 17 00:00:00 2001 From: Benjamin Cane Date: Sun, 19 May 2024 09:53:43 -0700 Subject: [PATCH] removing duplicate code --- testcerts.go | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/testcerts.go b/testcerts.go index 88e5f97..2d62373 100644 --- a/testcerts.go +++ b/testcerts.go @@ -135,35 +135,15 @@ func NewCA() *CertificateAuthority { // NewKeyPair generates a new KeyPair signed by the CertificateAuthority for the given domains. // The domains are used to populate the Subject Alternative Name field of the certificate. func (ca *CertificateAuthority) NewKeyPair(domains ...string) (*KeyPair, error) { - // Create a Certificate - kp := &KeyPair{cert: &x509.Certificate{ - Subject: pkix.Name{ - Organization: []string{"Never Use this Certificate in Production Inc."}, - }, - DNSNames: domains, - SerialNumber: big.NewInt(42), - NotBefore: time.Now().Add(-1 * time.Hour), - NotAfter: time.Now().Add(2 * time.Hour), - ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, - KeyUsage: x509.KeyUsageDigitalSignature, - }} - - var err error - - // Generate KeyPair - var privateKey *ecdsa.PrivateKey - kp.publicKey, privateKey, err = genKeyPair(ca.cert, ca.privateKeyEcdsa, kp.cert) - if err != nil { - return kp, fmt.Errorf("could not generate keypair: %w", err) - } - kp.privateKey, err = keyToPemBlock(privateKey) - if err != nil { - return kp, fmt.Errorf("could not convert private key to pem block: %w", err) + config := KeyPairConfig{Domains: domains} + if len(domains) == 0 { + config.Domains = []string{"localhost"} + config.IPAddresses = []string{"127.0.0.1", "::1"} } - return kp, nil + return ca.NewKeyPairFromConfig(config) } -// NewKeyPairFromConfig generates a new KeyPair signed by the CertificateAuthority for the given configuration. +// NewKeyPairFromConfig generates a new KeyPair signed by the CertificateAuthority from the given configuration. // The configuration is used to populate the Subject Alternative Name field of the certificate. func (ca *CertificateAuthority) NewKeyPairFromConfig(config KeyPairConfig) (*KeyPair, error) { // Validate the configuration