Search code examples
pythonapache-superset

What is the problem with this custom security manager for Apache Superset?


I'm running Apache Superset 1.4.0 on a Debian server, inside a Python virtual environment.

I'm trying to implement a custom security manager, but I'm stuck because Apache Superset is complaining about imports.

My superset and superset_config.py are in /home/administrator/venv-superset/bin/ directory.

I've created a new custom_security_manager.py file in the same directory, that is /home/administrator/venv-superset/bin, with the following contents:

from superset.security import SupersetSecurityManager
from flask_appbuilder.security.views import UserDBModelView,AuthDBView

class CustomAuthDBView(AuthDBView):

    @expose('/login/', methods=['GET', 'POST'])
    def login(self):
        token = request.args.get('token')
        if token == 'SOME_TOKEN_VALUE':
            login_user('some_user_name', remember=False)
        else:
            flash('Unable to auto login', 'warning')
            return super(CustomAuthDBView,self).login()

class CustomSecurityManager(SupersetSecurityManager):
    authdbview = CustomAuthDBView
    def __init__(self, appbuilder):
        super(CustomSecurityManager, self).__init__(appbuilder)

My superset_config.py has the following contents:

FEATURE_FLAGS = {
    "ENABLE_TEMPLATE_PROCESSING": True,
}

from custom_security_manager import CustomSecurityManager
CUSTOM_SECURITY_MANAGER = CustomSecurityManager

When I start the Superset service and then check the logs, I see the following:

Jan 28 13:46:46 dashboard-server python[17190]: Found but failed to import local superset_config
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/superset/config.py", line 1298, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:     import superset_config  # pylint: disable=import-error
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/superset_config.py", line 5, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/custom_security_manager.py", line 4, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/custom_security_manager.py", line 7, in CustomAuthDBView
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/superset", line 11, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:     load_entry_point('apache-superset==1.4.0', 'console_scripts', 'superset')()
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/pkg_resources/__init__.py", line 2443, in load
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/pkg_resources/__init__.py", line 2449, in resolve
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/superset/cli.py", line 39, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:     from superset import app, appbuilder, config, security_manager
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/lib/python3.9/site-packages/superset/config.py", line 1298, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:     import superset_config  # pylint: disable=import-error
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/superset_config.py", line 5, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/custom_security_manager.py", line 4, in <module>
Jan 28 13:46:46 dashboard-server python[17190]:   File "/home/administrator/venv-superset/bin/custom_security_manager.py", line 7, in CustomAuthDBView

If I put custom_security_manager.py in the wrong directory, where should I put it?

Or is it about the import lines inside custom_security_manager.py?


Solution

  • Apparently I was missing some imports in my custom_security_manager.py.

    When I modified it as the following, Apache Superset didn't complain anymore:

    from flask import redirect, g, flash, request
    from superset.security import SupersetSecurityManager
    from flask_appbuilder.security.views import UserDBModelView,AuthDBView
    from flask_appbuilder.security.views import expose
    from flask_login import login_user, logout_user
    
    class CustomAuthDBView(AuthDBView):
    
        @expose('/login/', methods=['GET', 'POST'])
        def login(self):
            token = request.args.get('token')
            if token == 'SOME_TOKEN_VALUE':
                login_user('some_user_name', remember=False)                
            else:
                flash('Unable to auto login', 'warning')
                return super(CustomAuthDBView,self).login()
    
    class CustomSecurityManager(SupersetSecurityManager):
        authdbview = CustomAuthDBView
        def __init__(self, appbuilder):
            super(CustomSecurityManager, self).__init__(appbuilder)