Skip to content

Commit

Permalink
more verbose error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusvonkohout committed Oct 5, 2023
1 parent e8bc10d commit 7c9c383
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions tests/e2e/runasnonroot.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# set -euxo

namespace="kubeflow"
error_flag=0

Expand All @@ -16,28 +16,49 @@ has_id_command() {
fi
}

# Function to check if 'runAsNonRoot' is present in the container's security context
has_runAsNonRoot() {
# Function to check 'securityContext' and 'runAsNonRoot' at the pod or container level
has_securityContext_and_runAsNonRoot() {
local pod_name="$1"
local container_name="$2"

# Use jq to check if 'securityContext' is missing in the container's security context
local securityContext=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.containers[] | select(.name == "'"$container_name"'").securityContext')

if [ "$securityContext" = "null" ]; then
echo "Error: 'securityContext' is missing in container $container_name of pod $pod_name"
return 1 # 'securityContext' is missing (fail)
# Use jq to check if 'securityContext' is defined at the pod level
local securityContextPod=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.securityContext')

if [ "$securityContextPod" = "null" ]; then
: # 'securityContext' is missing at the pod level, continue checking at the container level
else
# Check 'runAsNonRoot' at the pod level
local runAsNonRootPod=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.securityContext.runAsNonRoot // "Missing"')

if [ "$runAsNonRootPod" = "Missing" ]; then
: # 'runAsNonRoot' is missing at the pod level, continue checking at the container level
else
return 0 # 'runAsNonRoot' is present at the pod level (success)
fi
fi

# Use jq to check if 'runAsNonRoot' is missing in the container's security context
local runAsNonRoot=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.containers[] | select(.name == "'"$container_name"'").securityContext.runAsNonRoot // "Missing"')
# Use jq to check 'securityContext' at the container level
local securityContextContainer=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.containers[] | select(.name == "'"$container_name"'").securityContext')

if [ "$runAsNonRoot" = "Missing" ]; then
echo "Error: 'runAsNonRoot' is missing in container $container_name of pod $pod_name"
return 1 # 'runAsNonRoot' is missing (fail)
else
return 0 # 'runAsNonRoot' is present
if [ "$securityContextContainer" = "null" ]; then
if [ "$runAsNonRootPod" = "Missing" ]; then
echo "Error: 'securityContext' is missing at the pod and container level in container $container_name of pod $pod_name"
return 1
else
echo "Error: There is no runasnonroot on pod level and 'securityContext' is missing at container level in container $container_name of pod $pod_name"
return 1
fi
fi

# Check 'runAsNonRoot' at the container level
local runAsNonRootContainer=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.containers[] | select(.name == "'"$container_name"'").securityContext.runAsNonRoot // "Missing"')

if [ "$runAsNonRootContainer" = "Missing" ]; then
echo "Error: There is no runasnonroot on pod level and'runAsNonRoot' is missing in container $container_name of pod $pod_name"
return 1 # 'runAsNonRoot' is missing at the container level (fail)
fi

return 0 # 'securityContext' and 'runAsNonRoot' are defined at the container level
}

# Get a list of pod names in the specified namespace that are not in the "Completed" state
Expand All @@ -50,7 +71,7 @@ for pod_name in $pod_names; do
container_names=$(kubectl get pod -n "$namespace" "$pod_name" -o json | jq -r '.spec.containers[].name')

for container_name in $container_names; do
if ! has_runAsNonRoot "$pod_name" "$container_name"; then
if has_securityContext_and_runAsNonRoot "$pod_name" "$container_name"; then
error_flag=1
fi

Expand Down

0 comments on commit 7c9c383

Please sign in to comment.