--- reviewers: - pweil- - tallclair title: Pod Security Policies --- {% include feature-state-beta.md %} Pod Security Policies enable fine-grained authorization of pod creation and updates. * TOC {:toc} ## What is a Pod Security Policy? A _Pod Security Policy_ is a cluster-level resource that controls security sensitive aspects of the pod specification. The `PodSecurityPolicy` objects define a set of conditions that a pod must run with in order to be accepted into the system, as well as defaults for the related fields. They allow an administrator to control the following: | Control Aspect | Field Names | | ----------------------------------------------------| ------------------------------------------- | | Running of privileged containers | [`privileged`](#privileged) | | Usage of the root namespaces | [`hostPID`, `hostIPC`](#host-namespaces) | | Usage of host networking and ports | [`hostNetwork`, `hostPorts`](#host-namespaces) | | Usage of volume types | [`volumes`](#volumes-and-file-systems) | | Usage of the host filesystem | [`allowedHostPaths`](#volumes-and-file-systems) | | White list of FlexVolume drivers | [`allowedFlexVolumes`](#flexvolume-drivers) | | Allocating an FSGroup that owns the pod's volumes | [`fsGroup`](#volumes-and-file-systems) | | Requiring the use of a read only root file system | [`readOnlyRootFilesystem`](#volumes-and-file-systems) | | The user and group IDs of the container | [`runAsUser`, `supplementalGroups`](#users-and-groups) | | Restricting escalation to root privileges | [`allowPrivilegeEscalation`, `defaultAllowPrivilegeEscalation`](#privilege-escalation) | | Linux capabilities | [`defaultAddCapabilities`, `requiredDropCapabilities`, `allowedCapabilities`](#capabilities) | | The SELinux context of the container | [`seLinux`](#selinux) | | The AppArmor profile used by containers | [annotations](#apparmor) | | The seccomp profile used by containers | [annotations](#seccomp) | | The sysctl profile used by containers | [annotations](#sysctl) | ## Enabling Pod Security Policies Pod security policy control is implemented as an optional (but recommended) [admission controller](/docs/admin/admission-controllers/#podsecuritypolicy). PodSecurityPolicies are enforced by [enabling the admission controller](/docs/admin/admission-controllers/#how-do-i-turn-on-an-admission-control-plug-in), but doing so without authorizing any policies **will prevent any pods from being created** in the cluster. Since the pod security policy API (`policy/v1beta1/podsecuritypolicy`) is enabled independently of the admission controller, for existing clusters it is recommended that policies are added and authorized before enabling the admission controller. ## Authorizing Policies When a PodSecurityPolicy resource is created, it does nothing. In order to use it, the requesting user or target pod's [service account](/docs/tasks/configure-pod-container/configure-service-account/) must be authorized to use the policy, by allowing the `use` verb on the policy. Most Kubernetes pods are not created directly by users. Instead, they are typically created indirectly as part of a [Deployment](/docs/concepts/workloads/controllers/deployment/), [ReplicaSet](/docs/concepts/workloads/controllers/replicaset/), or other templated controller via the controller manager. Granting the controller access to the policy would grant access for *all* pods created by that the controller, so the preferred method for authorizing policies is to grant access to the pod's service account (see [example](#run-another-pod)). ### Via RBAC [RBAC](/docs/admin/authorization/rbac/) is a standard Kubernetes authorization mode, and can easily be used to authorize use of policies. First, a `Role` or `ClusterRole` needs to grant access to `use` the desired policies. The rules to grant access look like this: ```yaml kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: rules: - apiGroups: ['policy'] resources: ['podsecuritypolicies'] verbs: ['use'] resourceNames: - ``` Then the `(Cluster)Role` is bound to the authorized user(s): ```yaml kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: roleRef: kind: ClusterRole name: apiGroup: rbac.authorization.k8s.io subjects: # Authorize specific service accounts: - kind: ServiceAccount name: namespace: # Authorize specific users (not recommended): - kind: User apiGroup: rbac.authorization.k8s.io name: ``` If a `RoleBinding` (not a `ClusterRoleBinding`) is used, it will only grant usage for pods being run in the same namespace as the binding. This can be paired with system groups to grant access to all pods run in the namespace: ```yaml # Authorize all service accounts in a namespace: - kind: Group apiGroup: rbac.authorization.k8s.io name: system:serviceaccounts # Or equivalently, all authenticated users in a namespace: - kind: Group apiGroup: rbac.authorization.k8s.io name: system:authenticated ``` For more examples of RBAC bindings, see [Role Binding Examples](/docs/admin/authorization/rbac#role-binding-examples). For a complete example of authorizing a PodSecurityPolicy, see [below](#example). ### Troubleshooting - The [Controller Manager](/docs/admin/kube-controller-manager/) must be run against [the secured API port](/docs/admin/accessing-the-api/), and must not have superuser permissions. Otherwise requests would bypass authentication and authorization modules, all PodSecurityPolicy objects would be allowed, and users would be able to create privileged containers. For more details on configuring Controller Manager authorization, see [Controller Roles](/docs/admin/authorization/rbac/#controller-roles). ## Policy Order In addition to restricting pod creation and update, pod security policies can also be used to provide default values for many of the fields that it controls. When multiple policies are available, the pod security policy controller selects policies in the following order: 1. If any policies successfully validate the pod without altering it, they are used. 2. Otherwise, the first valid policy in alphabetical order is used. ## Example _This example assumes you have a running cluster with the PodSecurityPolicy admission controller enabled and you have cluster admin privileges._ ### Set up Set up a namespace and a service account to act as for this example. We'll use this service account to mock a non-admin user. ```shell $ kubectl create namespace psp-example $ kubectl create serviceaccount -n psp-example fake-user $ kubectl create rolebinding -n psp-example fake-editor --clusterrole=edit --serviceaccount=psp-example:fake-user ``` To make it clear which user we're acting as and save some typing, create 2 aliases: ```shell $ alias kubectl-admin='kubectl -n psp-example' $ alias kubectl-user='kubectl --as=system:serviceaccount:psp-example:fake-user -n psp-example' ``` ### Create a policy and a pod Define the example PodSecurityPolicy object in a file. This is a policy that simply prevents the creation of privileged pods. {% include code.html language="yaml" file="example-psp.yaml" ghlink="/docs/concepts/policy/example-psp.yaml" %} And create it with kubectl: ```shell $ kubectl-admin create -f example-psp.yaml ``` Now, as the unprivileged user, try to create a simple pod: ```shell $ kubectl-user create -f- <` - Specify a profile as a file on the node located at `/`, where `` is defined via the `--seccomp-profile-root` flag on the Kubelet. **seccomp.security.alpha.kubernetes.io/allowedProfileNames** - Annotation that specifies which values are allowed for the pod seccomp annotations. Specified as a comma-delimited list of allowed values. Possible values are those listed above, plus `*` to allow all profiles. Absence of this annotation means that the default cannot be changed. ### Sysctl Controlled via annotations on the PodSecurityPolicy. Refer to the [Sysctl documentation]( /docs/concepts/cluster-administration/sysctl-cluster/#podsecuritypolicy-annotations).