blob: 3c4be9c2aba90c1777a400479a170d29acac0444 [file] [log] [blame]
Nobuaki Sukegawaad835862015-12-23 23:32:09 +09001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090020import inspect
Nobuaki Sukegawa355116e2016-02-11 18:01:20 +090021import logging
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090022import os
23import platform
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090024import ssl
25import sys
John Siroisac067042016-02-12 08:10:13 -070026import tempfile
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090027import threading
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090028import unittest
29import warnings
John Siroisac067042016-02-12 08:10:13 -070030from contextlib import contextmanager
31
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +090032import _import_local_thrift # noqa
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090033
34SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__))
35ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(SCRIPT_DIR)))
36SERVER_PEM = os.path.join(ROOT_DIR, 'test', 'keys', 'server.pem')
37SERVER_CERT = os.path.join(ROOT_DIR, 'test', 'keys', 'server.crt')
38SERVER_KEY = os.path.join(ROOT_DIR, 'test', 'keys', 'server.key')
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +090039CLIENT_CERT_NO_IP = os.path.join(ROOT_DIR, 'test', 'keys', 'client.crt')
40CLIENT_KEY_NO_IP = os.path.join(ROOT_DIR, 'test', 'keys', 'client.key')
41CLIENT_CERT = os.path.join(ROOT_DIR, 'test', 'keys', 'client_v3.crt')
42CLIENT_KEY = os.path.join(ROOT_DIR, 'test', 'keys', 'client_v3.key')
43CLIENT_CA = os.path.join(ROOT_DIR, 'test', 'keys', 'CA.pem')
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090044
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090045TEST_CIPHERS = 'DES-CBC3-SHA'
46
47
48class ServerAcceptor(threading.Thread):
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090049 def __init__(self, server, expect_failure=False):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 super(ServerAcceptor, self).__init__()
John Siroisac067042016-02-12 08:10:13 -070051 self.daemon = True
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090052 self._server = server
John Siroisac067042016-02-12 08:10:13 -070053 self._listening = threading.Event()
54 self._port = None
55 self._port_bound = threading.Event()
56 self._client = None
57 self._client_accepted = threading.Event()
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090058 self._expect_failure = expect_failure
59 frame = inspect.stack(3)[2]
60 self.name = frame[3]
61 del frame
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090062
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 def run(self):
64 self._server.listen()
John Siroisac067042016-02-12 08:10:13 -070065 self._listening.set()
66
67 try:
68 address = self._server.handle.getsockname()
69 if len(address) > 1:
70 # AF_INET addresses are 2-tuples (host, port) and AF_INET6 are
71 # 4-tuples (host, port, ...), but in each case port is in the second slot.
72 self._port = address[1]
73 finally:
74 self._port_bound.set()
75
76 try:
77 self._client = self._server.accept()
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +090078 except Exception:
79 logging.exception('error on server side (%s):' % self.name)
80 if not self._expect_failure:
81 raise
John Siroisac067042016-02-12 08:10:13 -070082 finally:
83 self._client_accepted.set()
84
85 def await_listening(self):
86 self._listening.wait()
87
88 @property
89 def port(self):
90 self._port_bound.wait()
91 return self._port
92
93 @property
94 def client(self):
95 self._client_accepted.wait()
96 return self._client
Nobuaki Sukegawaad835862015-12-23 23:32:09 +090097
98
99# Python 2.6 compat
100class AssertRaises(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900101 def __init__(self, expected):
102 self._expected = expected
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900103
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900104 def __enter__(self):
105 pass
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900106
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900107 def __exit__(self, exc_type, exc_value, traceback):
108 if not exc_type or not issubclass(exc_type, self._expected):
109 raise Exception('fail')
110 return True
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900111
112
113class TSSLSocketTest(unittest.TestCase):
John Siroisac067042016-02-12 08:10:13 -0700114 def _server_socket(self, **kwargs):
115 return TSSLServerSocket(port=0, **kwargs)
116
117 @contextmanager
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +0900118 def _connectable_client(self, server, expect_failure=False, path=None, **client_kwargs):
119 acc = ServerAcceptor(server, expect_failure)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900120 try:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900121 acc.start()
John Siroisac067042016-02-12 08:10:13 -0700122 acc.await_listening()
123
124 host, port = ('localhost', acc.port) if path is None else (None, None)
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +0900125 client = TSSLSocket(host, port, unix_socket=path, **client_kwargs)
John Siroisac067042016-02-12 08:10:13 -0700126 yield acc, client
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900127 finally:
Nobuaki Sukegawa25536ad2016-02-04 15:08:55 +0900128 if acc.client:
129 acc.client.close()
130 server.close()
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900131
John Siroisac067042016-02-12 08:10:13 -0700132 def _assert_connection_failure(self, server, path=None, **client_args):
Nobuaki Sukegawa66f50532016-02-14 21:47:38 +0900133 logging.disable(logging.CRITICAL)
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900134 try:
135 with self._connectable_client(server, True, path=path, **client_args) as (acc, client):
John Siroisac067042016-02-12 08:10:13 -0700136 # We need to wait for a connection failure, but not too long. 20ms is a tunable
137 # compromise between test speed and stability
138 client.setTimeout(20)
139 with self._assert_raises(TTransportException):
140 client.open()
141 self.assertTrue(acc.client is None)
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900142 finally:
143 logging.disable(logging.NOTSET)
John Siroisac067042016-02-12 08:10:13 -0700144
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900145 def _assert_raises(self, exc):
146 if sys.hexversion >= 0x020700F0:
147 return self.assertRaises(exc)
148 else:
149 return AssertRaises(exc)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900150
John Siroisac067042016-02-12 08:10:13 -0700151 def _assert_connection_success(self, server, path=None, **client_args):
152 with self._connectable_client(server, path=path, **client_args) as (acc, client):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900153 client.open()
John Siroisac067042016-02-12 08:10:13 -0700154 try:
155 self.assertTrue(acc.client is not None)
156 finally:
157 client.close()
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900158
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900159 # deprecated feature
160 def test_deprecation(self):
161 with warnings.catch_warnings(record=True) as w:
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900162 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
John Siroisac067042016-02-12 08:10:13 -0700163 TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900164 self.assertEqual(len(w), 1)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900165
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900166 with warnings.catch_warnings(record=True) as w:
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900167 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900168 # Deprecated signature
169 # def __init__(self, host='localhost', port=9090, validate=True, ca_certs=None, keyfile=None, certfile=None, unix_socket=None, ciphers=None):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900170 TSSLSocket('localhost', 0, True, SERVER_CERT, CLIENT_KEY, CLIENT_CERT, None, TEST_CIPHERS)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900171 self.assertEqual(len(w), 7)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900172
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900173 with warnings.catch_warnings(record=True) as w:
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900174 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900175 # Deprecated signature
176 # def __init__(self, host=None, port=9090, certfile='cert.pem', unix_socket=None, ciphers=None):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900177 TSSLServerSocket(None, 0, SERVER_PEM, None, TEST_CIPHERS)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900178 self.assertEqual(len(w), 3)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900179
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900180 # deprecated feature
181 def test_set_cert_reqs_by_validate(self):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900182 with warnings.catch_warnings(record=True) as w:
183 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
184 c1 = TSSLSocket('localhost', 0, validate=True, ca_certs=SERVER_CERT)
185 self.assertEqual(c1.cert_reqs, ssl.CERT_REQUIRED)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900186
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900187 c1 = TSSLSocket('localhost', 0, validate=False)
188 self.assertEqual(c1.cert_reqs, ssl.CERT_NONE)
189
190 self.assertEqual(len(w), 2)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900191
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900192 # deprecated feature
193 def test_set_validate_by_cert_reqs(self):
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900194 with warnings.catch_warnings(record=True) as w:
195 warnings.filterwarnings('always', category=DeprecationWarning, module=self.__module__)
196 c1 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_NONE)
197 self.assertFalse(c1.validate)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900198
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900199 c2 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
200 self.assertTrue(c2.validate)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900201
Nobuaki Sukegawa6a0ca7f2016-02-13 03:11:16 +0900202 c3 = TSSLSocket('localhost', 0, cert_reqs=ssl.CERT_OPTIONAL, ca_certs=SERVER_CERT)
203 self.assertTrue(c3.validate)
204
205 self.assertEqual(len(w), 3)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900206
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900207 def test_unix_domain_socket(self):
208 if platform.system() == 'Windows':
209 print('skipping test_unix_domain_socket')
210 return
John Siroisac067042016-02-12 08:10:13 -0700211 fd, path = tempfile.mkstemp()
212 os.close(fd)
213 try:
214 server = self._server_socket(unix_socket=path, keyfile=SERVER_KEY, certfile=SERVER_CERT)
215 self._assert_connection_success(server, path=path, cert_reqs=ssl.CERT_NONE)
216 finally:
217 os.unlink(path)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900218
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900219 def test_server_cert(self):
John Siroisac067042016-02-12 08:10:13 -0700220 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
221 self._assert_connection_success(server, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900222
John Siroisac067042016-02-12 08:10:13 -0700223 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
224 # server cert not in ca_certs
225 self._assert_connection_failure(server, cert_reqs=ssl.CERT_REQUIRED, ca_certs=CLIENT_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900226
John Siroisac067042016-02-12 08:10:13 -0700227 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
228 self._assert_connection_success(server, cert_reqs=ssl.CERT_NONE)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900229
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900230 def test_set_server_cert(self):
John Siroisac067042016-02-12 08:10:13 -0700231 server = self._server_socket(keyfile=SERVER_KEY, certfile=CLIENT_CERT)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900232 with self._assert_raises(Exception):
233 server.certfile = 'foo'
234 with self._assert_raises(Exception):
235 server.certfile = None
236 server.certfile = SERVER_CERT
John Siroisac067042016-02-12 08:10:13 -0700237 self._assert_connection_success(server, cert_reqs=ssl.CERT_REQUIRED, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900238
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900239 def test_client_cert(self):
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +0900240 if not _match_has_ipaddress:
241 print('skipping test_client_cert')
242 return
John Siroisac067042016-02-12 08:10:13 -0700243 server = self._server_socket(
244 cert_reqs=ssl.CERT_REQUIRED, keyfile=SERVER_KEY,
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900245 certfile=SERVER_CERT, ca_certs=CLIENT_CERT)
John Siroisac067042016-02-12 08:10:13 -0700246 self._assert_connection_failure(server, cert_reqs=ssl.CERT_NONE, certfile=SERVER_CERT, keyfile=SERVER_KEY)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900247
John Siroisac067042016-02-12 08:10:13 -0700248 server = self._server_socket(
249 cert_reqs=ssl.CERT_REQUIRED, keyfile=SERVER_KEY,
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900250 certfile=SERVER_CERT, ca_certs=CLIENT_CA)
John Siroisac067042016-02-12 08:10:13 -0700251 self._assert_connection_failure(server, cert_reqs=ssl.CERT_NONE, certfile=CLIENT_CERT_NO_IP, keyfile=CLIENT_KEY_NO_IP)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900252
John Siroisac067042016-02-12 08:10:13 -0700253 server = self._server_socket(
254 cert_reqs=ssl.CERT_REQUIRED, keyfile=SERVER_KEY,
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900255 certfile=SERVER_CERT, ca_certs=CLIENT_CA)
John Siroisac067042016-02-12 08:10:13 -0700256 self._assert_connection_success(server, cert_reqs=ssl.CERT_NONE, certfile=CLIENT_CERT, keyfile=CLIENT_KEY)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900257
John Siroisac067042016-02-12 08:10:13 -0700258 server = self._server_socket(
259 cert_reqs=ssl.CERT_OPTIONAL, keyfile=SERVER_KEY,
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900260 certfile=SERVER_CERT, ca_certs=CLIENT_CA)
John Siroisac067042016-02-12 08:10:13 -0700261 self._assert_connection_success(server, cert_reqs=ssl.CERT_NONE, certfile=CLIENT_CERT, keyfile=CLIENT_KEY)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900262
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900263 def test_ciphers(self):
John Siroisac067042016-02-12 08:10:13 -0700264 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ciphers=TEST_CIPHERS)
265 self._assert_connection_success(server, ca_certs=SERVER_CERT, ciphers=TEST_CIPHERS)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900266
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900267 if not TSSLSocket._has_ciphers:
268 # unittest.skip is not available for Python 2.6
269 print('skipping test_ciphers')
270 return
John Siroisac067042016-02-12 08:10:13 -0700271 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
272 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ciphers='NULL')
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900273
John Siroisac067042016-02-12 08:10:13 -0700274 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ciphers=TEST_CIPHERS)
275 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ciphers='NULL')
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900276
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900277 def test_ssl2_and_ssl3_disabled(self):
278 if not hasattr(ssl, 'PROTOCOL_SSLv3'):
279 print('PROTOCOL_SSLv3 is not available')
280 else:
John Siroisac067042016-02-12 08:10:13 -0700281 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
282 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv3)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900283
John Siroisac067042016-02-12 08:10:13 -0700284 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv3)
285 self._assert_connection_failure(server, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900286
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900287 if not hasattr(ssl, 'PROTOCOL_SSLv2'):
288 print('PROTOCOL_SSLv2 is not available')
289 else:
John Siroisac067042016-02-12 08:10:13 -0700290 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT)
291 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv2)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900292
John Siroisac067042016-02-12 08:10:13 -0700293 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_SSLv2)
294 self._assert_connection_failure(server, ca_certs=SERVER_CERT)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900295
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900296 def test_newer_tls(self):
297 if not TSSLSocket._has_ssl_context:
298 # unittest.skip is not available for Python 2.6
299 print('skipping test_newer_tls')
300 return
301 if not hasattr(ssl, 'PROTOCOL_TLSv1_2'):
302 print('PROTOCOL_TLSv1_2 is not available')
303 else:
John Siroisac067042016-02-12 08:10:13 -0700304 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_2)
305 self._assert_connection_success(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_2)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900306
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900307 if not hasattr(ssl, 'PROTOCOL_TLSv1_1'):
308 print('PROTOCOL_TLSv1_1 is not available')
309 else:
John Siroisac067042016-02-12 08:10:13 -0700310 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_1)
311 self._assert_connection_success(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_1)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900312
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900313 if not hasattr(ssl, 'PROTOCOL_TLSv1_1') or not hasattr(ssl, 'PROTOCOL_TLSv1_2'):
314 print('PROTOCOL_TLSv1_1 and/or PROTOCOL_TLSv1_2 is not available')
315 else:
John Siroisac067042016-02-12 08:10:13 -0700316 server = self._server_socket(keyfile=SERVER_KEY, certfile=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_2)
317 self._assert_connection_failure(server, ca_certs=SERVER_CERT, ssl_version=ssl.PROTOCOL_TLSv1_1)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900318
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900319 def test_ssl_context(self):
320 if not TSSLSocket._has_ssl_context:
321 # unittest.skip is not available for Python 2.6
322 print('skipping test_ssl_context')
323 return
324 server_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
325 server_context.load_cert_chain(SERVER_CERT, SERVER_KEY)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900326 server_context.load_verify_locations(CLIENT_CA)
327 server_context.verify_mode = ssl.CERT_REQUIRED
John Siroisac067042016-02-12 08:10:13 -0700328 server = self._server_socket(ssl_context=server_context)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900329
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900330 client_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
331 client_context.load_cert_chain(CLIENT_CERT, CLIENT_KEY)
332 client_context.load_verify_locations(SERVER_CERT)
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900333 client_context.verify_mode = ssl.CERT_REQUIRED
Nobuaki Sukegawaf39f7db2016-02-04 15:09:41 +0900334
John Siroisac067042016-02-12 08:10:13 -0700335 self._assert_connection_success(server, ssl_context=client_context)
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900336
James E. King, III0ad20bd2017-09-30 15:44:16 -0700337
Nobuaki Sukegawaad835862015-12-23 23:32:09 +0900338if __name__ == '__main__':
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900339 logging.basicConfig(level=logging.WARN)
Nobuaki Sukegawae8ba7872017-02-12 21:14:48 +0900340 from thrift.transport.TSSLSocket import TSSLSocket, TSSLServerSocket, _match_has_ipaddress
Nobuaki Sukegawad2b4f242016-09-04 18:49:23 +0900341 from thrift.transport.TTransport import TTransportException
342
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900343 unittest.main()