You should see output like this:
namespace/policy-test created
>>> 1. Good config...
deployment.apps/test created
deployment.apps "test" deleted
>>> 2. Deployment - Missing container security context element...
deployment.apps/test created
>>> 3. Pod - Missing container security context element...
pod/test created
>>> 4. Pod - Pod security context, but Missing container security context element...
pod/test2 created
>>> 5. Pod - Container security context element present, with incorrect settings...
pod/test3 created
>>> 6. Pod - Container security context element present, with incorrect spec.hostNetwork, spec.hostPID, spec.hostIPC settings...
pod/test4 created
Observations and Discussion:
- Good config: The deployment with a known good configuration is created and immediately deleted, as expected.
- Bad configs: Notice that deployments and Pods with missing or incorrect securityContext settings are also successfully created. This happens because the default PSA policy does not enforce any restrictions.
- Spec level settings: Even the Pod with disallowed settings for
hostNetwork
, hostPID
, and hostIPC
is allowed to be created under the default, permissive PSA.
This lack of restrictions may suit specific test environments but exposes the cluster to various security risks. It's vital to understand that this is not recommended for production setups.
Cleanup:
After making your observations, it's good practice to clean up the resources you've created to leave the cluster clean.
Run the clean.sh
script:
> ./clean.sh
This will remove all the resources created during the testing of this scenario, thereby restoring your Kubernetes environment to its original state. Now, you're all set to move on to the following scenario, where we'll introduce some security policies to enforce.
Scenario 2: Applying baseline policies
Concept of "Baseline" in Pod Security Standards (PSS):
The baseline policy prevents known security vulnerabilities without causing operational issues. This moderate level of security should be applicable for most workloads and provides a good balance between safety and ease of operation.
Modifying the policy-test Namespace to Include "Baseline" Labels:
Update the policy-test namespace configuration to apply the baseline policies.
> kubectl edit namespace policy-test
Add or modify the labels to match the following:
apiVersion: v1
kind: Namespace
metadata:
name: policy-test
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/audit: baseline
pod-security.kubernetes.io/warn: baseline
Execute tests.sh:
Execute the tests.sh script to apply Kubernetes resources with the new baseline policies.
> ./tests.sh
Expected Outcomes:
The test output should look like this:
namespace/policy-test created
<redacted>
>>> 6. Pod - Container security context element present, with incorrect spec.hostNetwork, spec.hostPID, spec.hostIPC settings...
Error from server (Forbidden): error when creating "policy/psa-pss/tests/6-pod.yaml": pods "test4" is forbidden: violates PodSecurity "baseline:latest": host namespaces (hostNetwork=true, hostPID=true, hostIPC=true), hostPort (container "test" uses hostPort 8080)
- 4 Pods Allowed: Pods and deployments with configurations that match the baseline security profile are allowed (1, 2, 3, and 4).
- 1 Pod Disallowed: The Pod with hostNetwork, hostPID, and hostIPC settings is forbidden. This is because it violates the baseline security standards.
Discuss the Outcomes:
- This scenario demonstrates the impact of enabling baseline policies in the namespace. Most test Pods and deployments are allowed, except for the one with disallowed host-level settings.
- The disallowed Pod (test4) proves that the baseline PSS profile effectively blocks specific configurations that could expose the system to security risks.
Cleanup:
As before, run the clean.sh
script to remove the resources:
> ./clean.sh
This concludes Scenario 2. Now you understand the effects of applying a baseline security profile at the namespace level, and you're ready to explore more stringent security configurations in the following scenario.
Scenario 3: Applying restricted policies
Understanding the "Restricted" PSS Profile:
The restricted profile in Pod Security Standards (PSS) is the most stringent level of security, optimized for production-level, mission-critical workloads. This profile is designed to limit any operation that could compromise the security posture of the Kubernetes cluster. It applies multiple restrictions, such as prohibiting privileged containers, enforcing the principle of least privilege, and ensuring strong isolation between Pods.
Modifying the policy-test Namespace for "Restricted" Policies:
Update the configuration of the policy-test namespace to enforce the restricted PSS profile.
> kubectl edit namespace policy-test
The labels should look like this:
apiVersion: v1
kind: Namespace
metadata:
name: policy-test
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
Execute tests.sh:
Execute the tests.sh
script to try applying various Kubernetes resources under the restricted policies.
> ./tests.sh