You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB

#!/usr/bin/env bash
set -euo pipefail
scriptdir=$(dirname "$(realpath $0)")
config_json="$scriptdir/config.json"
output_dir="$scriptdir/reports"
docker_socket="/var/run/docker.sock"
usage() {
cat >&2 <<EOF
Usage: $0 [--file <path>] [--env <name>]
-c, --config Path to config JSON file (default: "config.json")
-o, --outdir Output directory (default: "reports")
--docker-socket Docker socket path (default: "/var/run/docker.sock")
-h, --help Show this help message
EOF
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--config)
[[ $# -ge 2 ]] || { echo "Error: $1 requires a value." >&2; usage; }
config_json="$2"
shift 2
;;
-o|--outdir)
[[ $# -ge 2 ]] || { echo "Error: $1 requires a value." >&2; usage; }
output_directory="$2"
shift 2
;;
-docker-socket)
[[ $# -ge 2 ]] || { echo "Error: $1 requires a value." >&2; usage; }
docker_socket="$2"
shift 2
;;
-h|--help)
usage
;;
*)
echo "Error: Unknown option '$1'" >&2
usage
;;
esac
done
reports_dir="$output_dir/$(date --utc +%Y_%m_%d_%H_%M_%S)"
echo "Saving trivy reports to $reports_dir"
while IFS= read -r item; do
app_id=$(printf '%s' "$item" | jq -r '.id')
app_type=$(printf '%s' "$item" | jq -r '.type')
app_image=$(printf '%s' "$item" | jq -r '.image')
echo "Processing $app_id"
image_dir="$reports_dir/$app_id"
mkdir -p "$image_dir"
command_file="$image_dir/command.txt"
execution_log="$image_dir/execution_log.txt"
echo "docker run --rm\\" >> "$command_file"
echo " -v $docker_socket:/var/run/docker.sock:ro \\" >> "$command_file"
echo " -v $image_dir:/output \\" >> "$command_file"
echo " aquasec/trivy:latest image \\" >> "$command_file"
echo " --severity HIGH,CRITICAL \\" >> "$command_file"
echo " --output /output/report.txt \\" >> "$command_file"
echo " $app_image" >> "$command_file"
echo "" >> "$command_file"
bash "$command_file" > "$execution_log" 2>&1
done < <(jq -c '.[]' "$config_json")