From 39963dad60e1b8197eda036e35f9d94c653c9476 Mon Sep 17 00:00:00 2001 From: Brad Solomon <81818815+brsolomon-deloitte@users.noreply.github.com> Date: Fri, 2 Jun 2023 09:18:42 -0400 Subject: [PATCH] fix: allow get_docker_configuration_file_args to parse minified json Closes #524. --- Dockerfile | 3 ++- README.md | 2 ++ functions/helper_lib.sh | 12 +++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index bf72e06..6aaa8da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,8 @@ LABEL \ RUN apk add --no-cache iproute2 \ docker-cli \ - dumb-init + dumb-init \ + jq COPY . /usr/local/bin/ diff --git a/README.md b/README.md index 1368c25..82d8a20 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ cd docker-bench-security sudo sh docker-bench-security.sh ``` +> Note: [`jq`](https://jqlang.github.io/jq/) is an optional but recommended dependency. + ### Run with Docker _Please note that the `docker/docker-bench-security` image is out-of-date and and a manual build is required. See [#405](https://github.com/docker/docker-bench-security/issues/405) for more information._ diff --git a/functions/helper_lib.sh b/functions/helper_lib.sh index 18b0801..d4170cc 100644 --- a/functions/helper_lib.sh +++ b/functions/helper_lib.sh @@ -112,12 +112,22 @@ get_docker_configuration_file() { CONFIG_FILE='/dev/null' } +if command -v jq &> /dev/null; then + HAVE_JQ=true +else + HAVE_JQ=false +fi + get_docker_configuration_file_args() { OPTION="$1" get_docker_configuration_file - grep "$OPTION" "$CONFIG_FILE" | sed 's/.*://g' | tr -d '" ', + if "$HAVE_JQ"; then + jq --monochrome-output --raw-output ".[\"${OPTION}\"]" "$CONFIG_FILE" + else + cat "$CONFIG_FILE" | tr -u { '\n' | tr , '\n' | tr } '\n' | grep "$OPTION" | sed 's/.*://g' | tr -d '" ', + fi } get_service_file() {