DEV Community

Manoj Kumar Kagitha
Manoj Kumar Kagitha

Posted on

Running your own private CA for internal mTLS

Running your own private CA for internal mTLS

Every service in our stack talks to another service. Most of that traffic
stays inside our own network. For years, the standard answer was to trust
the network. If you could reach the service, you were allowed to call it.

That answer stopped being good enough. Networks get segmented wrong.
A compromised host inside the perimeter can reach anything the network
lets it reach. mTLS fixes the trust question at the connection level:
both sides prove who they are with a certificate before any data moves.

The catch is the certificates. You need a certificate authority to
issue them, and the public CAs will not issue certs for your internal
hostnames. So you run your own.

What a private CA actually is

A private CA is a root certificate you generate yourself, plus a small
process for signing certificates with it. Your services trust the root.
Any cert signed by it is valid inside your network. Nothing else changes.
TLS works the same way it does on the public internet. The only
difference is who you trust as the issuer.

The root key is the whole game. Whoever holds it can issue a cert for
any internal name. Keep it offline. Keep it in one place. Sign with an
intermediate if you want the root to stay locked away, and keep the
intermediate short-lived enough that a compromise has a ceiling.

Issuing the root and a service cert

This is the whole setup, with openssl. Nothing here needs a product.

# Root CA: one time, offline
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -key ca.key -sha256 -days 3650 \
  -out ca.crt -subj "/CN=Internal Root CA"

# Service cert: per service, short-lived
openssl genrsa -out payments.key 2048
openssl req -new -key payments.key -out payments.csr \
  -subj "/CN=payments.internal"
openssl x509 -req -in payments.csr -CA ca.crt -CAkey ca.key \
  -CAcreateserial -out payments.crt -days 90 -sha256
Enter fullscreen mode Exit fullscreen mode

Ninety days is deliberate. Short-lived certs limit the damage when a
key leaks, and they force the renewal process to work. If renewal is
manual and painful, a 90-day lifetime will surface that pain fast. That
is the point. Automate renewal early, or the CA becomes a ticket queue
with a different name.

Where the root goes

Every client that calls an internal service needs the root cert in its
trust store. That is the one distribution problem you have to solve.
In practice it is a config item, the same as any other: baked into the
container image, mounted from a secret store, or installed by the
provisioning step that already sets up the host.

The failure mode to watch is partial distribution. One service has the
new root, another still has the old one, and calls fail with a TLS
error that looks like a network problem. Roll the root out like a
config change: same version everywhere, verified before the old one
retires. Keep two roots trusted during a rotation, then drop the old
one once every client reports the new one.

mTLS at the gateway

The gateway is where this pays off. In our setup, Apigee X and Azure
APIM sit in front of internal APIs. The gateway already terminates TLS
from outside callers. With mTLS, the gateway also checks the client
certificate on the way in, and presents its own certificate to the
backend service on the way out.

That gives you two things a network rule cannot. First, identity. The
gateway knows which service is calling, not just which IP the call
came from. IPs get reused. Certificates name the caller. Second,
revocation. When a service is decommissioned or a key is suspected,
you revoke its cert. The next call fails at the handshake. No firewall
rule to update, no allowlist to edit.

The backend check is simple to state: the service accepts a connection
only if the presented cert chains to the internal root and names an
expected caller. Everything else gets a handshake failure and nothing
more.

Rotation without an outage

Certs expire. Plan for it from day one.

  1. Issue the replacement cert while the old one is still valid.
  2. Deploy it next to the old one, or swap it in behind a reload that does not drop connections.
  3. Watch for handshake failures for a day. If none, the old cert can go.
  4. Automate steps 1 and 2. A cron job and a small script beat a calendar reminder every time.

The most common outage I have seen with internal certs is not a
compromise. It is an expired cert on a Friday night that nobody
noticed because the expiry alert went to a mailbox nobody reads.
Alert on expiry at 30 days, 14 days, and 7 days, and send it
somewhere a human actually looks.

What a private CA does not do

It does not replace authentication. mTLS proves which service is
calling. It does not prove which user is behind the call, or whether
that user is allowed to do what they asked for. Keep your auth checks
where they are. The cert gets you a trusted channel. Authorization
still happens inside the service.

It also does not help if your private keys leak. A CA is only as safe
as the keys it issues. Store service keys in your secret store, not in
the repo, not in the image layers, not in a shared drive. The same
discipline you apply to API keys applies here, with higher stakes.

The takeaway

A private CA is a small piece of infrastructure with an outsized
return. A root cert, a signing process, short-lived service certs,
and automated renewal. In exchange, every internal call carries
proof of who is on each end, and revocation is a single action
instead of a firewall change.

Start with one pair of services. Get the issue, trust, and renewal
loop working there. Then extend it. The CA does not need to cover
everything on day one. It needs to work reliably where it does
cover, because the day it fails is the day every internal call
fails with it.

Top comments (0)