๐Ÿ”’ Security

mTLS

Mutual TLS

Two-way TLS authentication. Both server and client verify certificates.

๐Ÿ“– Detailed Explanation

mTLS (Mutual TLS) is an extension of standard TLS in which not only the server but also the client presents a certificate, performing two-way authentication. As a core component of zero trust architecture, it is widely used for communication between microservices.

In standard TLS, the client verifies only the server's certificate (HTTPS). In mTLS, the server also verifies the client's certificate, so only authorized clients can connect. Strong authentication is possible even without API keys or tokens.

In a Service Mesh (Istio, Linkerd), the sidecar proxy handles mTLS automatically. All traffic between services can be encrypted and authenticated without modifying application code.

Because certificate management becomes complex, it is common to automate it using cert-manager or Vault PKI. SPIFFE/SPIRE is a workload identity standard that automates certificate issuance and rotation in dynamic environments.

๐Ÿ’ป Code Example

# Istio: PeerAuthentication์œผ๋กœ mTLS ๊ฐ•์ œ
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: production
spec:
  mtls:
    mode: STRICT  # mTLS ํ•„์ˆ˜

---
# Istio: DestinationRule๋กœ mTLS ์„ค์ •
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: my-service
spec:
  host: my-service.production.svc.cluster.local
  trafficPolicy:
    tls:
      mode: ISTIO_MUTUAL  # Istio๊ฐ€ ๋ฐœ๊ธ‰ํ•œ ์ธ์ฆ์„œ ์‚ฌ์šฉ

---
# Nginx: mTLS ์„ค์ •
server {
    listen 443 ssl;

    # ์„œ๋ฒ„ ์ธ์ฆ์„œ
    ssl_certificate /etc/nginx/certs/server.crt;
    ssl_certificate_key /etc/nginx/certs/server.key;

    # ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์„œ ๊ฒ€์ฆ (mTLS)
    ssl_client_certificate /etc/nginx/certs/ca.crt;
    ssl_verify_client on;  # ํ•„์ˆ˜
    ssl_verify_depth 2;

    location / {
        # ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์„œ ์ •๋ณด๋ฅผ ๋ฐฑ์—”๋“œ์— ์ „๋‹ฌ
        proxy_set_header X-Client-Cert-DN $ssl_client_s_dn;
        proxy_pass http://backend;
    }
}

---
# Node.js: mTLS ํด๋ผ์ด์–ธํŠธ
import https from 'https';
import fs from 'fs';

const options = {
  hostname: 'api.example.com',
  port: 443,
  path: '/data',
  method: 'GET',
  // ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์„œ (mTLS)
  cert: fs.readFileSync('client.crt'),
  key: fs.readFileSync('client.key'),
  // ์„œ๋ฒ„ CA ๊ฒ€์ฆ
  ca: fs.readFileSync('ca.crt'),
};

const req = https.request(options, (res) => {
  console.log(`Status: ${res.statusCode}`);
});

---
# cert-manager: ์ธ์ฆ์„œ ์ž๋™ ๋ฐœ๊ธ‰
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: my-service-cert
  namespace: production
spec:
  secretName: my-service-tls
  duration: 2160h  # 90์ผ
  renewBefore: 360h  # ๋งŒ๋ฃŒ 15์ผ ์ „ ๊ฐฑ์‹ 
  privateKey:
    algorithm: ECDSA
    size: 256
  usages:
    - server auth
    - client auth  # mTLS์šฉ
  dnsNames:
    - my-service.production.svc.cluster.local
  issuerRef:
    name: cluster-ca
    kind: ClusterIssuer

๐Ÿ—ฃ๏ธ How It's Said in Practice

Security: "If we apply mTLS to service-to-service communication, authentication happens at the network level, so the burden of managing API keys is reduced."

Development: "Managing certificate expiration seems like it will be difficult..."

Security: "You can just use cert-manager for automatic renewal. Or if you use Istio, the sidecar handles it for you."

Interviewer: "Please explain the difference between mTLS and standard TLS."

Candidate: "In standard TLS, the client verifies only the server certificate, which is one-way authentication. mTLS is two-way authentication in which the server also verifies the client's certificate, so only authorized clients can communicate. It is mainly used for service-to-service authentication in microservice environments and is central to zero trust architecture."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "There's no logic in the mTLS setup to check the certificate expiration date. What happens if a request is made with an expired certificate?"

Author: "It gets rejected during the TLS handshake. But it would be good to add an alert for approaching expiration."

โš ๏ธ Cautions

๐Ÿ“š Learn More