From 253987c281b439bbf8642eb9ae7647f2316e5a98 Mon Sep 17 00:00:00 2001 From: Eric Ribeiro Date: Mon, 5 Dec 2022 09:41:50 -0800 Subject: [PATCH] fix: evaluate and expand all orb parameters (#171) * feat: add utils function to expand orb parameters * fix: import `utils.sh` to emulate behaviour before orb-tools migration * fix: shellcheck * ci: add test with params coming from env vars --- .circleci/test-deploy.yml | 31 +++++++++++-- src/commands/build.yml | 1 + src/commands/check.yml | 1 + .../configure-docker-credentials-store.yml | 1 + src/commands/dockerlint.yml | 1 + src/commands/hadolint.yml | 1 + src/commands/install-docker-compose.yml | 1 + .../install-docker-credential-helper.yml | 1 + src/commands/install-docker.yml | 1 + src/commands/install-dockerize.yml | 1 + src/commands/install-goss.yml | 1 + src/commands/pull.yml | 1 + src/commands/push.yml | 1 + src/commands/update-description.yml | 1 + src/scripts/build.sh | 4 ++ src/scripts/check.sh | 6 ++- .../configure-docker-credentials-store.sh | 6 ++- src/scripts/dockerlint.sh | 10 ++-- src/scripts/hadolint.sh | 8 +++- src/scripts/install-docker-compose.sh | 4 ++ .../install-docker-credential-helper.sh | 6 ++- src/scripts/install-docker.sh | 6 ++- src/scripts/install-dockerize.sh | 6 ++- src/scripts/install-goss.sh | 6 ++- src/scripts/pull.sh | 6 ++- src/scripts/push.sh | 6 ++- src/scripts/update-description.sh | 6 ++- src/scripts/utils.sh | 46 +++++++++++++++++++ 28 files changed, 152 insertions(+), 18 deletions(-) create mode 100644 src/scripts/utils.sh diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 539de76..758fd2e 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -43,7 +43,7 @@ promotion_requires: &promotion_requires filters: &filters tags: only: /.*/ - + orbs: build-tools: circleci/build-tools@3.0.0 docker: circleci/docker@dev:<> @@ -246,7 +246,7 @@ jobs: - docker/install-docker - docker/install-docker-compose: version: << parameters.docker-compose-version>> - install-dir: << parameters.install-dir>> + install-dir: << parameters.install-dir>> workflows: test-deploy: @@ -485,6 +485,27 @@ workflows: docker-password: DOCKER_PASS use-docker-credentials-store: true filters: *filters + - docker/publish: + pre-steps: + - run: + name: Export env vars + command: | + echo 'export DOCKER_USERNAME=cpeorbtesting' >> $BASH_ENV + echo 'export DOCKER_NAME=docker-orb-test' >> $BASH_ENV + echo 'export DOCKERFILE=test.Dockerfile' >> $BASH_ENV + echo 'export REGISTRY=docker.io' >> $BASH_ENV + name: publish-docker-env-var-all-params + executor: docker-latest + context: CPE-orb-docker-testing + use-remote-docker: true + dockerfile: $DOCKERFILE + image: $DOCKER_USERNAME/$DOCKER_NAME + tag: $CIRCLE_SHA1,$CIRCLE_BUILD_NUM + docker-username: DOCKER_USER + docker-password: DOCKER_PASS + use-docker-credentials-store: true + registry: $REGISTRY + filters: *filters # end docker/publish # begin test-install-docker-tools @@ -502,7 +523,7 @@ workflows: install-goss: false filters: *filters # end test-install-docker-tools - + # begin test - test: name: test-<< matrix.executor >> @@ -552,4 +573,6 @@ executors: image: ubuntu-2004:202010-01 machine-latest: machine: - image: ubuntu-2004:current \ No newline at end of file + image: ubuntu-2004:current + +# VS Code Extension Version: 1.3.0 diff --git a/src/commands/build.yml b/src/commands/build.yml index decf3fc..8f1a40c 100644 --- a/src/commands/build.yml +++ b/src/commands/build.yml @@ -135,4 +135,5 @@ steps: PARAM_REGISTRY: <> PARAM_TAG: <> PARAM_USE_BUILDKIT: <> + SCRIPT_UTILS: <> command: <> diff --git a/src/commands/check.yml b/src/commands/check.yml index bfc98b0..4732cc9 100644 --- a/src/commands/check.yml +++ b/src/commands/check.yml @@ -41,4 +41,5 @@ steps: PARAM_REGISTRY: <> PARAM_DOCKER_USERNAME: <> PARAM_DOCKER_PASSWORD: <> + SCRIPT_UTILS: <> command: <> diff --git a/src/commands/configure-docker-credentials-store.yml b/src/commands/configure-docker-credentials-store.yml index 87b58fa..a7e9195 100644 --- a/src/commands/configure-docker-credentials-store.yml +++ b/src/commands/configure-docker-credentials-store.yml @@ -24,4 +24,5 @@ steps: environment: PARAM_HELPER_NAME: "<>" PARAM_DOCKER_CONFIG_PATH: "<>" + SCRIPT_UTILS: <> command: <> diff --git a/src/commands/dockerlint.yml b/src/commands/dockerlint.yml index d6ff984..6023b1b 100644 --- a/src/commands/dockerlint.yml +++ b/src/commands/dockerlint.yml @@ -30,4 +30,5 @@ steps: PARAM_DEBUG: <> PARAM_TREAT_WARNING_AS_ERRORS: <> PARAM_DOCKERFILE: <> + SCRIPT_UTILS: <> command: <> diff --git a/src/commands/hadolint.yml b/src/commands/hadolint.yml index 8459c1f..ab2bdc5 100644 --- a/src/commands/hadolint.yml +++ b/src/commands/hadolint.yml @@ -35,4 +35,5 @@ steps: PARAM_DOCKERFILES: <> PARAM_IGNORE_RULES: <> PARAM_TRUSTED_REGISTRIES: <> + SCRIPT_UTILS: <> command: <> diff --git a/src/commands/install-docker-compose.yml b/src/commands/install-docker-compose.yml index ac9be65..6369e7f 100644 --- a/src/commands/install-docker-compose.yml +++ b/src/commands/install-docker-compose.yml @@ -25,4 +25,5 @@ steps: environment: PARAM_DOCKER_COMPOSER_VERSION: << parameters.version >> PARAM_INSTALL_DIR: <> + SCRIPT_UTILS: <> command: <> diff --git a/src/commands/install-docker-credential-helper.yml b/src/commands/install-docker-credential-helper.yml index d613656..1fa9749 100644 --- a/src/commands/install-docker-credential-helper.yml +++ b/src/commands/install-docker-credential-helper.yml @@ -26,4 +26,5 @@ steps: environment: PARAM_HELPER_NAME: << parameters.helper-name >> PARAM_RELEASE_TAG: << parameters.release-tag >> + SCRIPT_UTILS: <> command: << include(scripts/install-docker-credential-helper.sh) >> diff --git a/src/commands/install-docker.yml b/src/commands/install-docker.yml index be92c32..1f4d1e6 100644 --- a/src/commands/install-docker.yml +++ b/src/commands/install-docker.yml @@ -25,4 +25,5 @@ steps: environment: PARAM_VERSION: << parameters.version >> PARAM_INSTALL_DIR: << parameters.install-dir >> + SCRIPT_UTILS: <> command: << include(scripts/install-docker.sh) >> diff --git a/src/commands/install-dockerize.yml b/src/commands/install-dockerize.yml index 22e995c..4592248 100644 --- a/src/commands/install-dockerize.yml +++ b/src/commands/install-dockerize.yml @@ -23,4 +23,5 @@ steps: environment: PARAM_VERSION: << parameters.version >> PARAM_INSTALL_DIR: << parameters.install-dir >> + SCRIPT_UTILS: <> command: << include(scripts/install-dockerize.sh) >> diff --git a/src/commands/install-goss.yml b/src/commands/install-goss.yml index 5267650..2823ee6 100644 --- a/src/commands/install-goss.yml +++ b/src/commands/install-goss.yml @@ -34,4 +34,5 @@ steps: PARAM_VERSION: <> PARAM_INSTALL_DIR: <> PARAM_DEBUG: <> + SCRIPT_UTILS: <> command: << include(scripts/install-goss.sh) >> diff --git a/src/commands/pull.yml b/src/commands/pull.yml index 53dbf83..939e539 100644 --- a/src/commands/pull.yml +++ b/src/commands/pull.yml @@ -19,4 +19,5 @@ steps: environment: PARAM_IMAGES: <> PARAM_IGNORE_DOCKER_PULL_ERROR: <> + SCRIPT_UTILS: <> command: << include(scripts/pull.sh) >> diff --git a/src/commands/push.yml b/src/commands/push.yml index f30aade..2b20871 100644 --- a/src/commands/push.yml +++ b/src/commands/push.yml @@ -34,4 +34,5 @@ steps: PARAM_IMAGE: <> PARAM_TAG: <> PARAM_DIGEST_PATH: <> + SCRIPT_UTILS: <> command: << include(scripts/push.sh) >> diff --git a/src/commands/update-description.yml b/src/commands/update-description.yml index 57f38aa..9a58506 100644 --- a/src/commands/update-description.yml +++ b/src/commands/update-description.yml @@ -46,4 +46,5 @@ steps: PARAM_IMAGE: <> PARAM_DOCKER_USERNAME: <> PARAM_DOCKER_PASSWORD: <> + SCRIPT_UTILS: <> command: << include(scripts/update-description.sh) >> diff --git a/src/scripts/build.sh b/src/scripts/build.sh index 08e0108..55daf7c 100644 --- a/src/scripts/build.sh +++ b/src/scripts/build.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + DOCKER_TAGS_ARG="" parse_tags_to_docker_arg() { diff --git a/src/scripts/check.sh b/src/scripts/check.sh index b6c61cf..8ec8de4 100644 --- a/src/scripts/check.sh +++ b/src/scripts/check.sh @@ -1,3 +1,7 @@ #!/usr/bin/env bash -echo "${!PARAM_DOCKER_PASSWORD}" | docker login -u "${!PARAM_DOCKER_USERNAME}" --password-stdin "$PARAM_REGISTRY" \ No newline at end of file +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + +echo "${!PARAM_DOCKER_PASSWORD}" | docker login -u "${!PARAM_DOCKER_USERNAME}" --password-stdin "$PARAM_REGISTRY" diff --git a/src/scripts/configure-docker-credentials-store.sh b/src/scripts/configure-docker-credentials-store.sh index 2500dbe..8eb1606 100644 --- a/src/scripts/configure-docker-credentials-store.sh +++ b/src/scripts/configure-docker-credentials-store.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + HELPER_NAME="$PARAM_HELPER_NAME" DOCKER_CONFIG_PATH="$(eval echo ${PARAM_DOCKER_CONFIG_PATH})" @@ -22,4 +26,4 @@ cat "$DOCKER_CONFIG_PATH" | >/tmp/docker-config-credsstore-update.json cat /tmp/docker-config-credsstore-update.json > "$DOCKER_CONFIG_PATH" -rm /tmp/docker-config-credsstore-update.json \ No newline at end of file +rm /tmp/docker-config-credsstore-update.json diff --git a/src/scripts/dockerlint.sh b/src/scripts/dockerlint.sh index 872029d..d5ec5f1 100644 --- a/src/scripts/dockerlint.sh +++ b/src/scripts/dockerlint.sh @@ -1,11 +1,15 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + if [[ $EUID == 0 ]]; then SUDO=""; else SUDO="sudo"; fi if ! command -v dockerlint &> /dev/null; then - if ! command -v npm &> /dev/null; then + if ! command -v npm &> /dev/null; then echo "npm is required to install dockerlint."; - echo "Consider running this command with an image that has node available: https://circleci.com/developer/images/image/cimg/node"; + echo "Consider running this command with an image that has node available: https://circleci.com/developer/images/image/cimg/node"; echo "Alternatively, use dockerlint's docker image: https://github.com/RedCoolBeans/dockerlint#docker-image." exit 1 fi @@ -21,4 +25,4 @@ if [ "$PARAM_TREAT_WARNING_AS_ERRORS" = true ]; then dockerlint -f "$PARAM_DOCKERFILE" -p else dockerlint -f "$PARAM_DOCKERFILE" -fi \ No newline at end of file +fi diff --git a/src/scripts/hadolint.sh b/src/scripts/hadolint.sh index be375c6..b742931 100644 --- a/src/scripts/hadolint.sh +++ b/src/scripts/hadolint.sh @@ -1,3 +1,7 @@ +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + if [ -n "$PARAM_IGNORE_RULES" ]; then ignore_rules=$(printf '%s' "--ignore ${PARAM_IGNORE_RULES//,/ --ignore }") readonly ignore_rules @@ -16,7 +20,7 @@ printf '%s\n' "$trusted_registries" readonly old_ifs="$IFS" IFS=":" -read -ra dockerfiles <<< "$PARAM_DOCKERFILES" +read -ra dockerfiles <<< "$PARAM_DOCKERFILES" IFS="$old_ifs" for dockerfile in "${dockerfiles[@]}"; do @@ -26,4 +30,4 @@ for dockerfile in "${dockerfiles[@]}"; do $dockerfile printf '%s\n' "Success! $dockerfile linted; no issues found" -done \ No newline at end of file +done diff --git a/src/scripts/install-docker-compose.sh b/src/scripts/install-docker-compose.sh index 0ea9893..5676e9f 100644 --- a/src/scripts/install-docker-compose.sh +++ b/src/scripts/install-docker-compose.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + trap_exit() { # clean-up printf '%s\n' "Cleaning up..." diff --git a/src/scripts/install-docker-credential-helper.sh b/src/scripts/install-docker-credential-helper.sh index e5eeb0c..7039209 100644 --- a/src/scripts/install-docker-credential-helper.sh +++ b/src/scripts/install-docker-credential-helper.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + HELPER_NAME="$PARAM_HELPER_NAME" if uname | grep -q "Darwin"; then platform="darwin" @@ -84,4 +88,4 @@ fi chmod +x "./$HELPER_FILENAME" $SUDO mv "./$HELPER_FILENAME" "$BIN_PATH/$HELPER_FILENAME" -"$BIN_PATH/$HELPER_FILENAME" version \ No newline at end of file +"$BIN_PATH/$HELPER_FILENAME" version diff --git a/src/scripts/install-docker.sh b/src/scripts/install-docker.sh index e7f8538..98aee48 100644 --- a/src/scripts/install-docker.sh +++ b/src/scripts/install-docker.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi # grab Docker version @@ -73,4 +77,4 @@ do done # verify version -echo "$(docker --version) has been installed to $(command -v docker)" \ No newline at end of file +echo "$(docker --version) has been installed to $(command -v docker)" diff --git a/src/scripts/install-dockerize.sh b/src/scripts/install-dockerize.sh index 2fdfb50..0db53ac 100644 --- a/src/scripts/install-dockerize.sh +++ b/src/scripts/install-dockerize.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi # grab dockerize version @@ -45,4 +49,4 @@ $SUDO mv dockerize "$PARAM_INSTALL_DIR" $SUDO chmod +x "$PARAM_INSTALL_DIR"/dockerize # verify version -echo "dockerize $(dockerize --version) has been installed to $(command -v dockerize)" \ No newline at end of file +echo "dockerize $(dockerize --version) has been installed to $(command -v dockerize)" diff --git a/src/scripts/install-goss.sh b/src/scripts/install-goss.sh index 6d5d2c9..f42b3e1 100644 --- a/src/scripts/install-goss.sh +++ b/src/scripts/install-goss.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + if [[ $EUID == 0 ]]; then export SUDO=""; else export SUDO="sudo"; fi # determine specified version @@ -69,4 +73,4 @@ if curl --output /dev/null --silent --head --fail "$DGOSS_URL"; then else echo "No dgoss wrapper found for the selected version of Goss ($VERSION)..." echo "Goss installation will proceed, but to use dgoss, please try again with a newer version" -fi \ No newline at end of file +fi diff --git a/src/scripts/pull.sh b/src/scripts/pull.sh index 741f167..148e4dc 100644 --- a/src/scripts/pull.sh +++ b/src/scripts/pull.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + echo "$PARAM_IMAGES" | sed -n 1'p' | tr ',' '\n' | while read -r image; do echo "Pulling ${image}"; @@ -8,4 +12,4 @@ echo "$PARAM_IMAGES" | sed -n 1'p' | tr ',' '\n' | while read -r image; do else docker pull "${image}"; fi -done \ No newline at end of file +done diff --git a/src/scripts/push.sh b/src/scripts/push.sh index 7bee301..e95da4d 100644 --- a/src/scripts/push.sh +++ b/src/scripts/push.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + IFS="," read -ra DOCKER_TAGS <<< "$PARAM_TAG" image="$(eval echo "$PARAM_IMAGE")" @@ -17,4 +21,4 @@ if [ -n "$PARAM_DIGEST_PATH" ]; then IFS="," read -ra DOCKER_TAGS <<< "$PARAM_TAG" tag=$(eval echo "${DOCKER_TAGS[0]}") docker image inspect --format="{{index .RepoDigests 0}}" "$PARAM_REGISTRY"/"$image":"$tag" > "$PARAM_DIGEST_PATH" -fi \ No newline at end of file +fi diff --git a/src/scripts/update-description.sh b/src/scripts/update-description.sh index 69638cd..c2409f5 100644 --- a/src/scripts/update-description.sh +++ b/src/scripts/update-description.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +# Import "utils.sh". +eval "$SCRIPT_UTILS" +expand_env_vars_with_prefix "PARAM_" + if [ "$PARAM_REGISTRY" != "docker.io" ]; then echo "Registry is not set to Docker Hub. Exiting" exit 1 @@ -20,4 +24,4 @@ if [ $STATUS -ne 200 ]; then echo "Could not update image description" echo "Error code: $STATUS" exit 1 -fi \ No newline at end of file +fi diff --git a/src/scripts/utils.sh b/src/scripts/utils.sh new file mode 100644 index 0000000..c430167 --- /dev/null +++ b/src/scripts/utils.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +# Public: Expand the value from environment variables with given prefix. +# +# Takes a prefix as an argument and expands the value of the environment variables +# starting with the prefix. The expansion is done by using the eval command. +# +# $1 - Prefix used to filter the envinronment variables. +# +# Examples +# +# expand_env_vars_with_prefix "ORB_PARAM_" +# expand_env_vars_with_prefix "PARAM_" +# +# Returns 1 if no argument is provided or no environment variables were found with prefix. +# Returns 0 if the expansion was successful. +expand_env_vars_with_prefix() { + if [ "$#" -eq 0 ]; then + >&2 printf '%s\n' "Please provide a prefix to filter the envinronment variables." + return 1 + fi + + # Fetch parameters from the environment variables. + local prefix="$1" + local env_vars + env_vars="$(printenv | grep "^$prefix")" + + if [ -z "$env_vars" ]; then + >&2 printf '%s\n' "No environment variables found with the prefix: \"$prefix\"." + return 1 + fi + + while IFS= read -ra line; do + # Split the line into key and value. + local var_value="${line#*=}" + local var_name="${line%="$var_value"}" + + # Expand the value. + local expanded_value + expanded_value="$(eval echo "$var_value")" + + # The -v option assignes the output to a variable rather than printing it. + printf -v "$var_name" "%s" "$expanded_value" + done <<< "$env_vars" + return 0 +}