mtls

package
v0.6.33 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package mtls is mutual TLS between services: a Source (the service's live certificate, the trusted roots and who may call it — the Consul driver's follows Consul's CA and intentions) turned into the TLS of a public port and of clients, and the caller of a request: its service, taken from the SPIFFE URI of the certificate it presented.

Certificates are read on every handshake, so a renewed certificate or a rotated root applies to the next connection without restarting anything; connections already open keep the certificates they were made with.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoIdentity: the source has no certificate or roots yet.
	ErrNoIdentity = errors.New("mtls: no certificate or roots yet")
	// ErrPeer: the peer's certificate is not a service's of this mesh.
	ErrPeer = errors.New("mtls: peer")
)

Errors.

Functions

func ClientConfig

func ClientConfig(src Source, target string, nextProtos ...string) *tls.Config

ClientConfig is the TLS of a call to target: the source's certificate, the peer verified against the current roots and as target. nextProtos: h2 for gRPC, http/1.1 for HTTP.

func ConnContext

func ConnContext(ctx context.Context, conn net.Conn) context.Context

ConnContext puts the peer of an HTTP connection into its requests' context (http.Server.ConnContext).

func GRPCServerOptions

func GRPCServerOptions(src Source) []grpc.ServerOption

GRPCServerOptions are the options of a gRPC server behind an mTLS listener: the peer as auth info, and every call authorized by src, the caller put in its context (PeerFrom).

func Middleware

func Middleware(src Source) func(http.Handler) http.Handler

Middleware authorizes every request of an mTLS port: 403 for a caller src refuses, 401 without a service certificate.

func ServerConfig

func ServerConfig(src Source) *tls.Config

ServerConfig is the TLS of a port only services of the mesh reach: the source's certificate, a client certificate required and verified against the current roots. It offers h2 (gRPC) and http/1.1.

func WithPeer

func WithPeer(ctx context.Context, p Peer) context.Context

WithPeer is ctx carrying p.

Types

type AuthInfo

type AuthInfo struct {
	credentials.CommonAuthInfo

	Peer Peer
}

AuthInfo is the gRPC auth info of a connection the listener's TLS verified.

func (AuthInfo) AuthType

func (AuthInfo) AuthType() string

AuthType implements credentials.AuthInfo.

type Failing

type Failing interface{ Err() error }

Failing is a source that cannot have an identity, and says why.

type Peer

type Peer struct {
	// Service is the peer's service name.
	Service string
	// Namespace, Partition and Datacenter of the service (Consul's; the
	// partition is "default" when the URI names none).
	Namespace, Partition, Datacenter string
	// TrustDomain is the CA's (the URI's host).
	TrustDomain string
	// URI is its SPIFFE identity:
	// spiffe://<trust domain>[/ap/<partition>]/ns/<ns>/dc/<dc>/svc/<service>.
	URI string
}

Peer is the service on the other end.

func ParseURI added in v0.6.21

func ParseURI(uri *url.URL) (Peer, bool)

ParseURI reads a SPIFFE URI in Consul's service identity form: spiffe://<trust domain>[/ap/<partition>]/ns/<ns>/dc/<dc>/svc/<service>, every segment present and non-empty, nothing else.

func PeerFrom

func PeerFrom(ctx context.Context) (Peer, bool)

PeerFrom is the peer ctx carries.

func PeerOf

func PeerOf(conn net.Conn) (Peer, bool)

PeerOf is the verified peer of a server connection: a *tls.Conn, possibly inside cmux's connection.

func ServiceOf

func ServiceOf(cert *x509.Certificate) (Peer, bool)

ServiceOf is the service a certificate speaks for: its spiffe URI in Consul's service identity form, exactly.

type Source

type Source interface {
	// Certificate is the service's current certificate; nil until there is one.
	Certificate() *tls.Certificate
	// Roots are the CA certificates peers' chains must lead to; nil until known.
	Roots() *x509.CertPool
	// Authorize decides whether caller (a service name) may call this
	// service, and why.
	Authorize(caller string) (bool, string)
}

Source is a service's identity.

Jump to

Keyboard shortcuts

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