Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ Workloads (like pod, deployment, etc) that contain a container with hostport. Th
* StatefulSet

## What does this policy do:
This Policy checks for every container and init container in the workload if `port.hostPort` is `not set or 0`. If `port.hostPort` is set to a non-zero value, the resource is denied from being deployed in the cluster.
This policy denies resources with a nonzero `hostPort` in a regular or init container port declaration. For Pods without host networking and for workload templates, containers with no ports, only `containerPort`, or `hostPort: 0` pass this check.

For a Pod with `spec.hostNetwork: true`, the Kubernetes API server defaults an omitted or zero `hostPort` to the declared `containerPort` on regular and init containers. The policy therefore denies a host-networked Pod with a nonzero `containerPort` even when `hostPort` is omitted or set to `0`. A host-networked Pod with no port declarations passes this check. See the [Kubernetes Pod API reference](https://kubernetes.io/docs/reference/kubernetes-api/core/pod-v1/#PodSpec).

Kubernetes applies this default before evaluating Pod admission. A workload template, such as a Deployment or CronJob template, may pass this check with `hostNetwork: true` and only `containerPort` set, while the Pods created from it are denied if the policy also applies to those Pods.

Ephemeral containers (added with `kubectl debug`) are not checked, because the Kubernetes API does not allow ports on ephemeral containers and rejects them before this policy runs. This policy also does not match the `pods/ephemeralcontainers` subresource, so it does not evaluate requests that add an ephemeral debug container to a Pod, even when the Pod's existing containers use `hostPort`.

`kubectl debug --copy-to` and node debugging (`kubectl debug node/<node>`) work differently: they create a new Pod with a normal `pods` CREATE request, and this policy does evaluate that request. A `--copy-to` Pod includes the ports of the containers and init containers it copies, so it is denied if a copied container or init container still has a nonzero `hostPort`. With `--keep-init-containers=false`, kubectl leaves the init containers out of the copy (except with the deprecated `legacy` profile), so a nonzero `hostPort` that only an init container has is not copied and does not cause a denial.

## Implementing this policy in the Cluster:
[Refer here for using the policy in the cluster](https://github.com/kubescape/cel-admission-library#using-the-library)
[Refer here for using the policy in the cluster](https://github.com/kubescape/cel-admission-library#using-the-library)
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ If you need to run containers which require HostPorts, this should be defined in
* StatefulSet

## What does this policy do:
This Policy checks every container in the resource, including init containers and ephemeral containers:
* If no container declares a port with a `hostPort`, the resource is allowed. If any container does, the resource is denied from being deployed in the cluster.
This policy denies resources with a nonzero `hostPort` in a container port declaration, including declarations on init containers. For Pods without host networking and for workload templates, containers with no ports, only `containerPort`, or `hostPort: 0` pass this check.

A container with no `ports` at all is allowed, and so is one that declares only `containerPort`.
For a Pod with `spec.hostNetwork: true`, the Kubernetes API server defaults an omitted or zero `hostPort` to the declared `containerPort` on regular and init containers. The policy therefore denies a host-networked Pod with a nonzero `containerPort` even when `hostPort` is omitted or set to `0`. A host-networked Pod with no port declarations passes this check. See the [Kubernetes Pod API reference](https://kubernetes.io/docs/reference/kubernetes-api/core/pod-v1/#PodSpec).

`hostPort: 0` is treated as allowed. `HostPort` is a plain `int32` in the Kubernetes API type rather than a pointer, so a manifest carrying an explicit zero means the same thing as leaving the field out: no port is claimed on the node.
Kubernetes applies this default before evaluating Pod admission. A workload template, such as a Deployment or CronJob template, may pass this check with `hostNetwork: true` and only `containerPort` set, while the Pods created from it are denied if the policy also applies to those Pods.

## How this relates to C-0044:
C-0044 checks the same field, and for an ordinary workload the two agree. They differ in two places:
* This policy walks init containers and ephemeral containers as well as `containers`. C-0044 only looks at `containers`, so a `hostPort` on an init container passes it and is denied here.
* C-0044 denies on the presence of the field, so it also denies an explicit `hostPort: 0`. This policy allows that.
Both policies check regular and init containers and handle host-networked Pod ports the same way. C-0204 also includes ephemeral containers in its check and matches requests to the `pods/ephemeralcontainers` subresource. Kubernetes forbids port declarations on ephemeral containers, but C-0204 can deny an update to that subresource if an existing regular or init container uses a host port.

## Implementing this policy in the Cluster:
[Refer here for using the policy in the cluster](https://github.com/kubescape/cel-admission-library#using-the-library)
Loading