From 5d8ccee9196e926ff1ccfb64f4bbb30ffa06d40f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 08:04:07 -0400 Subject: [PATCH 01/11] fix: disable filename globbing for all commands This will allow spaces in arguments when interacting with Dokku over ssh. Refs #2322 --- dokku | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/dokku b/dokku index 46f8d6f92..73c31a50d 100755 --- a/dokku +++ b/dokku @@ -106,15 +106,12 @@ fi if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then export -n SSH_ORIGINAL_COMMAND - if [[ $1 =~ config-* ]] || [[ $1 =~ docker-options* ]]; then - xargs $0 <<<$SSH_ORIGINAL_COMMAND - exit $? - else - set -f - $0 $SSH_ORIGINAL_COMMAND - set +f - exit $? - fi + exit_code=0 + set -f + xargs $0 <<<$SSH_ORIGINAL_COMMAND + echo $? + set +f + exit $exit_code fi if ! dokku_auth "$@"; then From 34768ca5b4d0736bfa8fbadb7bf8fa52508dad5e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 08:06:00 -0400 Subject: [PATCH 02/11] fix: set exit_code variable --- dokku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokku b/dokku index 73c31a50d..cb9bd9b4c 100755 --- a/dokku +++ b/dokku @@ -109,7 +109,7 @@ if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then exit_code=0 set -f xargs $0 <<<$SSH_ORIGINAL_COMMAND - echo $? + exit_code=$? set +f exit $exit_code fi From dc20a3875663acaa2b6c4088a77d296f4303f614 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 08:27:49 -0400 Subject: [PATCH 03/11] fix: use old method for calling the git plugin --- dokku | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/dokku b/dokku index cb9bd9b4c..8d72d24c4 100755 --- a/dokku +++ b/dokku @@ -107,11 +107,19 @@ fi if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then export -n SSH_ORIGINAL_COMMAND exit_code=0 - set -f - xargs $0 <<<$SSH_ORIGINAL_COMMAND - exit_code=$? - set +f - exit $exit_code + if [[ $1 =~ git-* ]] || [[ $1 =~ git:* ]]; then + set -f + $0 $SSH_ORIGINAL_COMMAND + exit_code=$? + set +f + exit $? + else + set -f + xargs $0 <<<$SSH_ORIGINAL_COMMAND + exit_code=$? + set +f + exit $exit_code + fi fi if ! dokku_auth "$@"; then From 91c8e62da5c7f06fe3e8aee4e612efb07b5d351e Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 08:35:24 -0400 Subject: [PATCH 04/11] refactor: reparse the original ssh command in the hopes of keeping array arguments in place --- dokku | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/dokku b/dokku index 8d72d24c4..59cdd93f9 100755 --- a/dokku +++ b/dokku @@ -107,19 +107,12 @@ fi if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then export -n SSH_ORIGINAL_COMMAND exit_code=0 - if [[ $1 =~ git-* ]] || [[ $1 =~ git:* ]]; then - set -f - $0 $SSH_ORIGINAL_COMMAND - exit_code=$? - set +f - exit $? - else - set -f - xargs $0 <<<$SSH_ORIGINAL_COMMAND - exit_code=$? - set +f - exit $exit_code - fi + readarray -t -O "${#ssh_arg_array[@]}" ssh_arg_array < <( printf '%s' "$SSH_ORIGINAL_COMMAND" | xargs -n 1 ) + set -f + $O "${ssh_arg_array[@]}" + exit_code=$? + set +f + exit $exit_code fi if ! dokku_auth "$@"; then From e50b941cee024be514be2b6881380cbdc863aefb Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 08:56:40 -0400 Subject: [PATCH 05/11] fix: correct arg parsing for git commands --- dokku | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/dokku b/dokku index 59cdd93f9..aa343cec9 100755 --- a/dokku +++ b/dokku @@ -107,11 +107,18 @@ fi if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then export -n SSH_ORIGINAL_COMMAND exit_code=0 - readarray -t -O "${#ssh_arg_array[@]}" ssh_arg_array < <( printf '%s' "$SSH_ORIGINAL_COMMAND" | xargs -n 1 ) - set -f - $O "${ssh_arg_array[@]}" - exit_code=$? - set +f + if [[ $1 =~ git-* ]] || [[ $1 =~ git:* ]]; then + set -f + $0 $SSH_ORIGINAL_COMMAND + exit_code=$? + set +f + else + readarray -t -O "${#ssh_arg_array[@]}" ssh_arg_array < <( printf '%s' "$SSH_ORIGINAL_COMMAND" | xargs -n 1 ) + set -f + $O "${ssh_arg_array[@]}" + exit_code=$? + set +f + fi exit $exit_code fi From 79bce7d2d51efbbcaedcffb2f17ad00a7f19f1be Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 08:58:02 -0400 Subject: [PATCH 06/11] fix: use dollar sign --- dokku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokku b/dokku index aa343cec9..3a4289cc7 100755 --- a/dokku +++ b/dokku @@ -115,7 +115,7 @@ if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then else readarray -t -O "${#ssh_arg_array[@]}" ssh_arg_array < <( printf '%s' "$SSH_ORIGINAL_COMMAND" | xargs -n 1 ) set -f - $O "${ssh_arg_array[@]}" + $0 "${ssh_arg_array[@]}" exit_code=$? set +f fi From cf742bb3e000e6f1ad92dc35888e235414f35896 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 09:05:07 -0400 Subject: [PATCH 07/11] chore: run shfmt --- dokku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokku b/dokku index 3a4289cc7..755e652e6 100755 --- a/dokku +++ b/dokku @@ -113,7 +113,7 @@ if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then exit_code=$? set +f else - readarray -t -O "${#ssh_arg_array[@]}" ssh_arg_array < <( printf '%s' "$SSH_ORIGINAL_COMMAND" | xargs -n 1 ) + readarray -t -O "${#ssh_arg_array[@]}" ssh_arg_array < <(printf '%s' "$SSH_ORIGINAL_COMMAND" | xargs -n 1) set -f $0 "${ssh_arg_array[@]}" exit_code=$? From ca55f5844675207eab784502b9ade1c311816617 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 10:51:08 -0400 Subject: [PATCH 08/11] refactor: move plugin test helper functions to test_helper.bash --- tests/unit/plugin.bats | 12 ------------ tests/unit/test_helper.bash | 12 ++++++++++++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/unit/plugin.bats b/tests/unit/plugin.bats index 10761acf8..9323bb417 100644 --- a/tests/unit/plugin.bats +++ b/tests/unit/plugin.bats @@ -1,24 +1,12 @@ #!/usr/bin/env bats load test_helper -TEST_PLUGIN_NAME=smoke-test-plugin -TEST_PLUGIN_GIT_REPO=https://github.com/dokku/${TEST_PLUGIN_NAME}.git -TEST_PLUGIN_LOCAL_REPO="$(mktemp -d)/$TEST_PLUGIN_NAME" - -clone_test_plugin() { - git clone "$TEST_PLUGIN_GIT_REPO" "$TEST_PLUGIN_LOCAL_REPO" -} setup() { global_setup clone_test_plugin } -remove_test_plugin() { - rm -rf $PLUGIN_ENABLED_PATH/$TEST_PLUGIN_NAME $PLUGIN_AVAILABLE_PATH/$TEST_PLUGIN_NAME - rm -rf $TEST_PLUGIN_LOCAL_REPO -} - teardown() { remove_test_plugin || true global_teardown diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index 56dba8a87..8d8512516 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -15,6 +15,9 @@ UUID=$(uuidgen) TEST_APP="rdmtestapp-${UUID}" TEST_NETWORK="test-network-${UUID}" SKIPPED_TEST_ERR_MSG="previous test failed! skipping remaining tests..." +TEST_PLUGIN_NAME=smoke-test-plugin +TEST_PLUGIN_GIT_REPO=https://github.com/dokku/${TEST_PLUGIN_NAME}.git +TEST_PLUGIN_LOCAL_REPO="$(mktemp -d)/$TEST_PLUGIN_NAME" # global setup() and teardown() # skips remaining tests on first failure @@ -45,6 +48,15 @@ cleanup_containers() { fi } +clone_test_plugin() { + git clone "$TEST_PLUGIN_GIT_REPO" "$TEST_PLUGIN_LOCAL_REPO" +} + +remove_test_plugin() { + rm -rf $PLUGIN_ENABLED_PATH/$TEST_PLUGIN_NAME $PLUGIN_AVAILABLE_PATH/$TEST_PLUGIN_NAME + rm -rf $TEST_PLUGIN_LOCAL_REPO +} + # test functions flunk() { { From e43927a69e634a67ecee53b0e199452c90588e96 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 11:06:55 -0400 Subject: [PATCH 09/11] tests: add a test for argument handling --- contrib/dokku_client.sh | 8 +++++++- tests/unit/client.bats | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 590d3d0dc..654acf03c 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -190,7 +190,13 @@ main() { [[ -n "$APP_ARG" ]] && [[ "$APP_ARG" == "--global" ]] && unset APP [[ -n "$@" ]] && [[ -n "$APP" ]] && app_arg="--app $APP" # echo "ssh -o LogLevel=QUIET -p $DOKKU_PORT -t dokku@$DOKKU_REMOTE_HOST -- $app_arg $@" - ssh -o LogLevel=QUIET -p $DOKKU_PORT -t dokku@$DOKKU_REMOTE_HOST -- $app_arg $@ || { + local ssh_args=("-o" "LogLevel=QUIET" "-p" "$DOKKU_PORT" "-t" "dokku@$DOKKU_REMOTE_HOST" "--") + if [[ -n "$app_arg" ]]; then + ssh_args+=("'$app_arg $@'") + else + ssh_args+=("$@") + fi + ssh "${ssh_args[@]}" || { ssh_exit_code="$?" echo " ! Failed to execute dokku command over ssh: exit code $?" 1>&2 echo " ! If there was no output from Dokku, ensure your configured SSH Key can connect to the remote server" 1>&2 diff --git a/tests/unit/client.bats b/tests/unit/client.bats index bfb51a64d..0c0610ea9 100644 --- a/tests/unit/client.bats +++ b/tests/unit/client.bats @@ -6,9 +6,11 @@ setup() { global_setup export "DOKKU_HOST=${DOKKU_DOMAIN}" create_app + clone_test_plugin } teardown() { + remove_test_plugin || true destroy_app unset DOKKU_HOST global_teardown @@ -311,3 +313,22 @@ teardown() { assert_success assert_output "dokku" } + +@test "(client) test-args" { + run /bin/bash -c "dokku plugin:install $TEST_PLUGIN_GIT_REPO --name $TEST_PLUGIN_NAME" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku smoke-test-plugin:args bash -c 'echo Hello'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "triggered smoke-test-plugin:args with args: smoke-test-plugin:args, bash, -c, echo Hello" + + run /bin/bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh 'smoke-test-plugin:args bash -c \"echo Hello\"'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "triggered smoke-test-plugin:args with args: smoke-test-plugin:args, bash, -c, echo Hello" +} From 6d07fea704ae6a5e6b073dd42e461a0538a8f85c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 11:09:25 -0400 Subject: [PATCH 10/11] fix: correct expansion issue --- tests/unit/test_helper.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index 8d8512516..cb5af752b 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -53,8 +53,8 @@ clone_test_plugin() { } remove_test_plugin() { - rm -rf $PLUGIN_ENABLED_PATH/$TEST_PLUGIN_NAME $PLUGIN_AVAILABLE_PATH/$TEST_PLUGIN_NAME - rm -rf $TEST_PLUGIN_LOCAL_REPO + rm -rf "${PLUGIN_ENABLED_PATH:?}/$TEST_PLUGIN_NAME" "${PLUGIN_AVAILABLE_PATH:?}/$TEST_PLUGIN_NAME" + rm -rf "$TEST_PLUGIN_LOCAL_REPO" } # test functions From ff18061987eb15aea67909618871f359e4f0e5bd Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 13 Mar 2024 22:43:49 -0400 Subject: [PATCH 11/11] fix: correctly prepend argument to passed ssh args --- contrib/dokku_client.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 654acf03c..d2c7c4f04 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -188,14 +188,13 @@ main() { [[ " storage:ensure-directory " == *" $CMD "* ]] && unset APP [[ "$CMD" =~ events*|plugin*|ssh-keys* ]] && unset APP [[ -n "$APP_ARG" ]] && [[ "$APP_ARG" == "--global" ]] && unset APP - [[ -n "$@" ]] && [[ -n "$APP" ]] && app_arg="--app $APP" + if [[ -n "$@" ]] && [[ -n "$APP" ]]; then + set -- "$APP" "$@" + set -- "--app" "$@" + fi # echo "ssh -o LogLevel=QUIET -p $DOKKU_PORT -t dokku@$DOKKU_REMOTE_HOST -- $app_arg $@" local ssh_args=("-o" "LogLevel=QUIET" "-p" "$DOKKU_PORT" "-t" "dokku@$DOKKU_REMOTE_HOST" "--") - if [[ -n "$app_arg" ]]; then - ssh_args+=("'$app_arg $@'") - else - ssh_args+=("$@") - fi + ssh_args+=("$@") ssh "${ssh_args[@]}" || { ssh_exit_code="$?" echo " ! Failed to execute dokku command over ssh: exit code $?" 1>&2