2015-05-11 06:08:28 +02:00
|
|
|
#!/bin/sh
|
|
|
|
# ------------------------------------------------------------------------------
|
2015-06-28 20:04:53 +02:00
|
|
|
# Docker Bench for Security v1.0.0
|
2015-05-11 06:08:28 +02:00
|
|
|
#
|
|
|
|
# Docker, Inc. (c) 2015
|
|
|
|
#
|
2015-06-28 20:04:53 +02:00
|
|
|
# Checks for dozens of common best-practices around deploying Docker containers in production.
|
|
|
|
# Inspired by the CIS Docker 1.6 Benchmark:
|
2015-05-11 06:08:28 +02:00
|
|
|
# https://benchmarks.cisecurity.org/tools2/docker/CIS_Docker_1.6_Benchmark_v1.0.0.pdf
|
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Load dependencies
|
|
|
|
. ./output_lib.sh
|
|
|
|
. ./helper_lib.sh
|
|
|
|
|
|
|
|
# Setup the paths
|
2015-05-29 13:42:34 +02:00
|
|
|
this_path=$(abspath "$0") ## Path of this file including filenamel
|
|
|
|
myname=$(basename "${this_path}") ## file name of this script.
|
2015-05-11 06:08:28 +02:00
|
|
|
|
2015-05-31 01:40:23 +02:00
|
|
|
export PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin/
|
2015-05-29 13:42:34 +02:00
|
|
|
logger="${myname}.log"
|
2015-05-11 06:08:28 +02:00
|
|
|
|
|
|
|
# Check for required program(s)
|
2015-06-17 23:23:59 +02:00
|
|
|
req_progs='awk docker grep netstat stat'
|
2015-05-11 06:08:28 +02:00
|
|
|
for p in $req_progs; do
|
2015-05-29 13:42:34 +02:00
|
|
|
command -v "$p" >/dev/null 2>&1 || { printf "%s command not found.\n" "$p"; exit 1; }
|
2015-05-11 06:08:28 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
# Ensure we can connect to docker daemon
|
2015-05-29 13:42:34 +02:00
|
|
|
docker ps -q >/dev/null 2>&1
|
2015-05-11 06:08:28 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
printf "Error connecting to docker daemon (does docker ps work?)\n"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
usage () {
|
2015-09-05 01:25:13 +02:00
|
|
|
cat <<EOF
|
|
|
|
usage: ${myname} [options]
|
2015-05-11 06:08:28 +02:00
|
|
|
|
2015-09-05 01:25:13 +02:00
|
|
|
-h optional Print this help message
|
|
|
|
-l PATH optional Log output in PATH
|
|
|
|
EOF
|
2015-05-11 06:08:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
yell "# ------------------------------------------------------------------------------
|
2015-06-28 20:04:53 +02:00
|
|
|
# Docker Bench for Security v1.0.0
|
2015-05-11 06:08:28 +02:00
|
|
|
#
|
|
|
|
# Docker, Inc. (c) 2015
|
|
|
|
#
|
2015-06-28 20:04:53 +02:00
|
|
|
# Checks for dozens of common best-practices around deploying Docker containers in production.
|
|
|
|
# Inspired by the CIS Docker 1.6 Benchmark:
|
2015-05-11 06:08:28 +02:00
|
|
|
# https://benchmarks.cisecurity.org/tools2/docker/CIS_Docker_1.6_Benchmark_v1.0.0.pdf
|
|
|
|
# ------------------------------------------------------------------------------"
|
|
|
|
|
2015-05-29 13:42:34 +02:00
|
|
|
logit "Initializing $(date)\n"
|
2015-05-11 06:08:28 +02:00
|
|
|
|
|
|
|
# Warn if not root
|
2015-05-29 13:42:34 +02:00
|
|
|
ID=$(id -u)
|
2015-05-15 05:26:32 +02:00
|
|
|
if [ "x$ID" != "x0" ]; then
|
2015-05-11 06:08:28 +02:00
|
|
|
warn "Some tests might require root to run"
|
|
|
|
sleep 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Get the flags
|
2015-09-05 01:25:13 +02:00
|
|
|
# If you add an option here, please
|
|
|
|
# remember to update usage() above.
|
2015-09-05 01:22:45 +02:00
|
|
|
while getopts hl: args
|
2015-05-11 06:08:28 +02:00
|
|
|
do
|
|
|
|
case $args in
|
2015-09-05 01:34:00 +02:00
|
|
|
h) usage; exit 0 ;;
|
2015-05-11 06:08:28 +02:00
|
|
|
l) logger="$OPTARG" ;;
|
2015-09-05 01:34:00 +02:00
|
|
|
*) usage; exit 1 ;;
|
2015-05-11 06:08:28 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Load all the tests from tests/ and run them
|
|
|
|
main () {
|
2015-05-14 02:08:12 +02:00
|
|
|
# List all running containers
|
2015-08-05 12:08:39 +02:00
|
|
|
containers=$(docker ps | sed '1d' | awk '{print $NF}')
|
2015-07-25 07:08:14 +02:00
|
|
|
# If there is a container with label docker_bench_security, memorize it:
|
2015-05-14 02:08:12 +02:00
|
|
|
benchcont="nil"
|
|
|
|
for c in $containers; do
|
2015-05-29 13:42:34 +02:00
|
|
|
labels=$(docker inspect --format '{{ .Config.Labels }}' "$c")
|
2015-07-25 14:20:56 +02:00
|
|
|
contains "$labels" "docker_bench_security" && benchcont="$c"
|
2015-05-14 02:08:12 +02:00
|
|
|
done
|
2015-07-23 11:14:13 +02:00
|
|
|
# List all running containers except docker-bench (use names to improve readability in logs)
|
|
|
|
containers=$(docker ps | sed '1d' | awk '{print $NF}' | grep -v "$benchcont")
|
2015-05-14 04:22:39 +02:00
|
|
|
|
2015-05-11 06:08:28 +02:00
|
|
|
for test in tests/*.sh
|
|
|
|
do
|
2015-05-29 13:42:34 +02:00
|
|
|
. ./"$test"
|
2015-05-11 06:08:28 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|