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
+9 -7
View File
@@ -44,27 +44,29 @@ class ServerManager(object):
self.certs = certs self.certs = certs
self.simple_http_resources = simple_http_resources self.simple_http_resources = simple_http_resources
def run(self, port, tls): def run(self, port, challenge_type):
"""Run ACME server on specified ``port``. """Run ACME server on specified ``port``.
This method is idempotent, i.e. all calls with the same pair of 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 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. :returns: Server instance.
:rtype: ACMEServerMixin :rtype: ACMEServerMixin
""" """
assert challenge_type in (challenges.DVSNI, challenges.SimpleHTTP)
if port in self._instances: if port in self._instances:
return self._instances[port].server return self._instances[port].server
address = ("", port) address = ("", port)
try: try:
if tls: if challenge_type is challenges.DVSNI:
server = acme_standalone.DVSNIServer(address, self.certs) server = acme_standalone.DVSNIServer(address, self.certs)
else: else: # challenges.SimpleHTTP
server = acme_standalone.SimpleHTTPServer( server = acme_standalone.SimpleHTTPServer(
address, self.simple_http_resources) address, self.simple_http_resources)
except socket.error as error: except socket.error as error:
@@ -224,7 +226,7 @@ class Authenticator(common.Plugin):
for achall in achalls: for achall in achalls:
if isinstance(achall, achallenges.SimpleHTTP): if isinstance(achall, achallenges.SimpleHTTP):
server = self.servers.run( 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( response, validation = achall.gen_response_and_validation(
tls=False) tls=False)
self.simple_http_resources.add( self.simple_http_resources.add(
@@ -234,7 +236,7 @@ class Authenticator(common.Plugin):
cert = self.simple_http_cert cert = self.simple_http_cert
domain = achall.domain domain = achall.domain
else: # DVSNI 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) response, cert, _ = achall.gen_cert_and_response(self.key)
domain = response.z_domain domain = response.z_domain
self.certs[domain] = (self.key, cert) self.certs[domain] = (self.key, cert)
+13 -10
View File
@@ -32,23 +32,23 @@ class ServerManagerTest(unittest.TestCase):
self.assertTrue( self.assertTrue(
self.mgr.simple_http_resources is self.simple_http_resources) self.mgr.simple_http_resources is self.simple_http_resources)
def _test_run_stop(self, tls): def _test_run_stop(self, challenge_type):
server = self.mgr.run(port=0, tls=tls) server = self.mgr.run(port=0, challenge_type=challenge_type)
port = server.socket.getsockname()[1] # pylint: disable=no-member port = server.socket.getsockname()[1] # pylint: disable=no-member
self.assertEqual(self.mgr.running(), {port: server}) self.assertEqual(self.mgr.running(), {port: server})
self.mgr.stop(port=port) self.mgr.stop(port=port)
self.assertEqual(self.mgr.running(), {}) self.assertEqual(self.mgr.running(), {})
def test_run_stop_tls(self): def test_run_stop_dvsni(self):
self._test_run_stop(tls=True) self._test_run_stop(challenges.DVSNI)
def test_run_stop_non_tls(self): def test_run_stop_simplehttp(self):
self._test_run_stop(tls=False) self._test_run_stop(challenges.SimpleHTTP)
def test_run_idempotent(self): 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 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.assertEqual(self.mgr.running(), {port: server})
self.assertTrue(server is server2) self.assertTrue(server is server2)
self.mgr.stop(port) self.mgr.stop(port)
@@ -59,7 +59,8 @@ class ServerManagerTest(unittest.TestCase):
some_server.bind(("", 0)) some_server.bind(("", 0))
port = some_server.getsockname()[1] port = some_server.getsockname()[1]
self.assertRaises( 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(), {}) self.assertEqual(self.mgr.running(), {})
@@ -165,7 +166,9 @@ class AuthenticatorTest(unittest.TestCase):
self.assertTrue(isinstance(responses[1], challenges.DVSNIResponse)) self.assertTrue(isinstance(responses[1], challenges.DVSNIResponse))
self.assertEqual(self.auth.servers.run.mock_calls, [ 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, { self.assertEqual(self.auth.served, {
"server1234": set([dvsni]), "server1234": set([dvsni]),
"server4321": set([simple_http]), "server4321": set([simple_http]),