-
Notifications
You must be signed in to change notification settings - Fork 14k
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
chore: Refactor dashboard security access #24804
Merged
john-bodley
merged 1 commit into
apache:master
from
john-bodley:john-bodley--integrate-raise-for-dashboard-access
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,16 +410,30 @@ def can_access_dashboard(self, dashboard: "Dashboard") -> bool: | |
:returns: Whether the user can access the dashboard | ||
""" | ||
|
||
# pylint: disable=import-outside-toplevel | ||
from superset.dashboards.commands.exceptions import DashboardAccessDeniedError | ||
|
||
try: | ||
self.raise_for_dashboard_access(dashboard) | ||
except DashboardAccessDeniedError: | ||
self.raise_for_access(dashboard=dashboard) | ||
except SupersetSecurityException: | ||
return False | ||
|
||
return True | ||
|
||
def get_dashboard_access_error_object( # pylint: disable=invalid-name | ||
self, | ||
dashboard: "Dashboard", # pylint: disable=unused-argument | ||
) -> SupersetError: | ||
""" | ||
Return the error object for the denied Superset dashboard. | ||
|
||
:param dashboard: The denied Superset dashboard | ||
:returns: The error object | ||
""" | ||
|
||
return SupersetError( | ||
error_type=SupersetErrorType.DASHBOARD_SECURITY_ACCESS_ERROR, | ||
message="You don't have access to this dashboard.", | ||
level=ErrorLevel.ERROR, | ||
) | ||
|
||
@staticmethod | ||
def get_datasource_access_error_msg(datasource: "BaseDatasource") -> str: | ||
""" | ||
|
@@ -1757,8 +1771,9 @@ def get_exclude_users_from_lists() -> list[str]: | |
return [] | ||
|
||
def raise_for_access( | ||
# pylint: disable=too-many-arguments, too-many-locals | ||
# pylint: disable=too-many-arguments,too-many-branches,too-many-locals | ||
self, | ||
dashboard: Optional["Dashboard"] = None, | ||
database: Optional["Database"] = None, | ||
datasource: Optional["BaseDatasource"] = None, | ||
query: Optional["Query"] = None, | ||
|
@@ -1779,6 +1794,7 @@ def raise_for_access( | |
""" | ||
|
||
# pylint: disable=import-outside-toplevel | ||
from superset import is_feature_enabled | ||
from superset.connectors.sqla.models import SqlaTable | ||
from superset.daos.dashboard import DashboardDAO | ||
from superset.sql_parse import Table | ||
|
@@ -1852,6 +1868,45 @@ def raise_for_access( | |
self.get_datasource_access_error_object(datasource) | ||
) | ||
|
||
if dashboard: | ||
if self.is_guest_user(): | ||
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. Identical logic which was previously in |
||
# Guest user is currently used for embedded dashboards only. If the guest | ||
# user doesn't have access to the dashboard, ignore all other checks. | ||
if self.has_guest_access(dashboard): | ||
return | ||
raise SupersetSecurityException( | ||
self.get_dashboard_access_error_object(dashboard) | ||
) | ||
|
||
if self.is_admin() or self.is_owner(dashboard): | ||
return | ||
|
||
# RBAC and legacy (datasource inferred) access controls. | ||
if is_feature_enabled("DASHBOARD_RBAC") and dashboard.roles: | ||
if dashboard.published and {role.id for role in dashboard.roles} & { | ||
role.id for role in self.get_user_roles() | ||
}: | ||
return | ||
elif ( | ||
# To understand why we rely on status and give access to draft dashboards | ||
# without roles take a look at: | ||
# | ||
# - https://github.com/apache/superset/pull/24350#discussion_r1225061550 | ||
# - https://github.com/apache/superset/pull/17511#issuecomment-975870169 | ||
# | ||
not dashboard.published | ||
or not dashboard.datasources | ||
or any( | ||
self.can_access_datasource(datasource) | ||
for datasource in dashboard.datasources | ||
) | ||
): | ||
return | ||
|
||
raise SupersetSecurityException( | ||
self.get_dashboard_access_error_object(dashboard) | ||
) | ||
|
||
def get_user_by_username( | ||
self, username: str, session: Session = None | ||
) -> Optional[User]: | ||
|
@@ -1987,55 +2042,6 @@ def get_rls_cache_key(self, datasource: "BaseDatasource") -> list[str]: | |
guest_rls = self.get_guest_rls_filters_str(datasource) | ||
return guest_rls + rls_str | ||
|
||
def raise_for_dashboard_access(self, dashboard: "Dashboard") -> None: | ||
""" | ||
Raise an exception if the user cannot access the dashboard. | ||
|
||
This does not check for the required role/permission pairs, it only concerns | ||
itself with entity relationships. | ||
|
||
:param dashboard: Dashboard the user wants access to | ||
:raises DashboardAccessDeniedError: If the user cannot access the resource | ||
""" | ||
|
||
# pylint: disable=import-outside-toplevel | ||
from superset import is_feature_enabled | ||
from superset.dashboards.commands.exceptions import DashboardAccessDeniedError | ||
|
||
if self.is_guest_user(): | ||
# Guest user is currently used for embedded dashboards only. If the guest user | ||
# doesn't have access to the dashboard, ignore all other checks. | ||
if self.has_guest_access(dashboard): | ||
return | ||
raise DashboardAccessDeniedError() | ||
|
||
if self.is_admin() or self.is_owner(dashboard): | ||
return | ||
|
||
# RBAC and legacy (datasource inferred) access controls. | ||
if is_feature_enabled("DASHBOARD_RBAC") and dashboard.roles: | ||
if dashboard.published and {role.id for role in dashboard.roles} & { | ||
role.id for role in self.get_user_roles() | ||
}: | ||
return | ||
elif ( | ||
# To understand why we rely on status and give access to draft dashboards | ||
# without roles take a look at: | ||
# | ||
# - https://github.com/apache/superset/pull/24350#discussion_r1225061550 | ||
# - https://github.com/apache/superset/pull/17511#issuecomment-975870169 | ||
# | ||
not dashboard.published | ||
or not dashboard.datasources | ||
or any( | ||
self.can_access_datasource(datasource) | ||
for datasource in dashboard.datasources | ||
) | ||
): | ||
return | ||
|
||
raise DashboardAccessDeniedError() | ||
|
||
@staticmethod | ||
def _get_current_epoch_time() -> float: | ||
"""This is used so the tests can mock time""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These docs no longer exist.