-
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
[SIP-29] Add support for row-level security #8699
Merged
+308
−6
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
ab6ae45
Support and apply filters.
altef 024912e
Added the UI for row level security, and moved it all under SQLA in o…
altef 4d6789f
Added a row level security filter documentation entry.
altef 4152dc2
Accidentally added two new lines to this file.
altef 13f1381
Blacked and iSorted, hopefully. Also, sometimes g.user may not be set.
altef 1100a60
Another isort, and handling g not having a user attribute another way.
altef 76a314a
Let's try this again #CI tests.
altef 86234fa
Adjusted import order for isort; I was sure I'd already done this..
altef 81c2bdb
Row level filters should be wrapped in parentheses in case one contai…
altef ff80add
Oops, did not think that would change Black's formatting.
altef aafce2f
Changes as per @mistercrunch.
altef fd0eaf6
RLS filters are now many-to-many with Roles.
altef 8b6ce72
Updated documentation to reflect RLS filters supporting multiple rows.
altef 5f69386
Let's see what happens when I set it to the previous revision ID
altef 032efcb
Merge branch 'master' into row-level-security
altef 2c252d9
Updated from upstream.
altef 7223da6
There was a pylint error.
altef 84a5009
Added RLS ids to the cache keys; modified documentation; added templa…
altef 7e0e3c8
Merge branch 'master' into row-level-security
altef 253e992
A new migration was merged in.
altef 1b3b8f6
Removed RLS cache key from query_object.
altef c0ac8e9
RLS added to the cache_key from query_context.
altef bdfaff6
Merge branch 'master' into row-level-security
altef 041038e
Changes as per @etr2460.
altef 8f0d0d1
Updating entry for RLS pull request.
altef b402fc2
Merge branch 'master' into row-level-security
altef e403e9b
Another migration to skip.
altef 9c71f05
Catchup.
altef 78ebc1c
Merge branch 'row-level-security' of https://github.com/altef/incubat…
altef 5a0257c
Changes as per @serenajiang.
altef 9d4bcad
Blacked.
altef 790f395
Blacked and added some attributes to check for.
altef 8adfc89
Changed to a manual query as per @mistercrunch.
altef 2b0ccfc
Blacked.
altef c68d6e6
Merge branch 'master' into row-level-security
altef b82acf6
Another migration in the meantime.
altef d94e6aa
Black wanted some whitespace changes.
altef f76e953
AttributeError: 'AnonymousUserMixin' object has no attribute 'id'.
altef 3d0822d
Oops, did hasattr backwards.
altef db9f20a
Merge branch 'master' into row-level-security
altef 524c109
Merge branch 'master' into row-level-security
altef a2e6b67
Changes as per @mistercrunch.
altef f581cf7
Doesn't look like text us required here anymore.
altef 085a501
Changes as per @dpgaspar
altef a625111
Two RLS tests.
altef a131a72
Row level security is now disabled by default via the feature flag EN…
altef 2ea460b
Merge branch 'master' into row-level-security
altef 1451c05
New head to revise.
altef 5eeb296
Changed the comment.
altef 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
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
61 changes: 61 additions & 0 deletions
61
superset/migrations/versions/0a6f12f60c73_add_role_level_security.py
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""add_role_level_security | ||
|
||
Revision ID: 0a6f12f60c73 | ||
Revises: 3325d4caccc8 | ||
Create Date: 2019-12-04 17:07:54.390805 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "0a6f12f60c73" | ||
down_revision = "3325d4caccc8" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"row_level_security_filters", | ||
sa.Column("created_on", sa.DateTime(), nullable=True), | ||
altef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sa.Column("changed_on", sa.DateTime(), nullable=True), | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("table_id", sa.Integer(), nullable=False), | ||
sa.Column("clause", sa.Text(), nullable=False), | ||
sa.Column("created_by_fk", sa.Integer(), nullable=True), | ||
sa.Column("changed_by_fk", sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(["changed_by_fk"], ["ab_user.id"]), | ||
sa.ForeignKeyConstraint(["created_by_fk"], ["ab_user.id"]), | ||
sa.ForeignKeyConstraint(["table_id"], ["tables.id"]), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_table( | ||
"rls_filter_roles", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("role_id", sa.Integer(), nullable=False), | ||
sa.Column("rls_filter_id", sa.Integer(), nullable=True), | ||
sa.ForeignKeyConstraint(["rls_filter_id"], ["row_level_security_filters.id"]), | ||
sa.ForeignKeyConstraint(["role_id"], ["ab_role.id"]), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("rls_filter_roles") | ||
op.drop_table("row_level_security_filters") |
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 |
---|---|---|
|
@@ -120,6 +120,7 @@ class SupersetSecurityManager(SecurityManager): | |
"RoleModelView", | ||
"LogModelView", | ||
"Security", | ||
"RowLevelSecurityFiltersModelView", | ||
} | USER_MODEL_VIEWS | ||
|
||
ALPHA_ONLY_VIEW_MENUS = {"Upload a CSV"} | ||
|
@@ -891,3 +892,48 @@ def assert_viz_permission(self, viz: "BaseViz") -> None: | |
""" | ||
|
||
self.assert_datasource_permission(viz.datasource) | ||
|
||
def get_rls_filters(self, table: "BaseDatasource"): | ||
""" | ||
Retrieves the appropriate row level security filters for the current user and the passed table. | ||
|
||
:param table: The table to check against | ||
:returns: A list of filters. | ||
""" | ||
if hasattr(g, "user") and hasattr(g.user, "id"): | ||
from superset import db | ||
from superset.connectors.sqla.models import ( | ||
RLSFilterRoles, | ||
RowLevelSecurityFilter, | ||
) | ||
|
||
user_roles = ( | ||
db.session.query(assoc_user_role.c.role_id) | ||
.filter(assoc_user_role.c.user_id == g.user.id) | ||
.subquery() | ||
) | ||
filter_roles = ( | ||
db.session.query(RLSFilterRoles.c.id) | ||
.filter(RLSFilterRoles.c.role_id.in_(user_roles)) | ||
.subquery() | ||
) | ||
query = ( | ||
db.session.query( | ||
RowLevelSecurityFilter.id, RowLevelSecurityFilter.clause | ||
) | ||
.filter(RowLevelSecurityFilter.table_id == table.id) | ||
.filter(RowLevelSecurityFilter.id.in_(filter_roles)) | ||
) | ||
return query.all() | ||
return [] | ||
|
||
def get_rls_ids(self, table: "BaseDatasource") -> List[int]: | ||
""" | ||
Retrieves the appropriate row level security filters IDs for the current user and the passed table. | ||
|
||
:param table: The table to check against | ||
:returns: A list of IDs. | ||
""" | ||
ids = [f.id for f in self.get_rls_filters(table)] | ||
ids.sort() # Combinations rather than permutations | ||
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. It seems these IDs are sorted to satisfy the cache consistency. Rather than storing these as an ordered list why not return these as a set? |
||
return ids |
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.
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.
Could we use an
f
string here as opposed to.format()
?