1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
# Copyright 2011-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for SSL support."""
from __future__ import annotations
import os
import pathlib
import socket
import sys
sys.path[0:0] = [""]
from test.asynchronous import (
HAVE_IPADDRESS,
AsyncIntegrationTest,
AsyncPyMongoTestCase,
SkipTest,
async_client_context,
connected,
remove_all_users,
unittest,
)
from test.utils_shared import (
EventListener,
OvertCommandListener,
cat_files,
ignore_deprecations,
)
from urllib.parse import quote_plus
from pymongo import AsyncMongoClient, ssl_support
from pymongo.errors import ConfigurationError, ConnectionFailure, OperationFailure
from pymongo.hello import HelloCompat
from pymongo.ssl_support import HAVE_PYSSL, HAVE_SSL, _ssl, get_ssl_context
from pymongo.write_concern import WriteConcern
_HAVE_PYOPENSSL = False
try:
# All of these must be available to use PyOpenSSL
import OpenSSL
import requests
import service_identity
# Ensure service_identity>=18.1 is installed
from service_identity.pyopenssl import verify_ip_address
from pymongo.ocsp_support import _load_trusted_ca_certs
_HAVE_PYOPENSSL = True
except ImportError:
_load_trusted_ca_certs = None # type: ignore
if HAVE_SSL:
import ssl
_IS_SYNC = False
if _IS_SYNC:
CERT_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "certificates")
else:
CERT_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "certificates")
CLIENT_PEM = os.path.join(CERT_PATH, "client.pem")
CLIENT_ENCRYPTED_PEM = os.path.join(CERT_PATH, "password_protected.pem")
CA_PEM = os.path.join(CERT_PATH, "ca.pem")
CA_BUNDLE_PEM = os.path.join(CERT_PATH, "trusted-ca.pem")
CRL_PEM = os.path.join(CERT_PATH, "crl.pem")
MONGODB_X509_USERNAME = "C=US,ST=New York,L=New York City,O=MDB,OU=Drivers,CN=client"
# To fully test this start a mongod instance (built with SSL support) like so:
# mongod --dbpath /path/to/data/directory --sslOnNormalPorts \
# --sslPEMKeyFile /path/to/pymongo/test/certificates/server.pem \
# --sslCAFile /path/to/pymongo/test/certificates/ca.pem \
# --sslWeakCertificateValidation
# Also, make sure you have 'server' as an alias for localhost in /etc/hosts
#
# Note: For all replica set tests to pass, the replica set configuration must
# use 'localhost' for the hostname of all hosts.
class TestClientSSL(AsyncPyMongoTestCase):
@unittest.skipIf(HAVE_SSL, "The ssl module is available, can't test what happens without it.")
def test_no_ssl_module(self):
# Explicit
self.assertRaises(ConfigurationError, self.simple_client, ssl=True)
# Implied
self.assertRaises(ConfigurationError, self.simple_client, tlsCertificateKeyFile=CLIENT_PEM)
@unittest.skipUnless(HAVE_SSL, "The ssl module is not available.")
@ignore_deprecations
def test_config_ssl(self):
# Tests various ssl configurations
self.assertRaises(ValueError, self.simple_client, ssl="foo")
self.assertRaises(
ConfigurationError, self.simple_client, tls=False, tlsCertificateKeyFile=CLIENT_PEM
)
self.assertRaises(TypeError, self.simple_client, ssl=0)
self.assertRaises(TypeError, self.simple_client, ssl=5.5)
self.assertRaises(TypeError, self.simple_client, ssl=[])
self.assertRaises(IOError, self.simple_client, tlsCertificateKeyFile="NoSuchFile")
self.assertRaises(TypeError, self.simple_client, tlsCertificateKeyFile=True)
self.assertRaises(TypeError, self.simple_client, tlsCertificateKeyFile=[])
# Test invalid combinations
self.assertRaises(
ConfigurationError, self.simple_client, tls=False, tlsCertificateKeyFile=CLIENT_PEM
)
self.assertRaises(ConfigurationError, self.simple_client, tls=False, tlsCAFile=CA_PEM)
self.assertRaises(ConfigurationError, self.simple_client, tls=False, tlsCRLFile=CRL_PEM)
self.assertRaises(
ConfigurationError, self.simple_client, tls=False, tlsAllowInvalidCertificates=False
)
self.assertRaises(
ConfigurationError, self.simple_client, tls=False, tlsAllowInvalidHostnames=False
)
self.assertRaises(
ConfigurationError, self.simple_client, tls=False, tlsDisableOCSPEndpointCheck=False
)
@unittest.skipUnless(_HAVE_PYOPENSSL, "PyOpenSSL is not available.")
def test_use_pyopenssl_when_available(self):
self.assertTrue(HAVE_PYSSL)
@unittest.skipUnless(_HAVE_PYOPENSSL, "Cannot test without PyOpenSSL")
def test_load_trusted_ca_certs(self):
trusted_ca_certs = _load_trusted_ca_certs(CA_BUNDLE_PEM)
self.assertEqual(2, len(trusted_ca_certs))
class TestSSL(AsyncIntegrationTest):
saved_port: int
async def assertClientWorks(self, client):
coll = client.pymongo_test.ssl_test.with_options(
write_concern=WriteConcern(w=async_client_context.w)
)
await coll.drop()
await coll.insert_one({"ssl": True})
self.assertTrue((await coll.find_one())["ssl"])
await coll.drop()
@unittest.skipUnless(HAVE_SSL, "The ssl module is not available.")
async def asyncSetUp(self):
await super().asyncSetUp()
# MongoClient should connect to the primary by default.
self.saved_port = AsyncMongoClient.PORT
AsyncMongoClient.PORT = await async_client_context.port
async def asyncTearDown(self):
AsyncMongoClient.PORT = self.saved_port
@async_client_context.require_tls
async def test_simple_ssl(self):
if "PyPy" in sys.version:
self.skipTest("Test is flaky on PyPy")
# Expects the server to be running with ssl and with
# no --sslPEMKeyFile or with --sslWeakCertificateValidation
await self.assertClientWorks(self.client)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_no_api_version
@ignore_deprecations
async def test_tlsCertificateKeyFilePassword(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
if not hasattr(ssl, "SSLContext") and not HAVE_PYSSL:
self.assertRaises(
ConfigurationError,
self.simple_client,
"localhost",
ssl=True,
tlsCertificateKeyFile=CLIENT_ENCRYPTED_PEM,
tlsCertificateKeyFilePassword="qwerty",
tlsCAFile=CA_PEM,
serverSelectionTimeoutMS=1000,
)
else:
await connected(
self.simple_client(
"localhost",
ssl=True,
tlsCertificateKeyFile=CLIENT_ENCRYPTED_PEM,
tlsCertificateKeyFilePassword="qwerty",
tlsCAFile=CA_PEM,
serverSelectionTimeoutMS=5000,
**self.credentials, # type: ignore[arg-type]
)
)
uri_fmt = (
"mongodb://localhost/?ssl=true"
"&tlsCertificateKeyFile=%s&tlsCertificateKeyFilePassword=qwerty"
"&tlsCAFile=%s&serverSelectionTimeoutMS=5000"
)
await connected(
self.simple_client(uri_fmt % (CLIENT_ENCRYPTED_PEM, CA_PEM), **self.credentials) # type: ignore[arg-type]
)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_no_auth
@ignore_deprecations
async def test_cert_ssl_implicitly_set(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
#
# test that setting tlsCertificateKeyFile causes ssl to be set to True
client = self.simple_client(
await async_client_context.host,
await async_client_context.port,
tlsAllowInvalidCertificates=True,
tlsCertificateKeyFile=CLIENT_PEM,
)
response = await client.admin.command(HelloCompat.LEGACY_CMD)
if "setName" in response:
client = self.simple_client(
await async_client_context.pair,
replicaSet=response["setName"],
w=len(response["hosts"]),
tlsAllowInvalidCertificates=True,
tlsCertificateKeyFile=CLIENT_PEM,
)
await self.assertClientWorks(client)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_no_auth
@ignore_deprecations
async def test_cert_ssl_validation(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
#
client = self.simple_client(
"localhost",
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
)
response = await client.admin.command(HelloCompat.LEGACY_CMD)
if "setName" in response:
if response["primary"].split(":")[0] != "localhost":
raise SkipTest(
"No hosts in the replicaset for 'localhost'. "
"Cannot validate hostname in the certificate"
)
client = self.simple_client(
"localhost",
replicaSet=response["setName"],
w=len(response["hosts"]),
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
)
await self.assertClientWorks(client)
if HAVE_IPADDRESS:
client = self.simple_client(
"127.0.0.1",
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
)
await self.assertClientWorks(client)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_no_auth
@ignore_deprecations
async def test_cert_ssl_uri_support(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
#
uri_fmt = (
"mongodb://localhost/?ssl=true&tlsCertificateKeyFile=%s&tlsAllowInvalidCertificates"
"=%s&tlsCAFile=%s&tlsAllowInvalidHostnames=false"
)
client = self.simple_client(uri_fmt % (CLIENT_PEM, "true", CA_PEM))
await self.assertClientWorks(client)
@unittest.skipIf(
"PyPy" in sys.version and not _IS_SYNC,
"https://github.com/pypy/pypy/issues/5131 flaky on async PyPy due to SSL EOF",
)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_server_resolvable
@async_client_context.require_no_api_version
@ignore_deprecations
async def test_cert_ssl_validation_hostname_matching(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
ctx = get_ssl_context(None, None, None, None, True, True, False, _IS_SYNC)
self.assertFalse(ctx.check_hostname)
ctx = get_ssl_context(None, None, None, None, True, False, False, _IS_SYNC)
self.assertFalse(ctx.check_hostname)
ctx = get_ssl_context(None, None, None, None, False, True, False, _IS_SYNC)
self.assertFalse(ctx.check_hostname)
ctx = get_ssl_context(None, None, None, None, False, False, False, _IS_SYNC)
self.assertTrue(ctx.check_hostname)
response = await self.client.admin.command(HelloCompat.LEGACY_CMD)
with self.assertRaises(ConnectionFailure) as cm:
await connected(
self.simple_client(
"server",
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
serverSelectionTimeoutMS=500,
**self.credentials, # type: ignore[arg-type]
)
)
# PYTHON-5414 Check for "module service_identity has no attribute SICertificateError"
self.assertNotIn("has no attribute", str(cm.exception))
await connected(
self.simple_client(
"server",
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
tlsAllowInvalidHostnames=True,
serverSelectionTimeoutMS=500,
**self.credentials, # type: ignore[arg-type]
)
)
if "setName" in response:
with self.assertRaises(ConnectionFailure):
await connected(
self.simple_client(
"server",
replicaSet=response["setName"],
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
serverSelectionTimeoutMS=500,
**self.credentials, # type: ignore[arg-type]
)
)
await connected(
self.simple_client(
"server",
replicaSet=response["setName"],
ssl=True,
tlsCertificateKeyFile=CLIENT_PEM,
tlsAllowInvalidCertificates=False,
tlsCAFile=CA_PEM,
tlsAllowInvalidHostnames=True,
serverSelectionTimeoutMS=500,
**self.credentials, # type: ignore[arg-type]
)
)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_sync
@async_client_context.require_no_api_version
@ignore_deprecations
async def test_tlsCRLFile_support(self):
if not hasattr(ssl, "VERIFY_CRL_CHECK_LEAF") or HAVE_PYSSL:
self.assertRaises(
ConfigurationError,
self.simple_client,
"localhost",
ssl=True,
tlsCAFile=CA_PEM,
tlsCRLFile=CRL_PEM,
serverSelectionTimeoutMS=1000,
)
else:
await connected(
self.simple_client(
"localhost",
ssl=True,
tlsCAFile=CA_PEM,
serverSelectionTimeoutMS=1000,
**self.credentials, # type: ignore[arg-type]
)
)
with self.assertRaises(ConnectionFailure):
await connected(
self.simple_client(
"localhost",
ssl=True,
tlsCAFile=CA_PEM,
tlsCRLFile=CRL_PEM,
serverSelectionTimeoutMS=1000,
**self.credentials, # type: ignore[arg-type]
)
)
uri_fmt = "mongodb://localhost/?ssl=true&tlsCAFile=%s&serverSelectionTimeoutMS=1000"
await connected(self.simple_client(uri_fmt % (CA_PEM,), **self.credentials)) # type: ignore
uri_fmt = (
"mongodb://localhost/?ssl=true&tlsCRLFile=%s"
"&tlsCAFile=%s&serverSelectionTimeoutMS=1000"
)
with self.assertRaises(ConnectionFailure):
await connected(
self.simple_client(uri_fmt % (CRL_PEM, CA_PEM), **self.credentials) # type: ignore[arg-type]
)
@unittest.skipIf(
"PyPy" in sys.version and not _IS_SYNC,
"https://github.com/pypy/pypy/issues/5131 flaky on async PyPy due to SSL EOF",
)
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_server_resolvable
@async_client_context.require_no_api_version
@ignore_deprecations
async def test_validation_with_system_ca_certs(self):
# Expects the server to be running with server.pem and ca.pem.
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
# --sslWeakCertificateValidation
#
self.patch_system_certs(CA_PEM)
with self.assertRaises(ConnectionFailure):
# Server cert is verified but hostname matching fails
await connected(
self.simple_client(
"server", ssl=True, serverSelectionTimeoutMS=1000, **self.credentials
) # type: ignore[arg-type]
)
# Server cert is verified. Disable hostname matching.
await connected(
self.simple_client(
"server",
ssl=True,
tlsAllowInvalidHostnames=True,
serverSelectionTimeoutMS=1000,
**self.credentials, # type: ignore[arg-type]
)
)
# Server cert and hostname are verified.
await connected(
self.simple_client(
"localhost", ssl=True, serverSelectionTimeoutMS=1000, **self.credentials
) # type: ignore[arg-type]
)
# Server cert and hostname are verified.
await connected(
self.simple_client(
"mongodb://localhost/?ssl=true&serverSelectionTimeoutMS=1000",
**self.credentials, # type: ignore[arg-type]
)
)
def test_system_certs_config_error(self):
ctx = get_ssl_context(None, None, None, None, True, True, False, _IS_SYNC)
if (sys.platform != "win32" and hasattr(ctx, "set_default_verify_paths")) or hasattr(
ctx, "load_default_certs"
):
raise SkipTest("Can't test when system CA certificates are loadable.")
have_certifi = ssl_support.HAVE_CERTIFI
have_wincertstore = ssl_support.HAVE_WINCERTSTORE
# Force the test regardless of environment.
ssl_support.HAVE_CERTIFI = False
ssl_support.HAVE_WINCERTSTORE = False
try:
with self.assertRaises(ConfigurationError):
self.simple_client("mongodb://localhost/?ssl=true")
finally:
ssl_support.HAVE_CERTIFI = have_certifi
ssl_support.HAVE_WINCERTSTORE = have_wincertstore
def test_certifi_support(self):
if hasattr(ssl, "SSLContext"):
# SSLSocket doesn't provide ca_certs attribute on pythons
# with SSLContext and SSLContext provides no information
# about ca_certs.
raise SkipTest("Can't test when SSLContext available.")
if not ssl_support.HAVE_CERTIFI:
raise SkipTest("Need certifi to test certifi support.")
have_wincertstore = ssl_support.HAVE_WINCERTSTORE
# Force the test on Windows, regardless of environment.
ssl_support.HAVE_WINCERTSTORE = False
try:
ctx = get_ssl_context(None, None, CA_PEM, None, False, False, False, _IS_SYNC)
ssl_sock = ctx.wrap_socket(socket.socket())
self.assertEqual(ssl_sock.ca_certs, CA_PEM)
ctx = get_ssl_context(None, None, None, None, False, False, False, _IS_SYNC)
ssl_sock = ctx.wrap_socket(socket.socket())
self.assertEqual(ssl_sock.ca_certs, ssl_support.certifi.where())
finally:
ssl_support.HAVE_WINCERTSTORE = have_wincertstore
def test_wincertstore(self):
if sys.platform != "win32":
raise SkipTest("Only valid on Windows.")
if hasattr(ssl, "SSLContext"):
# SSLSocket doesn't provide ca_certs attribute on pythons
# with SSLContext and SSLContext provides no information
# about ca_certs.
raise SkipTest("Can't test when SSLContext available.")
if not ssl_support.HAVE_WINCERTSTORE:
raise SkipTest("Need wincertstore to test wincertstore.")
ctx = get_ssl_context(None, None, CA_PEM, None, False, False, False, _IS_SYNC)
ssl_sock = ctx.wrap_socket(socket.socket())
self.assertEqual(ssl_sock.ca_certs, CA_PEM)
ctx = get_ssl_context(None, None, None, None, False, False, False, _IS_SYNC)
ssl_sock = ctx.wrap_socket(socket.socket())
self.assertEqual(ssl_sock.ca_certs, ssl_support._WINCERTS.name)
@async_client_context.require_auth
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_no_api_version
@ignore_deprecations
async def test_mongodb_x509_auth(self):
host, port = await async_client_context.host, await async_client_context.port
self.addAsyncCleanup(remove_all_users, async_client_context.client["$external"])
# Give x509 user all necessary privileges.
await async_client_context.create_user(
"$external",
MONGODB_X509_USERNAME,
roles=[
{"role": "readWriteAnyDatabase", "db": "admin"},
{"role": "userAdminAnyDatabase", "db": "admin"},
],
)
noauth = self.simple_client(
await async_client_context.pair,
ssl=True,
tlsAllowInvalidCertificates=True,
tlsCertificateKeyFile=CLIENT_PEM,
)
with self.assertRaises(OperationFailure):
await noauth.pymongo_test.test.find_one()
listener = EventListener()
auth = self.simple_client(
await async_client_context.pair,
authMechanism="MONGODB-X509",
ssl=True,
tlsAllowInvalidCertificates=True,
tlsCertificateKeyFile=CLIENT_PEM,
event_listeners=[listener],
)
# No error
await auth.pymongo_test.test.find_one()
names = listener.started_command_names()
if async_client_context.version.at_least(4, 4, -1):
# Speculative auth skips the authenticate command.
self.assertEqual(names, ["find"])
else:
self.assertEqual(names, ["authenticate", "find"])
uri = "mongodb://%s@%s:%d/?authMechanism=MONGODB-X509" % (
quote_plus(MONGODB_X509_USERNAME),
host,
port,
)
client = self.simple_client(
uri, ssl=True, tlsAllowInvalidCertificates=True, tlsCertificateKeyFile=CLIENT_PEM
)
# No error
await client.pymongo_test.test.find_one()
uri = "mongodb://%s:%d/?authMechanism=MONGODB-X509" % (host, port)
client = self.simple_client(
uri, ssl=True, tlsAllowInvalidCertificates=True, tlsCertificateKeyFile=CLIENT_PEM
)
# No error
await client.pymongo_test.test.find_one()
# Auth should fail if username and certificate do not match
uri = "mongodb://%s@%s:%d/?authMechanism=MONGODB-X509" % (
quote_plus("not the username"),
host,
port,
)
bad_client = self.simple_client(
uri, ssl=True, tlsAllowInvalidCertificates=True, tlsCertificateKeyFile=CLIENT_PEM
)
with self.assertRaises(OperationFailure):
await bad_client.pymongo_test.test.find_one()
bad_client = self.simple_client(
await async_client_context.pair,
username="not the username",
authMechanism="MONGODB-X509",
ssl=True,
tlsAllowInvalidCertificates=True,
tlsCertificateKeyFile=CLIENT_PEM,
)
with self.assertRaises(OperationFailure):
await bad_client.pymongo_test.test.find_one()
# Invalid certificate (using CA certificate as client certificate)
uri = "mongodb://%s@%s:%d/?authMechanism=MONGODB-X509" % (
quote_plus(MONGODB_X509_USERNAME),
host,
port,
)
try:
await connected(
self.simple_client(
uri,
ssl=True,
tlsAllowInvalidCertificates=True,
tlsCertificateKeyFile=CA_PEM,
serverSelectionTimeoutMS=1000,
)
)
except (ConnectionFailure, ConfigurationError):
pass
else:
self.fail("Invalid certificate accepted.")
@async_client_context.require_tlsCertificateKeyFile
@async_client_context.require_no_api_version
@ignore_deprecations
async def test_connect_with_ca_bundle(self):
def remove(path):
try:
os.remove(path)
except OSError:
pass
temp_ca_bundle = os.path.join(CERT_PATH, "trusted-ca-bundle.pem")
self.addCleanup(remove, temp_ca_bundle)
# Add the CA cert file to the bundle.
cat_files(temp_ca_bundle, CA_BUNDLE_PEM, CA_PEM)
async with self.simple_client(
"localhost", tls=True, tlsCertificateKeyFile=CLIENT_PEM, tlsCAFile=temp_ca_bundle
) as client:
self.assertTrue(await client.admin.command("ping"))
@async_client_context.require_async
@unittest.skipUnless(_HAVE_PYOPENSSL, "PyOpenSSL is not available.")
@unittest.skipUnless(HAVE_SSL, "The ssl module is not available.")
async def test_pyopenssl_ignored_in_async(self):
client = AsyncMongoClient(
"mongodb://localhost:27017?tls=true&tlsAllowInvalidCertificates=true"
)
await client.admin.command("ping") # command doesn't matter, just needs it to connect
await client.close()
if __name__ == "__main__":
unittest.main()
|