(self)
| 538 | @client_context.require_no_api_version |
| 539 | @ignore_deprecations |
| 540 | def test_mongodb_x509_auth(self): |
| 541 | host, port = client_context.host, client_context.port |
| 542 | self.addCleanup(remove_all_users, client_context.client["$external"]) |
| 543 | |
| 544 | # Give x509 user all necessary privileges. |
| 545 | client_context.create_user( |
| 546 | "$external", |
| 547 | MONGODB_X509_USERNAME, |
| 548 | roles=[ |
| 549 | {"role": "readWriteAnyDatabase", "db": "admin"}, |
| 550 | {"role": "userAdminAnyDatabase", "db": "admin"}, |
| 551 | ], |
| 552 | ) |
| 553 | |
| 554 | noauth = self.simple_client( |
| 555 | client_context.pair, |
| 556 | ssl=True, |
| 557 | tlsAllowInvalidCertificates=True, |
| 558 | tlsCertificateKeyFile=CLIENT_PEM, |
| 559 | ) |
| 560 | |
| 561 | with self.assertRaises(OperationFailure): |
| 562 | noauth.pymongo_test.test.find_one() |
| 563 | |
| 564 | listener = EventListener() |
| 565 | auth = self.simple_client( |
| 566 | client_context.pair, |
| 567 | authMechanism="MONGODB-X509", |
| 568 | ssl=True, |
| 569 | tlsAllowInvalidCertificates=True, |
| 570 | tlsCertificateKeyFile=CLIENT_PEM, |
| 571 | event_listeners=[listener], |
| 572 | ) |
| 573 | |
| 574 | # No error |
| 575 | auth.pymongo_test.test.find_one() |
| 576 | names = listener.started_command_names() |
| 577 | if client_context.version.at_least(4, 4, -1): |
| 578 | # Speculative auth skips the authenticate command. |
| 579 | self.assertEqual(names, ["find"]) |
| 580 | else: |
| 581 | self.assertEqual(names, ["authenticate", "find"]) |
| 582 | |
| 583 | uri = "mongodb://%s@%s:%d/?authMechanism=MONGODB-X509" % ( |
| 584 | quote_plus(MONGODB_X509_USERNAME), |
| 585 | host, |
| 586 | port, |
| 587 | ) |
| 588 | client = self.simple_client( |
| 589 | uri, ssl=True, tlsAllowInvalidCertificates=True, tlsCertificateKeyFile=CLIENT_PEM |
| 590 | ) |
| 591 | # No error |
| 592 | client.pymongo_test.test.find_one() |
| 593 | |
| 594 | uri = "mongodb://%s:%d/?authMechanism=MONGODB-X509" % (host, port) |
| 595 | client = self.simple_client( |
| 596 | uri, ssl=True, tlsAllowInvalidCertificates=True, tlsCertificateKeyFile=CLIENT_PEM |
| 597 | ) |
nothing calls this directly
no test coverage detected