Documentation
¶
Overview ¶
Package testcerts provides an easy-to-use suite of functions for generating x509 test certificates.
Stop saving test certificates in your code repos. Start generating them in your tests.
func TestFunc(t *testing.T) { // Create and write self-signed Certificate and Key to temporary files cert, key, err := testcerts.GenerateToTempFile("/tmp/") if err != nil { // do something } defer os.Remove(key) defer os.Remove(cert) // Start HTTP Listener with test certificates err = http.ListenAndServeTLS("127.0.0.1:443", cert, key, someHandler) if err != nil { // do something } }
For more complex tests, you can also use this package to create a Certificate Authority and a key pair signed by that Certificate Authority for any test domain you want.
func TestFunc(t *testing.T) { // Generate Certificate Authority ca := testcerts.NewCA() go func() { // Create a signed Certificate and Key for "localhost" certs, err := ca.NewKeyPair("localhost") if err != nil { // do something } // Write certificates to a file err = certs.ToFile("/tmp/cert", "/tmp/key") if err { // do something } // Start HTTP Listener err = http.ListenAndServeTLS("localhost:443", "/tmp/cert", "/tmp/key", someHandler) if err != nil { // do something } }() // Create a client with the self-signed CA client := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ RootCAs: ca.CertPool(), }, }, } // Make an HTTPS request r, _ := client.Get("https://localhost") }
Simplify your testing, and don't hassle with certificates anymore.
Index ¶
- func GenerateCerts(domains ...string) ([]byte, []byte, error)
- func GenerateCertsToFile(certFile, keyFile string) error
- func GenerateCertsToTempFile(dir string) (string, string, error)
- type CertificateAuthority
- func (ca *CertificateAuthority) CertPool() *x509.CertPool
- func (ca *CertificateAuthority) NewKeyPair(domains ...string) (*KeyPair, error)
- func (ca *CertificateAuthority) PrivateKey() []byte
- func (ca *CertificateAuthority) PublicKey() []byte
- func (ca *CertificateAuthority) ToFile(certFile, keyFile string) error
- func (ca *CertificateAuthority) ToTempFile(dir string) (*os.File, *os.File, error)
- type KeyPair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateCerts ¶
GenerateCerts generates an x509 certificate and key. It returns the certificate and key as byte slices, and any error that occurred.
cert, key, err := GenerateCerts() if err != nil { // handle error }
func GenerateCertsToFile ¶
GenerateCertsToFile creates an x509 certificate and key and writes it to the specified file paths.
err := GenerateCertsToFile("/path/to/cert", "/path/to/key") if err != nil { // handle error }
If the specified file paths already exist, it will overwrite the existing files.
func GenerateCertsToTempFile ¶
GenerateCertsToTempFile will create a temporary x509 certificate and key in a randomly generated file using the directory path provided. If no directory is specified, the default directory for temporary files as returned by os.TempDir will be used.
cert, key, err := GenerateCertsToTempFile("/tmp/") if err != nil { // handle error }
Types ¶
type CertificateAuthority ¶
type CertificateAuthority struct {
// contains filtered or unexported fields
}
CertificateAuthority represents a self-signed x509 certificate authority.
func (*CertificateAuthority) CertPool ¶
func (ca *CertificateAuthority) CertPool() *x509.CertPool
CertPool returns a Certificate Pool of the CertificateAuthority Certificate
func (*CertificateAuthority) NewKeyPair ¶
func (ca *CertificateAuthority) NewKeyPair(domains ...string) (*KeyPair, error)
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 (*CertificateAuthority) PrivateKey ¶
func (ca *CertificateAuthority) PrivateKey() []byte
PrivateKey returns the private key of the CertificateAuthority.
func (*CertificateAuthority) PublicKey ¶
func (ca *CertificateAuthority) PublicKey() []byte
PublicKey returns the public key of the CertificateAuthority.
func (*CertificateAuthority) ToFile ¶
func (ca *CertificateAuthority) ToFile(certFile, keyFile string) error
ToFile saves the CertificateAuthority certificate and private key to the specified files. Returns an error if any file operation fails.
func (*CertificateAuthority) ToTempFile ¶
ToTempFile saves the CertificateAuthority certificate and private key to temporary files. The temporary files are created in the specified directory and have random names.
type KeyPair ¶
type KeyPair struct {
// contains filtered or unexported fields
}
KeyPair represents a pair of self-signed x509 certificate and private key.
func (*KeyPair) PrivateKey ¶
PrivateKey returns the private key of the KeyPair.