|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +## Test for the hooks of acme.sh |
| 4 | +pre_hook_file="/tmp/prehook" |
| 5 | +pre_hook_command="touch $pre_hook_file" |
| 6 | +post_hook_file="/tmp/posthook" |
| 7 | +post_hook_command="touch $post_hook_file" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +if [[ -z $GITHUB_ACTIONS ]]; then |
| 12 | + le_container_name="$(basename "${0%/*}")_$(date "+%Y-%m-%d_%H.%M.%S")" |
| 13 | +else |
| 14 | + le_container_name="$(basename "${0%/*}")" |
| 15 | +fi |
| 16 | +run_le_container "${1:?}" "$le_container_name" --cli-args "--env ACME_PRE_HOOK=$pre_hook_command" --cli-args "--env ACME_POST_HOOK=$post_hook_command" |
| 17 | + |
| 18 | +# Create the $domains array from comma separated domains in TEST_DOMAINS. |
| 19 | +IFS=',' read -r -a domains <<< "$TEST_DOMAINS" |
| 20 | + |
| 21 | +# Cleanup function with EXIT trap |
| 22 | +function cleanup { |
| 23 | + # Remove the Nginx container silently. |
| 24 | + docker rm --force "${domains[0]}" &> /dev/null |
| 25 | + # Cleanup the files created by this run of the test to avoid foiling following test(s). |
| 26 | + docker exec "$le_container_name" /app/cleanup_test_artifacts |
| 27 | + # Stop the LE container |
| 28 | + docker stop "$le_container_name" > /dev/null |
| 29 | +} |
| 30 | +trap cleanup EXIT |
| 31 | + |
| 32 | +# Run an nginx container for ${domains[0]} with LETSENCRYPT_EMAIL set. |
| 33 | +container_email="contact@${domains[0]}" |
| 34 | +run_nginx_container --hosts "${domains[0]}" --cli-args "--env LETSENCRYPT_EMAIL=${container_email}" |
| 35 | + |
| 36 | +# Wait for a symlink at /etc/nginx/certs/${domains[0]}.crt |
| 37 | +wait_for_symlink "${domains[0]}" "$le_container_name" |
| 38 | + |
| 39 | +##Check if the command is deliverd properly in /etc/acme.sh |
| 40 | +if docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email" ]]; then |
| 41 | + echo "The /etc/acme.sh/$container_email folder does not exist." |
| 42 | +elif docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email/${domains[0]}" ]]; then |
| 43 | + echo "The /etc/acme.sh/$container_email/${domains[0]} folder does not exist." |
| 44 | +elif docker exec "$le_container_name" [[ ! -f "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf" ]]; then |
| 45 | + echo "The /etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf file does not exist." |
| 46 | +fi |
| 47 | +acme_pre_hook_key="Le_PreHook=" |
| 48 | +acme_post_hook_key="Le_PostHook=" |
| 49 | +acme_base64_start="'__ACME_BASE64__START_" |
| 50 | +acme_base64_end="__ACME_BASE64__END_'" |
| 51 | +pre_hook_command_base64=$(echo -n "$pre_hook_command" | base64) |
| 52 | +post_hook_command_base64=$(echo -n "$post_hook_command" | base64) |
| 53 | + |
| 54 | +acme_pre_hook="$(docker exec "$le_container_name" grep "$acme_pre_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")" |
| 55 | +acme_post_hook="$(docker exec "$le_container_name" grep "$acme_post_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")" |
| 56 | + |
| 57 | +if [[ "$acme_pre_hook_key$acme_base64_start$pre_hook_command_base64$acme_base64_end" != "$acme_pre_hook" ]]; then |
| 58 | + echo "Prehook command not saved properly" |
| 59 | +fi |
| 60 | +if [[ "$acme_post_hook_key$acme_base64_start$post_hook_command_base64$acme_base64_end" != "$acme_post_hook" ]]; then |
| 61 | + echo "Posthook command not saved properly" |
| 62 | +fi |
| 63 | + |
| 64 | + |
| 65 | +## Check if the action ist performed |
| 66 | +if docker exec "$le_container_name" [[ ! -f "$pre_hook_file" ]]; then |
| 67 | + echo "Prehook action failed" |
| 68 | +fi |
| 69 | +if docker exec "$le_container_name" [[ ! -f "$post_hook_file" ]]; then |
| 70 | + echo "Posthook action failed" |
| 71 | +fi |
0 commit comments