-
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
feat: RLS for SQL Lab #19999
Merged
Merged
feat: RLS for SQL Lab #19999
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c84c99f
feat: RLS for SQL Lab
betodealmeida ab419a1
Small fixes
betodealmeida b2b4612
Pass username to security manager
betodealmeida 12737e2
Update docstrings
betodealmeida 394a814
Add tests
betodealmeida 14848ac
Remove type from docstring
betodealmeida 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1135,72 +1135,85 @@ def get_guest_rls_filters( | |
] | ||
return [] | ||
|
||
def get_rls_filters(self, table: "BaseDatasource") -> List[SqlaQuery]: | ||
def get_rls_filters( | ||
self, | ||
table: "BaseDatasource", | ||
username: Optional[str] = None, | ||
) -> List[SqlaQuery]: | ||
""" | ||
Retrieves the appropriate row level security filters for the current user and | ||
the passed table. | ||
|
||
:param table: The table to check against | ||
:param BaseDatasource table: The table to check against. | ||
:param Optional[str] username: Optional username if there's no user in the Flask | ||
global namespace. | ||
:returns: A list of filters | ||
""" | ||
if hasattr(g, "user"): | ||
# pylint: disable=import-outside-toplevel | ||
from superset.connectors.sqla.models import ( | ||
RLSFilterRoles, | ||
RLSFilterTables, | ||
RowLevelSecurityFilter, | ||
) | ||
user = g.user | ||
elif username: | ||
user = self.find_user(username=username) | ||
else: | ||
return [] | ||
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 if flip, less indentation |
||
|
||
user_roles = [role.id for role in self.get_user_roles()] | ||
regular_filter_roles = ( | ||
self.get_session.query(RLSFilterRoles.c.rls_filter_id) | ||
.join(RowLevelSecurityFilter) | ||
.filter( | ||
RowLevelSecurityFilter.filter_type | ||
== RowLevelSecurityFilterType.REGULAR | ||
) | ||
.filter(RLSFilterRoles.c.role_id.in_(user_roles)) | ||
.subquery() | ||
# pylint: disable=import-outside-toplevel | ||
from superset.connectors.sqla.models import ( | ||
RLSFilterRoles, | ||
RLSFilterTables, | ||
RowLevelSecurityFilter, | ||
) | ||
|
||
user_roles = [role.id for role in self.get_user_roles(user)] | ||
regular_filter_roles = ( | ||
self.get_session() | ||
.query(RLSFilterRoles.c.rls_filter_id) | ||
.join(RowLevelSecurityFilter) | ||
.filter( | ||
RowLevelSecurityFilter.filter_type == RowLevelSecurityFilterType.REGULAR | ||
) | ||
base_filter_roles = ( | ||
self.get_session.query(RLSFilterRoles.c.rls_filter_id) | ||
.join(RowLevelSecurityFilter) | ||
.filter( | ||
RowLevelSecurityFilter.filter_type | ||
== RowLevelSecurityFilterType.BASE | ||
) | ||
.filter(RLSFilterRoles.c.role_id.in_(user_roles)) | ||
.subquery() | ||
.filter(RLSFilterRoles.c.role_id.in_(user_roles)) | ||
.subquery() | ||
) | ||
base_filter_roles = ( | ||
self.get_session() | ||
.query(RLSFilterRoles.c.rls_filter_id) | ||
.join(RowLevelSecurityFilter) | ||
.filter( | ||
RowLevelSecurityFilter.filter_type == RowLevelSecurityFilterType.BASE | ||
) | ||
filter_tables = ( | ||
self.get_session.query(RLSFilterTables.c.rls_filter_id) | ||
.filter(RLSFilterTables.c.table_id == table.id) | ||
.subquery() | ||
.filter(RLSFilterRoles.c.role_id.in_(user_roles)) | ||
.subquery() | ||
) | ||
filter_tables = ( | ||
self.get_session() | ||
.query(RLSFilterTables.c.rls_filter_id) | ||
.filter(RLSFilterTables.c.table_id == table.id) | ||
.subquery() | ||
) | ||
query = ( | ||
self.get_session() | ||
.query( | ||
RowLevelSecurityFilter.id, | ||
RowLevelSecurityFilter.group_key, | ||
RowLevelSecurityFilter.clause, | ||
) | ||
query = ( | ||
self.get_session.query( | ||
RowLevelSecurityFilter.id, | ||
RowLevelSecurityFilter.group_key, | ||
RowLevelSecurityFilter.clause, | ||
) | ||
.filter(RowLevelSecurityFilter.id.in_(filter_tables)) | ||
.filter( | ||
or_( | ||
and_( | ||
RowLevelSecurityFilter.filter_type | ||
== RowLevelSecurityFilterType.REGULAR, | ||
RowLevelSecurityFilter.id.in_(regular_filter_roles), | ||
), | ||
and_( | ||
RowLevelSecurityFilter.filter_type | ||
== RowLevelSecurityFilterType.BASE, | ||
RowLevelSecurityFilter.id.notin_(base_filter_roles), | ||
), | ||
) | ||
.filter(RowLevelSecurityFilter.id.in_(filter_tables)) | ||
.filter( | ||
or_( | ||
and_( | ||
RowLevelSecurityFilter.filter_type | ||
== RowLevelSecurityFilterType.REGULAR, | ||
RowLevelSecurityFilter.id.in_(regular_filter_roles), | ||
), | ||
and_( | ||
RowLevelSecurityFilter.filter_type | ||
== RowLevelSecurityFilterType.BASE, | ||
RowLevelSecurityFilter.id.notin_(base_filter_roles), | ||
), | ||
) | ||
) | ||
return query.all() | ||
return [] | ||
) | ||
return query.all() | ||
|
||
def get_rls_ids(self, table: "BaseDatasource") -> List[int]: | ||
""" | ||
|
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 |
---|---|---|
|
@@ -31,7 +31,13 @@ | |
from flask_babel import gettext as __ | ||
from sqlalchemy.orm import Session | ||
|
||
from superset import app, results_backend, results_backend_use_msgpack, security_manager | ||
from superset import ( | ||
app, | ||
is_feature_enabled, | ||
results_backend, | ||
results_backend_use_msgpack, | ||
security_manager, | ||
) | ||
from superset.common.db_query_status import QueryStatus | ||
from superset.dataframe import df_to_records | ||
from superset.db_engine_specs import BaseEngineSpec | ||
|
@@ -41,7 +47,7 @@ | |
from superset.models.core import Database | ||
from superset.models.sql_lab import Query | ||
from superset.result_set import SupersetResultSet | ||
from superset.sql_parse import CtasMethod, ParsedQuery | ||
from superset.sql_parse import CtasMethod, insert_rls, ParsedQuery | ||
from superset.sqllab.limiting_factor import LimitingFactor | ||
from superset.utils.celery import session_scope | ||
from superset.utils.core import json_iso_dttm_ser, QuerySource, zlib_compress | ||
|
@@ -176,7 +182,7 @@ def get_sql_results( # pylint: disable=too-many-arguments | |
return handle_query_error(ex, query, session) | ||
|
||
|
||
def execute_sql_statement( # pylint: disable=too-many-arguments,too-many-locals | ||
def execute_sql_statement( # pylint: disable=too-many-arguments,too-many-locals,too-many-statements | ||
sql_statement: str, | ||
query: Query, | ||
user_name: Optional[str], | ||
|
@@ -188,7 +194,21 @@ def execute_sql_statement( # pylint: disable=too-many-arguments,too-many-locals | |
"""Executes a single SQL statement""" | ||
database: Database = query.database | ||
db_engine_spec = database.db_engine_spec | ||
|
||
parsed_query = ParsedQuery(sql_statement) | ||
if is_feature_enabled("RLS_IN_SQLLAB"): | ||
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 is the core of the PR, the rest is just passing username around. |
||
# Insert any applicable RLS predicates | ||
parsed_query = ParsedQuery( | ||
str( | ||
insert_rls( | ||
parsed_query._parsed[0], # pylint: disable=protected-access | ||
database.id, | ||
query.schema, | ||
username=user_name, | ||
) | ||
) | ||
) | ||
|
||
sql = parsed_query.stripped() | ||
# This is a test to see if the query is being | ||
# limited by either the dropdown or the sql. | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
nit:
username
is missing in the docstring here and insecurity_manager.get_rls_filters
. The main description says it retrieves rls filters for "the current user" - we should perhaps remove that text from both docstrings and state that ifusername
is missing, it will fall back to the current user.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.
Ah, good catch and good point, @villebro! Let me fix the docstrings and add tests.