Skip to content

Commit b6478e9

Browse files
authored
Merge pull request #374 from draios/limit-num-items-reported
Limit the number of reported items
2 parents 41593e8 + 4cfb58f commit b6478e9

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

docker-bench-security.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ usage () {
5555
-e CHECK optional Comma delimited list of specific check(s) to exclude
5656
-i INCLUDE optional Comma delimited list of patterns within a container or image name to check
5757
-x EXCLUDE optional Comma delimited list of patterns within a container or image name to exclude from check
58+
-n LIMIT optional In JSON output, when reporting lists of items (containers, images, etc.), limit the number of reported items to LIMIT. Default 0 (no limit).
5859
EOF
5960
}
6061

6162
# Get the flags
6263
# If you add an option here, please
6364
# remember to update usage() above.
64-
while getopts bhl:c:e:i:x:t: args
65+
while getopts bhl:c:e:i:x:t:n: args
6566
do
6667
case $args in
6768
b) nocolor="nocolor";;
@@ -71,6 +72,7 @@ do
7172
e) checkexclude="$OPTARG" ;;
7273
i) include="$OPTARG" ;;
7374
x) exclude="$OPTARG" ;;
75+
n) limit="$OPTARG" ;;
7476
*) usage; exit 1 ;;
7577
esac
7678
done
@@ -79,6 +81,10 @@ if [ -z "$logger" ]; then
7981
logger="${myname}.log"
8082
fi
8183

84+
if [ -z "$limit" ]; then
85+
limit=0
86+
fi
87+
8288
# Load output formating
8389
. ./output_lib.sh
8490

output_lib.sh

+17-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,22 @@ resulttestjson() {
7575
printf "\"result\": \"%s\", \"details\": \"%s\"}" "$1" "$2" | tee -a "$logger.json" 2>/dev/null 1>&2
7676
else
7777
# Result also includes details and a list of items. Add that directly to details and to an array property "items"
78-
itemsJson=$(printf "["; ISEP=""; for item in $3; do printf "%s\"%s\"" "$ISEP" "$item"; ISEP=","; done; printf "]")
79-
printf "\"result\": \"%s\", \"details\": \"%s: %s\", \"items\": %s}" "$1" "$2" "$3" "$itemsJson" | tee -a "$logger.json" 2>/dev/null 1>&2
78+
# Also limit the number of items to $limit, if $limit is non-zero
79+
if [ $limit != 0 ]; then
80+
truncItems=""
81+
ITEM_COUNT=0
82+
for item in $3; do
83+
truncItems="$truncItems $item"
84+
ITEM_COUNT=$((ITEM_COUNT + 1));
85+
if [ "$ITEM_COUNT" == "$limit" ]; then
86+
truncItems="$truncItems (truncated)"
87+
break;
88+
fi
89+
done
90+
else
91+
truncItems=$3
92+
fi
93+
itemsJson=$(printf "["; ISEP=""; ITEMCOUNT=0; for item in $truncItems; do printf "%s\"%s\"" "$ISEP" "$item"; ISEP=","; done; printf "]")
94+
printf "\"result\": \"%s\", \"details\": \"%s: %s\", \"items\": %s}" "$1" "$2" "$truncItems" "$itemsJson" | tee -a "$logger.json" 2>/dev/null 1>&2
8095
fi
8196
}

0 commit comments

Comments
 (0)