|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +## Test for the hooks of acme.sh |
| 4 | + |
| 5 | +default_pre_hook_file="/tmp/default_prehook" |
| 6 | +default_pre_hook_command="touch $default_pre_hook_file" |
| 7 | +default_post_hook_file="/tmp/default_posthook" |
| 8 | +default_post_hook_ommand="touch $default_post_hook_file" |
| 9 | + |
| 10 | +percontainer_pre_hook_file="/tmp/percontainer_prehook" |
| 11 | +percontainer_pre_hook_command="touch $percontainer_pre_hook_file" |
| 12 | +percontainer_post_hook_file="/tmp/percontainer_posthook" |
| 13 | +percontainer_post_hook_command="touch $percontainer_post_hook_file" |
| 14 | + |
| 15 | +if [[ -z $GITHUB_ACTIONS ]]; then |
| 16 | + le_container_name="$(basename "${0%/*}")_$(date "+%Y-%m-%d_%H.%M.%S")" |
| 17 | +else |
| 18 | + le_container_name="$(basename "${0%/*}")" |
| 19 | +fi |
| 20 | +run_le_container "${1:?}" "$le_container_name" \ |
| 21 | + --cli-args "--env ACME_PRE_HOOK=$default_pre_hook_command" \ |
| 22 | + --cli-args "--env ACME_POST_HOOK=$default_post_hook_ommand" |
| 23 | + |
| 24 | +# Create the $domains array from comma separated domains in TEST_DOMAINS. |
| 25 | +IFS=',' read -r -a domains <<< "$TEST_DOMAINS" |
| 26 | + |
| 27 | +# Cleanup function with EXIT trap |
| 28 | +function cleanup { |
| 29 | + # Remove the Nginx container silently. |
| 30 | + docker rm --force "${domains[0]}" &> /dev/null |
| 31 | + # Cleanup the files created by this run of the test to avoid foiling following test(s). |
| 32 | + docker exec "$le_container_name" /app/cleanup_test_artifacts |
| 33 | + # Stop the LE container |
| 34 | + docker stop "$le_container_name" > /dev/null |
| 35 | +} |
| 36 | +trap cleanup EXIT |
| 37 | + |
| 38 | +container_email="contact@${domains[0]}" |
| 39 | + |
| 40 | +# Run an nginx container for ${domains[0]} with LETSENCRYPT_EMAIL set. |
| 41 | +run_nginx_container --hosts "${domains[0]}" \ |
| 42 | + --cli-args "--env LETSENCRYPT_EMAIL=${container_email}" |
| 43 | + |
| 44 | +# Run an nginx container for ${domains[1]} with LETSENCRYPT_EMAIL, ACME_PRE_HOOK and ACME_POST_HOOK set. |
| 45 | +run_nginx_container --hosts "${domains[1]}" \ |
| 46 | + --cli-args "--env LETSENCRYPT_EMAIL=${container_email}" \ |
| 47 | + --cli-args "--env ACME_PRE_HOOK=$percontainer_pre_hook_command" \ |
| 48 | + --cli-args "--env ACME_POST_HOOK=$percontainer_post_hook_command" |
| 49 | + |
| 50 | +# Wait for a symlink at /etc/nginx/certs/${domains[0]}.crt |
| 51 | +wait_for_symlink "${domains[0]}" "$le_container_name" |
| 52 | + |
| 53 | +acme_pre_hook_key="Le_PreHook=" |
| 54 | +acme_post_hook_key="Le_PostHook=" |
| 55 | +acme_base64_start="'__ACME_BASE64__START_" |
| 56 | +acme_base64_end="__ACME_BASE64__END_'" |
| 57 | + |
| 58 | +# Check if the default command is deliverd properly in /etc/acme.sh |
| 59 | +if docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email" ]]; then |
| 60 | + echo "The /etc/acme.sh/$container_email folder does not exist." |
| 61 | +elif docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email/${domains[0]}" ]]; then |
| 62 | + echo "The /etc/acme.sh/$container_email/${domains[0]} folder does not exist." |
| 63 | +elif docker exec "$le_container_name" [[ ! -f "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf" ]]; then |
| 64 | + echo "The /etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf file does not exist." |
| 65 | +fi |
| 66 | + |
| 67 | +default_pre_hook_command_base64="${acme_pre_hook_key}${acme_base64_start}$(echo -n "$default_pre_hook_command" | base64)${acme_base64_end}" |
| 68 | +default_post_hook_command_base64="${acme_post_hook_key}${acme_base64_start}$(echo -n "$default_post_hook_ommand" | base64)${acme_base64_end}" |
| 69 | + |
| 70 | +default_acme_pre_hook="$(docker exec "$le_container_name" grep "$acme_pre_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")" |
| 71 | +default_acme_post_hook="$(docker exec "$le_container_name" grep "$acme_post_hook_key" "/etc/acme.sh/$container_email/${domains[0]}/${domains[0]}.conf")" |
| 72 | + |
| 73 | +if [[ "$default_pre_hook_command_base64" != "$default_acme_pre_hook" ]]; then |
| 74 | + echo "Default prehook command not saved properly" |
| 75 | +fi |
| 76 | +if [[ "$default_post_hook_command_base64" != "$default_acme_post_hook" ]]; then |
| 77 | + echo "Default posthook command not saved properly" |
| 78 | +fi |
| 79 | + |
| 80 | + |
| 81 | +# Check if the default action is performed |
| 82 | +if docker exec "$le_container_name" [[ ! -f "$default_pre_hook_file" ]]; then |
| 83 | + echo "Default prehook action failed" |
| 84 | +fi |
| 85 | +if docker exec "$le_container_name" [[ ! -f "$default_post_hook_file" ]]; then |
| 86 | + echo "Default posthook action failed" |
| 87 | +fi |
| 88 | + |
| 89 | +# Wait for a symlink at /etc/nginx/certs/${domains[1]}.crt |
| 90 | +wait_for_symlink "${domains[1]}" "$le_container_name" |
| 91 | + |
| 92 | +# Check if the per-container command is deliverd properly in /etc/acme.sh |
| 93 | +if docker exec "$le_container_name" [[ ! -d "/etc/acme.sh/$container_email/${domains[1]}" ]]; then |
| 94 | + echo "The /etc/acme.sh/$container_email/${domains[1]} folder does not exist." |
| 95 | +elif docker exec "$le_container_name" [[ ! -f "/etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf" ]]; then |
| 96 | + echo "The /etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf file does not exist." |
| 97 | +fi |
| 98 | + |
| 99 | +percontainer_pre_hook_command_base64="${acme_pre_hook_key}${acme_base64_start}$(echo -n "$percontainer_pre_hook_command" | base64)${acme_base64_end}" |
| 100 | +percontainer_post_hook_command_base64="${acme_post_hook_key}${acme_base64_start}$(echo -n "$percontainer_post_hook_command" | base64)${acme_base64_end}" |
| 101 | + |
| 102 | +percontainer_acme_pre_hook="$(docker exec "$le_container_name" grep "$acme_pre_hook_key" "/etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf")" |
| 103 | +percontainer_acme_post_hook="$(docker exec "$le_container_name" grep "$acme_post_hook_key" "/etc/acme.sh/$container_email/${domains[1]}/${domains[1]}.conf")" |
| 104 | + |
| 105 | +if [[ "$percontainer_pre_hook_command_base64" != "$percontainer_acme_pre_hook" ]]; then |
| 106 | + echo "Per-container prehook command not saved properly" |
| 107 | +fi |
| 108 | +if [[ "$percontainer_post_hook_command_base64" != "$percontainer_acme_post_hook" ]]; then |
| 109 | + echo "Per-container posthook command not saved properly" |
| 110 | +fi |
| 111 | + |
| 112 | + |
| 113 | +# Check if the percontainer action is performed |
| 114 | +if docker exec "$le_container_name" [[ ! -f "$percontainer_pre_hook_file" ]]; then |
| 115 | + echo "Per-container prehook action failed" |
| 116 | +fi |
| 117 | +if docker exec "$le_container_name" [[ ! -f "$percontainer_post_hook_file" ]]; then |
| 118 | + echo "Per-container posthook action failed" |
| 119 | +fi |
0 commit comments