1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/samples/README.md

150 lines
8.5 KiB
Markdown
Raw Normal View History

2019-10-08 18:40:15 -07:00
# Best Practice Policies
2019-10-14 12:27:17 -07:00
Best practice policies are designed to be applied to your Kubernetes clusters with minimal changes. To import these policies [install Kyverno](../documentation/installation.md) and import the resources as follows:
2019-10-08 18:40:15 -07:00
2019-10-09 14:30:31 -07:00
````bash
kubectl create -f https://github.com/nirmata/kyverno/raw/master/samples/best_practices/
````
More information on each best-practice policy is provided below:
## Run as non-root user
2019-10-14 12:27:17 -07:00
By default, processes in a container run as a root user (uid 0). To prevent potential compromise of container hosts, specify a least privileged user ID when building the container image and require that application containers run as non root users i.e. set `runAsNonRoot` to `true`.
2019-10-09 14:30:31 -07:00
2019-10-09 18:17:47 -07:00
***Policy YAML***: [deny_runasrootuser.yaml](best_practices/deny_runasrootuser.yaml)
2019-10-09 14:30:31 -07:00
2019-10-09 18:40:52 -07:00
**Additional Information**
2019-10-09 14:30:31 -07:00
* [Pod Security Context](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/)
2019-10-08 18:40:15 -07:00
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
## Disallow automount of Service Account credentials
2019-10-10 12:29:48 -07:00
2019-10-14 12:27:17 -07:00
Kubernetes automounts default service account credentials in each pod. To restrict access, opt out of automounting credentials by setting `automountServiceAccountToken` to `false`.
2019-10-10 12:29:48 -07:00
***Policy YAML***: [disallow_automountingapicred.yaml](best_practices/disallow_automountingapicred.yaml)
2019-10-10 11:53:51 -07:00
## Disallow use of default namespace
2019-10-14 12:27:17 -07:00
Namespaces are a way to segment and isolate cluster resources across multiple users. When multiple users or teams are sharing a single cluster, it is recommended to isolate different workloads and restrict use of the default namespace.
2019-10-10 11:53:51 -07:00
***Policy YAML***: [disallow_default_namespace.yaml](best_practices/disallow_default_namespace.yaml)
2019-10-10 18:42:54 -07:00
## Disallow use of host filesystem
2019-10-14 12:27:17 -07:00
The volume of type `hostpath` binds pods to a specific host, and data persisted in the volume is dependent on the life of the node. In a shared cluster, it is recommeded that applications are independent of hosts.
2019-10-10 18:42:54 -07:00
***Policy YAML***: [disallow_host_filesystem.yaml](best_practices/disallow_host_filesystem.yaml)
2019-10-09 23:54:19 -07:00
## Disallow `hostNetwork` and `hostPort`
2019-10-09 18:17:47 -07:00
2019-10-14 12:27:17 -07:00
Using `hostPort` and `hostNetwork` allows pods to share the host network stack, allowing potential snooping of network traffic from an application pod.
2019-10-09 18:17:47 -07:00
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_host_network_hostport.yaml](best_practices/disallow_host_network_hostport.yaml)
2019-10-09 18:17:47 -07:00
2019-10-14 12:27:17 -07:00
2019-10-09 18:40:52 -07:00
## Disallow `hostPID` and `hostIPC`
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
Sharing the host's PID namespace allows visibility of process on the host, potentially exposing process information.
2019-10-14 12:27:17 -07:00
Sharing the host's IPC namespace allows the container process to communicate with processes on the host. To avoid pod container from having visibility to host process space, validate that `hostPID` and `hostIPC` are set to `false`.
2019-10-09 18:17:47 -07:00
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_hostpid_hostipc.yaml](best_practices/disallow_hostpid_hostipc.yaml)
2019-10-09 18:17:47 -07:00
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
## Restrict service type `NodePort`
A Kubernetes service of type NodePort uses a host port to receive traffic from any source. A `NetworkPolicy` resource cannot be used to control traffic to host ports. Although `NodePort` services can be useful, their use must be limited to services with additional upstream security checks.
2019-10-09 18:17:47 -07:00
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_node_port.yaml](best_practices/disallow_node_port.yaml)
2019-10-09 18:17:47 -07:00
2019-10-14 12:27:17 -07:00
2019-10-09 18:17:47 -07:00
## Disable privileged containers
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
Privileged containers are defined as any container where the container uid 0 is mapped to the hosts uid 0. A process within privileged containers can get unrestricted host access. With `securityContext.allowPrivilegeEscalation` enabled a process can gain privileges from its parent.
To disallow privileged containers and the escalation of privileges it is recommended to run pod containers with `securityContext.priveleged` as `false` and `allowPrivilegeEscalation` as `false`.
2019-10-09 18:17:47 -07:00
2019-10-09 18:21:04 -07:00
***Policy YAML***: [disallow_priviledged_priviligedescalation.yaml](best_practices/disallow_priviledged_priviligedescalation.yaml)
2019-10-09 18:40:52 -07:00
## Default deny all ingress traffic
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
By default, Kubernetes allows all ingress and egress traffic to and from pods within a cluster. A "default" `NetworkPolicy` resource for a namespace should be used to deny all ingress traffic to the pods in that namespace. Additional `NetworkPolicy` resources can then be configured to allow desired traffic to application pods.
2019-10-09 19:06:38 -07:00
2019-10-10 11:53:51 -07:00
***Policy YAML***: [require_default_network_policy.yaml](best_practices/require_default_network_policy.yaml)
2019-10-09 19:06:38 -07:00
2019-10-14 12:27:17 -07:00
2019-10-09 19:06:38 -07:00
## Disallow latest image tag
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
The `:latest` tag is mutable and can lead to unexpected errors if the image changes. A best practice is to use an immutable tag that maps to a specific version of an application pod.
2019-10-09 19:06:38 -07:00
2019-10-09 23:46:18 -07:00
***Policy YAML***: [require_image_tag_not_latest.yaml](best_practices/require_image_tag_not_latest.yaml)
2019-10-09 19:06:38 -07:00
2019-10-14 12:27:17 -07:00
## Configure namespace limits and quotas
2019-10-09 19:06:38 -07:00
2019-10-14 12:27:17 -07:00
To limit the number of objects, as well as the total amount of compute that may be consumed by an application, it is important to create resource limits and quotas for each namespace.
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
***Policy YAML***: [require_namespace_quota.yaml](best_practices/require_namespace_quota.yaml)
2019-10-10 11:53:51 -07:00
**Additional Information**
* [Resource Quota](https://kubernetes.io/docs/concepts/policy/resource-quotas/)
2019-10-10 12:50:17 -07:00
## Require pod resource requests and limits
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
As application workloads share cluster resources, it is important to limit resources requested and consumed by each pod. It is recommended to require `resources.requests` and `resources.limits` per pod. If a namespace level request or limit is specified, defaults will automatically be applied to each pod based on the `LimitRange` configuration.
2019-10-09 19:06:38 -07:00
2019-10-09 23:46:18 -07:00
***Policy YAML***: [require_pod_requests_limits.yaml](best_practices/require_pod_requests_limits.yaml)
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
## Require `livenessProbe` and `readinessProbe`
For each pod, a `livenessProbe` is carried out by the kubelet to determine when to restart a container. A `readinessProbe` is used by services and deployments to determine if the pod is ready to recieve network traffic.
Both liveness and readiness probes need to be configured to manage the pod lifecycle during restarts and upgrades.
2019-10-09 23:46:18 -07:00
***Policy YAML***: [require_probes.yaml](best_practices/require_probes.yaml)
2019-10-09 19:06:38 -07:00
## Read-only root filesystem
2019-10-10 11:53:51 -07:00
2019-10-14 10:47:54 -07:00
A read-only root file system helps to enforce an immutable infrastructure strategy; the container only needs to write on the mounted volume that persists the state. An immutable root filesystem can also prevent malicious binaries from writing to the host system.
2019-10-09 19:06:38 -07:00
***Policy YAML***: [require_readonly_rootfilesystem.yaml](best_practices/require_readonly_rootfilesystem.yaml)
2019-10-14 12:27:17 -07:00
## Disallow unknown image registries
2019-10-10 11:53:51 -07:00
2019-10-14 12:27:17 -07:00
Images from unknown registries may not be scanned and secured. Requiring use of known registries helps reduce threat exposure. You can customize this policy to allow image registries that you trust.
2019-10-10 11:53:51 -07:00
***Policy YAML***: [trusted_image_registries.yaml](best_practices/trusted_image_registries.yaml)
2019-10-14 12:27:17 -07:00
# More Policies
The policies listed here provide additional best practices that should be considered for production use. These policies may require workload specific configutration.
2019-10-08 18:40:15 -07:00
2019-10-09 18:40:52 -07:00
## Assign Linux capabilities inside Pod
2019-10-14 12:27:17 -07:00
Linux divides the privileges traditionally associated with superuser into distinct units, known as capabilities, which can be independently enabled or disabled by listing them in `securityContext.capabilites`.
2019-10-09 18:40:52 -07:00
2019-10-14 12:27:17 -07:00
***Policy YAML***: [policy_validate_container_capabilities.yaml](more/policy_validate_container_capabilities.yaml)
2019-10-09 18:40:52 -07:00
**Additional Information**
* [List of linux capabilities](https://github.com/torvalds/linux/blob/master/include/uapi/linux/capability.h)
2019-10-14 12:27:17 -07:00
2019-10-09 18:40:52 -07:00
## Check userID, groupIP & fsgroup used inside a Pod
2019-10-14 10:47:54 -07:00
All processes inside the pod can be made to run with specific user and groupID by setting `runAsUser` and `runAsGroup` respectively. `fsGroup` can be specified to make sure any file created in the volume with have the specified groupID. These options can be used to validate the IDs used for user and group.
2019-10-09 18:40:52 -07:00
2019-10-14 14:18:43 -07:00
***Policy YAML***: [policy_validate_user_group_fsgroup_id.yaml](more/policy_validate_user_group_fsgroup_id.yaml)
2019-10-14 12:27:17 -07:00
2019-10-09 18:40:52 -07:00
## Configure kernel parameters inside pod
2019-10-14 10:47:54 -07:00
The Sysctl interface allows to modify kernel parameters at runtime and in the pod can be specified under `securityContext.sysctls`. If kernel parameters in the pod are to be modified, should be handled cautiously, and policy with rules restricting these options will be helpful. We can control minimum and maximum port that a network connection can use as its source(local) port by checking net.ipv4.ip_local_port_range
2019-10-09 18:40:52 -07:00
2019-10-14 14:18:43 -07:00
***Policy YAML***: [policy_validate_sysctl_configs.yaml](more/policy_validate_sysctl_configs.yaml)
2019-10-09 18:40:52 -07:00
**Additional Information**
* [List of supported namespaced sysctl interfaces](https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/)