def setUp(self): self.dbpath = tempfile.mkdtemp() mongod_noauth = Mongod(port=mongo_port, auth=False, dbpath=self.dbpath) yield mongod_noauth.start() try: try: conn = connection.MongoConnection(mongo_host, mongo_port) server_status = yield conn.admin.command("serverStatus") major_version = int(server_status['version'].split('.')[0]) if major_version != 3: raise unittest.SkipTest( "This test is only for MongoDB 3.x") # Force MongoDB 3.x to use MONGODB-CR auth schema yield conn.admin.system.version.update_one( {"_id": "authSchema"}, {"$set": { "currentVersion": 3 }}, upsert=True) finally: yield conn.disconnect() yield mongod_noauth.stop() except unittest.SkipTest: shutil.rmtree(self.dbpath) raise self.mongod = Mongod(port=mongo_port, auth=True, dbpath=self.dbpath) yield self.mongod.start() try: conn = connection.MongoConnection(mongo_host, mongo_port) try: yield conn.admin.command("createUser", self.ua_login, pwd=self.ua_password, roles=[{ "role": "userAdminAnyDatabase", "db": "admin" }]) yield conn.admin.authenticate(self.ua_login, self.ua_password) yield conn[self.db1].command("createUser", self.login1, pwd=self.password1, roles=[{ "role": "readWrite", "db": self.db1 }]) finally: yield conn.disconnect() except: yield self.mongod.stop() raise
def setUp(self): self.dbpath = tempfile.mkdtemp() mongod_noauth = Mongod(port=mongo_port, auth=False, dbpath=self.dbpath) yield mongod_noauth.start() try: conn = connection.MongoConnection(mongo_host, mongo_port) try: ismaster = yield conn.admin.command("ismaster") if ismaster["maxWireVersion"] < 3: raise unittest.SkipTest( "This test is only for MongoDB 3.0") # Force MongoDB 3.x to use MONGODB-CR auth schema yield conn.admin.system.version.update_one( {"_id": "authSchema"}, {"$set": { "currentVersion": 3 }}, upsert=True) finally: yield conn.disconnect() finally: yield mongod_noauth.stop() self.mongod = Mongod(port=mongo_port, auth=True, dbpath=self.dbpath) yield self.mongod.start() try: conn = connection.MongoConnection(mongo_host, mongo_port) try: yield conn.admin.command("createUser", self.ua_login, pwd=self.ua_password, roles=[{ "role": "userAdminAnyDatabase", "db": "admin" }]) yield conn.admin.authenticate(self.ua_login, self.ua_password) yield conn[self.db1].command("createUser", self.login1, pwd=self.password1, roles=[{ "role": "readWrite", "db": self.db1 }]) finally: yield conn.disconnect() except: yield self.mongod.stop() raise
def test_lazy_fail(self): conn = connection.MongoConnection(port=mongo_port, ssl_context_factory=self.ssl_factory) try: yield conn.db.authenticate("DC=another,O=txmongo", '', mechanism="MONGODB-X509") yield self.assertFailure(conn.db.coll.find(), OperationFailure) finally: yield conn.disconnect()
def test_auth(self): conn = connection.MongoConnection(port=mongo_port, ssl_context_factory=self.ssl_factory) yield self.assertFailure(conn.db.coll.find(), OperationFailure) try: yield conn.db.authenticate(self.client_subject, '', mechanism="MONGODB-X509") yield conn.db.coll.insert_one({'x': 42}) cnt = yield conn.db.coll.count() self.assertEqual(cnt, 1) finally: yield conn.disconnect()
def setUp(self): self.dbpath = tempfile.mkdtemp() self.server_keyfile = self.__create_keyfile(self.server_keycert) self.ca_certfile = self.__create_keyfile(self.ca_cert) self.client_keyfile = self.__create_keyfile(self.client_key) self.client_certfile = self.__create_keyfile(self.client_cert) self.ssl_factory = ssl.DefaultOpenSSLContextFactory( privateKeyFileName=self.client_keyfile, certificateFileName=self.client_certfile, ) mongod_noauth = Mongod(port=mongo_port, auth=False, dbpath=self.dbpath) yield mongod_noauth.start() try: conn = connection.MongoConnection("localhost", mongo_port) yield conn["$external"].command("createUser", self.client_subject, roles=[{ "role": "root", "db": "admin" }]) finally: yield conn.disconnect() yield mongod_noauth.stop() self.mongod = Mongod(port=mongo_port, auth=True, dbpath=self.dbpath, args=[ "--clusterAuthMode", "x509", "--sslMode", "requireSSL", "--sslPEMKeyFile", self.server_keyfile, "--sslCAFile", self.ca_certfile ]) try: yield self.mongod.start() except: print(self.mongod.output()) raise