A minimal Rust-based sidecar proxy for enforcing inbound mTLS in Kubernetes pods
1.5K
A minimal Rust-based sidecar proxy for enforcing inbound mTLS in Kubernetes pods. It terminates mTLS connections, verifies client certificates against trusted CAs, and forwards validated HTTP requests to an upstream application in the same pod. The sidecar monitors mounted certificate files for updates (e.g., from Vault Secrets Operator) and reloads TLS configuration without restarts.
See https://github.com/value-medical/mtls-sidecar for source code and examples.
This component provides a lightweight layer for securing HTTP services with mutual TLS, integrating seamlessly with Kubernetes Secret mounts. It focuses on inbound termination and proxying, avoiding broader features like routing or outbound connections.
kubernetes.io/tls and VSO Opaque Secret formats via file auto-detection.Configuration is via environment variables only, with sensible defaults for common setups. All vars are optional strings unless noted.
| Variable | Default Value | Description |
|---|---|---|
TLS_LISTEN_PORT | 8443 | TCP port for inbound mTLS listener. |
UPSTREAM_URL | http://localhost:8080 | Full URL for the proxy target. |
UPSTREAM_READINESS_URL | http://localhost:8080/ready | URL for upstream readiness check. |
CERT_DIR | /etc/certs | Directory containing server cert/key files. |
CA_DIR | /etc/ca | Directory containing the CA bundle file. |
INJECT_CLIENT_HEADERS | false | If true, inject X-Client-TLS-Info header. |
MONITOR_PORT | 8081 | Port for health probes and metrics. |
ENABLE_METRICS | false | If true, expose Prometheus /metrics on the monitor port. |
CERT_DIR and CA_DIR.Mount entire Secrets to directories; the sidecar auto-detects files:
tls.crt + tls.key in CERT_DIR; falls back to certificate + private_key.ca-bundle.pem or ca.crt in CA_DIR; merges ca.crt or issuing_ca if found in CERT_DIR._raw, expiration).On load/reload failure, logs an error and retains the previous configuration.
When INJECT_CLIENT_HEADERS is set to true, the sidecar extracts key details from the validated client certificate during mTLS termination and injects them into the forwarded HTTP request via a single custom header: X-Client-TLS-Info. This provides the upstream application with lightweight, pre-parsed x509 information (e.g., subject, SANs, hash) without requiring it to handle TLS or certificate parsing. All extraction occurs in the sidecar using Rust's rustls and x509-parser crates, ensuring containment of TLS concerns.
The header value is a base64-encoded JSON object (for single-hop scenarios, which is the default in this minimal sidecar). This format avoids parsing complexities like quoting or delimiters in traditional schemes, relying only on universal base64 decoding followed by JSON parsing. The payload is compact (~200-500 bytes pre-encoding) and includes only authentication-relevant fields.
X-Client-TLS-Info (case-insensitive; value is opaque to intermediaries).| Field | Type | Description | Example Value |
|---|---|---|---|
subject | string | Full distinguished name (DN) as RFC 2253 string, normalized by sidecar. | "CN=client.example.com,O=Acme" |
uri_sans | array | URI subject alternative names (SANs), e.g., for SPIFFE identities. | ["spiffe://cluster/ns/default/sa/client"] |
dns_sans | array | DNS subject alternative names (SANs). | ["client.example.com"] |
hash | string | SHA-256 hex digest of the certificate DER, prefixed with "sha256:". | "sha256:abc123def456..." |
not_before | string | Issuance timestamp in ISO 8601 format. | "2025-01-01T00:00:00Z" |
not_after | string | Expiry timestamp in ISO 8601 format. | "2026-01-01T00:00:00Z" |
serial | string | Certificate serial number as hex string. | "0x1234567890abcdef" |
The format is designed for easy integration in common web service languages.
Upstream code should validate the header presence and decode/parse safely (e.g., handle missing/invalid values gracefully).
For minimal upstream examples, refer to the examples/ directory.
hash against a known trust store if required.This feature unburdens upstream services while exposing just enough cert info for auth/audit.
MIT License. See LICENSE file.
Content type
Image
Digest
sha256:5d1df52df…
Size
14.7 MB
Last updated
about 1 year ago
docker pull sebcvm/mtls-sidecar