cert

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2017 License: MIT, BSD-3-Clause Imports: 17 Imported by: 0

README

package cert

This package contains functions for creating a local development root certificate that can then be used with net/http tlsConfig servers to dynamically generate SSL certificates in development.

The generated certificates are kept in-memory in a cache and are re-generated on next execution.

On macOS the certificate is installed into the system keychain (a password prompt is shown) so all generated certificates are trusted automatically.

Usage example

You need to run the root certificate creation step once:

  err := cert.CreateCert()

Now you can create an HTTPS server that uses the root certificate to generate valid certificates dynamically:

  cache, err := cert.NewCache()

  tlsConfig := &tls.Config{
    GetCertificate: cache.GetCertificate,
  }

See https://github.com/moomerman/zap/tree/master/cert/example_test.go for a full example.

Credits

The majority of the code for this package was extracted from https://github.com/puma/puma-dev.

Documentation

Overview

Example
package main

import (
	"crypto/tls"
	"fmt"
	"log"
	"net/http"

	"github.com/moomerman/zap/cert"
)

func main() {
	cache, err := cert.NewCache()
	if err != nil {
		log.Fatal("Unable to create new cert cache", err)
	}

	tlsConfig := &tls.Config{
		GetCertificate: cache.GetCertificate,
	}

	server := &http.Server{
		TLSConfig: tlsConfig,
	}

	listener, err := tls.Listen("tcp", ":4443", tlsConfig)
	if err != nil {
		log.Fatal("Unable to create listener", err)
	}

	fmt.Println(server.Serve(listener))
}

Index

Examples

Constants

This section is empty.

Variables

CACert is the self-signed root certificate

Functions

func CreateCert

func CreateCert() error

CreateCert creates a new self-signed root certificate

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a struct to hold the dynamic certificates and a lock

func NewCache

func NewCache() (*Cache, error)

NewCache holds the dynamically generated host certificates

func (*Cache) GetCertificate

func (c *Cache) GetCertificate(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error)

GetCertificate implements the required function for tls config

Jump to

Keyboard shortcuts

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