Exemple #1
0
 def test_bolt_routing_uri_only(self):
     data = get_connection_data("bolt+routing://host:9999")
     del data["hash"]
     self.assertEqual(
         data, {
             'auth': ('neo4j', 'password'),
             'host': 'host',
             'password': '******',
             'port': 9999,
             'scheme': 'bolt+routing',
             'secure': False,
             'verified': False,
             'uri': 'bolt+routing://host:9999',
             'user': '******',
             'user_agent': bolt_user_agent(),
         })
Exemple #2
0
 def test_uri_and_host(self):
     data = get_connection_data("bolt://host:9999", host="other")
     del data["hash"]
     self.assertEqual(
         data, {
             'auth': ('neo4j', 'password'),
             'host': 'other',
             'password': '******',
             'port': 9999,
             'scheme': 'bolt',
             'secure': False,
             'verified': False,
             'uri': 'bolt://other:9999',
             'user': '******',
             'user_agent': bolt_user_agent(),
         })
Exemple #3
0
 def test_uri_and_port(self):
     data = get_connection_data("bolt://host:8888", port=8888)
     del data["hash"]
     self.assertEqual(
         data, {
             'auth': ('neo4j', 'password'),
             'host': 'host',
             'password': '******',
             'port': 8888,
             'scheme': 'bolt',
             'secure': False,
             'verified': False,
             'uri': 'bolt://host:8888',
             'user': '******',
             'user_agent': bolt_user_agent(),
         })
Exemple #4
0
def get_connection_data(uri=None, **settings):
    """ Generate a dictionary of connection data for an optional URI plus
    additional connection settings.

    :param uri:
    :param settings:
    :return:
    """
    data = {
        "host": None,
        "password": None,
        "port": None,
        "scheme": None,
        "secure": None,
        "verified": None,
        "user": None,
        "user_agent": None,
    }
    # apply uri
    uri = coalesce(uri, NEO4J_URI)
    if uri is not None:
        parsed = urlsplit(uri)
        if parsed.scheme is not None:
            data["scheme"] = parsed.scheme
            if data["scheme"] in ["https"]:
                data["secure"] = True
            elif data["scheme"] in ["http"]:
                data["secure"] = False
        data["user"] = coalesce(parsed.username, data["user"])
        data["password"] = coalesce(parsed.password, data["password"])
        data["host"] = coalesce(parsed.hostname, data["host"])
        data["port"] = coalesce(parsed.port, data["port"])
    # apply auth (this can override `uri`)
    if "auth" in settings and settings["auth"] is not None:
        data["user"], data["password"] = settings["auth"]
    elif NEO4J_AUTH is not None:
        data["user"], _, data["password"] = NEO4J_AUTH.partition(":")
    # apply components (these can override `uri` and `auth`)
    data["user_agent"] = coalesce(settings.get("user_agent"), NEO4J_USER_AGENT,
                                  data["user_agent"])
    data["secure"] = coalesce(settings.get("secure"), data["secure"],
                              NEO4J_SECURE)
    data["verified"] = coalesce(settings.get("verified"), data["verified"],
                                NEO4J_VERIFIED)
    data["scheme"] = coalesce(settings.get("scheme"), data["scheme"])
    data["user"] = coalesce(settings.get("user"), data["user"])
    data["password"] = coalesce(settings.get("password"), data["password"])
    data["host"] = coalesce(settings.get("host"), data["host"])
    data["port"] = coalesce(settings.get("port"), data["port"])
    # apply correct scheme for security
    if data["secure"] is True and data["scheme"] == "http":
        data["scheme"] = "https"
    if data["secure"] is False and data["scheme"] == "https":
        data["scheme"] = "http"
    # apply default port for scheme
    if data["scheme"] and not data["port"]:
        if data["scheme"] == "http":
            data["port"] = DEFAULT_HTTP_PORT
        elif data["scheme"] == "https":
            data["port"] = DEFAULT_HTTPS_PORT
        elif data["scheme"] in ["bolt", "bolt+routing"]:
            data["port"] = DEFAULT_BOLT_PORT
    # apply other defaults
    if not data["user_agent"]:
        data["user_agent"] = http_user_agent() if data["scheme"] in [
            "http", "https"
        ] else bolt_user_agent()
    if data["secure"] is None:
        data["secure"] = DEFAULT_SECURE
    if data["verified"] is None:
        data["verified"] = DEFAULT_VERIFIED
    if not data["scheme"]:
        data["scheme"] = DEFAULT_SCHEME
        if data["scheme"] == "http":
            data["secure"] = False
            data["verified"] = False
        if data["scheme"] == "https":
            data["secure"] = True
            data["verified"] = True
    if not data["user"]:
        data["user"] = DEFAULT_USER
    if not data["password"]:
        data["password"] = DEFAULT_PASSWORD
    if not data["host"]:
        data["host"] = DEFAULT_HOST
    if not data["port"]:
        data["port"] = DEFAULT_BOLT_PORT
    # apply composites
    data["auth"] = (data["user"], data["password"])
    data["uri"] = "%s://%s:%s" % (data["scheme"], data["host"], data["port"])
    h = hashlib_new("md5")
    for key in sorted(data):
        h.update(bstr(data[key]))
    data["hash"] = h.hexdigest()
    return data
Exemple #5
0
 def __new__(cls, *uris, **settings):
     from py2neo.addressing import register_graph_service, get_graph_service_auth
     from py2neo.http import register_http_driver
     from neo4j.v1 import GraphDatabase
     register_http_driver()
     address = register_graph_service(*uris, **settings)
     auth = get_graph_service_auth(address)
     key = (address, auth)
     try:
         inst = cls.__instances[key]
     except KeyError:
         http_uri = address.http_uri
         bolt_uri = address.bolt_uri
         inst = super(GraphService, cls).__new__(cls)
         inst._initial_uris = uris
         inst._initial_settings = settings
         inst.__remote__ = Remote(http_uri["/"])
         inst.address = address
         inst.user = auth.user if auth else None
         auth_token = auth.token if auth else None
         inst._http_driver = GraphDatabase.driver(http_uri["/"], auth=auth_token,
                                                  encrypted=address.secure, user_agent=http_user_agent())
         if bolt_uri:
             inst._bolt_driver = GraphDatabase.driver(bolt_uri["/"], auth=auth_token,
                                                      encrypted=address.secure, user_agent=bolt_user_agent())
         inst._jmx_remote = Remote(http_uri["/db/manage/server/jmx/domain/org.neo4j"])
         inst._graphs = {}
         cls.__instances[key] = inst
     return inst