Disallow Host Ports

Access to host ports allows potential snooping of network traffic and should not be allowed, or at minimum restricted to a known list.

Policy Definition

/pod-security/default/disallow-host-ports.yaml

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: disallow-host-ports
  annotations:
    policies.kyverno.io/category: Pod Security Standards (Default)
    policies.kyverno.io/description: >-
      Access to host ports allows potential snooping of network traffic and should not be
      allowed, or at minimum restricted to a known list.
spec:
  validationFailureAction: audit
  background: true
  rules:
  - name: host-ports
    match:
      resources:
        kinds:
        - Pod
    validate:
      message: >-
        Use of host ports is disallowed. The fields spec.containers[*].ports[*].hostPort
        and spec.initContainers[*].ports[*].hostPort must be empty.
      pattern:
        spec:
          =(initContainers):
          - =(ports):
              - X(hostPort): 0
          containers:
          - =(ports):
              - X(hostPort): 0

Last modified January 2, 2021: fix titles (9a0d72f)