mistercrunch closed pull request #4505: Expose hook to inject database connection logic on the fly URL: https://github.com/apache/incubator-superset/pull/4505
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/superset/config.py b/superset/config.py index ae81cfcb6e..c9b8607e3e 100644 --- a/superset/config.py +++ b/superset/config.py @@ -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 can wire different users to +# 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 diff --git a/superset/models/core.py b/superset/models/core.py index b4dbada947..41d8742b65 100644 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -675,6 +675,9 @@ def get_sqla_engine(self, schema=None, nullpool=True, user_name=None): if configuration: params['connect_args'] = {'configuration': configuration} + DB_CONNECTION_MUTATOR = config.get('DB_CONNECTION_MUTATOR') + if DB_CONNECTION_MUTATOR: + url, params = DB_CONNECTION_MUTATOR(url, params, g.user) return create_engine(url, **params) def get_reserved_words(self): ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services