Skip to content

Commit 4d51074

Browse files
committed
Add helper to get the hostname for ssl matching
1 parent 8f36106 commit 4d51074

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

rethinkdb/helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import re
12
import six
23

4+
35
def decode_utf8(string, encoding='utf-8'):
46
if hasattr(string, 'decode'):
57
return string.decode(encoding)
68

79
return string
810

11+
912
def chain_to_bytes(*strings):
1013
return b''.join([six.b(string) if isinstance(string, six.string_types) else string for string in strings])
14+
15+
16+
def get_hostname_for_ssl_match(hostname):
17+
match = re.match(r'^((?P<subdomain>[^\.]+)\.)?(?P<domain>[^\./]+\.[^/]+)/?.*$', hostname)
18+
domain = match.group('domain')
19+
return '*.{domain}'.format(domain=domain) if match.group('subdomain') else domain

tests/test_helpers.py

Lines changed: 22 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,24 @@ 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_no_subdomain_to_replace(self):
57+
expected_string = 'example.com'
58+
59+
result = get_hostname_for_ssl_match('example.com')
60+
61+
assert result == expected_string
62+
63+
def test_no_match(self):
64+
with pytest.raises(AttributeError) as exc:
65+
get_hostname_for_ssl_match('')

0 commit comments

Comments
 (0)