Mutual TLS for service communication in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When services talk securely using mutual TLS, extra steps happen to check identities.
We want to see how the time to connect grows as more services communicate.
Analyze the time complexity of the following Kubernetes configuration snippet.
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: myservice-mtls
spec:
host: myservice.default.svc.cluster.local
trafficPolicy:
tls:
mode: MUTUAL_TLS
This snippet enables mutual TLS for a service called "myservice" to secure its communication.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each service connection performs a TLS handshake involving certificate exchange and verification.
- How many times: This handshake happens every time a new connection is established between services.
As the number of service connections grows, the total handshake operations increase linearly.
| Input Size (n connections) | Approx. Handshake Operations |
|---|---|
| 10 | 10 handshakes |
| 100 | 100 handshakes |
| 1000 | 1000 handshakes |
Pattern observation: Each new connection adds one handshake, so the total work grows directly with the number of connections.
Time Complexity: O(n)
This means the time to complete all handshakes grows in direct proportion to how many connections happen.
[X] Wrong: "Mutual TLS handshakes happen only once for all connections, so time stays the same no matter how many connections."
[OK] Correct: Each new connection requires its own handshake, so time grows with the number of connections, not fixed.
Understanding how mutual TLS scales helps you design secure systems that stay fast as they grow.
"What if connections reuse TLS sessions instead of new handshakes? How would the time complexity change?"
Practice
Solution
Step 1: Understand mTLS purpose
Mutual TLS encrypts data and verifies both client and server identities to secure communication.Step 2: Compare options
Only To encrypt data and verify identities between services correctly describes encryption and identity verification, others are incorrect or opposite.Final Answer:
To encrypt data and verify identities between services -> Option DQuick Check:
mTLS = encrypt + verify identities [OK]
- Thinking mTLS only encrypts but doesn't verify identity
- Confusing mTLS with disabling security
- Assuming mTLS speeds up communication
Solution
Step 1: Recall PeerAuthentication modes
STRICT enforces mTLS only, PERMISSIVE allows both mTLS and plain, DISABLE turns off mTLS.Step 2: Match mode to description
PERMISSIVE mode allows both encrypted and plain traffic, matching the question.Final Answer:
PERMISSIVE -> Option BQuick Check:
PERMISSIVE = both encrypted and plain allowed [OK]
- Confusing STRICT with PERMISSIVE
- Thinking DISABLE allows encrypted traffic
- Assuming ENFORCED is a valid mode
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: myapp
spec:
mtls:
mode: STRICT
What happens when a service in namespace
myapp receives plain HTTP traffic?Solution
Step 1: Analyze PeerAuthentication mode
The mode is STRICT, which enforces mTLS for all incoming traffic in the namespace.Step 2: Understand effect on plain HTTP
Plain HTTP traffic without mTLS will be rejected because encryption and identity verification are mandatory.Final Answer:
The traffic is rejected because mTLS is enforced -> Option AQuick Check:
STRICT mode rejects plain HTTP [OK]
- Assuming plain HTTP is accepted in STRICT mode
- Thinking traffic is redirected automatically
- Confusing logging with rejection
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: test
spec:
mtls:
mode: STRICT
What is the most likely reason?
Solution
Step 1: Check namespace scope
PeerAuthentication applies only to the specified namespace 'test'. If the service is outside, it won't be affected.Step 2: Understand effect on service
If the service is in another namespace, it won't enforce STRICT mode and may accept plain HTTP.Final Answer:
The service is in a different namespace than 'test' -> Option AQuick Check:
Namespace mismatch causes no mTLS enforcement [OK]
- Assuming STRICT mode allows plain HTTP
- Thinking selector is mandatory for namespace-wide policy
- Ignoring namespace differences
payments in namespace finance, but allow other services to accept plain traffic. Which PeerAuthentication config achieves this?Solution
Step 1: Understand requirement
Enforce mTLS STRICT only for 'payments' service, allow others to accept plain traffic.Step 2: Analyze options
apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: payments-mtls namespace: finance spec: selector: matchLabels: app: payments mtls: mode: STRICTapplies STRICT mode with selector for 'payments' only.apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: finance-wide namespace: finance spec: mtls: mode: STRICTenforces STRICT for whole namespace, not desired.apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: permissive-all namespace: finance spec: mtls: mode: PERMISSIVEallows both for all services.apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: payments-disable namespace: finance spec: selector: matchLabels: app: payments mtls: mode: DISABLEdisables mTLS for payments, opposite of requirement.Final Answer:
PeerAuthentication with selector for payments and STRICT mode -> Option CQuick Check:
Selector + STRICT = mTLS only for selected service [OK]
- Applying STRICT mode to whole namespace accidentally
- Using DISABLE mode when enforcement is needed
- Not using selector to target specific service
