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

Add ability to pass Serial Number and Common Name #15

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 7 additions & 0 deletions kpconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcerts

import (
"errors"
"math/big"
"net"
)

Expand All @@ -22,6 +23,12 @@ type KeyPairConfig struct {
// IPAddresses is a list of IP addresses to include in the certificate
// as Subject Alternative Names.
IPAddresses []string

// SerialNumber is the serial number to use for the certificate.
SerialNumber *big.Int

// CommonName is the Common Name to use for the certificate.
CommonName string
}

// Validate validates the KeyPairConfig ensuring that it is not empty and that
Expand Down
4 changes: 3 additions & 1 deletion testcerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Simplify your testing, and don't hassle with certificates anymore.
package testcerts

import (
"cmp"
ellgreen marked this conversation as resolved.
Show resolved Hide resolved
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -162,10 +163,11 @@ func (ca *CertificateAuthority) NewKeyPairFromConfig(config KeyPairConfig) (*Key
kp := &KeyPair{cert: &x509.Certificate{
Subject: pkix.Name{
Organization: []string{"Never Use this Certificate in Production Inc."},
CommonName: config.CommonName,
},
DNSNames: config.Domains,
IPAddresses: ips,
SerialNumber: big.NewInt(42),
SerialNumber: cmp.Or(config.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},
Expand Down
56 changes: 56 additions & 0 deletions testcerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"math/big"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -273,6 +274,22 @@ func TestKeyPairConfig(t *testing.T) {
},
err: ErrInvalidIP,
},
{
name: "Happy Path - Serial Number provided",
cfg: KeyPairConfig{
Domains: []string{"example.com"},
SerialNumber: big.NewInt(123),
},
err: nil,
},
{
name: "Happy Path - Common Name provided",
cfg: KeyPairConfig{
Domains: []string{"example.com"},
CommonName: "Example Common Name",
},
err: nil,
},
}

for _, c := range tc {
Expand All @@ -290,6 +307,34 @@ func TestKeyPairConfig(t *testing.T) {
}
})
}

t.Run("Serial Number is correct in Key Pair", func(t *testing.T) {
certs, err := NewCA().NewKeyPairFromConfig(KeyPairConfig{
Domains: []string{"example.com"},
SerialNumber: big.NewInt(123),
})
if err != nil {
t.Fatalf("KeyPair Generation Failed expected nil got %v", err)
}

if certs.cert.SerialNumber.Cmp(big.NewInt(123)) != 0 {
t.Fatalf("Unexpected Serial Number expected 123 got %v", certs.cert.SerialNumber)
}
})

t.Run("Common Name is correct in Key Pair", func(t *testing.T) {
certs, err := NewCA().NewKeyPairFromConfig(KeyPairConfig{
Domains: []string{"example.com"},
CommonName: "Example Common Name",
})
if err != nil {
t.Fatalf("KeyPair Generation Failed expected nil got %v", err)
}

if certs.cert.Subject.CommonName != "Example Common Name" {
t.Fatalf("Unexpected Common Name expected 'Example Common Name' got %v", certs.cert.Subject.CommonName)
}
})
}

type FullFlowTestCase struct {
Expand Down Expand Up @@ -327,6 +372,17 @@ func TestFullFlow(t *testing.T) {
},
kpErr: nil,
},
{
name: "Localhost IP, Domain, Serial Number, and Common Name",
listenAddr: "0.0.0.0",
kpCfg: KeyPairConfig{
IPAddresses: []string{"127.0.0.1", "::1"},
Domains: []string{"localhost"},
SerialNumber: big.NewInt(123),
CommonName: "Example Common Name",
},
kpErr: nil,
},
}

for _, c := range tc {
Expand Down