1
0
mirror of https://github.com/certbot/certbot.git synced 2026-01-21 19:01:07 +03:00

standalone2: run(): tls -> challenge_type.

This commit is contained in:
Jakub Warmuz
2015-10-15 20:31:22 +00:00
parent 6f44bcf117
commit ec24641511
2 changed files with 22 additions and 17 deletions

View File

@@ -44,27 +44,29 @@ class ServerManager(object):
self.certs = certs
self.simple_http_resources = simple_http_resources
def run(self, port, tls):
def run(self, port, challenge_type):
"""Run ACME server on specified ``port``.
This method is idempotent, i.e. all calls with the same pair of
``(port, tls)`` will reuse the same server.
``(port, challenge_type)`` will reuse the same server.
:param int port: Port to run the server on.
:param bool tls: TLS or non-TLS?
:param challenge_type: Subclass of `acme.challenges.Challenge`,
either `acme.challenge.SimpleHTTP` or `acme.challenges.DVSNI`.
:returns: Server instance.
:rtype: ACMEServerMixin
"""
assert challenge_type in (challenges.DVSNI, challenges.SimpleHTTP)
if port in self._instances:
return self._instances[port].server
address = ("", port)
try:
if tls:
if challenge_type is challenges.DVSNI:
server = acme_standalone.DVSNIServer(address, self.certs)
else:
else: # challenges.SimpleHTTP
server = acme_standalone.SimpleHTTPServer(
address, self.simple_http_resources)
except socket.error as error:
@@ -224,7 +226,7 @@ class Authenticator(common.Plugin):
for achall in achalls:
if isinstance(achall, achallenges.SimpleHTTP):
server = self.servers.run(
self.config.simple_http_port, tls=False)
self.config.simple_http_port, challenges.SimpleHTTP)
response, validation = achall.gen_response_and_validation(
tls=False)
self.simple_http_resources.add(
@@ -234,7 +236,7 @@ class Authenticator(common.Plugin):
cert = self.simple_http_cert
domain = achall.domain
else: # DVSNI
server = self.servers.run(self.config.dvsni_port, tls=True)
server = self.servers.run(self.config.dvsni_port, challenges.DVSNI)
response, cert, _ = achall.gen_cert_and_response(self.key)
domain = response.z_domain
self.certs[domain] = (self.key, cert)

View File

@@ -32,23 +32,23 @@ class ServerManagerTest(unittest.TestCase):
self.assertTrue(
self.mgr.simple_http_resources is self.simple_http_resources)
def _test_run_stop(self, tls):
server = self.mgr.run(port=0, tls=tls)
def _test_run_stop(self, challenge_type):
server = self.mgr.run(port=0, challenge_type=challenge_type)
port = server.socket.getsockname()[1] # pylint: disable=no-member
self.assertEqual(self.mgr.running(), {port: server})
self.mgr.stop(port=port)
self.assertEqual(self.mgr.running(), {})
def test_run_stop_tls(self):
self._test_run_stop(tls=True)
def test_run_stop_dvsni(self):
self._test_run_stop(challenges.DVSNI)
def test_run_stop_non_tls(self):
self._test_run_stop(tls=False)
def test_run_stop_simplehttp(self):
self._test_run_stop(challenges.SimpleHTTP)
def test_run_idempotent(self):
server = self.mgr.run(port=0, tls=False)
server = self.mgr.run(port=0, challenge_type=challenges.SimpleHTTP)
port = server.socket.getsockname()[1] # pylint: disable=no-member
server2 = self.mgr.run(port=port, tls=False)
server2 = self.mgr.run(port=port, challenge_type=challenges.SimpleHTTP)
self.assertEqual(self.mgr.running(), {port: server})
self.assertTrue(server is server2)
self.mgr.stop(port)
@@ -59,7 +59,8 @@ class ServerManagerTest(unittest.TestCase):
some_server.bind(("", 0))
port = some_server.getsockname()[1]
self.assertRaises(
errors.StandaloneBindError, self.mgr.run, port, tls=False)
errors.StandaloneBindError, self.mgr.run, port,
challenge_type=challenges.SimpleHTTP)
self.assertEqual(self.mgr.running(), {})
@@ -165,7 +166,9 @@ class AuthenticatorTest(unittest.TestCase):
self.assertTrue(isinstance(responses[1], challenges.DVSNIResponse))
self.assertEqual(self.auth.servers.run.mock_calls, [
mock.call(4321, tls=False), mock.call(1234, tls=True)])
mock.call(4321, challenges.SimpleHTTP),
mock.call(1234, challenges.DVSNI),
])
self.assertEqual(self.auth.served, {
"server1234": set([dvsni]),
"server4321": set([simple_http]),