From 3a7e1bfa16f4ff95366a0b192c62e09336bb3eea Mon Sep 17 00:00:00 2001 From: Lewis Marshall Date: Sat, 28 Feb 2015 16:00:32 +0000 Subject: [PATCH] Fix URL schemes in `dokku urls` output Previously, the presence of TLS certificates would cause `dokku urls` to output all https URLs, even for domains which were not configured to use TLS. This change introduces a URLS file in the app's directory which contains the URLs with schemes based on whether they have been configured to use TLS in the nginx config. Signed-off-by: Lewis Marshall --- plugins/00_dokku-standard/commands | 29 +++++++++++--------- plugins/nginx-vhosts/commands | 5 ++++ tests/unit/00_dokku-standard.bats | 43 +++++++++++++++++++++--------- tests/unit/test_helper.bash | 4 +++ 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index 35196d09b..364d95606 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -153,24 +153,27 @@ case "$1" in url | urls) [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 verify_app_name "$2" - APP="$2"; SCHEME="http"; SSL="$DOKKU_ROOT/$APP/tls"; WILDCARD_SSL="$DOKKU_ROOT/tls" + APP="$2"; + if [[ -s "$DOKKU_ROOT/$APP/URLS" ]]; then + case "$1" in + url) + grep "^http" "$DOKKU_ROOT/$APP/URLS" | head -1 + ;; + urls) + grep "^http" "$DOKKU_ROOT/$APP/URLS" + ;; + esac + + exit 0 + fi + + SCHEME="http"; SSL="$DOKKU_ROOT/$APP/tls"; WILDCARD_SSL="$DOKKU_ROOT/tls" if [[ -e "$SSL/server.crt" && -e "$SSL/server.key" ]] || [[ -e "$WILDCARD_SSL/server.crt" && -e "$WILDCARD_SSL/server.key" ]]; then SCHEME="https" fi - if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then - case "$1" in - url) - echo "$SCHEME://$(head -n1 "$DOKKU_ROOT/$APP/VHOST")" - ;; - urls) - for vhost in $(< "$DOKKU_ROOT/$APP/VHOST"); do - echo "$SCHEME://$vhost" - done - ;; - esac - elif [[ -f "$DOKKU_ROOT/VHOST" ]]; then + if [[ -f "$DOKKU_ROOT/VHOST" ]]; then echo "$SCHEME://$(< "$DOKKU_ROOT/VHOST")" else echo "$SCHEME://$(< "$DOKKU_ROOT/HOSTNAME"):$(< "$DOKKU_ROOT/$APP/PORT")" diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index a75745871..84724dc95 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -18,6 +18,7 @@ case "$1" in nginx:build-config) APP="$2"; DOKKU_APP_LISTEN_PORT="$3"; DOKKU_APP_LISTEN_IP="${4}" VHOST_PATH="$DOKKU_ROOT/$APP/VHOST" + URLS_PATH="$DOKKU_ROOT/$APP/URLS" WILDCARD_SSL="$DOKKU_ROOT/tls" SSL="$DOKKU_ROOT/$APP/tls" @@ -94,6 +95,10 @@ EOF echo " Reloading nginx" restart_nginx fi + + echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" > $URLS_PATH + xargs -i echo "https://{}" <<< "${SSL_VHOSTS}" >> $URLS_PATH + xargs -i echo "http://{}" <<< "${NONSSL_VHOSTS}" >> $URLS_PATH else if [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then dokku_log_info1 "VHOST support disabled, deleting $APP/VHOST" diff --git a/tests/unit/00_dokku-standard.bats b/tests/unit/00_dokku-standard.bats index cb9857e39..2c93d57c2 100644 --- a/tests/unit/00_dokku-standard.bats +++ b/tests/unit/00_dokku-standard.bats @@ -12,6 +12,20 @@ teardown() { disable_tls_wildcard } +assert_urls() { + urls=$@ + run dokku urls $TEST_APP + echo "output: "$output + echo "status: "$status + assert_output < <(tr ' ' '\n' <<< "${urls}") +} + +build_nginx_config() { + # simulate nginx post-deploy + dokku domains:setup $TEST_APP + dokku nginx:build-config $TEST_APP +} + @test "run (with tty)" { deploy_app run /bin/bash -c "dokku run $TEST_APP ls /app/package.json" @@ -29,24 +43,29 @@ teardown() { } @test "urls (non-ssl)" { - run bash -c "dokku urls $TEST_APP | grep dokku.me" - echo "output: "$output - echo "status: "$status - assert_output "http://dokku.me" + assert_urls "http://dokku.me" + build_nginx_config + assert_urls "http://${TEST_APP}.dokku.me" + add_domain "test.dokku.me" + assert_urls "http://${TEST_APP}.dokku.me" "http://test.dokku.me" } @test "urls (app ssl)" { setup_test_tls - run bash -c "dokku urls $TEST_APP | grep dokku.me" - echo "output: "$output - echo "status: "$status - assert_output "https://dokku.me" + assert_urls "https://dokku.me" + build_nginx_config + assert_urls "https://node-js-app.dokku.me" "http://${TEST_APP}.dokku.me" + add_domain "test.dokku.me" + assert_urls "https://node-js-app.dokku.me" "http://${TEST_APP}.dokku.me" "http://test.dokku.me" } @test "urls (wildcard ssl)" { setup_test_tls_wildcard - run bash -c "dokku urls $TEST_APP | grep dokku.me" - echo "output: "$output - echo "status: "$status - assert_output "https://dokku.me" + assert_urls "https://dokku.me" + build_nginx_config + assert_urls "https://${TEST_APP}.dokku.me" + add_domain "test.dokku.me" + assert_urls "https://${TEST_APP}.dokku.me" "https://test.dokku.me" + add_domain "dokku.example.com" + assert_urls "https://${TEST_APP}.dokku.me" "https://test.dokku.me" "http://dokku.example.com" } diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index 08b05c45a..08d16f8ae 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -92,6 +92,10 @@ destroy_app() { echo $TEST_APP | dokku apps:destroy $TEST_APP } +add_domain() { + dokku domains:add $TEST_APP $1 +} + deploy_app() { APP_TYPE="$1"; APP_TYPE=${APP_TYPE:="nodejs-express"} TMP=$(mktemp -d -t "$TARGET.XXXXX")