networkpolicy.yaml 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # NetworkPolicy (task 46). Default-deny for the app pods.
  2. #
  3. # Selecting the pods below with BOTH policy types makes this a default-deny
  4. # for them in any CNI that enforces NetworkPolicy (Calico, Cilium, ...): only
  5. # the flows listed here are admitted, everything else is dropped. A CNI that
  6. # ignores NetworkPolicy does not apply this control; verify the cluster CNI.
  7. #
  8. # Egress:
  9. # - DNS only, to the `kube-system` namespace on port 53 (UDP + TCP). Adjust if
  10. # CoreDNS runs elsewhere; NodeLocal DNSCache also lives in `kube-system`, so
  11. # it is covered.
  12. # - Postgres (5432) and Redis (6379) scoped to the namespace that hosts them.
  13. # The reference ConfigMap DSNs point at `*.d-oauth.svc.cluster.local`, so the
  14. # default selects the `d-oauth` namespace. There is deliberately NO
  15. # `ipBlock: 0.0.0.0/0` peer: that would leave both ports as an exfiltration
  16. # channel to any internet host. Point the selector at the data namespace if
  17. # it differs, and AND in a `podSelector` for their labels to admit only the
  18. # database and cache pods rather than the whole namespace.
  19. # - A managed database outside the cluster cannot be named by a selector. Give
  20. # its private endpoint a specific `ipBlock` CIDR (never `0.0.0.0/0`) or, on a
  21. # CNI with FQDN policy (Cilium `toFQDNs`), the DNS name. Every `ipBlock` peer
  22. # must exclude `169.254.0.0/16` so the cloud metadata endpoint
  23. # (`169.254.169.254`) can never be reached.
  24. # - Everything else, including the Kubernetes API and arbitrary HTTPS
  25. # exfiltration, is denied. The app never calls the API and runs with
  26. # `automountServiceAccountToken: false`.
  27. #
  28. # Ingress: only the public TLS port (8080) and the plaintext health port
  29. # (8081) are reachable. NetworkPolicy cannot name a Service (the Service is
  30. # virtual, and traffic arrives from the L4 gateway or the kubelet), so the
  31. # source is unrestricted but the ports are not. The Service keeps publishing
  32. # 8080 only; 8081 must never be internet-facing.
  33. apiVersion: networking.k8s.io/v1
  34. kind: NetworkPolicy
  35. metadata:
  36. name: d-oauth-server
  37. namespace: d-oauth
  38. labels:
  39. app.kubernetes.io/name: d-oauth-server
  40. app.kubernetes.io/part-of: d-oauth
  41. spec:
  42. podSelector:
  43. matchLabels:
  44. app.kubernetes.io/name: d-oauth-server
  45. policyTypes:
  46. - Ingress
  47. - Egress
  48. ingress:
  49. - ports:
  50. - port: 8080
  51. protocol: TCP
  52. - port: 8081
  53. protocol: TCP
  54. egress:
  55. # DNS (kube-dns / CoreDNS in kube-system).
  56. - to:
  57. - namespaceSelector:
  58. matchLabels:
  59. kubernetes.io/metadata.name: kube-system
  60. ports:
  61. - port: 53
  62. protocol: UDP
  63. - port: 53
  64. protocol: TCP
  65. # Postgres + Redis, scoped to their namespace (the reference deployment
  66. # runs the data services alongside the app in `d-oauth`). No internet-wide
  67. # `ipBlock`: these ports are not an exfiltration channel.
  68. - to:
  69. - namespaceSelector:
  70. matchLabels:
  71. kubernetes.io/metadata.name: d-oauth
  72. ports:
  73. - port: 5432
  74. protocol: TCP
  75. - port: 6379
  76. protocol: TCP