Skip to content

Add support for impersonation #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 13, 2021
42 changes: 41 additions & 1 deletion docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,12 @@ Name of the database to query.

.. Note::

The default database can be set on the Neo4j instance settings.
The default database can be set on the Neo4j instance settings.

.. Note::
It is recommended to always specify the database explicitly when possible.
This allows the driver to work more efficiently, as it will not have to
resolve the home database first.


.. code-block:: python
Expand All @@ -499,6 +504,41 @@ Name of the database to query.
:Default: ``neo4j.DEFAULT_DATABASE``


.. _impersonated-user-ref:

``impersonated_user``
---------------------
Name of the user to impersonate.
This means that all actions in the session will be executed in the security
context of the impersonated user. For this, the user for which the
:class:``Driver`` has been created needs to have the appropriate permissions.

:Type: ``str``, None


.. py:data:: None
:noindex:

Will not perform impersonation.


.. Note::

The server or all servers of the cluster need to support impersonation when.
Otherwise, the driver will raise :py:exc:`.ConfigurationError`
as soon as it encounters a server that does not.


.. code-block:: python

from neo4j import GraphDatabase
driver = GraphDatabase.driver(uri, auth=(user, password))
session = driver.session(impersonated_user="alice")


:Default: ``None``


.. _default-access-mode-ref:

``default_access_mode``
Expand Down
9 changes: 4 additions & 5 deletions neo4j/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,9 @@ def supports_multi_db(self):
:return: Returns true if the server or cluster the driver connects to supports multi-databases, otherwise false.
:rtype: bool
"""
cx = self._pool.acquire(access_mode=READ_ACCESS, timeout=self._pool.workspace_config.connection_acquisition_timeout, database=self._pool.workspace_config.database)
support = cx.supports_multiple_databases
self._pool.release(cx)

return support
with self.session() as session:
session._connect(READ_ACCESS)
return session._connection.supports_multiple_databases


class BoltDriver(Direct, Driver):
Expand Down Expand Up @@ -447,6 +445,7 @@ def _verify_routing_connectivity(self):
routing_info[ix] = self._pool.fetch_routing_info(
address=table.routers[0],
database=self._default_workspace_config.database,
imp_user=self._default_workspace_config.impersonated_user,
bookmarks=None,
timeout=self._default_workspace_config
.connection_acquisition_timeout
Expand Down
4 changes: 4 additions & 0 deletions neo4j/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ class WorkspaceConfig(Config):
#: Fetch Size
fetch_size = 1000

#: User to impersonate
impersonated_user = None
# Note that you need appropriate permissions to do so.


class SessionConfig(WorkspaceConfig):
""" Session configuration.
Expand Down
Loading