Skip to content

Commit 1c5a831

Browse files
committed
Fix webroot path param
1 parent c485af4 commit 1c5a831

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The command `unitc` should be installed and executable.
2424
```
2525
install and configure this plugin
2626
```
27-
sudo snap install --classic certbot-nginx-unit
27+
sudo snap install certbot-nginx-unit
2828
sudo snap set certbot trust-plugin-with-root=ok
2929
sudo snap connect certbot:plugin certbot-nginx-unit
3030
```
@@ -85,7 +85,7 @@ Configure the unit listener with `*:80` or `*:443`
8585
Now, generate and automatically install the certificate with
8686
8787
```
88-
# certbot --configurator nginx_unit -d www.myapp.com
88+
# certbot --configurator nginx-unit -d www.myapp.com
8989
```
9090
9191
The result is a certificate created and installed.
@@ -157,8 +157,8 @@ Certbot installs a timer on the system to renew certificates one month before th
157157
You can run the certbot command for each domain
158158
159159
```
160-
# certbot --configurator nginx_unit -d www.myapp1.com
161-
# certbot --configurator nginx_unit -d www.myapp2.com
160+
# certbot --configurator nginx-unit -d www.myapp1.com
161+
# certbot --configurator nginx-unit -d www.myapp2.com
162162
# unitc '/config/listeners/*:443'
163163
```
164164

certbot_nginx_unit/configurator.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
"""
77
import collections
8+
import copy
89
import json
910
import logging
1011
from datetime import datetime
@@ -128,9 +129,7 @@ def _ensure_tls_listener(self):
128129
if "*:443" not in self._configuration["listeners"]:
129130
if "*:80" not in self._configuration["listeners"]:
130131
raise errors.PluginError("No '*:80' default listeners configured")
131-
self._configuration["listeners"]["*:443"] = self._configuration["listeners"]["*:80"]
132-
new_route = self._ensure_acme_route(self._configuration["listeners"]["*:443"]["pass"])
133-
self._configuration["listeners"]["*:80"] = {"pass": new_route}
132+
self._configuration["listeners"]["*:443"] = copy.deepcopy(self._configuration["listeners"]["*:80"])
134133

135134
if "tls" not in self._configuration["listeners"]["*:443"]:
136135
self._configuration["listeners"]["*:443"]["tls"] = {}
@@ -157,12 +156,11 @@ def _ensure_challenge_listener(self):
157156
self.unitc.put(listener_route, default_route.encode(), success_message, error_message)
158157

159158
def _ensure_acme_route(self, actual_route: str) -> str:
160-
acme_challenge_url = "/.well-known/acme-challenge/*"
161-
acme_base_path = "/srv/www/unit"
159+
acme_challenge_url = "/" + challenges.HTTP01.URI_ROOT_PATH + "/*"
162160
acme_route = [
163161
{
164162
"match": {"uri": acme_challenge_url},
165-
"action": {"share": acme_base_path + "/$uri"},
163+
"action": {"share": self._challenge_path + "/$uri"},
166164
}
167165
]
168166
if actual_route != "routes" and actual_route != "routes/acme":
@@ -190,8 +188,8 @@ def _ensure_acme_route(self, actual_route: str) -> str:
190188
raise errors.PluginError("Cannot configure the routes: unknown route[0] type")
191189

192190
first_route = self._configuration["routes"][0]
193-
if "match" in first_route and "uri" in first_route["match"] and first_route["match"][
194-
"uri"] == acme_challenge_url:
191+
if ("match" in first_route and "uri" in first_route["match"]
192+
and first_route["match"]["uri"] == acme_challenge_url):
195193
return "routes"
196194

197195
routes = acme_route + self._configuration["routes"]
@@ -255,9 +253,9 @@ def get_chall_pref(self, domain: str) -> Iterable[Type[challenges.Challenge]]:
255253
# pylint: disable=unused-argument,missing-function-docstring
256254
return [challenges.HTTP01]
257255

258-
def perform(self, achalls: List[AnnotatedChallenge]) -> List[
259-
challenges.ChallengeResponse]: # pylint: disable=missing-function-docstring
256+
def perform(self, achalls: List[AnnotatedChallenge]) -> List[challenges.ChallengeResponse]:
260257

258+
self.prepare()
261259
self._set_webroot(achalls)
262260
self._create_challenge_dir()
263261

@@ -268,7 +266,7 @@ def perform(self, achalls: List[AnnotatedChallenge]) -> List[
268266
def _set_webroot(self, achalls: Iterable[AnnotatedChallenge]) -> None:
269267
webroot_path = '/srv/www/unit/'
270268
if self.conf("path"):
271-
webroot_path = self.conf("path")[-1]
269+
webroot_path = self.conf("path")
272270

273271
logger.info("Using the webroot path %s for all domains.", webroot_path)
274272

certbot_nginx_unit/tests/configurator_test.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def get_nginx_unit_configurator(self, logs_dir):
128128

129129
backups = os.path.join(logs_dir, "backups")
130130
self.configuration.backup_dir = backups
131+
self.configuration.nginx_unit_path = logs_dir
131132

132133
return Configurator(self.configuration, name="nginx_unit")
133134

@@ -181,15 +182,43 @@ def test_only_80_listener_configuration(self, unitc_mock):
181182
'Certificate deployed',
182183
'nginx unit copy to /certificates failed'
183184
)
184-
unitc_mock.put.assert_any_call(
185-
'/config/routes',
186-
b'[{"match": {"uri": "/.well-known/acme-challenge/*"}, "action": {"share": "/srv/www/unit/$uri"}}, {"action": {"share": "/srv/www/unit/index.html"}}]'
187-
)
185+
186+
print(unitc_mock.put.mock_calls)
188187
unitc_mock.put.assert_any_call(
189188
'/config/listeners',
190-
b'{"*:80": {"pass": "routes"}, "*:443": {"pass": "routes", "tls": {"certificate": ["domain_' + entropy.encode() + b'"]}}}',
189+
b'{"*:80": {"pass": "routes"}, "*:443": {"pass": "routes", "tls": {"certificate": ["domain_' +
190+
entropy.encode() + b'"]}}}',
191191
put_success_message,
192192
put_error_message
193193
)
194194

195195
notify.stop()
196+
197+
@mock.patch('certbot_nginx_unit.unitc')
198+
@mock.patch('certbot.achallenges.AnnotatedChallenge')
199+
def test_authenticate(self, unitc_mock, challenge_mock):
200+
unitc_mock.get.side_effect = get_configuration_side_effect_80_listener
201+
unitc_mock.put.side_effect = put_configuration_side_effect_80_listener
202+
203+
challenge_mock.response_and_validation.return_value = ("response", "validation")
204+
challenge_mock.chall.encode.return_value = "token"
205+
206+
webroot = self.configuration.nginx_unit_path.encode()
207+
configurator = self.config
208+
configurator.unitc = unitc_mock
209+
notify = mock.patch('certbot.display.util.notify')
210+
notify.start()
211+
212+
assert ["response"] == configurator.perform([challenge_mock])
213+
214+
get_success_message = 'Get configuration'
215+
get_error_message = 'nginx unit get configuration failed'
216+
217+
unitc_mock.get.assert_any_call("/config", get_success_message, get_error_message)
218+
unitc_mock.put.assert_any_call(
219+
'/config/routes',
220+
b'[{"match": {"uri": "/.well-known/acme-challenge/*"}, "action": {"share": "' +
221+
webroot + b'/$uri"}}, {"action": {"share": "/srv/www/unit/index.html"}}]'
222+
)
223+
224+
notify.stop()

0 commit comments

Comments
 (0)