Comply with Pod Security Restricted Standard
TOC
Feature Overview
The pod-security.kubernetes.io/enforce=restricted standard is the most restrictive Pod Security Standard, enforcing current pod hardening best practices. When a namespace is labeled with this standard, all pods created in that namespace must comply with strict security requirements.
This guide explains how to configure Tekton Pipelines to meet the restricted security standard, ensuring that all containers in your Tasks can run successfully in restricted namespaces.
Use Cases
- Running
Tekton Pipelinesin namespaces withpod-security.kubernetes.io/enforce=restrictedlabel - Meeting organizational security compliance requirements
- Enhancing the security posture of your CI/CD pipelines
- Running workloads in highly regulated environments
Prerequisites
Tekton Pipelinesis installed viaTektonPipelineorTektonConfigCR- You have permission to modify
TektonPipelineorTektonConfigresources - You can create or edit
Taskdefinitions - For custom images, you have access to modify
Dockerfilesand rebuild images
Understanding the Restricted Standard
The pod-security.kubernetes.io/enforce=restricted standard requires all three types of containers in a Pod to meet the following security context requirements:
- Regular containers (
containers) - Init containers (
initContainers) - Ephemeral containers (
ephemeralContainers)
Each container must have the following security context configuration:
Configuration breakdown:
Steps
1. Configure Tekton to add security context to init containers
Tekton creates init containers for each Pod it manages. To comply with the restricted standard, you need to enable Tekton to automatically add the required security context to these init containers.
Option A: Configure via TektonPipeline
If you manage Tekton directly through TektonPipeline CR:
Apply the configuration:
Option B: Configure via TektonConfig
If you manage Tekton through TektonConfig CR:
Apply the configuration:
Note: This configuration change takes effect immediately without requiring a restart of the
Tektoncontroller. NewTaskRunsandPipelineRunscreated after this change will automatically have the required security context applied to init containers.
2. Add security context to custom Task definitions
For custom Tasks (not the built-in Tasks provided by the platform), you need to explicitly add the security context to each step.
Example Task with security context:
Important: This step applies to custom
Tasks. Built-inTasksprovided by the platform (exceptbuildah, see Troubleshooting section) are compatible with these security constraints, but theirTaskdefinitions do not include explicitsecurityContextconfigurations since they are designed to work in various security contexts, not just restricted mode.
3. Configure container images to use non-root users
Container images must be configured to run as a non-root user. The user must be specified using a numeric UID rather than a username.
In your Dockerfile, add a non-root user and set it as the default. Here's an example for Alpine-based images:
Note: The
addusercommand syntax varies by base image. See the referenced documentation below for examples with other base images.
Verify the image configuration:
For detailed guidance on adjusting Dockerfiles, see Adjust Dockerfile for Building Task-Compatible Custom Images.
Operation Results
After completing the above steps:
- Init Containers:
Tekton-created init containers will automatically comply with therestrictedstandard - Custom Tasks: Your custom
Tasksteps will have the required security context - Container Images: Images will run as non-root users with minimal privileges
You can verify compliance by creating a TaskRun in a restricted namespace:
A successful TaskRun shows SUCCEEDED as True and REASON as Succeeded, indicating that all containers ran successfully with the required security constraints.
Using Policy Enforcement Tools
For a more automated approach, you can use policy enforcement tools like Kyverno to automatically inject the required security context into all containers. By creating a Kyverno Policy (using apiVersion: kyverno.io/v1 and kind: Policy), you can automatically mutate Pods to add the necessary security context configurations.
For more info please refer to Scenario 4: Specified Namespace Security Context Enforcement.
This approach eliminates the need to manually configure security context for each Task, simplifying compliance management.
For detailed guidance on configuring Kyverno policies for this purpose, refer to:
Troubleshooting
Built-in Tasks and the Restricted Standard
Among the built-in Tasks provided by the platform, only the buildah Task cannot run under the restricted standard. This is because buildah requires elevated privileges to build container images.
The buildah Task requires the following security context:
Solutions:
- Use the
buildahTaskin a separate namespace withbaselinemode instead ofrestrictedmode (e.g.,pod-security.kubernetes.io/enforce=baseline) - Explore alternative container image build methods that may have different security requirements
TaskRun fails with container user errors
You may encounter one of the following errors:
- "container has runAsNonRoot and image will run as root" - The container image has no
USERdirective in theDockerfile(defaults to root) or explicitly setsUSER 0orUSER root - "container has runAsNonRoot and image has non-numeric user" - The container image uses a symbolic username (e.g.,
USER appuser) instead of a numeric UID
Solutions:
-
Rebuild the image with a numeric non-root user ID as described in Step 3. For example, use
USER 65532instead ofUSER appuserorUSER root. -
Specify the user in the
TasksecurityContext: -
Specify the user in the
TaskRunpodTemplate:
Note: The
podTemplate.securityContextsets Pod-level security context, which is inherited by all containers unless overridden at the container level.
Options 2 and 3 are useful when you cannot modify the container image.
TaskRun fails with "Forbidden: cannot set securityContext.capabilities"
This error occurs when the security context tries to add capabilities while dropping all capabilities.
Solution: Ensure your Task does not add any capabilities. Only drop capabilities as shown in the examples.
Init containers fail with permission errors
If init containers fail with permission errors after setting set-security-context: true, verify that:
- The namespace has appropriate security policies
- The container images used by
Tektonare configured to run as non-root - The
Tektoninstallation is up to date