One weak pod, and a flat network
An attacker gets into a small pod that only shows a status page. From there they reach the orders service, then the database.
Nothing stopped them, because in a new cluster every pod can reach every other pod.
The one rule
Warning
Everything in this module happens in the cluster of your own lab. Testing a cluster you do not own needs the owner's written permission. In the UK, without it, it can break the Computer Misuse Act 1990.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can add default-deny network policies and prove that traffic stops.
Key words
| Word | What it means | Example |
|---|---|---|
| NetworkPolicy | rules for which traffic may reach or leave pods | |
| Ingress | traffic coming in to a pod | |
| Egress | traffic going out of a pod | |
| Default deny | a policy that selects every pod and allows nothing | |
| Selector | labels that choose pods | app: api |
| Policy controller | the part of the network that enforces policies |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsA selected pod takes only allowed traffic
A pod no policy selects takes traffic from anyone. As soon as one policy selects it, only traffic some policy allows gets through.
Policies add up and never take away. So you start with a default deny that selects every pod, then add one small policy for each path the app needs.
The paths in orders
| From | To | Needed? |
|---|---|---|
| front (role=frontend) | api on 8080 | Yes |
| front | DNS in kube-system | Yes, to find api by name |
| visitor in guest | api | No |
The cells students fill in are left blank in this preview.
Try it: selected or not
policies = [{"selects": "api", "from": ["frontend"]}]
pods = ["api", "db"]
for target in pods:
chosen = [p for p in policies if p["selects"] == target]
for source in ["frontend", "visitor"]:
ok = not chosen or any(source in p["from"] for p in chosen)
print(source, "->", target, "allowed" if ok else "refused")
# Add a policy that selects "db" and allows nobody. What changes?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsA policy is only an object until enforced
The API server stores a NetworkPolicy and does nothing else. The network must enforce it. Flannel, your cluster's network, does not.
k3s adds a policy controller beside flannel, from kube-router. Never trust that from a document: send traffic before and after, and see it stop.
Start the api, the frontend and a visitor
kubectl create namespace orders; kubectl create namespace guest; kubectl run api -n orders --image=busybox:1.37.0 --labels=app=api -- sh -c "mkdir -p /tmp/w && echo orders > /tmp/w/index.html && exec httpd -f -p 8080 -h /tmp/w"; kubectl expose pod api -n orders --port=80 --target-port=8080; kubectl run front -n orders --image=busybox:1.37.0 --labels=role=frontend -- sleep 3600; kubectl run visitor -n guest --image=busybox:1.37.0 -- sleep 3600; kubectl wait -n orders --for=condition=Ready pod/api pod/front --timeout=120s && kubectl wait -n guest --for=condition=Ready pod/visitor --timeout=120sShown here, not run. Press Start lab to type it into your own lab’s terminal.
Both reach the api before any policy
kubectl exec -n orders front -- wget -qO- -T 3 http://api; kubectl exec -n guest visitor -- wget -qO- -T 3 http://api.ordersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the default deny
mkdir -p ~/ch && printf '%s\n' 'apiVersion: networking.k8s.io/v1' 'kind: NetworkPolicy' 'metadata:' ' name: default-deny' ' namespace: orders' 'spec:' ' podSelector: {}' ' policyTypes: ["Ingress", "Egress"]' > ~/ch/deny.yamlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Apply it, try again, look up a name
cd ~/ch && kubectl apply -f deny.yaml && sleep 5; kubectl exec -n orders front -- wget -qO- -T 3 http://api; kubectl exec -n guest visitor -- wget -qO- -T 3 http://api.orders; kubectl exec -n orders front -- nslookup apiShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a stored NetworkPolicy means the traffic is blocked. The API server accepts policies on a network that ignores them. Prove each policy by sending traffic that should fail, on the cluster you run.
Open only the two paths the app needs
Two small policies open what the table needs. The api takes port 8080 from pods labelled role=frontend. The frontend may go out to the api on 8080 and to DNS in kube-system on port 53.
Nothing else changes, so the visitor is still refused.
Write the two openings
mkdir -p ~/ch && printf '%s\n' 'apiVersion: networking.k8s.io/v1' 'kind: NetworkPolicy' 'metadata:' ' name: api-from-frontend' ' namespace: orders' 'spec:' ' podSelector:' ' matchLabels:' ' app: api' ' policyTypes: ["Ingress"]' ' ingress:' ' - from:' ' - podSelector:' ' matchLabels:' ' role: frontend' ' ports:' ' - port: 8080' '---' 'apiVersion: networking.k8s.io/v1' 'kind: NetworkPolicy' 'metadata:' ' name: frontend-out' ' namespace: orders' 'spec:' ' podSelector:' ' matchLabels:' ' role: frontend' ' policyTypes: ["Egress"]' ' egress:' ' - to:' ' - podSelector:' ' matchLabels:' ' app: api' ' ports:' ' - port: 8080' ' - to:' ' - namespaceSelector:' ' matchLabels:' ' kubernetes.io/metadata.name: kube-system' ' ports:' ' - port: 53' ' protocol: UDP' ' - port: 53' ' protocol: TCP' > ~/ch/allow.yamlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Apply them and try both again
cd ~/ch && kubectl apply -f allow.yaml && sleep 5; kubectl exec -n orders front -- wget -qO- -T 3 http://api; kubectl exec -n guest visitor -- wget -qO- -T 3 http://api.ordersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Find namespaces with no policy at all
A new namespace starts flat, with no policy. So list every namespace with its number of policies and ask about each zero.
Keep a test that sends one connection which must fail. If it ever succeeds, a policy or the controller has gone.
Count the policies in every namespace
for n in $(kubectl get namespaces -o name | cut -d/ -f2); do echo "$n: $(kubectl get networkpolicy -n $n --no-headers 2>/dev/null | wc -l)"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: decide like a policy controller
Write a small ingress policy checker.
selects(selector, labels) is True when every key and value of the selector is in the labels; {} selects every pod. ingress_allowed(policies, pod, source, port) is True when no policy selects the pod, or when some rule of a selecting policy matches: a from selector matches the source (no from means anyone) and the port is listed (no ports means any).
def selects(selector, labels):
# True when every key and value of selector is in labels. {} selects every pod.
return False
def ingress_allowed(policies, pod, source, port):
# policies: [{"podSelector": {...}, "ingress": [{"from": [selector, ...], "ports": [8080]}]}]
# A pod no policy selects takes anything. Otherwise one rule must match:
# a "from" selector matches the source (no "from" means anyone),
# and the port is listed (no "ports" means any port).
return False
Make a free account to save your work and track your progress.
Exit quiz
Have a go