service.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # This file is part of acme-updater, written by Helmut Pozimski 2016-2017.
  2. #
  3. # stov is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, version 2 of the License.
  6. #
  7. # stov is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  14. # -*- coding: utf8 -*-
  15. """ Contains the service module which manages certificates for generic
  16. services that don't need any special configuration. The configuration
  17. needs to at least contain the keys "certificate_path", "key_path",
  18. "tlsa"and "tlsa_ports".
  19. """
  20. import logging
  21. import socket
  22. import os
  23. import subprocess
  24. import shutil
  25. from amulib import helpers
  26. import OpenSSL
  27. LOGGER = logging.getLogger("acme_tlsa_mail")
  28. def run(service_name, config, acme_dir="/var/lib/acme",
  29. named_key_path="/run/named/session.key"):
  30. """
  31. :param service_name: name of the service
  32. :type service_name: str
  33. :param config: configuration for the service
  34. :type config: dict
  35. :param acme_dir: path to the acme state dir
  36. :type acme_dir: str
  37. :param named_key_path: path to the named session.key
  38. :type named_key_path: str
  39. """
  40. fqdn = socket.getfqdn()
  41. certificate_path = config["certificate_path"]
  42. key_path = config["key_path"]
  43. tlsa = config["tlsa"]
  44. tlsa_ports = config["tlsa_ports"]
  45. try:
  46. with open(certificate_path, "r") as cert_file:
  47. cert_text = cert_file.read()
  48. except IOError:
  49. LOGGER.error("Error while opening the %s certificate", service_name)
  50. else:
  51. current_cert = OpenSSL.crypto.load_certificate(
  52. OpenSSL.crypto.FILETYPE_PEM, cert_text
  53. )
  54. acme_cert_path = os.path.join(acme_dir, "live", fqdn,
  55. "cert")
  56. acme_fullchain_path = os.path.join(acme_dir, "live", fqdn,
  57. "fullchain")
  58. if helpers.check_renewal(current_cert, acme_cert_path):
  59. try:
  60. with open(acme_cert_path, "r") as acme_cert_file:
  61. acme_cert_text = acme_cert_file.read()
  62. except IOError:
  63. LOGGER.error("Error while opening new %s "
  64. "certificate file", service_name)
  65. else:
  66. acme_cert = OpenSSL.crypto.load_certificate(
  67. OpenSSL.crypto.FILETYPE_PEM, acme_cert_text
  68. )
  69. if tlsa:
  70. for port in tlsa_ports:
  71. helpers.create_tlsa_records(fqdn, port, acme_cert,
  72. named_key_path)
  73. newkey_path = os.path.join(acme_dir, "live",
  74. fqdn, "privkey")
  75. renewal_successful = False
  76. if certificate_path == key_path:
  77. if helpers.create_backup_copy(certificate_path):
  78. try:
  79. with open(certificate_path, "wb") as target:
  80. with open(acme_fullchain_path, "rb") as chain:
  81. with open(newkey_path, "rb") as newkey:
  82. shutil.copyfileobj(newkey,
  83. target)
  84. shutil.copyfileobj(chain, target)
  85. except IOError:
  86. LOGGER.error("Renewal of cert for %s failed, "
  87. "please clean up manually and "
  88. "check the backup files!",
  89. service_name)
  90. else:
  91. renewal_successful = True
  92. else:
  93. LOGGER.error("Renewal of cert for %s failed, "
  94. "please clean up manually and "
  95. "check the backup files!", service_name)
  96. else:
  97. if helpers.copy_file(acme_fullchain_path,
  98. certificate_path):
  99. if helpers.copy_file(newkey_path, key_path):
  100. renewal_successful = True
  101. else:
  102. LOGGER.error("Renewal of cert for %s failed, "
  103. "please clean up manually and "
  104. "check the backup files!",
  105. service_name)
  106. else:
  107. LOGGER.error("Renewal of cert for %s failed, "
  108. "please clean up manually and "
  109. "check the backup files!", service_name)
  110. if renewal_successful:
  111. LOGGER.info("Certificate for %s successfully "
  112. "renewed, restarting service.",
  113. service_name)
  114. subprocess.call(["/etc/init.d/%s" % service_name,
  115. "restart"])