testcerts

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 10 Imported by: 0

README

testcerts

Actions Status codecov Go Report Card Go Reference license

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.

Contributing

If you find a bug or have an idea for a feature, please open an issue or a pull request.

License

testcerts is released under the MIT License. See LICENSE for details.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCerts

func GenerateCerts(domains ...string) ([]byte, []byte, error)

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

func GenerateCertsToFile(certFile, keyFile string) error

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

func GenerateCertsToTempFile(dir string) (string, string, error)

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 NewCA

func NewCA() *CertificateAuthority

NewCA creates a new CertificateAuthority.

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

func (ca *CertificateAuthority) ToTempFile(dir string) (*os.File, *os.File, error)

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

func (kp *KeyPair) PrivateKey() []byte

PrivateKey returns the private key of the KeyPair.

func (*KeyPair) PublicKey

func (kp *KeyPair) PublicKey() []byte

PublicKey returns the public key of the KeyPair.

func (*KeyPair) ToFile

func (kp *KeyPair) ToFile(certFile, keyFile string) error

ToFile saves the KeyPair certificate and private key to the specified files. Returns an error if any file operation fails.

func (*KeyPair) ToTempFile

func (kp *KeyPair) ToTempFile(dir string) (*os.File, *os.File, error)

ToTempFile saves the KeyPair certificate and private key to temporary files. The temporary files are created in the specified directory and have random names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL