All-Things-Docker-and-Kubernetes

Pod Security

Pod Security Policies

PodSecurityPolicy serves as an admission controller within Kubernetes, enabling cluster administrators to effectively manage security-related aspects of the Pod specification.

By creating PodSecurityPolicy resources and defining requirements for Pods, administrators can control which Pods are allowed to run based on security policies. If a Pod complies with the defined PSP requirements, it is admitted to the cluster; otherwise, it is rejected.

The Need for Pod Security Policy

Kubernetes resources like Deployments, StatefulSets, and Services form the foundation of applications. However, RBAC alone, which controls access to these resources, does not consider the specific settings within the resources.

PodSecurityPolicy was introduced to address this gap and provide fine-grained control over security-related fields in Pods. It enabled administrators to prevent privileges and settings that could pose security risks, without relying on external admission controllers.

Over time, it became evident that PodSecurityPolicy had inherent usability challenges that necessitated breaking changes.

Deployed as an Admission Controller

The PSP is deployed as an admission controller. It is enabled by adding it in the kube-apiserver service file.

After this, we need to create the PSP object.

apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: restrictive-psp
spec:
  privileged: false
  allowPrivilegeEscalation: false
  readOnlyRootFilesystem: true
  seLinux:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  runAsUser:
    rule: MustRunAsNonRoot
  fsGroup:
    rule: RunAsAny
  requiredDropCapabilities:
    - 'ALL'
  defaultAddCapabilities:
    - 'CAP_SYS_TIME'
  allowedCapabilities:
    - 'NET_ADMIN'
    - 'SYS_TIME'
    - 'AUDIT_WRITE'

What makes PSPs difficult to rollout is that before you can enable PSPs, you have to make sure that the appropriate policies are created in advance. If you don’t create the policies such as the roles and rolebindings, all requests to create pods woulds be denied.

Challenges of PSPs

PSP faces two major drawbacks: the absence of support for additional resource types and its limited set of controls that overlooks certain container runtime-specific characteristics. Other than that, several complexities are attached to PSPs.

These usability challenges collectively drove the need for breaking changes and a more user-friendly solution to secure Pod deployments, leading to the deprecation of PodSecurityPolicy in Kubernetes.

For more information, please see SIG Auth Update and Deep Dive – Mo Khan, Red Hat; Mike Danese, Google; & Tim Allclair, Google.

Transitioning to a New Pod Security Solution

As of Kubernetes version 1.21, PSP is deprecated.

With the phasing out and discontinuation of actively developed Pod Security Policies (PSPs), it becomes crucial for cluster administrators and operators to find alternative security measures. Fortunately, there are two promising options available to meet this need:

In the Kubernetes community, several open source PAC solutions have emerged, providing a reliable alternative to PSPs. These solutions, although not officially part of the Kubernetes project, can be obtained from the Kubernetes ecosystem. Some notable examples of PAC solutions include:

PSA and PSS

The Pod Security Standards (PSS) and Pod Security Admission (PSA) were introduced by the Kubernetes Auth Special Interest Group (SIG) in response to the deprecation of the Pod Security Policy (PSP) and the ongoing requirement of managing pod security in Kubernetes.

Pod Security Admission (PSA)

PSA is an integrated solution within Kubernetes that offers built-in capabilities for governing pod security. It is important to note that this is enabled by default.

Pod Security Standards(PSS)

PSS defines three different security policies that cover a wide range of security needs. These policies are cumulative and vary in their level of restrictiveness:

For more information, please see Pod Security Standards.

Namespace Labels in PSA

PSA is configured in the namespace level.

Namespace labels play a crucial role in implementing PSA. They determine the policy level that applies to all Pods within a specific namespace.

Example: Enforce Mode

The Namespace Label looks like this:

pod-security.kubernetes.io/enforce: restricted

The Effect will be that any Pod that doesn’t meet the ‘restricted’ PSS in this namespace will be rejected.

Example: Audit Mode

The Namespace Label looks like this.

pod-security.kubernetes.io/audit: baseline

The Effect of this label will be that violations and violations of the ‘baseline’ PSS are logged in the audit log, but Pods are allowed to run.

Example: Warn Mode

The Namespace Label is like this.

pod-security.kubernetes.io/warn: privileged

The Effect is that warnings and warnings are generated for any Pod that doesn’t meet the ‘privileged’ standard, but no enforcement occurs.

PSA Modes

PSA Exemptions

There are some special cases where some exceptions to the rule is needed.

Configuring PSA Exemptions

PSA Exemptions can be set in the following ways:

  1. Directly program these special passes into the PSA admission controller.

  2. Define exemptions in the Validating Webhook, using a Kubernetes ConfigMap resource.

    • This is a digital file that contains all the special rules and exemptions.

    • This file is then placed right inside the ‘pod-security-webhook’ container.

In both cases, whether it’s directly through the API server or via the Validating Webhook, you’re ensuring that your Kubernetes cluster remains secure while also being flexible enough to accommodate special cases and needs.

PSS in Action

Here are practical examples of how different Pod Security Standards can be applied to a hypothetical pod spec:

First, the Privileged Level is appropriate for workloads that require all capabilities and access to the host.

apiVersion: v1
kind: Pod
metadata:
  name: privileged-pod
spec:
  containers:
  – name: privileged-container
    image: nginx
    securityContext:
      privileged: true  

Second, the Baseline Level, which is the default for most clusters, disallows privilege escalation.

apiVersion: v1
kind: Pod
metadata:
  name: baseline-pod
spec:
  containers:
  – name: baseline-container
    image: nginx
    securityContext:
      allowPrivilegeEscalation: false 

Finally, Restricted Level for workloads that require the highest level of security.

apiVersion: v1
kind: Pod
metadata:
  name: restricted-pod
spec:
  securityContext:
    seccompProfile:
      type: RuntimeDefault
  containers:
  – name: restricted-container
    image: nginx
    securityContext:
      allowPrivilegeEscalation: false
      runAsNonRoot: true
      readOnlyRootFilesystem: true 

Real-world user cases:

Migrating from PSP to PSA

The following guidelines can be used when migrating from PodSecurityPolicy to Pod Security Admission in Kubernetes.

  1. Look over your existing PSPs and note down what security policies you’ve got in place.

  2. Understand how PSA works.
    • PSA has three levels:
      • Privileged
      • Baseline
      • Restricted
    • Think about which level fits best with your current PSPs. It’s like choosing the right tool for the job.
  3. Turn on PSA in audit mode and see how your policies would work in real-time.

  4. Translate. Find the closest match in PSA for each PSP, or write a new policy if you need to.

  5. Use PSA in Warn Mode to get alerts for any issues.

  6. Monitor your audit and warning logs.

  7. Finally, switch from audit to enforce mode in PSA.

  8. Take it slow, and start with one area at a time.

  9. Keep everyone in the loop. Make sure your team knows about the new policies and how they work.

By following these steps, you can switch from PSP to PSA with as little disruption as possible, keeping your Kubernetes environment secure and up-to-date.


Back to first page