Challenge ·Medium

Pick and Deploy the Least Permissive NetworkPolicy

The security team left five NetworkPolicy files on the workstation. Only one of them lets the frontend reach the backend while granting nothing more. Identify it and deploy it, unchanged.

Scenario

A two-tier application spread across three namespaces. Each of the three namespaces below carries the label name=<namespace>.

NamespaceWorkload - deploymentPod labels
frontendfrontendapp=frontend
frontendlegacy-clientapp=legacy
backendbackendapp=backend
otherexternal-clientapp=external

The backend Pods listen on two ports, 80 and 8080. The backend Service publishes both and does not remap them:

Service portContainer port on the backend Pods
8080
80808080

Clients reach them at:

http://backend.backend.svc.cluster.local:80
http://backend.backend.svc.cluster.local:8080

Nothing is restricted yet. Every Pod in the cluster can reach both ports.


Task

The security team left five NetworkPolicy files in /home/laborant/network-policies on dev-machine:

/home/laborant/network-policies
├── policy-1.yaml
├── policy-2.yaml
├── policy-3.yaml
├── policy-4.yaml
└── policy-5.yaml

1 directory, 5 files

Exactly one file meets all of the requirements below. Apply it, unchanged.

  • The frontend Deployment's Pods reach the backend Pods on TCP 80, and on no other port.
  • Every other source, in every namespace, is denied.
  • backend ends up with exactly one NetworkPolicy: the one that grants the access above and nothing beyond it.
Required end state, only frontend Pods reach the backend on port 80

Required end state, only frontend Pods reach the backend on port 80.

Important

Leave the manifests, Deployments, Pods, and namespace labels untouched. This task is about selection, not authoring.


Hint 1

Before comparing the files, read what each field means:

kubectl explain networkpolicy.spec.ingress.from
kubectl explain networkpolicy.spec.ingress.ports

Three things decide how wide a policy is:

  • from is a list of source rules, one per -. A source is let in if it matches at least one source rule. Inside a single source rule, every selector present has to match that same source.
  • An omitted field is not a restriction. A missing ports list means every port; podSelector: {} matches every Pod.
  • A ports item names a port on the Pods the policy protects. It is not the Service port, even when the two numbers happen to match.
Hint 2

A source rule that carries only a podSelector is evaluated within the namespace of the policy itself. Matching Pods in a different namespace takes a namespaceSelector in the same source rule.

Compare, for any one file, how many source rules its from list holds, whether a ports list is present at all, and which of the two selectors each source rule carries.

See: Network Policies


⚒ Test Cases


🚀 More NetworkPolicy Challenges