Merge pull request #1008 from lmars/fix-nginx-multiple-ssl-vhosts

Support multiple domains using a wildcard TLS certificate
This commit is contained in:
Michael Hobbs committed 2015-03-09 12:05:20 -07:00
commit b468c2e09e
3 files changed
+72 -60

No files matched your search

+18 -13
View File
@@ -45,49 +45,54 @@ EOF
SSL_DIRECTIVES="" SSL_DIRECTIVES=""
fi fi
NGINX_CONF="$PLUGIN_PATH/nginx-vhosts/templates/nginx.conf" NGINX_CONF=$(mktemp -t "nginx.conf.XXXXXX")
SCHEME="http" SCHEME="http"
if [[ -n "$SSL_INUSE" ]]; then if [[ -n "$SSL_INUSE" ]]; then
NGINX_CONF="$PLUGIN_PATH/nginx-vhosts/templates/nginx.ssl.conf"
SCHEME="https" SCHEME="https"
SSL_HOSTNAME=$(openssl x509 -in $SSL_INUSE/server.crt -noout -subject | tr '/' '\n' | grep CN= | cut -c4-) SSL_HOSTNAME=$(openssl x509 -in $SSL_INUSE/server.crt -noout -subject | tr '/' '\n' | grep CN= | cut -c4-)
if [[ -n "$SSL_HOSTNAME" ]]; then if [[ -n "$SSL_HOSTNAME" ]]; then
SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | sed 's|\.|\\.|g' | sed 's/\*/\.\*/g') SSL_HOSTNAME_REGEX=$(echo "$SSL_HOSTNAME" | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g')
[[ -z "$(egrep "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH)" ]] && echo "$SSL_HOSTNAME" >> $VHOST_PATH [[ -z "$(egrep "^${SSL_HOSTNAME_REGEX}$" $VHOST_PATH)" ]] && echo "$SSL_HOSTNAME" >> $VHOST_PATH
fi fi
SSL_HOSTNAME_ALT=$(openssl x509 -in $SSL_INUSE/server.crt -noout -text | grep --after-context=1 '509v3 Subject Alternative Name:' | tail -n 1 | sed -e "s/[[:space:]]*DNS://g" | tr ',' '\n' || true) SSL_HOSTNAME_ALT=$(openssl x509 -in $SSL_INUSE/server.crt -noout -text | grep --after-context=1 '509v3 Subject Alternative Name:' | tail -n 1 | sed -e "s/[[:space:]]*DNS://g" | tr ',' '\n' || true)
if [[ -n "$SSL_HOSTNAME_ALT" ]]; then if [[ -n "$SSL_HOSTNAME_ALT" ]]; then
SSL_HOSTNAME_ALT_REGEX=$(echo "$SSL_HOSTNAME_ALT" | sed 's|\.|\\.|g' | sed 's/\*/\.\*/g') SSL_HOSTNAME_ALT_REGEX=$(echo "$SSL_HOSTNAME_ALT" | sed 's|\.|\\.|g' | sed 's/\*/\[^\.\]\*/g')
[[ -z "$(egrep "^${SSL_HOSTNAME_ALT_REGEX}$" $VHOST_PATH)" ]] && echo "$SSL_HOSTNAME_ALT" >> $VHOST_PATH [[ -z "$(egrep "^${SSL_HOSTNAME_ALT_REGEX}$" $VHOST_PATH)" ]] && echo "$SSL_HOSTNAME_ALT" >> $VHOST_PATH
fi fi
SSL_VHOSTS=$(egrep "^${SSL_HOSTNAME_REGEX}$|^${SSL_HOSTNAME_ALT_REGEX}$" $VHOST_PATH || exit 0) SSL_VHOSTS=$(egrep "^${SSL_HOSTNAME_REGEX}$|^${SSL_HOSTNAME_ALT_REGEX}$" $VHOST_PATH || exit 0)
NONSSL_VHOSTS=$(egrep -v "^${SSL_HOSTNAME}$|^${SSL_HOSTNAME_ALT}$" $VHOST_PATH || exit 0) NONSSL_VHOSTS=$(egrep -v "^${SSL_HOSTNAME}$|^${SSL_HOSTNAME_ALT}$" $VHOST_PATH || exit 0)
NGINX_TEMPLATE="$PLUGIN_PATH/nginx-vhosts/templates/nginx.ssl.conf"
while read line; do while read line; do
dokku_log_info1 "Configuring SSL for $line..." dokku_log_info1 "Configuring SSL for $line..."
SSL_SERVER_NAME=$line SSL_SERVER_NAME=$line
eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf" NOSSL_SERVER_NAME=$line
eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF"
done <<< "$SSL_VHOSTS" done <<< "$SSL_VHOSTS"
fi fi
NOSSL_SERVER_NAME=$(echo $NONSSL_VHOSTS | tr '\n' ' ')
APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template" APP_NGINX_TEMPLATE="$DOKKU_ROOT/$APP/nginx.conf.template"
if [[ -f $APP_NGINX_TEMPLATE ]]; then if [[ -f $APP_NGINX_TEMPLATE ]]; then
dokku_log_info1 "Overriding default nginx.conf with detected nginx.conf.template" dokku_log_info1 "Overriding default nginx.conf with detected nginx.conf.template"
NGINX_CONF=$APP_NGINX_TEMPLATE eval "cat <<< \"$(< $APP_NGINX_TEMPLATE)\" > $NGINX_CONF"
elif [[ -n "$NONSSL_VHOSTS" ]]; then
xargs -i echo "-----> Configuring {}..." <<< "$NONSSL_VHOSTS"
NGINX_TEMPLATE="$PLUGIN_PATH/nginx-vhosts/templates/nginx.conf"
eval "cat <<< \"$(< $NGINX_TEMPLATE)\" >> $NGINX_CONF"
fi fi
xargs -i echo "-----> Configuring {}..." < $VHOST_PATH if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then
# Include SSL_VHOSTS so we can redirect http to https on that hostname as well echo "upstream $APP { server $DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT; }" >> $NGINX_CONF
NOSSL_SERVER_NAME=$(echo $NONSSL_VHOSTS $SSL_VHOSTS| tr '\n' ' ') fi
dokku_log_info1 "Creating $SCHEME nginx.conf"
mv $NGINX_CONF "$DOKKU_ROOT/$APP/nginx.conf"
if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then
dokku_log_info1 "Creating $SCHEME nginx.conf"
echo "upstream $APP { server $DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT; }" > $DOKKU_ROOT/$APP/nginx.conf
eval "cat <<< \"$(< $NGINX_CONF)\" >> $DOKKU_ROOT/$APP/nginx.conf"
dokku_log_info1 "Running nginx-pre-reload" dokku_log_info1 "Running nginx-pre-reload"
pluginhook nginx-pre-reload $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP pluginhook nginx-pre-reload $APP $DOKKU_APP_LISTEN_PORT $DOKKU_APP_LISTEN_IP
+50 -47
View File
@@ -15,6 +15,44 @@ teardown() {
disable_tls_wildcard disable_tls_wildcard
} }
assert_ssl_domain() {
local domain=$1
assert_app_domain "${domain}"
assert_http_redirect "http://${domain}" "https://${domain}/"
assert_http_success "https://${domain}"
}
assert_nonssl_domain() {
local domain=$1
assert_app_domain "${domain}"
assert_http_success "http://${domain}"
}
assert_app_domain() {
local domain=$1
run /bin/bash -c "dokku domains $TEST_APP | grep -xF ${domain}"
echo "output: "$output
echo "status: "$status
assert_output "${domain}"
}
assert_http_redirect() {
local from=$1
local to=$2
run curl -kSso /dev/null -w "%{redirect_url}" "${from}"
echo "output: "$output
echo "status: "$status
assert_output "${to}"
}
assert_http_success() {
local url=$1
run curl -kSso /dev/null -w "%{http_code}" "${url}"
echo "output: "$output
echo "status: "$status
assert_output "200"
}
@test "nginx (no server tokens)" { @test "nginx (no server tokens)" {
deploy_app deploy_app
run /bin/bash -c "curl -s -D - $(dokku url $TEST_APP) -o /dev/null | egrep '^Server' | egrep '[0-9]+'" run /bin/bash -c "curl -s -D - $(dokku url $TEST_APP) -o /dev/null | egrep '^Server' | egrep '[0-9]+'"
@@ -24,70 +62,35 @@ teardown() {
} }
@test "nginx:build-config (wildcard SSL)" { @test "nginx:build-config (wildcard SSL)" {
destroy_app
setup_test_tls_wildcard setup_test_tls_wildcard
create_app add_domain "wildcard1.dokku.me"
run dokku domains:add $TEST_APP wildcard.dokku.me add_domain "wildcard2.dokku.me"
echo "output: "$output add_domain "www.test.dokku.me"
echo "status: "$status
assert_success
deploy_app deploy_app
run bash -c "response=\"$(curl -LkSs wildcard.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\"" assert_ssl_domain "wildcard1.dokku.me"
echo "output: "$output assert_ssl_domain "wildcard2.dokku.me"
echo "status: "$status assert_nonssl_domain "www.test.dokku.me"
assert_success
} }
@test "nginx:build-config (with SSL CN mismatch)" { @test "nginx:build-config (with SSL CN mismatch)" {
setup_test_tls setup_test_tls
deploy_app deploy_app
run /bin/bash -c "dokku domains $TEST_APP | egrep ^node-js-app\.dokku\.me$" assert_ssl_domain "node-js-app.dokku.me"
echo "output: "$output
echo "status: "$status
assert_output "node-js-app.dokku.me"
run bash -c "response=\"$(curl -LkSs node-js-app.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\""
echo "output: "$output
echo "status: "$status
assert_success
} }
@test "nginx:build-config (with SSL and Multiple SANs)" { @test "nginx:build-config (with SSL and Multiple SANs)" {
setup_test_tls_with_sans setup_test_tls_with_sans
deploy_app deploy_app
run /bin/bash -c "dokku domains $TEST_APP | egrep ^test\.dokku\.me$" assert_ssl_domain "test.dokku.me"
echo "output: "$output assert_ssl_domain "www.test.dokku.me"
echo "status: "$status assert_ssl_domain "www.test.app.dokku.me"
assert_output "test.dokku.me"
run /bin/bash -c "dokku domains $TEST_APP | grep ^www\.test\.dokku\.me$"
echo "output: "$output
echo "status: "$status
assert_output "www.test.dokku.me"
run bash -c "response=\"$(curl -LkSs test.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\""
echo "output: "$output
echo "status: "$status
assert_success
run bash -c "response=\"$(curl -LkSs www.test.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\""
echo "output: "$output
echo "status: "$status
assert_success
run bash -c "response=\"$(curl -LkSs www.test.app.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\""
echo "output: "$output
echo "status: "$status
assert_success
} }
@test "nginx:build-config (no global VHOST and domains:add)" { @test "nginx:build-config (no global VHOST and domains:add)" {
destroy_app destroy_app
rm "$DOKKU_ROOT/VHOST" rm "$DOKKU_ROOT/VHOST"
create_app create_app
run dokku domains:add $TEST_APP www.test.app.dokku.me add_domain "www.test.app.dokku.me"
echo "output: "$output
echo "status: "$status
assert_success
deploy_app deploy_app
sleep 5 # wait for nginx to reload assert_nonssl_domain "www.test.app.dokku.me"
run bash -c "response=\"$(curl -s -S www.test.app.dokku.me)\"; echo \$response; test \"\$response\" == \"nodejs/express\""
echo "output: "$output
echo "status: "$status
assert_success
} }
+4
View File
@@ -92,6 +92,10 @@ destroy_app() {
echo $TEST_APP | dokku apps:destroy $TEST_APP echo $TEST_APP | dokku apps:destroy $TEST_APP
} }
add_domain() {
dokku domains:add $TEST_APP $1
}
deploy_app() { deploy_app() {
APP_TYPE="$1"; APP_TYPE=${APP_TYPE:="nodejs-express"} APP_TYPE="$1"; APP_TYPE=${APP_TYPE:="nodejs-express"}
TMP=$(mktemp -d -t "$TARGET.XXXXX") TMP=$(mktemp -d -t "$TARGET.XXXXX")