From 5de622a8247c4cf4fc2bd4e5f8a947e60a8c4bfb Mon Sep 17 00:00:00 2001 From: mrichomme Date: Mon, 20 Apr 2020 14:50:27 +0200 Subject: Resync integration/xtesting repo Issue-ID: INT-1366 Signed-off-by: mrichomme Change-Id: I3af9c4697f0e67d3ce5b6d2fceeb978aeb20a0ff --- security/scripts/check_security_root.sh | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 security/scripts/check_security_root.sh (limited to 'security/scripts/check_security_root.sh') diff --git a/security/scripts/check_security_root.sh b/security/scripts/check_security_root.sh new file mode 100644 index 0000000..ca388fd --- /dev/null +++ b/security/scripts/check_security_root.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +usage() { + cat < [-l ] + -l: rooted pod xfail file +EOF + exit ${1:-0} +} + +if [ "$#" -lt 1 ]; then + usage + exit 1 +fi + +K8S_NAMESPACE=$1 +FILTERED_PODS_LIST=$(mktemp rooted_pods_XXXXXX) +WL_RAW_FILE_PATH=$(mktemp raw_filtered_pods_XXXXXX) + +manage_white_list() { + # init filtered port list file + if [ ! -f $WL_FILE_PATH ];then + echo "File not found" + usage + fi + grep -o '^[^#]*' $WL_FILE_PATH > $WL_RAW_FILE_PATH +} + +### getopts +while : +do + case $2 in + -h|--help|help) usage;; + -l) WL_FILE_PATH=$3;manage_white_list;shift;; + -*) usage 1 ;; + *) break ;; + esac +done + +echo "------------------------------------------------------------------------" +echo "-------------------- ONAP Security tests ----------------------------" +echo "-------------------- Test root user in pods -------------------------" +echo "------------------------------------------------------------------------" + +code=0 + +# get the pod list +for pod in `kubectl get pod -n $K8S_NAMESPACE| grep "Running" | grep -v functest | grep -v integration | awk '{print $1}'` ;do + list=`kubectl top pod $pod --containers -n onap |grep -v "POD"|awk '{print $1":"$2}'`; + for po in $list; do + contname=`echo $po|cut -d':' -f2`;uid=`kubectl exec $pod --container $contname -n $K8S_NAMESPACE id|sed -r "s/^uid=(.*) gid.*$/\1/"`;echo "POD: $pod container: $contname uid: $uid"; + done; +done | grep root > $FILTERED_PODS_LIST + +while IFS= read -r line; do + # for each line we test if it is in the white list with a regular expression + while IFS= read -r wl_line; do + wl_name=$(echo $wl_line | awk {'print $1'}) + if grep -e $K8S_NAMESPACE-$wl_name <<< "$line" > /dev/null ;then + # Found in white list, exclude it + sed -i "/$line/d" $FILTERED_PODS_LIST + fi + done < $WL_RAW_FILE_PATH +done < $FILTERED_PODS_LIST + +if [ -s $FILTERED_PODS_LIST ] +then + code=1 + nb_errors=`cat $FILTERED_PODS_LIST | wc -l` + echo "Test FAIL: $nb_errors pod(s) launched as root found" + cat $FILTERED_PODS_LIST +else + echo "Test PASS: No pod launched as root found" +fi + +exit $code -- cgit 1.2.3-korg