Skip to content

Commit 95e96de

Browse files
authored
Merge pull request #140 from rethinkdb/feature/wildcard-certs-hostname
Feature/wildcard certs hostname
2 parents 9d85d0b + b5dfb95 commit 95e96de

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

rethinkdb/gevent_net/net_gevent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from rethinkdb import net, ql2_pb2
2727
from rethinkdb.errors import ReqlAuthError, ReqlCursorEmpty, ReqlDriverError, ReqlTimeoutError, RqlDriverError, \
2828
RqlTimeoutError
29+
from rethinkdb.helpers import get_hostname_for_ssl_match
2930
from rethinkdb.logger import default_logger
3031

3132
__all__ = ['Connection']
@@ -103,7 +104,10 @@ def __init__(self, parent):
103104
self._socket.close()
104105
raise ReqlDriverError("SSL handshake failed (see server log for more information): %s" % str(exc))
105106
try:
106-
ssl.match_hostname(self._socket.getpeercert(), hostname=self.host)
107+
ssl.match_hostname(
108+
self._socket.getpeercert(),
109+
hostname=get_hostname_for_ssl_match(self.host)
110+
)
107111
except ssl.CertificateError:
108112
self._socket.close()
109113
raise

rethinkdb/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import six
22

3+
34
def decode_utf8(string, encoding='utf-8'):
45
if hasattr(string, 'decode'):
56
return string.decode(encoding)
67

78
return string
89

10+
911
def chain_to_bytes(*strings):
1012
return b''.join([six.b(string) if isinstance(string, six.string_types) else string for string in strings])
13+
14+
15+
def get_hostname_for_ssl_match(hostname):
16+
parts = hostname.split('.')
17+
18+
if len(parts) < 3:
19+
return hostname
20+
21+
parts[0] = '*'
22+
return '.'.join(parts)

rethinkdb/net.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
ReqlTimeoutError,
4545
ReqlUserError)
4646
from rethinkdb.handshake import HandshakeV1_0
47+
from rethinkdb.helpers import get_hostname_for_ssl_match
4748
from rethinkdb.logger import default_logger
4849

4950
__all__ = ['Connection', 'Cursor', 'DEFAULT_PORT', 'DefaultConnection', 'make_connection']
@@ -352,7 +353,10 @@ def __init__(self, parent, timeout):
352353
"SSL handshake failed (see server log for more information): %s" %
353354
str(err))
354355
try:
355-
match_hostname(self._socket.getpeercert(), hostname=self.host)
356+
ssl.match_hostname(
357+
self._socket.getpeercert(),
358+
hostname=get_hostname_for_ssl_match(self.host)
359+
)
356360
except CertificateError:
357361
self._socket.close()
358362
raise

tests/test_helpers.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22
from mock import Mock
3-
from rethinkdb.helpers import decode_utf8, chain_to_bytes
3+
from rethinkdb.helpers import decode_utf8, chain_to_bytes, get_hostname_for_ssl_match
44

55
@pytest.mark.unit
66
class TestDecodeUTF8Helper(object):
@@ -42,3 +42,34 @@ def test_mixed_chaining(self):
4242
result = chain_to_bytes('iron', ' ', b'man')
4343

4444
assert result == expected_string
45+
46+
47+
@pytest.mark.unit
48+
class TestSSLMatchHostHostnameHelper(object):
49+
def test_subdomain_replaced_to_star(self):
50+
expected_string = '*.example.com'
51+
52+
result = get_hostname_for_ssl_match('test.example.com')
53+
54+
assert result == expected_string
55+
56+
def test_subdomain_replaced_to_star_special_tld(self):
57+
expected_string = '*.example.co.uk'
58+
59+
result = get_hostname_for_ssl_match('test.example.co.uk')
60+
61+
assert result == expected_string
62+
63+
def test_no_subdomain_to_replace(self):
64+
expected_string = 'example.com'
65+
66+
result = get_hostname_for_ssl_match(expected_string)
67+
68+
assert result == expected_string
69+
70+
def test_no_tld(self):
71+
expected_string = 'localhost'
72+
73+
result = get_hostname_for_ssl_match(expected_string)
74+
75+
assert result == expected_string

0 commit comments

Comments
 (0)