mariovor opened a new issue, #20455:
URL: https://github.com/apache/superset/issues/20455

   ### Following Setup
   - Superset 1.5.0 running in docker.
   - Postgres database 13 running in trust mode accepting connections from 
Superset host
   - Authentication via Keycloak, custom SecurityManager created for user 
authentication.
   - DB_CONNECTION_MUTATOR used to modify the username of the database 
connection (see [here](https://github.com/apache/superset/issues/17074) from 
where I took inspiration) 
   
   DB_CONNECTION_MUTATOR
   ```
   def DB_CONNECTION_MUTATOR(uri, params, username, security_manager, source):
       uri.username = security_manager.current_user.username
       return uri, params
   ```
   
   ### Expected results:
   - On each database call, the username of the logged-in user is used as 
connection parameter for the database. 
   
   
   ### Actual results
   - It **does** work for  creating charts. 
   - If fails when running a query in SqlLab
   ```
   [SupersetError(message="'_AppCtxGlobals' object has no attribute 'user'", 
error_type=<SupersetErrorType.GENERIC_DB_ENGINE_ERROR: 
'GENERIC_DB_ENGINE_ERROR'>, level=<ErrorLevel.ERROR: 'error'>, 
extra={'engine_name': 'PostgreSQL', 'issue_codes': [{'code': 1002, 'message': 
'Issue 1002 - The database returned an unexpected error.'}]})]
   Traceback (most recent call last):
     File "/app/superset/views/base.py", line 211, in wraps
       return f(self, *args, **kwargs)
     File "/app/superset/utils/log.py", line 245, in wrapper
       value = f(*args, **kwargs)
     File "/app/superset/views/core.py", line 2574, in sql_json
       command_result: CommandResult = command.run()
     File "/app/superset/sqllab/command.py", line 104, in run
       raise ex
     File "/app/superset/sqllab/command.py", line 96, in run
       status = self._run_sql_json_exec_from_scratch()
     File "/app/superset/sqllab/command.py", line 138, in 
_run_sql_json_exec_from_scratch
       raise ex
     File "/app/superset/sqllab/command.py", line 133, in 
_run_sql_json_exec_from_scratch
       return self._sql_json_executor.execute(
     File "/app/superset/sqllab/sql_json_executer.py", line 111, in execute
       raise SupersetErrorsException(
   superset.exceptions.SupersetErrorsException: 
[SupersetError(message="'_AppCtxGlobals' object has no attribute 'user'", 
error_type=<SupersetErrorType.GENERIC_DB_ENGINE_ERROR: 
'GENERIC_DB_ENGINE_ERROR'>, level=<ErrorLevel.ERROR: 'error'>, 
extra={'engine_name': 'PostgreSQL', 'issue_codes': [{'code': 1002, 'message': 
'Issue 1002 - The database returned an unexpected error.'}]})]
   ```
   
   The object `security_manager.current_user` is available when creating 
charts, but not in the SqlLab. 
   
   Looking at the source code of 
[BaseSecurityManager](https://github.com/dpgaspar/Flask-AppBuilder/blob/357c8100e96034c736860737a41c2004aa4e993b/flask_appbuilder/security/manager.py#L493)
   ```
   @property
       def current_user(self):
           if current_user.is_authenticated:
               return g.user
           elif current_user_jwt:
               return current_user_jwt
   ```
   it seems that `g` does not have a `user` property.
   
   Is it somehow expected that SqlLab behaves differently?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to