|
| 1 | +--- |
| 2 | +title: "Pod Security Standards" |
| 3 | +linkTitle: "Pod Security Standards" |
| 4 | +weight: 5 |
| 5 | +description: This guide describes good practices for security standards in Pods and containers. |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +The [PodSecurityPolicy][pod-security] API is deprecated and will be removed from Kubernetes in version 1.25. |
| 11 | +This API is replaced by a new built-in admission controller ([KEP-2579: Pod Security Admission Control][2579-psp-replacement]) which allows cluster admins to [enforce |
| 12 | + Pod Security Standards][enforce-standards-namespace-labels]. |
| 13 | + |
| 14 | +#### What does that mean? |
| 15 | + |
| 16 | +Pods and containers that are **not** configured according to the enforced security standards defined globally or |
| 17 | +on the namespace level will **not** be admitted. In this way, it will not be possible to run them. |
| 18 | + |
| 19 | +**As a best practice, you must ensure that workloads (Operators and Operands) are defined to run under restricted permissions.** |
| 20 | + |
| 21 | +#### How should I configure my Operators and Operands to comply with the criteria? |
| 22 | + |
| 23 | +- **For common cases that do not require escalating privileges:** configure all containers to comply with the [restrictive][restricted] policy as shown in the following the examples: |
| 24 | + |
| 25 | +**On Kubernetes manifests:** |
| 26 | + |
| 27 | +```yaml |
| 28 | + spec: |
| 29 | + securityContext: |
| 30 | + runAsNonRoot: true |
| 31 | + seccompProfile: |
| 32 | + type: RuntimeDefault |
| 33 | + ... |
| 34 | + containers: |
| 35 | + - name: controller-manager |
| 36 | + securityContext: |
| 37 | + allowPrivilegeEscalation: false |
| 38 | + capabilities: |
| 39 | + drop: |
| 40 | + - ALL |
| 41 | + ... |
| 42 | +``` |
| 43 | + |
| 44 | +**On Reconciliations, such as code implementation in Go:** |
| 45 | + |
| 46 | +```go |
| 47 | +dep:= &appsv1.Deployment{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + …. |
| 50 | + }, |
| 51 | + Spec: appsv1.DeploymentSpec{ |
| 52 | + … |
| 53 | + Template: corev1.PodTemplateSpec{ |
| 54 | + …. |
| 55 | + Spec: corev1.PodSpec{ |
| 56 | + // Ensure restrictive context for the Pod |
| 57 | + SecurityContext: &corev1.PodSecurityContext{ |
| 58 | + RunAsNonRoot: &[]bool{true}[0], |
| 59 | + SeccompProfile: &corev1.SeccompProfile{ |
| 60 | + Type: corev1.SeccompProfileTypeRuntimeDefault, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Containers: []corev1.Container{{ |
| 64 | + Image: "memcached:1.4.36-alpine", |
| 65 | + Name: "memcached", |
| 66 | + // Ensure restrictive context for the container |
| 67 | + SecurityContext: &corev1.SecurityContext{ |
| 68 | + RunAsNonRoot: &[]bool{true}[0], |
| 69 | + AllowPrivilegeEscalation: &[]bool{false}[0], |
| 70 | + Capabilities: &corev1.Capabilities{ |
| 71 | + Drop: []corev1.Capability{ |
| 72 | + "ALL", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +**Note:** For Ansible- and Helm-based Operator projects, your Ansible playbooks or Helm charts must create manifests that comply with the requirements. |
| 84 | + |
| 85 | +**OR** |
| 86 | + |
| 87 | +- B) **For workloads that need elevated permissions:** Ensure the namespace has the appropriate enforcement level label as shown in the following example. |
| 88 | +You might need include this in the installation documentation for your Operator. While the label syncer should handle this for you in most cases, it is a good practice for Operators to explicitly state its requirements. |
| 89 | + |
| 90 | +```yaml |
| 91 | + labels: |
| 92 | + ... |
| 93 | + pod-security.kubernetes.io/enforce: privileged |
| 94 | + pod-security.kubernetes.io/audit: privileged |
| 95 | + pod-security.kubernetes.io/warn: privileged |
| 96 | +``` |
| 97 | +
|
| 98 | +**You should ensure the configuration is carried to the Pod/Containers on the bundle CSV (install.spec.deployments.containers).** |
| 99 | +To check an example of CSV which complies with the [restrictive][restricted] policy, see the Golang sample |
| 100 | +under the [testdata/go/v3/memcached-operator/bundle/manifests/memcached-operator.clusterserviceversion.yaml](https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/go/v3/memcached-operator/bundle/manifests/memcached-operator.clusterserviceversion.yaml) |
| 101 | +
|
| 102 | +- [pod-security]: https://kubernetes.io/blog/2021/04/06/podsecuritypolicy-deprecation-past-present-and-future/#what-is-podsecuritypolicy |
| 103 | +- [2579-psp-replacement]: https://github.com/kubernetes/enhancements/tree/master/keps/sig-auth/2579-psp-replacement |
| 104 | +- [enforce-standards-namespace-labels]: https://kubernetes.io/docs/tasks/configure-pod-container/enforce-standards-namespace-labels/ |
| 105 | +- [restricted]: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted |
0 commit comments