| 1 |
# Copyright 2017 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
"""Tests server certificate rotation. |
| 15 |
|
| 16 |
Here we test various aspects of gRPC Python, and in some cases gRPC |
| 17 |
Core by extension, support for server certificate rotation. |
| 18 |
|
| 19 |
* ServerSSLCertReloadTestWithClientAuth: test ability to rotate |
| 20 |
server's SSL cert for use in future channels with clients while not |
| 21 |
affecting any existing channel. The server requires client |
| 22 |
authentication. |
| 23 |
|
| 24 |
* ServerSSLCertReloadTestWithoutClientAuth: like |
| 25 |
ServerSSLCertReloadTestWithClientAuth except that the server does |
| 26 |
not authenticate the client. |
| 27 |
|
| 28 |
* ServerSSLCertReloadTestCertConfigReuse: tests gRPC Python's ability |
| 29 |
to deal with user's reuse of ServerCertificateConfiguration instances. |
| 30 |
""" |
| 31 |
|
| 32 |
import abc |
| 33 |
import collections |
| 34 |
from concurrent import futures |
| 35 |
import logging |
| 36 |
import os |
| 37 |
import threading |
| 38 |
import unittest |
| 39 |
|
| 40 |
import grpc |
| 41 |
|
| 42 |
from tests.testing import _application_common |
| 43 |
from tests.testing import _server_application |
| 44 |
from tests.testing.proto import services_pb2_grpc |
| 45 |
from tests.unit import resources |
| 46 |
from tests.unit import test_common |
| 47 |
|
| 48 |
CA_1_PEM = resources.cert_hier_1_root_ca_cert() |
| 49 |
CA_2_PEM = resources.cert_hier_2_root_ca_cert() |
| 50 |
|
| 51 |
CLIENT_KEY_1_PEM = resources.cert_hier_1_client_1_key() |
| 52 |
CLIENT_CERT_CHAIN_1_PEM = (resources.cert_hier_1_client_1_cert() + |
| 53 |
resources.cert_hier_1_intermediate_ca_cert()) |
| 54 |
|
| 55 |
CLIENT_KEY_2_PEM = resources.cert_hier_2_client_1_key() |
| 56 |
CLIENT_CERT_CHAIN_2_PEM = (resources.cert_hier_2_client_1_cert() + |
| 57 |
resources.cert_hier_2_intermediate_ca_cert()) |
| 58 |
|
| 59 |
SERVER_KEY_1_PEM = resources.cert_hier_1_server_1_key() |
| 60 |
SERVER_CERT_CHAIN_1_PEM = (resources.cert_hier_1_server_1_cert() + |
| 61 |
resources.cert_hier_1_intermediate_ca_cert()) |
| 62 |
|
| 63 |
SERVER_KEY_2_PEM = resources.cert_hier_2_server_1_key() |
| 64 |
SERVER_CERT_CHAIN_2_PEM = (resources.cert_hier_2_server_1_cert() + |
| 65 |
resources.cert_hier_2_intermediate_ca_cert()) |
| 66 |
|
| 67 |
# for use with the CertConfigFetcher. Roughly a simple custom mock |
| 68 |
# implementation |
| 69 |
Call = collections.namedtuple('Call', ['did_raise', 'returned_cert_config']) |
| 70 |
|
| 71 |
|
| 72 |
def _create_channel(port, credentials): |
| 73 |
return grpc.secure_channel('localhost:{}'.format(port), credentials) |
| 74 |
|
| 75 |
|
| 76 |
def _create_client_stub(channel, expect_success): |
| 77 |
if expect_success: |
| 78 |
# per Nathaniel: there's some robustness issue if we start |
| 79 |
# using a channel without waiting for it to be actually ready |
| 80 |
grpc.channel_ready_future(channel).result(timeout=10) |
| 81 |
return services_pb2_grpc.FirstServiceStub(channel) |
| 82 |
|
| 83 |
|
| 84 |
class CertConfigFetcher(object): |
| 85 |
|
| 86 |
def __init__(self): |
| 87 |
self._lock = threading.Lock() |
| 88 |
self._calls = [] |
| 89 |
self._should_raise = False |
| 90 |
self._cert_config = None |
| 91 |
|
| 92 |
def reset(self): |
| 93 |
with self._lock: |
| 94 |
self._calls = [] |
| 95 |
self._should_raise = False |
| 96 |
self._cert_config = None |
| 97 |
|
| 98 |
def configure(self, should_raise, cert_config): |
| 99 |
assert not (should_raise and cert_config), ( |
| 100 |
"should not specify both should_raise and a cert_config at the same time" |
| 101 |
) |
| 102 |
with self._lock: |
| 103 |
self._should_raise = should_raise |
| 104 |
self._cert_config = cert_config |
| 105 |
|
| 106 |
def getCalls(self): |
| 107 |
with self._lock: |
| 108 |
return self._calls |
| 109 |
|
| 110 |
def __call__(self): |
| 111 |
with self._lock: |
| 112 |
if self._should_raise: |
| 113 |
self._calls.append(Call(True, None)) |
| 114 |
raise ValueError('just for fun, should not affect the test') |
| 115 |
else: |
| 116 |
self._calls.append(Call(False, self._cert_config)) |
| 117 |
return self._cert_config |
| 118 |
|
| 119 |
|
| 120 |
class _ServerSSLCertReloadTest(unittest.TestCase, metaclass=abc.ABCMeta): |
| 121 |
|
| 122 |
def __init__(self, *args, **kwargs): |
| 123 |
super(_ServerSSLCertReloadTest, self).__init__(*args, **kwargs) |
| 124 |
self.server = None |
| 125 |
self.port = None |
| 126 |
|
| 127 |
@abc.abstractmethod |
| 128 |
def require_client_auth(self): |
| 129 |
raise NotImplementedError() |
| 130 |
|
| 131 |
def setUp(self): |
| 132 |
self.server = test_common.test_server() |
| 133 |
services_pb2_grpc.add_FirstServiceServicer_to_server( |
| 134 |
_server_application.FirstServiceServicer(), self.server) |
| 135 |
switch_cert_on_client_num = 10 |
| 136 |
initial_cert_config = grpc.ssl_server_certificate_configuration( |
| 137 |
[(SERVER_KEY_1_PEM, SERVER_CERT_CHAIN_1_PEM)], |
| 138 |
root_certificates=CA_2_PEM) |
| 139 |
self.cert_config_fetcher = CertConfigFetcher() |
| 140 |
server_credentials = grpc.dynamic_ssl_server_credentials( |
| 141 |
initial_cert_config, |
| 142 |
self.cert_config_fetcher, |
| 143 |
require_client_authentication=self.require_client_auth()) |
| 144 |
self.port = self.server.add_secure_port('[::]:0', server_credentials) |
| 145 |
self.server.start() |
| 146 |
|
| 147 |
def tearDown(self): |
| 148 |
if self.server: |
| 149 |
self.server.stop(None) |
| 150 |
|
| 151 |
def _perform_rpc(self, client_stub, expect_success): |
| 152 |
# we don't care about the actual response of the rpc; only |
| 153 |
# whether we can perform it or not, and if not, the status |
| 154 |
# code must be UNAVAILABLE |
| 155 |
request = _application_common.UNARY_UNARY_REQUEST |
| 156 |
if expect_success: |
| 157 |
response = client_stub.UnUn(request) |
| 158 |
self.assertEqual(response, _application_common.UNARY_UNARY_RESPONSE) |
| 159 |
else: |
| 160 |
with self.assertRaises(grpc.RpcError) as exception_context: |
| 161 |
client_stub.UnUn(request) |
| 162 |
# If TLS 1.2 is used, then the client receives an alert message |
| 163 |
# before the handshake is complete, so the status is UNAVAILABLE. If |
| 164 |
# TLS 1.3 is used, then the client receives the alert message after |
| 165 |
# the handshake is complete, so the TSI handshaker returns the |
| 166 |
# TSI_PROTOCOL_FAILURE result. This result does not have a |
| 167 |
# corresponding status code, so this yields an UNKNOWN status. |
| 168 |
self.assertTrue(exception_context.exception.code( |
| 169 |
) in [grpc.StatusCode.UNAVAILABLE, grpc.StatusCode.UNKNOWN]) |
| 170 |
|
| 171 |
def _do_one_shot_client_rpc(self, |
| 172 |
expect_success, |
| 173 |
root_certificates=None, |
| 174 |
private_key=None, |
| 175 |
certificate_chain=None): |
| 176 |
credentials = grpc.ssl_channel_credentials( |
| 177 |
root_certificates=root_certificates, |
| 178 |
private_key=private_key, |
| 179 |
certificate_chain=certificate_chain) |
| 180 |
with _create_channel(self.port, credentials) as client_channel: |
| 181 |
client_stub = _create_client_stub(client_channel, expect_success) |
| 182 |
self._perform_rpc(client_stub, expect_success) |
| 183 |
|
| 184 |
def _test(self): |
| 185 |
# things should work... |
| 186 |
self.cert_config_fetcher.configure(False, None) |
| 187 |
self._do_one_shot_client_rpc(True, |
| 188 |
root_certificates=CA_1_PEM, |
| 189 |
private_key=CLIENT_KEY_2_PEM, |
| 190 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 191 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 192 |
self.assertEqual(len(actual_calls), 1) |
| 193 |
self.assertFalse(actual_calls[0].did_raise) |
| 194 |
self.assertIsNone(actual_calls[0].returned_cert_config) |
| 195 |
|
| 196 |
# client should reject server... |
| 197 |
# fails because client trusts ca2 and so will reject server |
| 198 |
self.cert_config_fetcher.reset() |
| 199 |
self.cert_config_fetcher.configure(False, None) |
| 200 |
self._do_one_shot_client_rpc(False, |
| 201 |
root_certificates=CA_2_PEM, |
| 202 |
private_key=CLIENT_KEY_2_PEM, |
| 203 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 204 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 205 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 206 |
self.assertFalse(actual_calls[0].did_raise) |
| 207 |
for i, call in enumerate(actual_calls): |
| 208 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 209 |
self.assertIsNone(call.returned_cert_config, 'i= {}'.format(i)) |
| 210 |
|
| 211 |
# should work again... |
| 212 |
self.cert_config_fetcher.reset() |
| 213 |
self.cert_config_fetcher.configure(True, None) |
| 214 |
self._do_one_shot_client_rpc(True, |
| 215 |
root_certificates=CA_1_PEM, |
| 216 |
private_key=CLIENT_KEY_2_PEM, |
| 217 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 218 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 219 |
self.assertEqual(len(actual_calls), 1) |
| 220 |
self.assertTrue(actual_calls[0].did_raise) |
| 221 |
self.assertIsNone(actual_calls[0].returned_cert_config) |
| 222 |
|
| 223 |
# if with_client_auth, then client should be rejected by |
| 224 |
# server because client uses key/cert1, but server trusts ca2, |
| 225 |
# so server will reject |
| 226 |
self.cert_config_fetcher.reset() |
| 227 |
self.cert_config_fetcher.configure(False, None) |
| 228 |
self._do_one_shot_client_rpc(not self.require_client_auth(), |
| 229 |
root_certificates=CA_1_PEM, |
| 230 |
private_key=CLIENT_KEY_1_PEM, |
| 231 |
certificate_chain=CLIENT_CERT_CHAIN_1_PEM) |
| 232 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 233 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 234 |
for i, call in enumerate(actual_calls): |
| 235 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 236 |
self.assertIsNone(call.returned_cert_config, 'i= {}'.format(i)) |
| 237 |
|
| 238 |
# should work again... |
| 239 |
self.cert_config_fetcher.reset() |
| 240 |
self.cert_config_fetcher.configure(False, None) |
| 241 |
self._do_one_shot_client_rpc(True, |
| 242 |
root_certificates=CA_1_PEM, |
| 243 |
private_key=CLIENT_KEY_2_PEM, |
| 244 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 245 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 246 |
self.assertEqual(len(actual_calls), 1) |
| 247 |
self.assertFalse(actual_calls[0].did_raise) |
| 248 |
self.assertIsNone(actual_calls[0].returned_cert_config) |
| 249 |
|
| 250 |
# now create the "persistent" clients |
| 251 |
self.cert_config_fetcher.reset() |
| 252 |
self.cert_config_fetcher.configure(False, None) |
| 253 |
channel_A = _create_channel( |
| 254 |
self.port, |
| 255 |
grpc.ssl_channel_credentials( |
| 256 |
root_certificates=CA_1_PEM, |
| 257 |
private_key=CLIENT_KEY_2_PEM, |
| 258 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM)) |
| 259 |
persistent_client_stub_A = _create_client_stub(channel_A, True) |
| 260 |
self._perform_rpc(persistent_client_stub_A, True) |
| 261 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 262 |
self.assertEqual(len(actual_calls), 1) |
| 263 |
self.assertFalse(actual_calls[0].did_raise) |
| 264 |
self.assertIsNone(actual_calls[0].returned_cert_config) |
| 265 |
|
| 266 |
self.cert_config_fetcher.reset() |
| 267 |
self.cert_config_fetcher.configure(False, None) |
| 268 |
channel_B = _create_channel( |
| 269 |
self.port, |
| 270 |
grpc.ssl_channel_credentials( |
| 271 |
root_certificates=CA_1_PEM, |
| 272 |
private_key=CLIENT_KEY_2_PEM, |
| 273 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM)) |
| 274 |
persistent_client_stub_B = _create_client_stub(channel_B, True) |
| 275 |
self._perform_rpc(persistent_client_stub_B, True) |
| 276 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 277 |
self.assertEqual(len(actual_calls), 1) |
| 278 |
self.assertFalse(actual_calls[0].did_raise) |
| 279 |
self.assertIsNone(actual_calls[0].returned_cert_config) |
| 280 |
|
| 281 |
# moment of truth!! client should reject server because the |
| 282 |
# server switch cert... |
| 283 |
cert_config = grpc.ssl_server_certificate_configuration( |
| 284 |
[(SERVER_KEY_2_PEM, SERVER_CERT_CHAIN_2_PEM)], |
| 285 |
root_certificates=CA_1_PEM) |
| 286 |
self.cert_config_fetcher.reset() |
| 287 |
self.cert_config_fetcher.configure(False, cert_config) |
| 288 |
self._do_one_shot_client_rpc(False, |
| 289 |
root_certificates=CA_1_PEM, |
| 290 |
private_key=CLIENT_KEY_2_PEM, |
| 291 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 292 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 293 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 294 |
self.assertFalse(actual_calls[0].did_raise) |
| 295 |
for i, call in enumerate(actual_calls): |
| 296 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 297 |
self.assertEqual(call.returned_cert_config, cert_config, |
| 298 |
'i= {}'.format(i)) |
| 299 |
|
| 300 |
# now should work again... |
| 301 |
self.cert_config_fetcher.reset() |
| 302 |
self.cert_config_fetcher.configure(False, None) |
| 303 |
self._do_one_shot_client_rpc(True, |
| 304 |
root_certificates=CA_2_PEM, |
| 305 |
private_key=CLIENT_KEY_1_PEM, |
| 306 |
certificate_chain=CLIENT_CERT_CHAIN_1_PEM) |
| 307 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 308 |
self.assertEqual(len(actual_calls), 1) |
| 309 |
self.assertFalse(actual_calls[0].did_raise) |
| 310 |
self.assertIsNone(actual_calls[0].returned_cert_config) |
| 311 |
|
| 312 |
# client should be rejected by server if with_client_auth |
| 313 |
self.cert_config_fetcher.reset() |
| 314 |
self.cert_config_fetcher.configure(False, None) |
| 315 |
self._do_one_shot_client_rpc(not self.require_client_auth(), |
| 316 |
root_certificates=CA_2_PEM, |
| 317 |
private_key=CLIENT_KEY_2_PEM, |
| 318 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 319 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 320 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 321 |
for i, call in enumerate(actual_calls): |
| 322 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 323 |
self.assertIsNone(call.returned_cert_config, 'i= {}'.format(i)) |
| 324 |
|
| 325 |
# here client should reject server... |
| 326 |
self.cert_config_fetcher.reset() |
| 327 |
self.cert_config_fetcher.configure(False, None) |
| 328 |
self._do_one_shot_client_rpc(False, |
| 329 |
root_certificates=CA_1_PEM, |
| 330 |
private_key=CLIENT_KEY_2_PEM, |
| 331 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 332 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 333 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 334 |
for i, call in enumerate(actual_calls): |
| 335 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 336 |
self.assertIsNone(call.returned_cert_config, 'i= {}'.format(i)) |
| 337 |
|
| 338 |
# persistent clients should continue to work |
| 339 |
self.cert_config_fetcher.reset() |
| 340 |
self.cert_config_fetcher.configure(False, None) |
| 341 |
self._perform_rpc(persistent_client_stub_A, True) |
| 342 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 343 |
self.assertEqual(len(actual_calls), 0) |
| 344 |
|
| 345 |
self.cert_config_fetcher.reset() |
| 346 |
self.cert_config_fetcher.configure(False, None) |
| 347 |
self._perform_rpc(persistent_client_stub_B, True) |
| 348 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 349 |
self.assertEqual(len(actual_calls), 0) |
| 350 |
|
| 351 |
channel_A.close() |
| 352 |
channel_B.close() |
| 353 |
|
| 354 |
|
| 355 |
class ServerSSLCertConfigFetcherParamsChecks(unittest.TestCase): |
| 356 |
|
| 357 |
def test_check_on_initial_config(self): |
| 358 |
with self.assertRaises(TypeError): |
| 359 |
grpc.dynamic_ssl_server_credentials(None, str) |
| 360 |
with self.assertRaises(TypeError): |
| 361 |
grpc.dynamic_ssl_server_credentials(1, str) |
| 362 |
|
| 363 |
def test_check_on_config_fetcher(self): |
| 364 |
cert_config = grpc.ssl_server_certificate_configuration( |
| 365 |
[(SERVER_KEY_2_PEM, SERVER_CERT_CHAIN_2_PEM)], |
| 366 |
root_certificates=CA_1_PEM) |
| 367 |
with self.assertRaises(TypeError): |
| 368 |
grpc.dynamic_ssl_server_credentials(cert_config, None) |
| 369 |
with self.assertRaises(TypeError): |
| 370 |
grpc.dynamic_ssl_server_credentials(cert_config, 1) |
| 371 |
|
| 372 |
|
| 373 |
class ServerSSLCertReloadTestWithClientAuth(_ServerSSLCertReloadTest): |
| 374 |
|
| 375 |
def require_client_auth(self): |
| 376 |
return True |
| 377 |
|
| 378 |
test = _ServerSSLCertReloadTest._test |
| 379 |
|
| 380 |
|
| 381 |
class ServerSSLCertReloadTestWithoutClientAuth(_ServerSSLCertReloadTest): |
| 382 |
|
| 383 |
def require_client_auth(self): |
| 384 |
return False |
| 385 |
|
| 386 |
test = _ServerSSLCertReloadTest._test |
| 387 |
|
| 388 |
|
| 389 |
class ServerSSLCertReloadTestCertConfigReuse(_ServerSSLCertReloadTest): |
| 390 |
"""Ensures that `ServerCertificateConfiguration` instances can be reused. |
| 391 |
|
| 392 |
Because gRPC Core takes ownership of the |
| 393 |
`grpc_ssl_server_certificate_config` encapsulated by |
| 394 |
`ServerCertificateConfiguration`, this test reuses the same |
| 395 |
`ServerCertificateConfiguration` instances multiple times to make sure |
| 396 |
gRPC Python takes care of maintaining the validity of |
| 397 |
`ServerCertificateConfiguration` instances, so that such instances can be |
| 398 |
re-used by user application. |
| 399 |
""" |
| 400 |
|
| 401 |
def require_client_auth(self): |
| 402 |
return True |
| 403 |
|
| 404 |
def setUp(self): |
| 405 |
self.server = test_common.test_server() |
| 406 |
services_pb2_grpc.add_FirstServiceServicer_to_server( |
| 407 |
_server_application.FirstServiceServicer(), self.server) |
| 408 |
self.cert_config_A = grpc.ssl_server_certificate_configuration( |
| 409 |
[(SERVER_KEY_1_PEM, SERVER_CERT_CHAIN_1_PEM)], |
| 410 |
root_certificates=CA_2_PEM) |
| 411 |
self.cert_config_B = grpc.ssl_server_certificate_configuration( |
| 412 |
[(SERVER_KEY_2_PEM, SERVER_CERT_CHAIN_2_PEM)], |
| 413 |
root_certificates=CA_1_PEM) |
| 414 |
self.cert_config_fetcher = CertConfigFetcher() |
| 415 |
server_credentials = grpc.dynamic_ssl_server_credentials( |
| 416 |
self.cert_config_A, |
| 417 |
self.cert_config_fetcher, |
| 418 |
require_client_authentication=True) |
| 419 |
self.port = self.server.add_secure_port('[::]:0', server_credentials) |
| 420 |
self.server.start() |
| 421 |
|
| 422 |
def test_cert_config_reuse(self): |
| 423 |
|
| 424 |
# succeed with A |
| 425 |
self.cert_config_fetcher.reset() |
| 426 |
self.cert_config_fetcher.configure(False, self.cert_config_A) |
| 427 |
self._do_one_shot_client_rpc(True, |
| 428 |
root_certificates=CA_1_PEM, |
| 429 |
private_key=CLIENT_KEY_2_PEM, |
| 430 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 431 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 432 |
self.assertEqual(len(actual_calls), 1) |
| 433 |
self.assertFalse(actual_calls[0].did_raise) |
| 434 |
self.assertEqual(actual_calls[0].returned_cert_config, |
| 435 |
self.cert_config_A) |
| 436 |
|
| 437 |
# fail with A |
| 438 |
self.cert_config_fetcher.reset() |
| 439 |
self.cert_config_fetcher.configure(False, self.cert_config_A) |
| 440 |
self._do_one_shot_client_rpc(False, |
| 441 |
root_certificates=CA_2_PEM, |
| 442 |
private_key=CLIENT_KEY_1_PEM, |
| 443 |
certificate_chain=CLIENT_CERT_CHAIN_1_PEM) |
| 444 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 445 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 446 |
self.assertFalse(actual_calls[0].did_raise) |
| 447 |
for i, call in enumerate(actual_calls): |
| 448 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 449 |
self.assertEqual(call.returned_cert_config, self.cert_config_A, |
| 450 |
'i= {}'.format(i)) |
| 451 |
|
| 452 |
# succeed again with A |
| 453 |
self.cert_config_fetcher.reset() |
| 454 |
self.cert_config_fetcher.configure(False, self.cert_config_A) |
| 455 |
self._do_one_shot_client_rpc(True, |
| 456 |
root_certificates=CA_1_PEM, |
| 457 |
private_key=CLIENT_KEY_2_PEM, |
| 458 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 459 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 460 |
self.assertEqual(len(actual_calls), 1) |
| 461 |
self.assertFalse(actual_calls[0].did_raise) |
| 462 |
self.assertEqual(actual_calls[0].returned_cert_config, |
| 463 |
self.cert_config_A) |
| 464 |
|
| 465 |
# succeed with B |
| 466 |
self.cert_config_fetcher.reset() |
| 467 |
self.cert_config_fetcher.configure(False, self.cert_config_B) |
| 468 |
self._do_one_shot_client_rpc(True, |
| 469 |
root_certificates=CA_2_PEM, |
| 470 |
private_key=CLIENT_KEY_1_PEM, |
| 471 |
certificate_chain=CLIENT_CERT_CHAIN_1_PEM) |
| 472 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 473 |
self.assertEqual(len(actual_calls), 1) |
| 474 |
self.assertFalse(actual_calls[0].did_raise) |
| 475 |
self.assertEqual(actual_calls[0].returned_cert_config, |
| 476 |
self.cert_config_B) |
| 477 |
|
| 478 |
# fail with B |
| 479 |
self.cert_config_fetcher.reset() |
| 480 |
self.cert_config_fetcher.configure(False, self.cert_config_B) |
| 481 |
self._do_one_shot_client_rpc(False, |
| 482 |
root_certificates=CA_1_PEM, |
| 483 |
private_key=CLIENT_KEY_2_PEM, |
| 484 |
certificate_chain=CLIENT_CERT_CHAIN_2_PEM) |
| 485 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 486 |
self.assertGreaterEqual(len(actual_calls), 1) |
| 487 |
self.assertFalse(actual_calls[0].did_raise) |
| 488 |
for i, call in enumerate(actual_calls): |
| 489 |
self.assertFalse(call.did_raise, 'i= {}'.format(i)) |
| 490 |
self.assertEqual(call.returned_cert_config, self.cert_config_B, |
| 491 |
'i= {}'.format(i)) |
| 492 |
|
| 493 |
# succeed again with B |
| 494 |
self.cert_config_fetcher.reset() |
| 495 |
self.cert_config_fetcher.configure(False, self.cert_config_B) |
| 496 |
self._do_one_shot_client_rpc(True, |
| 497 |
root_certificates=CA_2_PEM, |
| 498 |
private_key=CLIENT_KEY_1_PEM, |
| 499 |
certificate_chain=CLIENT_CERT_CHAIN_1_PEM) |
| 500 |
actual_calls = self.cert_config_fetcher.getCalls() |
| 501 |
self.assertEqual(len(actual_calls), 1) |
| 502 |
self.assertFalse(actual_calls[0].did_raise) |
| 503 |
self.assertEqual(actual_calls[0].returned_cert_config, |
| 504 |
self.cert_config_B) |
| 505 |
|
| 506 |
|
| 507 |
if __name__ == '__main__': |
| 508 |
logging.basicConfig() |
| 509 |
unittest.main(verbosity=2) |