1
0

certs-daemon 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. source "${SNAP}/init"
  3. DOMAIN=`snapctl get domain`
  4. if [ "${DOMAIN}" != "--not-set" ]; then
  5. snapctl is-connected certs
  6. if [ $? -ne 0 ]; then
  7. logger "${SNAP_NAME} is disconnected (uuid: ${UUID})"
  8. logger "${SNAP_NAME} domain is set to '--not-set'"
  9. snapctl set domain="--not-set"
  10. exit 1
  11. fi
  12. readarray -d '' DOMAINS < <(find "${CERTS_DIR}" -type d -name "${DOMAIN}" -print0)
  13. logger "Certificate (${DOMAIN}) available for ${SNAP_NAME}:${UUID}"
  14. if [ "${#DOMAINS[@]}" -ne 0 ]; then
  15. DOMAIN_DIR="${DOMAINS[0]}"
  16. gpg_start_agent
  17. i=0
  18. LAST_EDITED=`stat "${DOMAIN_DIR}/.time" --format="%Y" 2> /dev/null || echo 0`
  19. ORIG_EDITED=`stat "${SSL_DIR}/.time" --format="%Y" 2> /dev/null || echo 0`
  20. if [ "${LAST_EDITED}" -le "${ORIG_EDITED}" ]; then
  21. logger "Certificate for ${DOMAIN} is not changed, exiting"
  22. exit 1
  23. fi
  24. readarray -d '' CERTIFICATES < <(find "${DOMAIN_DIR}" -type f -name "*.gpg" -print0)
  25. for CERTIFICATE in "${CERTIFICATES[@]}"; do
  26. DEST="${SSL_DIR}/`basename "${CERTIFICATE}" ".gpg"`"
  27. ORIG=""
  28. if [ -f "${DEST}" ]; then
  29. ORIG="`cat "${DEST}"`"
  30. fi
  31. gpg --batch --yes --output "${DEST}" --decrypt "${CERTIFICATE}"
  32. DIFF=`echo "${ORIG}" | diff "${DEST}" -`
  33. RET=$?
  34. if [ "${RET}" -ne 0 ]; then
  35. echo "${ORIG}" > "${DEST}.backup"
  36. (( i = $i + 1 ))
  37. fi
  38. done
  39. if [ "${i}" -ne 0 ]; then
  40. cp -f "${DOMAIN_DIR}/.time" "${SSL_DIR}/.time"
  41. logger "Certificates (${DOMAIN}) changed for ${SNAP_NAME}-${UUID}, restart"
  42. snapctl restart "${SNAP_NAME}.hass"
  43. fi
  44. gpg_close_agent
  45. fi
  46. fi