Skip to content
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

Expose hook to inject database connection logic on the fly #4505

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ class CeleryConfig(object):
# an XSS security vulnerability
ENABLE_JAVASCRIPT_CONTROLS = False

# A callable that allows altering the database conneciton URL and params
# on the fly, at runtime. This allows for things like impersonation or
# arbitrary logic. For instance you could wired different users to

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/you could wired/you can wire/g

# use different connection parameters, or pass their email address as the
# username. The function receives the connection uri object, connection
# params, and user object, and returns the mutated uri and params objects.
# Example:
# def DB_CONNECTION_MUTATOR(uri, params, user):
# if user and user.email:
# uri.username = user.email
# return uri, params
#
# Note that the returned uri and params are passed directly to sqlalchemy's
# as such `create_engine(url, **params)`
DB_CONNECTION_MUTATOR = None

try:
if CONFIG_PATH_ENV_VAR in os.environ:
# Explicitly import config module that is not in pythonpath; useful
Expand Down
2 changes: 2 additions & 0 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ def get_sqla_engine(self, schema=None, nullpool=True, user_name=None):
if configuration:
params['connect_args'] = {'configuration': configuration}

if config.DB_CONNECTION_MUTATOR:
url, params = config.DB_CONNECTION_MUTATOR(url, params, g.user)
return create_engine(url, **params)

def get_reserved_words(self):
Expand Down