How to Configure mTLS Between Services
Issue short-lived SAN certificates, require and verify client certificates on every hop, extract identity from the cert, and automate rotation.
On this page
Introduction
Service-to-service authentication is the layer most fleets get wrong by default: traffic inside the cluster is trusted because it is inside the cluster, which collapses the moment one workload is compromised, because every other service is now reachable with no further questions asked. Shared secrets improve it slightly and rot badly, ending up in environment variables, config files, and chat history. Mutual TLS does the job properly with machinery TLS already has: every service presents a certificate, every service verifies its peer’s certificate against a private CA, and both sides have cryptographically proven identities before a single application byte moves. The operational ingredients that make it real rather than aspirational are a CA that issues automatically, certificates that are short-lived, and authorization wired to the certificate identity instead of the network. This tutorial builds the whole path, private CA and issuance, server and client configuration in Go, identity extraction for authorization, and the rotation and failure-mode discipline that separates a working deployment from a weekend project.
The CA and certificate profile
The private CA is the trust root, and its design decisions are workload identity and lifetime. Identity: each service certificate carries the service’s name in a Subject Alternative Name, and the SPIFFE convention, spiffe://cluster/ns/team/sa/service, standardizes this as a URI SAN so the identity is portable across mesh frameworks. Authorization rules then read naturally: the payments service may call the ledger service. Lifetime: internal certificates should live hours to days, not years, because short lifetimes cap the value of a stolen key and make revocation largely unnecessary, expiry does the revoking. Issuance must therefore be automatic, and cert-manager on Kubernetes is the standard machinery: a Certificate resource with a dnsNames or uriSANs template, an Issuer pointing at the CA, and a Secret that receives the rotating key pair.
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: ledger-serving-cert
spec:
secretName: ledger-tls
duration: 24h
renewBefore: 8h
dnsNames: [ledger.payments.svc.cluster.local]
issuerRef:
name: internal-ca
kind: ClusterIssuer
The renewBefore field is the rotation headroom: the certificate is reissued a third of its lifetime early, so a failed renewal still leaves working hours to fix it. Alerting on issuance failure is the operational teeth, covered with the other failure modes later.
Require and verify on both sides
The server side requests and verifies client certificates, which is the mutual in mTLS; the client side verifies the server certificate against the same CA. In Go, the configuration is explicit and small.
func serverTLS(caPEM, certPEM, keyPEM []byte) *tls.Config {
ca := x509.NewCertPool()
ca.AppendCertsFromPEM(caPEM)
cert, _ := tls.X509KeyPair(certPEM, keyPEM)
return &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.VerifyClientCertIfGiven, // or RequireAnyClientCert
ClientCAs: ca,
MinVersion: tls.VersionTLS12,
}
}
func clientTLS(caPEM, certPEM, keyPEM []byte) *tls.Config {
ca := x509.NewCertPool()
ca.AppendCertsFromPEM(caPEM)
cert, _ := tls.X509KeyPair(certPEM, keyPEM)
return &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: ca,
MinVersion: tls.VersionTLS12,
}
}
One configuration choice deserves the honest treatment: RequireAnyClientCert versus VerifyClientCertIfGiven. The strict setting rejects any client without a valid chain, which is the security posture mTLS exists for. The lenient setting accepts the connection and moves the decision to application code, which fits deployments that must serve both mTLS and public traffic on one listener, at the cost of every handler needing to enforce what the handshake did not. Whatever the choice, the verification of the chain against the private CA is the gate, and a deployment that skips ClientCAs verification has transport encryption with no authentication, which is a different and much weaker property.
Authorization from certificate identity
Authentication without authorization is a bouncer checking IDs at a door with no guest list. After the handshake, the peer’s certificate carries the identity, and the authorization layer maps identities to permissions. Extract the SAN, prefer the SPIFFE URI form for portability, and check it against a policy table.
func authorizePeer(r *http.Request) error {
if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
return fmt.Errorf("no client certificate")
}
id := spiffeID(r.TLS.PeerCertificates[0])
allowed, ok := policy[id]
if !ok || !allowed.contains(r.URL.Path) {
return fmt.Errorf("caller %s not authorized for %s", id, r.URL.Path)
}
return nil
}
The policy table is ordinary configuration, versioned and reviewed like any access control. What it replaces is the older heuristics: IP allowlists that break under dynamic scheduling and say nothing about compromise of an allowed IP, and shared API keys that cannot distinguish two services holding the same key. Service mesh frameworks, Istio with its AuthorizationPolicy, automate exactly this shape, peer identity from mTLS plus path and method rules, and hand-rolled deployments converge on the same design, which is a sign the design is right rather than a sign you should hand-roll it.
Rotation and the failure modes
Short-lived certificates make rotation the system’s heartbeat, and the failure modes are all timing-shaped. Renewal failure: cert-manager retries, and the renewBefore headroom means the fleet keeps working while the failure persists, but only for hours, so alert on issuance failure with the urgency of a disk filling. Clock skew: a certificate valid-from in the future reads as not-yet-valid on a skewed node, so time synchronization is a prerequisite, not a nicety. CA rotation: the trust root itself expires eventually, and rotating it means serving both old and new CA chains during the transition window, both in ClientCAs and RootCAs pools, long enough for every workload to cycle its certificates, a change that deserves a rehearsal in staging. Restart behavior: services must load certificates from disk at start and ideally reload on change, or a rotated certificate leaves a running process presenting the expired one until the next deploy. The drill that finds all four is scheduled: expire a certificate deliberately in staging, watch renewal fire, break renewal, watch the alert, and verify the fleet survives the whole exercise without a page to the on-call.
Verify end to end
mTLS deployments fail quietly, a misconfigured service accepts unauthenticated traffic for months, so verification belongs in the pipeline. Handshake verification: for every service, an unauthenticated client must fail the handshake, and a client with a certificate from a different CA must fail too, two probes that catch the lenient-mode misconfiguration and the missing ClientCAs respectively. Identity verification: a call from service A must present A’s SAN, and the authorization table must be exercised with both allowed and denied pairs in integration tests. Rotation verification: in staging, force a certificate near expiry and assert the fleet serves traffic continuously across renewal. Tooling: openssl s_client with the client certificate confirms the handshake interactively, and the Go tls.ConnectionState exposed per request, PeerCertificates, VerifiedChains, gives the observability to log caller identity per request, which turns authorization debugging from speculation into grep.
openssl s_client -connect ledger:8443 -CAfile internal-ca.pem \\
-cert frontend.pem -key frontend-key.pem -brief
Common pitfalls
- Encryption without authentication. Skipping client certificate verification yields TLS shape with none of the identity guarantees; ClientCAs is the gate.
- Year-long certificates. Stolen keys stay valuable for months; short lifetimes are the revocation story, and automation is what makes them possible.
- Identity ignored after handshake. The SAN sits in the connection state while authorization still checks IPs; wire policy to the identity you just paid to verify.
- **Lenient mode without enforcement. ** VerifyClientCertIfGiven pushes the decision to every handler; one forgetful handler is an open door.
- **No reload on rotation. ** Running processes serve expired certificates until restart; load from disk and reload on change.
- **CA rotation without a transition window. ** Serving both chains during rotation is the difference between a maintenance window and an outage.
Conclusion
Mutual TLS turns service-to-service trust from network folklore into cryptographic identity. Run an automated CA, cert-manager on Kubernetes or equivalent, issuing short-lived certificates with SAN or SPIFFE identities, and renew them with headroom so rotation is heartbeat rather than event. Require and verify client certificates on every hop, with the strict posture unless a dual-purpose listener forces leniency, and then enforce in every handler. Authorize by certificate identity, replacing IP allowlists and shared secrets with policy tables that read like the architecture diagram. Verify adversarially, unauthenticated and wrong-CA clients must fail, identity must appear in logs, rotation must be rehearsed, and treat issuance failure with the urgency of the disk-filling event it is. The machinery is mature, the failure modes are known, and the result is an interior network where every connection answers the question of who is calling.
Sources: RFC 8446: TLS 1.3, cert-manager documentation, Istio: mutual TLS.
FAQ
- How is mTLS different from regular TLS?
- Regular TLS authenticates the server to the client. Mutual TLS also authenticates the client to the server by requiring a client certificate, so both sides prove identity before any application data flows.
- Why short-lived certificates?
- Short lifetimes shrink the blast radius of a stolen key and make revocation mostly unnecessary: a certificate expiring in hours cannot outlive an incident response. Rotation must be automatic, because manual renewal at that cadence fails.
- Do I still need tokens or API keys with mTLS?
- mTLS authenticates the service, the workload. End-user identity still needs its own mechanism, a token forwarded with the request. What mTLS replaces is service-to-service secrets and IP allowlists, not user authentication.
Report a problem with this articleOpens a pre-filled issue on GitHub.