obtain-certificate.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Return values / exit codes
  3. # 0 = cert created successfully
  4. # 1 = cert renewed successfully
  5. # 2 = cert not due for renewal
  6. # * = errors
  7. source /srv/functions.sh
  8. CERT_DOMAINS=(${DOMAINS[@]})
  9. CERT_DOMAIN=${CERT_DOMAINS[0]}
  10. ACME_BASE=/var/lib/acme
  11. TYPE=${1}
  12. PREFIX=""
  13. # only support rsa certificates for now
  14. if [[ "${TYPE}" != "rsa" ]]; then
  15. log_f "Unknown certificate type '${TYPE}' requested"
  16. exit 5
  17. fi
  18. DOMAINS_FILE=${ACME_BASE}/${CERT_DOMAIN}/domains
  19. CERT=${ACME_BASE}/${CERT_DOMAIN}/${PREFIX}cert.pem
  20. SHARED_KEY=${ACME_BASE}/acme/${PREFIX}key.pem # must already exist
  21. KEY=${ACME_BASE}/${CERT_DOMAIN}/${PREFIX}key.pem
  22. CSR=${ACME_BASE}/${CERT_DOMAIN}/${PREFIX}acme.csr
  23. if [[ -z ${CERT_DOMAINS[*]} ]]; then
  24. log_f "Missing CERT_DOMAINS to obtain a certificate"
  25. exit 3
  26. fi
  27. if [[ "${LE_STAGING}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  28. if [[ ! -z "${DIRECTORY_URL}" ]]; then
  29. log_f "Cannot use DIRECTORY_URL with LE_STAGING=y - ignoring DIRECTORY_URL"
  30. fi
  31. log_f "Using Let's Encrypt staging servers"
  32. DIRECTORY_URL='--directory-url https://acme-staging-v02.api.letsencrypt.org/directory'
  33. elif [[ ! -z "${DIRECTORY_URL}" ]]; then
  34. log_f "Using custom directory URL ${DIRECTORY_URL}"
  35. DIRECTORY_URL="--directory-url ${DIRECTORY_URL}"
  36. fi
  37. if [[ -f ${DOMAINS_FILE} && "$(cat ${DOMAINS_FILE})" == "${CERT_DOMAINS[*]}" ]]; then
  38. if [[ ! -f ${CERT} || ! -f "${KEY}" || -f "${ACME_BASE}/force_renew" ]]; then
  39. log_f "Certificate ${CERT} doesn't exist yet or forced renewal - start obtaining"
  40. # Certificate exists and did not change but could be due for renewal (30 days)
  41. elif ! openssl x509 -checkend 2592000 -noout -in ${CERT} > /dev/null; then
  42. log_f "Certificate ${CERT} is due for renewal (< 30 days) - start renewing"
  43. else
  44. log_f "Certificate ${CERT} validation done, neither changed nor due for renewal."
  45. exit 2
  46. fi
  47. else
  48. log_f "Certificate ${CERT} missing or changed domains '${CERT_DOMAINS[*]}' - start obtaining"
  49. fi
  50. # Make backup
  51. if [[ -f ${CERT} ]]; then
  52. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  53. BACKUP_DIR=${ACME_BASE}/backups/${CERT_DOMAIN}/${PREFIX}${DATE}
  54. log_f "Creating backups in ${BACKUP_DIR} ..."
  55. mkdir -p ${BACKUP_DIR}/
  56. [[ -f ${DOMAINS_FILE} ]] && cp ${DOMAINS_FILE} ${BACKUP_DIR}/
  57. [[ -f ${CERT} ]] && cp ${CERT} ${BACKUP_DIR}/
  58. [[ -f ${KEY} ]] && cp ${KEY} ${BACKUP_DIR}/
  59. [[ -f ${CSR} ]] && cp ${CSR} ${BACKUP_DIR}/
  60. fi
  61. mkdir -p ${ACME_BASE}/${CERT_DOMAIN}
  62. if [[ ! -f ${KEY} ]]; then
  63. log_f "Copying shared private key for this certificate..."
  64. cp ${SHARED_KEY} ${KEY}
  65. chmod 600 ${KEY}
  66. fi
  67. # Generating CSR
  68. printf "[SAN]\nsubjectAltName=" > /tmp/_SAN
  69. printf "DNS:%s," "${CERT_DOMAINS[@]}" >> /tmp/_SAN
  70. sed -i '$s/,$//' /tmp/_SAN
  71. openssl req -new -sha256 -key ${KEY} -subj "/" -reqexts SAN -config <(cat "$(openssl version -d | sed 's/.*"\(.*\)"/\1/g')/openssl.cnf" /tmp/_SAN) > ${CSR}
  72. # acme-tiny writes info to stderr and ceritifcate to stdout
  73. # The redirects will do the following:
  74. # - redirect stdout to temp certificate file
  75. # - redirect acme-tiny stderr to stdout (logs to variable ACME_RESPONSE)
  76. # - tee stderr to get live output and log to dockerd
  77. log_f "Checking resolver..."
  78. until dig letsencrypt.org +time=3 +tries=1 @unbound > /dev/null; do
  79. sleep 2
  80. done
  81. log_f "Resolver OK"
  82. log_f "Using command acme-tiny ${DIRECTORY_URL} ${ACME_CONTACT_PARAMETER} --account-key ${ACME_BASE}/acme/account.pem --disable-check --csr ${CSR} --acme-dir /var/www/acme/"
  83. ACME_RESPONSE=$(acme-tiny ${DIRECTORY_URL} ${ACME_CONTACT_PARAMETER} \
  84. --account-key ${ACME_BASE}/acme/account.pem \
  85. --disable-check \
  86. --csr ${CSR} \
  87. --acme-dir /var/www/acme/ 2>&1 > /tmp/_cert.pem | tee /dev/fd/5; exit ${PIPESTATUS[0]})
  88. SUCCESS="$?"
  89. ACME_RESPONSE_B64=$(echo "${ACME_RESPONSE}" | openssl enc -e -A -base64)
  90. log_f "${ACME_RESPONSE_B64}" redis_only b64
  91. case "$SUCCESS" in
  92. 0) # cert requested
  93. log_f "Deploying certificate ${CERT}..."
  94. # Deploy the new certificate and key
  95. # Moving temp cert to {domain} folder
  96. if verify_hash_match /tmp/_cert.pem ${KEY}; then
  97. RETURN=0 # certificate created
  98. if [[ -f ${CERT} ]]; then
  99. RETURN=1 # certificate renewed
  100. fi
  101. mv -f /tmp/_cert.pem ${CERT}
  102. echo -n ${CERT_DOMAINS[*]} > ${DOMAINS_FILE}
  103. rm /var/www/acme/* 2> /dev/null
  104. log_f "Certificate successfully obtained"
  105. exit ${RETURN}
  106. else
  107. log_f "Certificate was successfully requested, but key and certificate have non-matching hashes, ignoring certificate"
  108. exit 4
  109. fi
  110. ;;
  111. *) # non-zero is non-fun
  112. log_f "Failed to obtain certificate ${CERT} for domains '${CERT_DOMAINS[*]}'"
  113. redis-cli -h redis SET ACME_FAIL_TIME "$(date +%s)"
  114. exit 100${SUCCESS}
  115. ;;
  116. esac