Skip to content

Commit c082a03

Browse files
committed
Restore routes on challenge cleanup and add challenge *:80 listener if needed
1 parent 5525117 commit c082a03

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

certbot_nginx_unit/configurator.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Configurator(common.Installer, interfaces.Authenticator):
4747

4848
def __init__(self, *args: Any, **kwargs: Any):
4949
super().__init__(*args, **kwargs)
50+
self._prepared = False
5051
self._configuration = None
5152
self.unitc = Unitc()
5253
self._entropy = datetime.now().strftime("%Y%m%d%H%M%S")
@@ -55,6 +56,8 @@ def __init__(self, *args: Any, **kwargs: Any):
5556
self._full_root: str = ""
5657
self._performed: DefaultDict[str, Set[AnnotatedChallenge]] = collections.defaultdict(set)
5758
self._created_dirs: List[str] = []
59+
self._to_remove: List[str] = []
60+
self._backup_routes: List[str] = []
5861

5962
def get_all_names(self) -> Iterable[str]:
6063
return []
@@ -140,11 +143,13 @@ def _ensure_challenge_listener(self):
140143
if "listeners" not in self._configuration:
141144
raise errors.PluginError("No listeners configured")
142145
if "*:80" not in self._configuration["listeners"]:
143-
raise errors.PluginError("No '*:80' default listeners configured")
146+
self._configuration["listeners"]["*:80"] = {"pass": "routes"}
147+
self._to_remove.append("/config/listeners/*:80")
144148
if "pass" not in self._configuration["listeners"]["*:80"]:
145149
raise errors.PluginError("Cannot configure the route for the *:80 listener")
146150

147151
actual_route = self._configuration["listeners"]["*:80"]["pass"]
152+
self._backup_routes = self._configuration.get("routes", [])
148153
default_route = self._ensure_acme_route(actual_route)
149154
if actual_route == default_route:
150155
return
@@ -237,7 +242,10 @@ def prepare(self) -> None:
237242
"""Prepare the authenticator/installer."""
238243
# @todo verify "unitc" executable
239244
# @todo lock to prevent concurrent multi update
245+
if self._prepared:
246+
return
240247
self._configuration = self._get_unit_configuration("/config")
248+
self._prepared = True
241249

242250
def more_info(self) -> str: # pylint: disable=missing-function-docstring
243251
return self.MORE_INFO.format(self.conf("path"))
@@ -339,6 +347,12 @@ def _perform_single(self, achall: AnnotatedChallenge) -> challenges.ChallengeRes
339347
return response
340348

341349
def cleanup(self, achalls: List[AnnotatedChallenge]) -> None: # pylint: disable=missing-function-docstring
350+
for config_path in self._to_remove:
351+
self.unitc.delete(config_path, None, "Delete tmp configuration failed")
352+
353+
if self._configuration["routes"] != self._backup_routes:
354+
self.unitc.put("/config/routes", json.dumps(self._backup_routes).encode())
355+
342356
for achall in achalls:
343357
root_path = self._full_root
344358
if root_path is not None:

certbot_nginx_unit/tests/configurator_test.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,6 @@ def only_80_listener_configuration_after_cert_list():
7171
}
7272

7373

74-
def only_80_listener_configuration_after_cert_dictionary():
75-
return {
76-
"listeners": {
77-
"*:80": {"pass": "routes/acme"},
78-
"*:443": {"pass": "routes/default"}
79-
},
80-
"routes": {
81-
"acme": {
82-
"match": {"uri": "/.well-known/acme-challenge/*"},
83-
"action": {"share": "/srv/www/unit/$uri"},
84-
},
85-
"default": {
86-
"action": {"share": "/srv/www/unit/index.html"}
87-
}
88-
}
89-
}
90-
91-
9274
def get_configuration_side_effect(*args):
9375
if args[0] == "/config":
9476
return json.dumps(empty_configuration())
@@ -183,7 +165,6 @@ def test_only_80_listener_configuration(self, unitc_mock):
183165
'nginx unit copy to /certificates failed'
184166
)
185167

186-
print(unitc_mock.put.mock_calls)
187168
unitc_mock.put.assert_any_call(
188169
'/config/listeners',
189170
b'{"*:80": {"pass": "routes"}, "*:443": {"pass": "routes", "tls": {"certificate": ["domain_' +
@@ -221,4 +202,10 @@ def test_authenticate(self, unitc_mock, challenge_mock):
221202
webroot + b'/$uri"}}, {"action": {"share": "/srv/www/unit/index.html"}}]'
222203
)
223204

205+
configurator.cleanup(challenge_mock)
206+
unitc_mock.put.assert_any_call(
207+
'/config/routes',
208+
json.dumps(only_80_listener_configuration()['routes']).encode()
209+
)
210+
224211
notify.stop()

0 commit comments

Comments
 (0)