def __init__(self, uri, **config): self.initial_address = initial_address = SocketAddress.from_uri( uri, DEFAULT_PORT) self.security_plan = security_plan = SecurityPlan.build(**config) self.encrypted = security_plan.encrypted routing_context = SocketAddress.parse_routing_context(uri) if not security_plan.routing_compatible: # this error message is case-specific as there is only one incompatible # scenario right now raise ValueError( "TRUST_ON_FIRST_USE is not compatible with routing") def connector(address, error_handler): return connect(address, security_plan.ssl_context, error_handler, **config) pool = RoutingConnectionPool(connector, initial_address, routing_context, initial_address, **config) try: pool.update_routing_table() except: pool.close() raise else: Driver.__init__(self, pool, **config)
def __new__(cls, uri, **config): cls._check_uri(uri) instance = object.__new__(cls) instance.initial_address = initial_address = SocketAddress.from_uri( uri, DEFAULT_PORT) instance.security_plan = security_plan = SecurityPlan.build(**config) instance.encrypted = security_plan.encrypted routing_context = SocketAddress.parse_routing_context(uri) if not security_plan.routing_compatible: # this error message is case-specific as there is only one incompatible # scenario right now raise ValueError( "TRUST_ON_FIRST_USE is not compatible with routing") def connector(address, error_handler): return connect(address, security_plan.ssl_context, error_handler, **config) pool = RoutingConnectionPool(connector, initial_address, routing_context, initial_address, **config) try: pool.update_routing_table() except: pool.close() raise else: instance._pool = pool instance._max_retry_time = config.get( "max_retry_time", default_config["max_retry_time"]) return instance
def __new__(cls, uri, **config): cls._check_uri(uri) if SocketAddress.parse_routing_context(uri): raise ValueError( "Parameters are not supported with scheme 'bolt'. Given URI: '%s'." % uri) instance = object.__new__(cls) # We keep the address containing the host name or IP address exactly # as-is from the original URI. This means that every new connection # will carry out DNS resolution, leading to the possibility that # the connection pool may contain multiple IP address keys, one for # an old address and one for a new address. instance.address = SocketAddress.from_uri(uri, DEFAULT_PORT) instance.security_plan = security_plan = SecurityPlan.build(**config) instance.encrypted = security_plan.encrypted def connector(address, error_handler): return connect(address, security_plan.ssl_context, error_handler, **config) pool = DirectConnectionPool(connector, instance.address, **config) pool.release(pool.acquire()) instance._pool = pool instance._max_retry_time = config.get("max_retry_time", default_config["max_retry_time"]) return instance
def __init__(self, uri, **config): # We keep the address containing the host name or IP address exactly # as-is from the original URI. This means that every new connection # will carry out DNS resolution, leading to the possibility that # the connection pool may contain multiple IP address keys, one for # an old address and one for a new address. if SocketAddress.parse_routing_context(uri): raise ValueError("Parameters are not supported with scheme 'bolt'. Given URI: '%s'." % uri) self.address = SocketAddress.from_uri(uri, DEFAULT_PORT) self.security_plan = security_plan = SecurityPlan.build(**config) self.encrypted = security_plan.encrypted pool = DirectConnectionPool(lambda a: connect(a, security_plan.ssl_context, **config), self.address) pool.release(pool.acquire()) Driver.__init__(self, pool, **config)
def parse_routing_info(cls, records): """ Parse the records returned from a getServers call and return a new RoutingTable instance. """ if len(records) != 1: raise ProtocolError("Expected exactly one record") record = records[0] routers = [] readers = [] writers = [] try: servers = record["servers"] for server in servers: role = server["role"] addresses = [] for address in server["addresses"]: addresses.extend(resolve(SocketAddress.parse(address, DEFAULT_PORT))) if role == "ROUTE": routers.extend(addresses) elif role == "READ": readers.extend(addresses) elif role == "WRITE": writers.extend(addresses) ttl = record["ttl"] except (KeyError, TypeError): raise ProtocolError("Cannot parse routing info") else: return cls(routers, readers, writers, ttl)
def __init__(self, address, sock, protocol_version, error_handler, **config): self.address = address self.socket = sock self.protocol_version = protocol_version self.error_handler = error_handler self.server = ServerInfo(SocketAddress.from_socket(sock)) self.input_buffer = ChunkedInputBuffer() self.output_buffer = ChunkedOutputBuffer() self.packer = Packer(self.output_buffer) self.unpacker = Unpacker() self.responses = deque() self._max_connection_lifetime = config.get("max_connection_lifetime", default_config["max_connection_lifetime"]) self._creation_timestamp = perf_counter() # Determine the user agent and ensure it is a Unicode value user_agent = config.get("user_agent", default_config["user_agent"]) if isinstance(user_agent, bytes): user_agent = user_agent.decode("UTF-8") self.user_agent = user_agent # Determine auth details auth = config.get("auth") if not auth: self.auth_dict = {} elif isinstance(auth, tuple) and 2 <= len(auth) <= 3: from neo4j.v1 import basic_auth self.auth_dict = vars(basic_auth(*auth)) else: try: self.auth_dict = vars(auth) except (KeyError, TypeError): raise TypeError("Cannot determine auth details from %r" % auth) # Pick up the server certificate, if any self.der_encoded_server_certificate = config.get("der_encoded_server_certificate")
def load(result): from neo4j.v1 import BoltStatementResultSummary keys = self._keys = self._hydrant.keys = tuple(result["columns"]) hydrate = self._hydrant.hydrate for record in result["data"]: self._records.append(Record(zip(keys, hydrate(record["rest"])))) stats = result["stats"] # fix broken key if "relationship_deleted" in stats: stats["relationships_deleted"] = stats["relationship_deleted"] del stats["relationship_deleted"] if "contains_updates" in stats: del stats["contains_updates"] metadata = {"stats": stats} if "plan" in result: metadata["http_plan"] = result["plan"] # TODO: fill these in metadata["statement"] = None metadata["parameters"] = None metadata["server"] = ServerInfo(SocketAddress.from_uri(session.graph.database.uri)) self._summary = BoltStatementResultSummary(**metadata) self._session = None return len(self._records)
def __init__(self, sock, **config): self.socket = sock self.server = ServerInfo(SocketAddress.from_socket(sock)) self.input_buffer = ChunkedInputBuffer() self.output_buffer = ChunkedOutputBuffer() self.packer = Packer(self.output_buffer) self.unpacker = Unpacker() self.responses = deque() # Determine the user agent and ensure it is a Unicode value user_agent = config.get("user_agent", DEFAULT_USER_AGENT) if isinstance(user_agent, bytes): user_agent = user_agent.decode("UTF-8") self.user_agent = user_agent # Determine auth details auth = config.get("auth") if not auth: self.auth_dict = {} elif isinstance(auth, tuple) and 2 <= len(auth) <= 3: from neo4j.v1 import basic_auth self.auth_dict = vars(basic_auth(*auth)) else: try: self.auth_dict = vars(auth) except (KeyError, TypeError): raise TypeError("Cannot determine auth details from %r" % auth) # Pick up the server certificate, if any self.der_encoded_server_certificate = config.get( "der_encoded_server_certificate") response = InitResponse(self) self.append(INIT, (self.user_agent, self.auth_dict), response=response) self.sync() self._supports_statement_reuse = self.server.supports_statement_reuse() self.packer.supports_bytes = self.server.supports_bytes()
def verify_routing_context(expected, uri): context = SocketAddress.parse_routing_context(uri) assert context == expected
def test_should_error_when_key_duplicate(self): with self.assertRaises(ValueError): SocketAddress.parse_routing_context("bolt+routing://127.0.0.1/?name=molly&name=white")
def test_should_error_when_value_missing(self): with self.assertRaises(ValueError): SocketAddress.parse_routing_context("bolt+routing://127.0.0.1/?name=&color=white")
def test_should_fail_on_non_numeric_port(self): with self.assertRaises(ValueError): _ = SocketAddress.parse("127.0.0.1:X")
def test_should_parse_host_name_and_port(self): parsed = SocketAddress.parse("localhost:7687") assert parsed == ("localhost", 7687)
def test_should_parse_ipv6_address_and_port(self): parsed = SocketAddress.parse("[::1]:7687") assert parsed == ("::1", 7687, 0, 0)
def test_should_parse_ipv4_address_and_port(self): parsed = SocketAddress.parse("127.0.0.1:7687") assert parsed == ("127.0.0.1", 7687)