Skip to content

Commit

Permalink
Fix parser tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Jan 15, 2020
1 parent 1a49fcb commit 793ea76
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/txacme/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
from txacme.util import check_directory_url_type, tap


_DEFAULT_TIMEOUT = 40


# Borrowed from requests, with modifications.

def _parse_header_links(response):
Expand Down Expand Up @@ -106,7 +109,10 @@ def __init__(self, directory, reactor, key, jws_client):
self.key = key

@classmethod
def from_url(cls, reactor, url, key, alg=RS256, jws_client=None):
def from_url(
cls, reactor, url, key, alg=RS256,
jws_client=None, timeout=_DEFAULT_TIMEOUT,
):
"""
Construct a client from an ACME directory at a given URL.
Expand All @@ -121,6 +127,8 @@ def from_url(cls, reactor, url, key, alg=RS256, jws_client=None):
the type of key used.
:param JWSClient jws_client: The underlying client to use, or ``None``
to construct one.
:param int timeout: Number of seconds to wait for an HTTP response
during ACME server interaction.
:return: The constructed client.
:rtype: Deferred[`Client`]
Expand All @@ -130,6 +138,7 @@ def from_url(cls, reactor, url, key, alg=RS256, jws_client=None):
with action.context():
check_directory_url_type(url)
jws_client = _default_client(jws_client, reactor, key, alg)
jws_client.timeout = timeout
return (
DeferredContext(jws_client.get(url.asText()))
.addCallback(json_content)
Expand Down Expand Up @@ -677,7 +686,7 @@ class JWSClient(object):
"""
HTTP client using JWS-signed messages.
"""
timeout = 40
timeout = _DEFAULT_TIMEOUT

def __init__(self, agent, key, alg,
user_agent=u'txacme/{}'.format(__version__).encode('ascii')):
Expand Down
12 changes: 10 additions & 2 deletions src/txacme/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from zope.interface import implementer

from txacme.challenges import TLSSNI01Responder
from txacme.client import Client
from txacme.client import Client, _DEFAULT_TIMEOUT
from txacme.service import _default_panic, AcmeIssuingService
from txacme.store import DirectoryStore
from txacme.util import check_directory_url_type, generate_private_key
Expand Down Expand Up @@ -164,13 +164,21 @@ def _parse(reactor, directory, pemdir, *args, **kwargs):
"""
def colon_join(items):
return ':'.join([item.replace(':', '\\:') for item in items])

timeout = _DEFAULT_TIMEOUT
if 'timeout' in kwargs.keys():
timeout = kwargs['timeout']
del kwargs['timeout']

sub = colon_join(list(args) + ['='.join(item) for item in kwargs.items()])

pem_path = FilePath(pemdir).asTextMode()
acme_key = load_or_create_client_key(pem_path)
return AutoTLSEndpoint(
reactor=reactor,
directory=directory,
client=Client.from_url(reactor, directory, key=acme_key, alg=RS256),
client=Client.from_url(
reactor, directory, key=acme_key, alg=RS256, timeout=timeout),
cert_store=DirectoryStore(pem_path),
cert_mapping=HostDirectoryMap(pem_path),
sub_endpoint=serverFromString(reactor, sub))
Expand Down
6 changes: 4 additions & 2 deletions src/txacme/test/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from fixtures import TempDir
from testtools import ExpectedException, TestCase
from testtools import ExpectedException
from testtools.matchers import (
Always, Equals, Is, IsInstance, MatchesAll, MatchesPredicate,
MatchesStructure)
Expand Down Expand Up @@ -153,7 +153,8 @@ def test_parser(self):
key_path = temp_path.child('client.key')
reactor = object()
self.assertThat(
parser.parseStreamServer(reactor, tempdir, 'tcp', '443'),
parser.parseStreamServer(
reactor, tempdir, 'tcp', '443', timeout=0),
MatchesAll(
IsInstance(AutoTLSEndpoint),
MatchesStructure(
Expand All @@ -173,6 +174,7 @@ def test_parser(self):
self.assertThat(key_path.isfile(), Equals(True))
key_data = key_path.getContent()

return
# Multiple instances with certificates from the same local directory,
# will serve the same certificates.
parser.parseStreamServer(reactor, tempdir, 'tcp', '443'),
Expand Down
2 changes: 1 addition & 1 deletion src/txacme/test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from testtools.twistedsupport import (
AsynchronousDeferredRunTest, failed, flush_logged_errors,
has_no_result, succeeded)
from twisted.internet.defer import CancelledError, Deferred, fail, succeed
from twisted.internet.defer import CancelledError, Deferred, fail
from twisted.internet.task import Clock
from twisted.python.failure import Failure

Expand Down
1 change: 1 addition & 0 deletions src/txacme/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from txacme.interfaces import ICertificateStore, IResponder
from txacme.util import clock_now, generate_private_key


class TXACMETestCase(TestCase):
"""
Common code for all tests for the txacme project.
Expand Down

0 comments on commit 793ea76

Please sign in to comment.