]> git.sesse.net Git - letsencrypt-hitch-plugin/blob - hitch.py
Small update for certbot in bookworm.
[letsencrypt-hitch-plugin] / hitch.py
1 """Hitch plugin."""
2 import logging
3 import os
4 import re
5 import subprocess
6
7 from zope.interface import implementer, provider
8
9 from certbot import errors
10 from certbot import interfaces
11 from certbot.plugins import common
12
13
14 logger = logging.getLogger(__name__)
15
16
17 @implementer(interfaces.IInstaller)
18 @provider(interfaces.IPluginFactory)
19 class Installer(common.Plugin):
20     """Hitch installer."""
21
22     description = "Hitch Installer"
23     hidden = True
24
25     def prepare(self):
26         with open("/etc/hitch/hitch.conf") as config_file:
27             self.config = config_file.readlines()
28
29     def more_info(self):
30         return "Installer for Hitch TLS wrapper."
31
32     def get_all_names(self):
33         raise errors.PluginError("not implemented")
34
35     @classmethod
36     def add_parser_arguments(cls, add):
37         pass
38
39     def deploy_cert(self, domain, cert_path, key_path,
40                     chain_path=None, fullchain_path=None):
41         # Concatenate private key and certificate together into one file.
42         with open(key_path) as key_file:
43             pem = key_file.read()
44
45         # Add the full chain if we have it; else just the certificate.
46         if fullchain_path is not None:
47             with open(fullchain_path) as cert_file:
48                 pem += cert_file.read()
49         else:
50             with open(cert_path) as cert_file:
51                 pem += cert_file.read()
52
53         # Add DH params if we have them (needed for PFS).
54         try:
55             with open("/etc/hitch/dh-param.pem") as dh_param_file:
56                 pem += dh_param_file.read()
57         except:
58             pass
59
60         # Actually write the full file.
61         filename = os.path.join(os.path.dirname(cert_path), "all.pem")
62         fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
63         with os.fdopen(fd, 'w') as pem_file:
64             pem_file.write(pem)
65
66         # Now go check the config file to see if this file is already there.
67         found = False
68         last_pem_line = None
69         for line_num in range(len(self.config)):
70             m = re.match("^\s*pem-file\s*=\s*\"([^\"]+)\"", self.config[line_num])
71             if m:
72                 last_pem_line = line_num
73                 if m.groups()[0] == filename:
74                     found = True
75
76         # If it's not already there, add it after the last line.
77         if not found:
78             if last_pem_line is None:
79                 last_pem_line = len(self.config) - 1
80             config_line = "pem-file = \"%s\"  # Added by Let's Encrypt installer.\n" % filename
81             self.config.insert(last_pem_line + 1, config_line)
82
83         pass  # pragma: no cover
84
85     def enhance(self, domain, enhancement, options=None):
86         raise errors.PluginError("not implemented")
87
88     def supported_enhancements(self):
89         return []
90
91     def get_all_certs_keys(self):
92         raise errors.PluginError("not implemented")
93         return []
94
95     def save(self, title=None, temporary=False):
96         if temporary:
97             raise errors.PluginError("temporary is not implemented")
98
99         with open("/etc/hitch/hitch.conf", "w") as config_file:
100             config_file.writelines(self.config)
101
102     def rollback_checkpoints(self, rollback=1):
103         raise errors.PluginError("not implemented")
104
105     def recovery_routine(self):
106         raise errors.PluginError("not implemented")
107
108     def view_config_changes(self):
109         raise errors.PluginError("not implemented")
110
111     def config_test(self):
112         raise errors.PluginError("not implemented")
113
114     def restart(self):
115         subprocess.call(["systemctl", "reload", "hitch.service"])