Prompt
How do I set up an access control gateway for mTLS and JWT on internal APIs?
Latest observation
A good pattern is to put a gateway in front of your internal APIs that performs both transport authentication and request authentication/authorization:
- mTLS proves the client service is a trusted workload.
- JWT proves the caller has a valid identity/claims for the specific request.
1) Recommended architecture
Client service → Gateway → Internal API
At the gateway:
-
Terminate mTLS
- Validate the client certificate against your internal CA.
- Optionally verify SAN/URI/DNS names and certificate rotation policy.
- Map certificate identity to a service identity, team, or workload label.
-
Validate JWT
- Verify signature using JWKS or public keys from your IdP.
- Check standard claims:
ississueraudaudienceexp,nbf,iat- optionally
sub,scope,roles,tenant
- Enforce audience per API or route.
-
Authorize
- Apply policy that combines:
- certificate identity
- JWT subject/claims
- route/method
- environment/tenant
- Example: “service A with cert CN/SAN X may call
GET /orders/*only if JWT hasscope=orders.read.”
- Apply policy that combines:
-
Pass trusted identity downstream
- Forward only sanitized identity headers or a signed internal token.
- Do not blindly trust client-supplied
X-User-*headers.
2) What to validate
mTLS checks
- Cert chains to your private/internal CA
- Cert is not expired
- Revocation/rotation strategy is in place
- SAN matches expected workload identity
- Key usage and EKU are appropriate for client auth
JWT checks
- Signature valid
issmatches your identity provideraudmatches the gateway/API- Token not expired
- Optional: nonce/jti checks if needed
- Claims match authorization policy
3) Policy model
A practical model is:
-
Authentication layer
- mTLS required for all internal traffic
- JWT required for all user/workflow/API requests
-
Authorization layer
- RBAC or ABAC:
- RBAC: roles/scopes
- ABAC: attributes like service identity, namespace, tenant, environment
- RBAC or ABAC:
Example policy:
- Allow if:
- client cert SAN is
spiffe://prod/ns/payments/sa/billing - JWT
aud=internal-api - JWT
scopecontainspayments:read - request is
GET /v1/payments/*
- client cert SAN is
4) Implementation options
Option A: API gateway / reverse proxy
Common choices:
- Envoy
- NGINX
- Kong
- Traefik
- HAProxy
These can:
- enforce mTLS
- validate JWTs
- integrate with ext-auth services
- apply route-based policy
Option B: Service mesh
If this is east-west traffic between internal services:
- Istio
- Linkerd
- Consul Connect
- Cilium Service Mesh
A mesh often handles mTLS automatically, while the gateway handles JWT and app-level auth.
5) Example flow
- Client service connects to gateway using mTLS.
- Gateway authenticates the client cert.
- Client sends request with
Authorization: Bearer <JWT>. - Gateway verifies JWT against JWKS.
- Gateway evaluates policy:
- cert identity allowed?
- token audience correct?
- scopes/roles sufficient?
- Gateway forwards request to internal API with trusted headers:
X-Client-IdX-Client-SubjectX-User-SubX-Auth-Scopes
Optionally, the gateway can mint an internal signed token for backend services instead of forwarding the original JWT.
6) Example with Envoy-like logic
You generally configure:
- Downstream TLS context for mTLS
- JWT authn filter for token validation
- RBAC/ext-authz for authorization
Conceptually:
- Reject if client cert invalid
- Reject if JWT invalid
- Reject if cert identity and JWT claims don’t satisfy policy
- Otherwise route to backend
7) Security best practices
- Use a private CA for workload certs
- Automate cert issuance and rotation
- Keep token lifetimes short
- Use least-privilege scopes
- Separate service-to-service JWTs from human user JWTs
- Strip incoming spoofable identity headers
- Log auth decisions with correlation IDs
- Cache JWKS and CA material safely
- Enforce clock sync across systems
- Prefer SPIFFE/SPIRE if you want standardized workload identity
8) A simple rule set to start with
If you want a strong baseline:
- All internal API requests must use mTLS
- All requests must include a valid JWT
- Gateway checks:
- cert subject/SAN matches service identity
- JWT
audequals API name - JWT
issmatches your IdP - JWT scope matches endpoint
- Backend trusts only gateway-added identity context
9) If you want to implement quickly
Tell me what stack you use, and I can give you a concrete config for one of these:
- Envoy
- NGINX
- Kong
- Istio
- AWS API Gateway / ALB
- Kubernetes Ingress
If you want, I can also provide a sample Envoy YAML or NGINX config that enforces mTLS + JWT for a specific internal API.