Skip to content

Commit

Permalink
Merge pull request #588 from buchdag/fix-561
Browse files Browse the repository at this point in the history
Enable use of wildcard location configurations
  • Loading branch information
buchdag committed Oct 10, 2019
2 parents dc37364 + 308bd8f commit d26118f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
55 changes: 53 additions & 2 deletions app/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,61 @@ function check_nginx_proxy_container_run {
fi
}

function ascending_wildcard_locations {
# Given foo.bar.baz.example.com as argument, will output:
# - *.bar.baz.example.com
# - *.baz.example.com
# - *.example.com
local domain="${1:?}"
local first_label
regex="^[[:alnum:]_\-]+(\.[[:alpha:]]+)?$"
until [[ "$domain" =~ $regex ]]; do
first_label="${domain%%.*}"
domain="${domain/${first_label}./}"
echo "*.${domain}"
done
}

function descending_wildcard_locations {
# Given foo.bar.baz.example.com as argument, will output:
# - foo.bar.baz.example.*
# - foo.bar.baz.*
# - foo.bar.*
# - foo.*
local domain="${1:?}"
local last_label
regex="^[[:alnum:]_\-]+$"
until [[ "$domain" =~ $regex ]]; do
last_label="${domain##*.}"
domain="${domain/.${last_label}/}"
echo "${domain}.*"
done
}

function enumerate_wildcard_locations {
# Goes through ascending then descending wildcard locations for a given FQDN
local domain="${1:?}"
ascending_wildcard_locations "$domain"
descending_wildcard_locations "$domain"
}

function add_location_configuration {
local domain="${1:-}"
# If no domain was passed or if the domain has no custom conf, use default instead
[[ -z "$domain" || ! -f "${VHOST_DIR}/${domain}" ]] && domain=default
local wildcard_domain
# If no domain was passed use default instead
[[ -z "$domain" ]] && domain='default'

# If the domain does not have an exact matching location file, test the possible
# wildcard locations files. Use default is no location file is present at all.
if [[ ! -f "${VHOST_DIR}/${domain}" ]]; then
for wildcard_domain in $(enumerate_wildcard_locations "$domain"); do
if [[ -f "${VHOST_DIR}/${wildcard_domain}" ]]; then
domain="$wildcard_domain"
break
fi
domain='default'
done
fi

if [[ -f "${VHOST_DIR}/${domain}" && -n $(sed -n "/$START_HEADER/,/$END_HEADER/p" "${VHOST_DIR}/${domain}") ]]; then
# If the config file exist and already have the location configuration, end with exit code 0
Expand Down
7 changes: 7 additions & 0 deletions test/tests/location_config/expected-std-out.txt
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
Started letsencrypt container for test location_config
*.bar.baz.example.com
*.baz.example.com
*.example.com
foo.bar.baz.example.*
foo.bar.baz.*
foo.bar.*
foo.*
41 changes: 36 additions & 5 deletions test/tests/location_config/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ vhost_path='/etc/nginx/vhost.d'
location_file="${TRAVIS_BUILD_DIR}/test/tests/location_config/le2.wtf"
echo "$test_comment" > "$location_file"

# Create le1.wtf configuration file from inside the nginx container
# Create le1.wtf configuration file, *.le3.wtf and test.* from inside the nginx container
docker exec "$NGINX_CONTAINER_NAME" sh -c "echo '### This is a test comment' > /etc/nginx/vhost.d/le1.wtf"
docker exec "$NGINX_CONTAINER_NAME" sh -c "echo '### This is a test comment' > /etc/nginx/vhost.d/\*.example.com"
docker exec "$NGINX_CONTAINER_NAME" sh -c "echo '### This is a test comment' > /etc/nginx/vhost.d/test.\*"

# Zero the default configuration file.
docker exec "$NGINX_CONTAINER_NAME" sh -c "echo '' > /etc/nginx/vhost.d/default"
Expand All @@ -30,6 +32,8 @@ IFS=',' read -r -a domains <<< "$TEST_DOMAINS"
function cleanup {
# Cleanup the files created by this run of the test to avoid foiling following test(s).
docker exec "$le_container_name" bash -c 'rm -rf /etc/nginx/vhost.d/le1.wtf'
docker exec "$le_container_name" bash -c 'rm -rf /etc/nginx/vhost.d/\*.example.com'
docker exec "$le_container_name" bash -c 'rm -rf /etc/nginx/vhost.d/test.\*'
# Stop the LE container
docker stop "$le_container_name" > /dev/null
}
Expand All @@ -51,15 +55,18 @@ function check_location {
fi
}

# check the wildcard location enumeration function
docker exec "$le_container_name" bash -c 'source /app/functions.sh; enumerate_wildcard_locations foo.bar.baz.example.com'

# default configuration file should be empty
config_path="$vhost_path/default"
if docker exec "$le_container_name" [ ! -s "$config_path" ]; then
echo "$config_path should be empty at container startup:"
docker exec "$le_container_name" cat "$config_path"
fi

# le1.wtf and le2.wtf configuration files should only contains the test comment
for domain in "${domains[@]:0:2}"; do
# custom configuration files should only contains the test comment
for domain in "${domains[@]:0:2}" '*.example.com' 'test.*'; do
config_path="$vhost_path/$domain"
if check_location "$le_container_name" "$config_path"; then
echo "Unexpected location configuration on $config_path at container startup:"
Expand Down Expand Up @@ -99,6 +106,30 @@ for domain in "${domains[@]:0:2}"; do
fi
done

# Adding subdomain.example.com location configurations should use the *.example.com file
domain="subdomain.example.com"
config_path="$vhost_path/*.example.com"
docker exec "$le_container_name" bash -c "source /app/functions.sh; add_location_configuration $domain"
if ! check_location "$le_container_name" "$config_path" ; then
echo "Unexpected location configuration on $config_path after call to add_location_configuration $domain:"
docker exec "$le_container_name" cat "$config_path"
elif ! docker exec "$le_container_name" grep -q "$test_comment" "$config_path"; then
echo "$config_path should still have test comment after call to add_location_configuration $domain:"
docker exec "$le_container_name" cat "$config_path"
fi

# Adding test.domain.tld location configurations should use the test.* file
domain="test.domain.tld"
config_path="$vhost_path/test.*"
docker exec "$le_container_name" bash -c "source /app/functions.sh; add_location_configuration $domain"
if ! check_location "$le_container_name" "$config_path" ; then
echo "Unexpected location configuration on $config_path after call to add_location_configuration $domain:"
docker exec "$le_container_name" cat "$config_path"
elif ! docker exec "$le_container_name" grep -q "$test_comment" "$config_path"; then
echo "$config_path should still have test comment after call to add_location_configuration $domain:"
docker exec "$le_container_name" cat "$config_path"
fi

# Remove all location configurations
docker exec "$le_container_name" bash -c "source /app/functions.sh; remove_all_location_configurations"

Expand All @@ -109,8 +140,8 @@ if docker exec "$le_container_name" [ ! -s "$config_path" ]; then
docker exec "$le_container_name" cat "$config_path"
fi

# le1.wtf and le2.wtf configuration files should have reverted to only containing the test comment
for domain in "${domains[@]:0:2}"; do
# Custom configuration files should have reverted to only containing the test comment
for domain in "${domains[@]:0:2}" '*.example.com' 'test.*'; do
config_path="$vhost_path/$domain"
if check_location "$le_container_name" "$config_path"; then
echo "Unexpected location configuration on $config_path after call to remove_all_location_configurations:"
Expand Down

0 comments on commit d26118f

Please sign in to comment.