def test_ssl(self):
        config = {
            'configsvrs': [{}],
            'routers': [{}],
            'shards': [{}, {
                'shardParams': {
                    'members': [{}]
                }
            }],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.sh = ShardedCluster(config)

        # Server should require SSL.
        host = self.sh.router['hostname']
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient(host))
        # This shouldn't raise.
        connected(
            pymongo.MongoClient(host,
                                ssl_certfile=certificate('client.pem'),
                                ssl_cert_reqs=ssl.CERT_NONE))
 def test_reset(self):
     self.server.stop()
     with self.assertRaises(pymongo.errors.ConnectionFailure):
         connected(pymongo.MongoClient(self.server.hostname))
     self.server.reset()
     # No ConnectionFailure.
     connected(pymongo.MongoClient(self.server.hostname))
 def test_reset(self):
     self.server.stop()
     with self.assertRaises(pymongo.errors.ConnectionFailure):
         connected(pymongo.MongoClient(self.server.hostname))
     self.server.reset()
     # No ConnectionFailure.
     connected(pymongo.MongoClient(self.server.hostname))
Exemplo n.º 4
0
 def connection(self,
                hostname=None,
                read_preference=pymongo.ReadPreference.PRIMARY,
                timeout=300):
     """return MongoReplicaSetClient object if hostname specified
     return MongoClient object if hostname doesn't specified
     Args:
         hostname - connection uri
         read_preference - default PRIMARY
         timeout - specify how long, in seconds, a command can take before server times out.
     """
     logger.debug(
         "connection({hostname}, {read_preference}, {timeout})".format(
             **locals()))
     t_start = time.time()
     servers = hostname or ",".join(self.server_map.values())
     while True:
         try:
             if hostname is None:
                 c = pymongo.MongoReplicaSetClient(
                     servers,
                     replicaSet=self.repl_id,
                     read_preference=read_preference,
                     socketTimeoutMS=self.socket_timeout,
                     w=self._write_concern,
                     fsync=True,
                     **self.kwargs)
                 connected(c)
                 if c.primary:
                     self._authenticate_client(c)
                     return c
                 raise pymongo.errors.AutoReconnect(
                     "No replica set primary available")
             else:
                 logger.debug(
                     "connection to the {servers}".format(**locals()))
                 c = pymongo.MongoClient(
                     servers,
                     socketTimeoutMS=self.socket_timeout,
                     w=self._write_concern,
                     fsync=True,
                     **self.kwargs)
                 connected(c)
                 self._authenticate_client(c)
                 return c
         except (pymongo.errors.PyMongoError):
             exc_type, exc_value, exc_tb = sys.exc_info()
             err_message = traceback.format_exception(
                 exc_type, exc_value, exc_tb)
             logger.error(
                 "Exception {exc_type} {exc_value}".format(**locals()))
             logger.error(err_message)
             if time.time() - t_start > timeout:
                 raise pymongo.errors.AutoReconnect(
                     "Couldn't connect while timeout {timeout} second".
                     format(**locals()))
             time.sleep(1)
 def test_rs_del(self):
     self.rs.create({'members': [{}, {}]})
     repl_id = self.rs.create({'members': [{}, {}]})
     self.assertEqual(len(self.rs), 2)
     primary = self.rs.primary(repl_id)['mongodb_uri']
     connected(pymongo.MongoClient(primary))  # No error.
     self.rs.remove(repl_id)
     self.assertEqual(len(self.rs), 1)
     with self.assertRaises(pymongo.errors.PyMongoError):
         connected(pymongo.MongoClient(primary))
Exemplo n.º 6
0
 def test_rs_del(self):
     self.rs.create({'members': [{}, {}]})
     repl_id = self.rs.create({'members': [{}, {}]})
     self.assertEqual(len(self.rs), 2)
     primary = self.rs.primary(repl_id)['mongodb_uri']
     connected(pymongo.MongoClient(primary))  # No error.
     self.rs.remove(repl_id)
     self.assertEqual(len(self.rs), 1)
     with self.assertRaises(pymongo.errors.PyMongoError):
         connected(pymongo.MongoClient(primary))
 def test_member_del(self):
     repl_id = self.rs.create(
         {'members': [{"rsParams": {"priority": 1.5}}, {}, {}]})
     self.assertEqual(len(self.rs.members(repl_id)), 3)
     assert_eventually(lambda: len(self.rs.secondaries(repl_id)) > 0)
     secondary = self.rs.secondaries(repl_id)[0]
     connected(pymongo.MongoClient(secondary['host']))  # No error.
     self.assertTrue(self.rs.member_del(repl_id, secondary['_id']))
     self.assertEqual(len(self.rs.members(repl_id)), 2)
     with self.assertRaises(pymongo.errors.PyMongoError):
         connected(pymongo.MongoClient(secondary['host']))
Exemplo n.º 8
0
 def test_member_del(self):
     repl_id = self.rs.create(
         {'members': [{
             "rsParams": {
                 "priority": 1.5
             }
         }, {}, {}]})
     self.assertEqual(len(self.rs.members(repl_id)), 3)
     secondary = self.rs.secondaries(repl_id)[0]
     connected(pymongo.MongoClient(secondary['host']))  # No error.
     self.assertTrue(self.rs.member_del(repl_id, secondary['_id']))
     self.assertEqual(len(self.rs.members(repl_id)), 2)
     with self.assertRaises(pymongo.errors.PyMongoError):
         connected(pymongo.MongoClient(secondary['host']))
 def test_ssl(self):
     ssl_params = {
         'sslPEMKeyFile': certificate('server.pem'),
         'sslCAFile': certificate('ca.pem'),
         'sslMode': 'requireSSL',
         'sslAllowInvalidCertificates': True
     }
     # Should not raise an Exception.
     self.server = Server('mongod', {}, ssl_params)
     self.server.start()
     # Server should require SSL.
     with self.assertRaises(pymongo.errors.ConnectionFailure):
         connected(pymongo.MongoClient, self.server.hostname)
     # Doesn't raise with certificate provided.
     connected(pymongo.MongoClient(
         self.server.hostname, ssl_certfile=certificate('client.pem')))
 def test_ssl(self):
     ssl_params = {
         'sslPEMKeyFile': certificate('server.pem'),
         'sslCAFile': certificate('ca.pem'),
         'sslMode': 'requireSSL',
         'sslAllowInvalidCertificates': True
     }
     # Should not raise an Exception.
     self.server = Server('mongod', {}, ssl_params)
     self.server.start()
     # Server should require SSL.
     with self.assertRaises(pymongo.errors.ConnectionFailure):
         connected(pymongo.MongoClient(self.server.hostname))
     # Doesn't raise with certificate provided.
     connected(pymongo.MongoClient(
         self.server.hostname, ssl_certfile=certificate('client.pem'),
         ssl_cert_reqs=ssl.CERT_NONE))
Exemplo n.º 11
0
    def test_reset(self):
        self.repl_cfg = {'members': [{}, {}, {}]}
        self.repl = ReplicaSet(self.repl_cfg)

        server_ids = [m['server_id'] for m in self.repl.members()]
        all_hosts = [Servers().hostname(server_id) for server_id in server_ids]

        # Shut down all members of the ReplicaSet.
        for server_id in server_ids:
            Servers().command(server_id, 'stop')

        # Reset the ReplicaSet. --- We should be able to connect to all members.
        self.repl.reset()

        for host in all_hosts:
            # No ConnectionFailure/AutoReconnect.
            connected(pymongo.MongoClient(host))
Exemplo n.º 12
0
    def test_reset(self):
        self.repl_cfg = {'members': [{}, {}, {}]}
        self.repl = ReplicaSet(self.repl_cfg)

        server_ids = [m['server_id'] for m in self.repl.members()]
        all_hosts = map(Servers().hostname, server_ids)

        # Shut down all members of the ReplicaSet.
        for server_id in server_ids:
            Servers().command(server_id, 'stop')

        # Reset the ReplicaSet. --- We should be able to connect to all members.
        self.repl.reset()

        for host in all_hosts:
            # No ConnectionFailure/AutoReconnect.
            connected(pymongo.MongoClient(host))
Exemplo n.º 13
0
 def connection(self):
     """return authenticated connection"""
     c = pymongo.MongoClient(self.hostname, fsync=True, **self.kwargs)
     connected(c)
     if not self.is_mongos and self.login and not self.restart_required:
         db = c[self.auth_source]
         if self.x509_extra_user:
             auth_dict = {
                 'name': DEFAULT_SUBJECT,
                 'mechanism': 'MONGODB-X509'
             }
         else:
             auth_dict = {'name': self.login, 'password': self.password}
         try:
             db.authenticate(**auth_dict)
         except:
             logger.exception("Could not authenticate to %s with %r" %
                              (self.hostname, auth_dict))
             raise
     return c
Exemplo n.º 14
0
 def connection(self):
     """return authenticated connection"""
     c = pymongo.MongoClient(
         self.hostname, fsync=True,
         socketTimeoutMS=self.socket_timeout, **self.kwargs)
     connected(c)
     if not self.is_mongos and self.login and not self.restart_required:
         db = c[self.auth_source]
         if self.x509_extra_user:
             auth_dict = {
                 'name': DEFAULT_SUBJECT, 'mechanism': 'MONGODB-X509'}
         else:
             auth_dict = {'name': self.login, 'password': self.password}
         try:
             db.authenticate(**auth_dict)
         except:
             logger.exception("Could not authenticate to %s with %r"
                              % (self.hostname, auth_dict))
             raise
     return c
Exemplo n.º 15
0
 def connection(self, hostname=None, read_preference=pymongo.ReadPreference.PRIMARY, timeout=300):
     """return MongoReplicaSetClient object if hostname specified
     return MongoClient object if hostname doesn't specified
     Args:
         hostname - connection uri
         read_preference - default PRIMARY
         timeout - specify how long, in seconds, a command can take before server times out.
     """
     logger.debug("connection({hostname}, {read_preference}, {timeout})".format(**locals()))
     t_start = time.time()
     servers = hostname or ",".join(self.server_map.values())
     while True:
         try:
             if hostname is None:
                 c = pymongo.MongoReplicaSetClient(
                     servers, replicaSet=self.repl_id,
                     read_preference=read_preference,
                     socketTimeoutMS=self.socket_timeout,
                     w=self._write_concern, fsync=True, **self.kwargs)
                 connected(c)
                 if c.primary:
                     self._authenticate_client(c)
                     return c
                 raise pymongo.errors.AutoReconnect("No replica set primary available")
             else:
                 logger.debug("connection to the {servers}".format(**locals()))
                 c = pymongo.MongoClient(
                     servers, socketTimeoutMS=self.socket_timeout,
                     w=self._write_concern, fsync=True, **self.kwargs)
                 connected(c)
                 self._authenticate_client(c)
                 return c
         except (pymongo.errors.PyMongoError):
             exc_type, exc_value, exc_tb = sys.exc_info()
             err_message = traceback.format_exception(exc_type, exc_value, exc_tb)
             logger.error("Exception {exc_type} {exc_value}".format(**locals()))
             logger.error(err_message)
             if time.time() - t_start > timeout:
                 raise pymongo.errors.AutoReconnect("Couldn't connect while timeout {timeout} second".format(**locals()))
             time.sleep(1)
Exemplo n.º 16
0
    def test_ssl(self):
        member_params = {}
        self.repl_cfg = {
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Server should require SSL.
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient, self.repl.primary())

        # This shouldn't raise.
        connected(
            pymongo.MongoClient(self.repl.primary(),
                                ssl_certfile=certificate('client.pem')))
Exemplo n.º 17
0
    def test_ssl(self):
        member_params = {}
        self.repl_cfg = {
            'members': [member_params, member_params],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.repl = ReplicaSet(self.repl_cfg)

        # Server should require SSL.
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient(self.repl.primary()))

        # This shouldn't raise.
        connected(pymongo.MongoClient(
            self.repl.primary(), ssl_certfile=certificate('client.pem'),
            ssl_cert_reqs=ssl.CERT_NONE))
    def test_ssl(self):
        config = {
            'configsvrs': [{}],
            'routers': [{}],
            'shards': [{}, {'shardParams': {'members': [{}]}}],
            'sslParams': {
                'sslCAFile': certificate('ca.pem'),
                'sslPEMKeyFile': certificate('server.pem'),
                'sslMode': 'requireSSL',
                'sslClusterFile': certificate('cluster_cert.pem'),
                'sslAllowInvalidCertificates': True
            }
        }
        # Should not raise an Exception.
        self.sh = ShardedCluster(config)

        # Server should require SSL.
        host = self.sh.router['hostname']
        with self.assertRaises(pymongo.errors.ConnectionFailure):
            connected(pymongo.MongoClient(host))
        # This shouldn't raise.
        connected(
            pymongo.MongoClient(host, ssl_certfile=certificate('client.pem'),
                                ssl_cert_reqs=ssl.CERT_NONE))
Exemplo n.º 19
0
 def test_reset(self):
     self.server.stop()
     self.server.reset()
     # No ConnectionFailure.
     connected(pymongo.MongoClient(self.server.hostname))
 def test_reset(self):
     self.server.stop()
     self.server.reset()
     # No ConnectionFailure.
     connected(pymongo.MongoClient(self.server.hostname))
Exemplo n.º 21
0
 def test_primary(self):
     repl_id = self.rs.create({'id': 'test-rs-1', 'members': [{}, {}]})
     primary = self.rs.primary(repl_id)['mongodb_uri']
     c = connected(pymongo.MongoClient(primary))
     self.assertTrue(c.is_primary)
     c.close()
Exemplo n.º 22
0
 def test_primary(self):
     repl_id = self.rs.create({'id': 'test-rs-1', 'members': [{}, {}]})
     primary = self.rs.primary(repl_id)['mongodb_uri']
     c = connected(pymongo.MongoClient(primary))
     self.assertTrue(c.is_primary)
     c.close()