service.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 os
  22. import subprocess
  23. import shutil
  24. from amulib import helpers
  25. import OpenSSL
  26. LOGGER = logging.getLogger("acme_tlsa_mail")
  27. def run(service_name, config, acme_dir="/var/lib/acme",
  28. named_key_path="/run/named/session.key"):
  29. """
  30. :param service_name: name of the service
  31. :type service_name: str
  32. :param config: configuration for the service
  33. :type config: dict
  34. :param acme_dir: path to the acme state dir
  35. :type acme_dir: str
  36. :param named_key_path: path to the named session.key
  37. :type named_key_path: str
  38. """
  39. certificate_path = config["certificate_path"]
  40. key_path = config["key_path"]
  41. tlsa = config["tlsa"]
  42. tlsa_ports = config["tlsa_ports"]
  43. renewal_successful = False
  44. try:
  45. with open(certificate_path, "r") as cert_file:
  46. cert_text = cert_file.read()
  47. except IOError:
  48. LOGGER.error("Error while opening the %s certificate", service_name)
  49. else:
  50. current_cert = OpenSSL.crypto.load_certificate(
  51. OpenSSL.crypto.FILETYPE_PEM, cert_text
  52. )
  53. fqdn = current_cert.get_subject().CN
  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. if certificate_path == key_path:
  76. if helpers.create_backup_copy(certificate_path):
  77. try:
  78. with open(certificate_path, "wb") as target:
  79. with open(acme_fullchain_path, "rb") as chain:
  80. with open(newkey_path, "rb") as newkey:
  81. shutil.copyfileobj(newkey,
  82. target)
  83. shutil.copyfileobj(chain, target)
  84. except IOError:
  85. LOGGER.error("Renewal of cert for %s failed, "
  86. "please clean up manually and "
  87. "check the backup files!",
  88. service_name)
  89. else:
  90. renewal_successful = True
  91. else:
  92. LOGGER.error("Renewal of cert for %s failed, "
  93. "please clean up manually and "
  94. "check the backup files!", service_name)
  95. else:
  96. if helpers.copy_file(acme_fullchain_path,
  97. certificate_path):
  98. if helpers.copy_file(newkey_path, key_path):
  99. renewal_successful = True
  100. else:
  101. LOGGER.error("Renewal of cert for %s failed, "
  102. "please clean up manually and "
  103. "check the backup files!",
  104. service_name)
  105. else:
  106. LOGGER.error("Renewal of cert for %s failed, "
  107. "please clean up manually and "
  108. "check the backup files!", service_name)
  109. if renewal_successful:
  110. LOGGER.info("Certificate for %s successfully "
  111. "renewed, restarting service.",
  112. service_name)
  113. subprocess.call(["/etc/init.d/%s" % service_name,
  114. "restart"])