-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
[security] Refactor security code into SupersetSecurityManager #4565
Changes from 5 commits
1cd6a9e
434a146
f3d9fd7
7c32934
a385444
4124ac0
c766500
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,9 @@ | |
from flask_wtf.csrf import CSRFProtect | ||
from werkzeug.contrib.fixers import ProxyFix | ||
|
||
from superset import config, utils | ||
from superset.connectors.connector_registry import ConnectorRegistry | ||
from superset import utils, config # noqa | ||
from superset.security import SupersetSecurityManager | ||
|
||
APP_DIR = os.path.dirname(__file__) | ||
CONFIG_MODULE = os.environ.get('SUPERSET_CONFIG', 'superset.config') | ||
|
@@ -149,12 +150,16 @@ def index(self): | |
return redirect('/superset/welcome') | ||
|
||
|
||
custom_sm = app.config.get('CUSTOM_SECURITY_MANAGER') | ||
if not custom_sm: | ||
custom_sm = SupersetSecurityManager | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should absolutely raise with an explicit message with perhaps a link to the UPDATE notes |
||
appbuilder = AppBuilder( | ||
app, | ||
db.session, | ||
base_template='superset/base.html', | ||
indexview=MyIndexView, | ||
security_manager_class=app.config.get('CUSTOM_SECURITY_MANAGER'), | ||
security_manager_class=custom_sm, | ||
update_perms=utils.get_update_perms_flag(), | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
from pathlib2 import Path | ||
import yaml | ||
|
||
from superset import app, db, dict_import_export_util, security, utils | ||
from superset import app, data, db, dict_import_export_util, sm, utils | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be out-of-scope for this PR, but it would be nice to always be explicit about It may be reasonable to take it on as part of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t mind the shortened sm (inline with db and other variables) as it’s used frequently and is considerably shorter than security_manager (2 characters vs. 16). |
||
|
||
config = app.config | ||
celery_app = utils.get_celery_app(config) | ||
|
@@ -28,7 +28,8 @@ | |
@manager.command | ||
def init(): | ||
"""Inits the Superset application""" | ||
security.sync_role_definitions() | ||
utils.get_or_create_main_db() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this now needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously it was called inside of sync_role_definitions and that led to weird cyclical dependencies after moving the method to utils.py. |
||
sm.sync_role_definitions() | ||
|
||
|
||
@manager.option( | ||
|
@@ -108,7 +109,6 @@ def version(verbose): | |
help='Load additional test data') | ||
def load_examples(load_test_data): | ||
"""Loads a set of Slices and Dashboards and a supporting dataset """ | ||
from superset import data | ||
print('Loading examples into {}'.format(db)) | ||
|
||
data.load_css_templates() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
from flask_babel import lazy_gettext as _ | ||
from past.builtins import basestring | ||
|
||
from superset import appbuilder, db, security, sm, utils | ||
from superset import appbuilder, db, sm, utils | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Having both |
||
from superset.connectors.base.views import DatasourceModelView | ||
from superset.utils import has_access | ||
from superset.views.base import ( | ||
|
@@ -144,11 +144,11 @@ class SqlMetricInlineView(CompactCRUDMixin, SupersetModelView): # noqa | |
|
||
def post_add(self, metric): | ||
if metric.is_restricted: | ||
security.merge_perm(sm, 'metric_access', metric.get_perm()) | ||
sm.merge_perm('metric_access', metric.get_perm()) | ||
|
||
def post_update(self, metric): | ||
if metric.is_restricted: | ||
security.merge_perm(sm, 'metric_access', metric.get_perm()) | ||
sm.merge_perm('metric_access', metric.get_perm()) | ||
|
||
|
||
appbuilder.add_view_no_menu(SqlMetricInlineView) | ||
|
@@ -253,9 +253,9 @@ def pre_add(self, table): | |
|
||
def post_add(self, table, flash_message=True): | ||
table.fetch_metadata() | ||
security.merge_perm(sm, 'datasource_access', table.get_perm()) | ||
sm.merge_perm('datasource_access', table.get_perm()) | ||
if table.schema: | ||
security.merge_perm(sm, 'schema_access', table.schema_perm) | ||
sm.merge_perm('schema_access', table.schema_perm) | ||
|
||
if flash_message: | ||
flash(_( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be shortened to be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found out that this only works when the variable doesn't exist. In this case, it is set to be None and so it returns None.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then you could
sm = app.config.get('CUSTOM_SECURITY_MANAGER') or SupersetSecurityManager
:)