Documentation
¶
Overview ¶
Package tlsconfig builds tls.Config values from PEM files and keeps the certificate fresh with a file-polling reloader.
Entry points:
- NewServerTLSFromFiles / NewClientTLSFromFiles build a tls.Config with MinVersion TLS 1.2 and ALPN "h2","http/1.1" from cert, key and optional CA bundle files. An optional "<cert basename>.ocsp" file next to the certificate is loaded as an OCSP staple if it is valid and not expired.
- KeypairReloader polls the cert/key files' modification times on a ticker and reloads them (also forced once per hour). Use GetKeypairFunc as tls.Config.GetCertificate on servers and GetClientCertificateFunc as tls.Config.GetClientCertificate on clients.
- NewHTTPTransportWithReloader returns an http.RoundTripper whose client certificate is swapped in on reload.
- UpdateCipherSuites maps cipher suite names to tls.Config.CipherSuites.
Server example:
cfg, err := tlsconfig.NewServerTLSFromFiles(certFile, keyFile, trustedCAFile, clientCAFile, tls.VerifyClientCertIfGiven)
if err != nil {
return err
}
reloader, err := tlsconfig.NewKeypairReloader("", certFile, keyFile, 5*time.Minute)
if err != nil {
return err
}
defer reloader.Close()
cfg.GetCertificate = reloader.GetKeypairFunc()
Client example:
cfg, reloader, err := tlsconfig.NewClientTLSWithReloader(certFile, keyFile, rootsFile, 5*time.Minute)
if err != nil {
return err
}
defer reloader.Close()
client := &http.Client{Transport: &http.Transport{TLSClientConfig: cfg}}
The reloader panics (via the package logger) when it serves a certificate whose NotAfter has passed; callers that cannot tolerate that must validate expiry before use (see transport.TLSInfo.ServerTLSWithReloader).
Index ¶
- func GetCipherSuite(s string) (uint16, bool)
- func LoadX509KeyPairWithOCSP(certFile, keyFile string) (*tls.Certificate, error)
- func NewClientTLSFromFiles(certFile, keyFile, rootsFile string) (*tls.Config, error)
- func NewServerTLSFromFiles(certFile, keyFile, rootsFile, caFile string, clientauthType tls.ClientAuthType) (*tls.Config, error)
- func UpdateCipherSuites(tls *tls.Config, ss []string) error
- func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (*tls.Certificate, error)
- func X509KeyPairWithOCSP(certPEMBlock, keyPEMBlock, ocspStaple []byte) (*tls.Certificate, error)
- type HTTPTransport
- type KeypairReloader
- func (k *KeypairReloader) CertAndKeyFiles() (string, string)
- func (k *KeypairReloader) Close() error
- func (k *KeypairReloader) GetClientCertificateFunc() func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
- func (k *KeypairReloader) GetKeypairFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error)
- func (k *KeypairReloader) Keypair() *tls.Certificate
- func (k *KeypairReloader) LoadedAt() time.Time
- func (k *KeypairReloader) LoadedCount() uint32
- func (k *KeypairReloader) OnReload(f OnReloadFunc) *KeypairReloader
- func (k *KeypairReloader) Reload() error
- type OnReloadFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetCipherSuite ¶
GetCipherSuite returns the ID for a cipher suite name such as "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" and whether the name is known.
func LoadX509KeyPairWithOCSP ¶
func LoadX509KeyPairWithOCSP(certFile, keyFile string) (*tls.Certificate, error)
LoadX509KeyPairWithOCSP reads a PEM certificate chain and private key from files and, if a file named "<certFile without extension>.ocsp" exists, uses its content as the OCSP staple (see X509KeyPairWithOCSP). A missing OCSP file is not an error. The certificate file may contain intermediates after the leaf.
func NewClientTLSFromFiles ¶
NewClientTLSFromFiles builds a client tls.Config (MinVersion TLS 1.2, ALPN h2/http1.1) from PEM files. rootsFile (optional) sets RootCAs; when empty the OS roots are used. certFile/keyFile (optional) set the client certificate for mutual TLS. The certificate is loaded once; use NewClientTLSWithReloader for rotation.
func NewServerTLSFromFiles ¶
func NewServerTLSFromFiles(certFile, keyFile, rootsFile, caFile string, clientauthType tls.ClientAuthType) (*tls.Config, error)
NewServerTLSFromFiles builds a server tls.Config (MinVersion TLS 1.2, ALPN h2/http1.1) from PEM files. certFile and keyFile are required; an OCSP staple is loaded from "<cert basename>.ocsp" if present. rootsFile (optional) is used as both RootCAs and ClientCAs; caFile (optional) overrides ClientCAs. When both are empty the OS roots are used and client certificates cannot be verified. clientauthType is applied as-is. Files that contain no valid certificate produce an empty pool without error. The returned config has no GetCertificate; pair it with KeypairReloader for rotation.
func UpdateCipherSuites ¶
UpdateCipherSuites sets tls.CipherSuites from names, preserving order. An empty list is a no-op. It returns an error if CipherSuites is already set or a name is unknown; names are not filtered for security.
func X509KeyPair ¶
func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (*tls.Certificate, error)
X509KeyPair is X509KeyPairWithOCSP without an OCSP staple. Unlike tls.X509KeyPair it always populates Certificate.Leaf.
func X509KeyPairWithOCSP ¶
func X509KeyPairWithOCSP(certPEMBlock, keyPEMBlock, ocspStaple []byte) (*tls.Certificate, error)
X509KeyPairWithOCSP parses a PEM certificate chain and private key (PKCS#1, PKCS#8 or SEC1; RSA, ECDSA or Ed25519), verifies the key matches the leaf, and sets Certificate.Leaf. Expired certificates are accepted. If ocspStaple is given and the chain has an issuer, the staple is validated against the leaf: a Revoked status is an error, an expired or unparsable staple is dropped with a warning, and a good staple is set as Certificate.OCSPStaple.
Types ¶
type HTTPTransport ¶
type HTTPTransport struct {
// contains filtered or unexported fields
}
HTTPTransport is an http.RoundTripper that re-applies its current TLSClientConfig to the wrapped *http.Transport on every request so that a reloaded client certificate takes effect. Create it with NewHTTPTransportWithReloader.
func NewHTTPTransportWithReloader ¶
func NewHTTPTransportWithReloader( certFile, keyFile, rootsFile string, checkInterval time.Duration, HTTPUserTransport *http.Transport) (*HTTPTransport, error)
NewHTTPTransportWithReloader returns an HTTPTransport that installs a fresh TLS client config (with the reloaded certificate) on the underlying *http.Transport whenever the cert file changes, closing idle connections so new dials use it. When HTTPUserTransport is nil a clone of http.DefaultTransport with 100 max idle/per-host connections is used. The caller must Close the returned transport to stop the reloader.
func (*HTTPTransport) Close ¶
func (t *HTTPTransport) Close() error
Close stops the certificate reloader. It returns an error on a second call. The wrapped *http.Transport is left open.
type KeypairReloader ¶
type KeypairReloader struct {
// contains filtered or unexported fields
}
KeypairReloader loads a TLS certificate/key pair from files and reloads it in a background goroutine when either file's modification time changes, or at least once per hour. Obtain the current pair via Keypair, GetKeypairFunc (server) or GetClientCertificateFunc (client). Close stops the goroutine. Serving an expired certificate panics via the package logger.
func NewClientTLSWithReloader ¶
func NewClientTLSWithReloader(certFile, keyFile, rootsFile string, checkInterval time.Duration) (*tls.Config, *KeypairReloader, error)
NewClientTLSWithReloader is NewClientTLSFromFiles plus a KeypairReloader wired as GetClientCertificate, polling every checkInterval. certFile and keyFile are required. The caller must Close the returned reloader.
func NewKeypairReloader ¶
func NewKeypairReloader(label, certPath, keyPath string, checkInterval time.Duration) (*KeypairReloader, error)
NewKeypairReloader loads the pair once (returning an error on failure) and starts a goroutine that polls the files every checkInterval. label is used in logs and defaults to the certificate file's base name. The initial load includes a fixed 100ms delay. The caller must call Close.
func (*KeypairReloader) CertAndKeyFiles ¶
func (k *KeypairReloader) CertAndKeyFiles() (string, string)
CertAndKeyFiles returns the certificate and key file paths being watched.
func (*KeypairReloader) Close ¶
func (k *KeypairReloader) Close() error
Close stops the polling goroutine, blocking until it acknowledges. It is nil-safe and returns an error on a second call.
func (*KeypairReloader) GetClientCertificateFunc ¶
func (k *KeypairReloader) GetClientCertificateFunc() func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
GetClientCertificateFunc returns a function suitable for tls.Config.GetClientCertificate that serves the current pair. It panics if the pair has expired.
func (*KeypairReloader) GetKeypairFunc ¶
func (k *KeypairReloader) GetKeypairFunc() func(*tls.ClientHelloInfo) (*tls.Certificate, error)
GetKeypairFunc returns a function suitable for tls.Config.GetCertificate that serves the current pair. It panics if the pair has expired.
func (*KeypairReloader) Keypair ¶
func (k *KeypairReloader) Keypair() *tls.Certificate
Keypair returns the current pair, or nil on a nil receiver. It panics if the pair has expired.
func (*KeypairReloader) LoadedAt ¶
func (k *KeypairReloader) LoadedAt() time.Time
LoadedAt returns the UTC time of the last successful load.
func (*KeypairReloader) LoadedCount ¶
func (k *KeypairReloader) LoadedCount() uint32
LoadedCount returns the number of successful loads, including the first.
func (*KeypairReloader) OnReload ¶
func (k *KeypairReloader) OnReload(f OnReloadFunc) *KeypairReloader
OnReload registers a handler called after each reload that changed the certificate file's modification time. A nil handler is ignored.
func (*KeypairReloader) Reload ¶
func (k *KeypairReloader) Reload() error
Reload synchronously re-reads the pair from disk, retrying up to three times with a 100ms sleep before each attempt while holding the write lock, and notifies OnReload handlers if the certificate's mtime changed. It is a no-op returning nil if another Reload is in progress. On failure the previous pair is kept.
type OnReloadFunc ¶
type OnReloadFunc func(pair *tls.Certificate)
OnReloadFunc is invoked, in its own goroutine, with the newly loaded certificate each time the certificate file's modification time changes.