certs-daemon 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env bash
  2. snapctl is-connected certs
  3. if [ $? -eq 0 ]; then
  4. DOMAIN=`snapctl get domain`
  5. if [ $? -ne 0 ]; then
  6. logger "Missing domain, use 'snap set ${SNAP_NAME} domain=<domain>'"
  7. exit 0
  8. fi
  9. source "${SNAP}/helper/init"
  10. readarray -d '' DOMAIN_DIR < <(find "${CERTS_DIR}" -type d -name "${DOMAIN}" -print0)
  11. if [ "${#DOMAIN_DIR[@]}" -ne 1 ]; then
  12. logger "Not a unique match for domain ${DOMAIN}"
  13. exit 1
  14. fi
  15. DOMAIN_DIR="${DOMAIN_DIR[0]}"
  16. LAST_EDIT=`stat "${DOMAIN_DIR}/.time" --format="%Y" 2> /dev/null || echo 0`
  17. CURR_EDIT=`stat "${SSL_DIR}/.time" --format="%Y" 2> /dev/null || echo 0`
  18. if [ "${LAST_EDIT}" -le "${CURR_EDIT}" ]; then
  19. logger "No new certificate for ${DOMAIN}: `expr ${CURR_EDIT} - ${LAST_EDIT}`"
  20. exit 0
  21. fi
  22. readarray -d '' CERTS < <(find "${DOMAIN_DIR}" -type f -name "*.gpg" -print0)
  23. gpg_start_agent
  24. for CERT in "${CERTS[@]}"; do
  25. DEST="${SSL_DIR}/`basename "${CERT}" ".gpg"`"
  26. CURR=""
  27. if [ -f "${DEST}" ]; then
  28. CURR=`cat "${DEST}"`
  29. fi
  30. gpg --batch --yes --output "${DEST}" --decrypt "${CERT}"
  31. if [ -n "${CURR}" ]; then
  32. DIFF=`echo "${CURR}" | diff "${DEST}" -`
  33. if [ $? -ne 0 ]; then
  34. echo "${ORIG}" > "${DEST}.backup"
  35. fi
  36. fi
  37. done
  38. cp -f "${DOMAIN_DIR}/.time" "${SSL_DIR}/.time"
  39. logger "Replaced certificate ${DOMAIN} for snap ${SNAP_NAME}"
  40. snapctl restart "${SNAP_NAME}.server"
  41. gpg_close_agent
  42. fi