From 28f24149154bc1f596c82fe42e0f7215675702fb Mon Sep 17 00:00:00 2001 From: Kevin Hill Date: Wed, 25 Jun 2025 13:23:27 -0300 Subject: [PATCH] feat: Make graph authentication optional for Neo4j and Memgraph Introduces optional authentication for Neo4j and Memgraph adapter handling. --- **DCO Affirmation** I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: openhands Co-authored-by: hajdul88 <52442977+hajdul88@users.noreply.github.com> --- .../databases/graph/get_graph_engine.py | 16 ++++++++-------- .../databases/graph/memgraph/memgraph_adapter.py | 11 ++++++++--- .../databases/graph/neo4j_driver/adapter.py | 14 +++++++++++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cognee/infrastructure/databases/graph/get_graph_engine.py b/cognee/infrastructure/databases/graph/get_graph_engine.py index bb6544fd9..4607b93ca 100644 --- a/cognee/infrastructure/databases/graph/get_graph_engine.py +++ b/cognee/infrastructure/databases/graph/get_graph_engine.py @@ -77,15 +77,15 @@ def create_graph_engine( ) if graph_database_provider == "neo4j": - if not (graph_database_url and graph_database_username and graph_database_password): - raise EnvironmentError("Missing required Neo4j credentials.") + if not graph_database_url: + raise EnvironmentError("Missing required Neo4j URL.") from .neo4j_driver.adapter import Neo4jAdapter return Neo4jAdapter( graph_database_url=graph_database_url, - graph_database_username=graph_database_username, - graph_database_password=graph_database_password, + graph_database_username=graph_database_username or None, + graph_database_password=graph_database_password or None, ) elif graph_database_provider == "falkordb": @@ -124,15 +124,15 @@ def create_graph_engine( ) elif graph_database_provider == "memgraph": - if not (graph_database_url and graph_database_username and graph_database_password): - raise EnvironmentError("Missing required Memgraph credentials.") + if not graph_database_url: + raise EnvironmentError("Missing required Memgraph URL.") from .memgraph.memgraph_adapter import MemgraphAdapter return MemgraphAdapter( graph_database_url=graph_database_url, - graph_database_username=graph_database_username, - graph_database_password=graph_database_password, + graph_database_username=graph_database_username or None, + graph_database_password=graph_database_password or None, ) from .networkx.adapter import NetworkXAdapter diff --git a/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py b/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py index bd2cffeb8..871a9543e 100644 --- a/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py +++ b/cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py @@ -58,13 +58,18 @@ class MemgraphAdapter(GraphDBInterface): def __init__( self, graph_database_url: str, - graph_database_username: str, - graph_database_password: str, + graph_database_username: Optional[str] = None, + graph_database_password: Optional[str] = None, driver: Optional[Any] = None, ): + # Only use auth if both username and password are provided + auth = None + if graph_database_username and graph_database_password: + auth = (graph_database_username, graph_database_password) + self.driver = driver or AsyncGraphDatabase.driver( graph_database_url, - auth=(graph_database_username, graph_database_password), + auth=auth, max_connection_lifetime=120, ) diff --git a/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py b/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py index f9a759500..d57c7797c 100644 --- a/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +++ b/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py @@ -40,13 +40,21 @@ class Neo4jAdapter(GraphDBInterface): def __init__( self, graph_database_url: str, - graph_database_username: str, - graph_database_password: str, + graph_database_username: Optional[str] = None, + graph_database_password: Optional[str] = None, driver: Optional[Any] = None, ): + # Only use auth if both username and password are provided + auth = None + if graph_database_username and graph_database_password: + auth = (graph_database_username, graph_database_password) + elif graph_database_username or graph_database_password: + logger = get_logger(__name__) + logger.warning("Neo4j credentials incomplete – falling back to anonymous connection.") + self.driver = driver or AsyncGraphDatabase.driver( graph_database_url, - auth=(graph_database_username, graph_database_password), + auth=auth, max_connection_lifetime=120, notifications_min_severity="OFF", )