Skip to content

Instantly share code, notes, and snippets.

@vicenteherrera
Created November 4, 2022 10:57
Show Gist options
  • Save vicenteherrera/3c5094f95b61963b68d16158ba5096ff to your computer and use it in GitHub Desktop.
Save vicenteherrera/3c5094f95b61963b68d16158ba5096ff to your computer and use it in GitHub Desktop.
Script to get recommended Pod Security Standard level for all namespaces in a running cluster
#!/bin/bash
set -e
echo "" > test_psa.log
ALL_NAMESPACES=$(kubectl get namespaces -o name)
for NAMESPACE in $ALL_NAMESPACES
do
echo "Checking namespace: $NAMESPACE" | tee -a test_psa.log
RECOMMENDED="restricted"
LEVEL="restricted"
RESTRICTED_OUTPUT=$(kubectl label --dry-run=server --overwrite "$NAMESPACE" pod-security.kubernetes.io/enforce=$LEVEL 2>&1 > /dev/null)
if [ "$RESTRICTED_OUTPUT" == "" ]; then
echo " Restricted: ok" >> test_psa.log
else
echo " Restricted: warnings" >> test_psa.log
echo "$RESTRICTED_OUTPUT" >> test_psa.log
RECOMMENDED="baseline"
fi
LEVEL="baseline"
BASELINE_OUTPUT=$(kubectl label --dry-run=server --overwrite "$NAMESPACE" pod-security.kubernetes.io/enforce=$LEVEL 2>&1 > /dev/null)
if [ "$BASELINE_OUTPUT" == "" ]; then
echo " Baseline: ok" >> test_psa.log
else
echo " Baseline: warnings" >> test_psa.log
echo "$BASELINE_OUTPUT" >> test_psa.log
RECOMMENDED="privileged"
fi
echo " Recommended level: $RECOMMENDED" | tee -a test_psa.log
echo "-------------------------------------------------------------------" >> test_psa.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment