Skip to content

Commit

Permalink
Merge pull request #4 from madflojo/ca
Browse files Browse the repository at this point in the history
Adding more functionality
  • Loading branch information
madflojo authored Apr 7, 2023
2 parents ad7679b + d3c9f81 commit 5b36a4a
Show file tree
Hide file tree
Showing 4 changed files with 503 additions and 135 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,3 @@ jobs:

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
tickeryzer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Install Tickeryzer
run: go get -u github.com/orijtech/tickeryzer/cmd/tickeryzer

- name: Run Tickeryzer
run: tickeryzer ./...
89 changes: 49 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,74 @@
# testcerts

![Actions Status](https://github.com/madflojo/testcerts/actions/workflows/go.yaml/badge.svg?branch=main)
[![Coverage Status](https://coveralls.io/repos/github/madflojo/testcerts/badge.svg?branch=master)](https://coveralls.io/github/madflojo/testcerts?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/madflojo/testcerts)](https://goreportcard.com/report/github.com/madflojo/testcerts) [![Documentation](https://godoc.org/github.com/madflojo/testcerts?status.svg)](http://godoc.org/github.com/madflojo/testcerts)
[![codecov](https://codecov.io/gh/madflojo/testcerts/branch/main/graph/badge.svg?token=H9C9B6I0AS)](https://codecov.io/gh/madflojo/testcerts)
[![Go Report Card](https://goreportcard.com/badge/github.com/madflojo/testcerts)](https://goreportcard.com/report/github.com/madflojo/testcerts)
[![Go Reference](https://pkg.go.dev/badge/github.com/madflojo/testcerts.svg)](https://pkg.go.dev/github.com/madflojo/testcerts)
[![license](https://img.shields.io/github/license/madflojo/testcerts.svg?maxAge=2592000)](https://github.com/madflojo/testcerts/LICENSE)

testcerts is a Go package that makes it easy for developers to generate x509 certificates for testing and development purposes. The package provides an easy-to-use API for generating self-signed certificates and keys for testing.

What makes testcerts unique is its ability to generate certificates and keys with a single line of code, and also its ability to handle saving them to temp and non-temp files, which eliminates the need for developers to handle file operations while testing their code.

Overall, testcerts simplifies the process of generating and managing test certificates, making it a valuable tool for any developer working with x509 certificates.

## Usage

### Generating Certificates to File

The `GenerateCertsToFile` function generates an X.509 certificate and key and writes them to the file paths provided.
Stop saving test certificates in your code repos. Start generating them in your tests.

```go
func TestSomething(t *testing.T) {
err := testcerts.GenerateCertsToFile("/tmp/cert", "/tmp/key")
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 stuff
// do something
}
defer os.Remove(key)
defer os.Remove(cert)

_ = something.Run("/tmp/cert", "/tmp/key")
// do more testing
}
```

### Generating Certificates to Temporary File

The `GenerateCertsToTempFile` function generates an X.509 certificate and key and writes them to randomly generated files in the directory provided or the system's temporary directory if none is provided. The function returns the file paths of the generated files.

```go
func TestSomething(t *testing.T) {
certFile, keyFile, err := testcerts.GenerateCertsToTempFile("/tmp/")
// Start HTTP Listener with test certificates
err = http.ListenAndServeTLS("127.0.0.1:443", cert, key, someHandler)
if err != nil {
// do stuff
// do something
}

_ = something.Run(certFile, keyFile)
// do more testing
}
```

### Generating Certificates

The `GenerateCerts` function generates an X.509 certificate and key and returns them as byte slices.
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.

```go
func TestSomething(t *testing.T) {
cert, key, err := testcerts.GenerateCerts()
if err != nil {
// do stuff
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(),
},
},
}

_ = something.Run(cert, key)
// do more testing
// 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.
Expand Down
Loading

0 comments on commit 5b36a4a

Please sign in to comment.