Documentation
¶
Overview ¶
Package tls provides hardened, opinionated TLS plumbing for Go servers and clients: a curated default crypto/tls.Config (TLS 1.2 floor, AEAD cipher suites, modern curve preferences), the typed Pair shape (enabled/cert/key plus the client-CA fields for server-side mTLS) with a pure per-field merge (ResolvePair), the Pair.ServerConfig and ClientConfig builders, the Pair.ApplyTo merge into a caller-owned config, and the CertPool helper for trusting private CAs.
The package is framework-free: it works entirely from typed Pair values, so callers own how those values are sourced (flags, environment, a config file). A single certificate can serve multiple transports by resolving a shared Pair against per-transport overrides with ResolvePair.
Index ¶
Examples ¶
Constants ¶
const ( // ClientAuthRequest asks the client for a certificate but neither // requires nor verifies one (tls.RequestClientCert). ClientAuthRequest = "request" // ClientAuthVerifyIfGiven does not require a client certificate, but // verifies any that is presented against ClientCAs // (tls.VerifyClientCertIfGiven) — useful for mixed-auth listeners. ClientAuthVerifyIfGiven = "verify-if-given" // ClientAuthRequireVerify requires a client certificate chaining to // ClientCAs (tls.RequireAndVerifyClientCert). This is the mode implied // when ClientCAs is set and ClientAuth is empty. ClientAuthRequireVerify = "require-verify" )
Client-certificate enforcement modes accepted by Pair.ClientAuth. Any other non-empty value is rejected (fail closed).
Variables ¶
This section is empty.
Functions ¶
func CertPool ¶
CertPool builds an x509 certificate pool seeded with the given PEM CA/cert files, so clients can trust certificates that are not in the system roots (self-signed or private CA). Pass the same cert files the servers present to share one trust anchor across gRPC, HTTP and the gateway.
func ClientConfig ¶
ClientConfig returns a hardened client TLS config (DefaultConfig) that trusts the given CA/cert files via a custom pool. With no files it returns the default config, which trusts the system roots.
func DefaultConfig ¶
DefaultConfig returns the hardened TLS configuration shared across HTTP and gRPC servers and the HTTP client. It enforces TLS 1.2 minimum with curated AEAD cipher suites and modern curve preferences.
Example ¶
package main
import (
"fmt"
"gitlab.com/phpboyscout/go/tls"
)
func main() {
// DefaultConfig returns the shared hardened TLS configuration used by the
// HTTP, gRPC and gateway transports.
cfg := tls.DefaultConfig()
fmt.Println("Min TLS version:", cfg.MinVersion)
fmt.Println("Cipher suites:", len(cfg.CipherSuites))
}
Output: Min TLS version: 771 Cipher suites: 6
Types ¶
type Pair ¶
type Pair struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
Cert string `mapstructure:"cert" yaml:"cert" json:"cert"`
Key string `mapstructure:"key" yaml:"key" json:"key"`
// ClientCAs lists PEM CA/cert files client certificates must chain to.
// Empty means no client-certificate auth (unless ClientAuth is set).
ClientCAs []string `mapstructure:"client_cas" yaml:"client_cas" json:"client_cas"`
// ClientAuth selects the client-certificate mode: "" (none, unless
// ClientCAs is set — then require-verify), "request", "verify-if-given"
// or "require-verify". Any other value is a hard error (fail closed).
ClientAuth string `mapstructure:"client_auth" yaml:"client_auth" json:"client_auth"`
}
Pair is the typed TLS settings block used to configure TLS for any transport. It carries struct tags so the same shape marshals to and from config consistently wherever it is used.
Beyond the enabled/cert/key triple, the optional client-CA fields drive server-side mTLS: ClientCAs lists PEM CA files that client certificates must chain to, and ClientAuth selects the enforcement mode (see the ClientAuth* constants). Both are consumed by ServerConfig/ApplyTo; an empty ClientCAs with an empty ClientAuth means no client-certificate auth, preserving the zero-value behaviour.
func ResolvePair ¶
func ResolvePair(shared Pair, transport Pair, overrides PairOverrides) Pair
ResolvePair resolves TLS settings from already-materialised typed values. It starts from the shared pair and overrides individual fields when the transport section explicitly supplied them.
func (Pair) ApplyTo ¶ added in v0.1.2
ApplyTo merges this pair into a caller-supplied server TLS config instead of replacing it: the pair's certificate is appended to cfg.Certificates, each of nextProtos is appended to cfg.NextProtos unless already advertised, and the pair's client-CA policy (ClientCAs/ClientAuth) is applied only when cfg does not already carry one.
Precedence: everything the caller set on cfg survives. In particular, when cfg already has a non-nil ClientCAs pool or a ClientAuth other than tls.NoClientCert, the caller's client-certificate policy wins and the pair's ClientCAs/ClientAuth fields are ignored — though an invalid ClientAuth value still errors, so a typo never silently disables mTLS.
func (Pair) Certificate ¶
func (p Pair) Certificate() (cryptotls.Certificate, error)
Certificate loads the X509 key pair described by the pair.
func (Pair) ServerConfig ¶
ServerConfig returns the hardened DefaultConfig with this pair's certificate loaded and its client-CA policy (if any) applied. Pass nextProtos to advertise ALPN protocols (e.g. "h2" for a raw gRPC TLS listener); when empty the config's defaults are left as-is.