-
Notifications
You must be signed in to change notification settings - Fork 17.2k
fix(rbac): show objects accessible by database access perm #23118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5725506
fix(rbac): show objects accessible by database access perm
villebro 49f14bc
add test
villebro 55ceba8
DRY
villebro 8b005b4
fix test
villebro dd1491a
cleanup after dupe test
villebro 3864a90
skip sqlite test
villebro a9b32fb
improve matching
villebro 85c1924
remove extra base filter
villebro 12a7bc0
use named group in regex
villebro a5abc24
add UPDATING entry
villebro 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| # 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. | ||
| from typing import Any, Type | ||
|
|
||
| from flask_appbuilder import Model | ||
| from sqlalchemy import or_ | ||
| from sqlalchemy.sql.elements import BooleanClauseList | ||
|
|
||
|
|
||
| def get_dataset_access_filters( | ||
| base_model: Type[Model], | ||
| *args: Any, | ||
| ) -> BooleanClauseList: | ||
| # pylint: disable=import-outside-toplevel | ||
| from superset import security_manager | ||
| from superset.connectors.sqla.models import Database | ||
|
|
||
| database_ids = security_manager.get_accessible_databases() | ||
| perms = security_manager.user_view_menu_names("datasource_access") | ||
| schema_perms = security_manager.user_view_menu_names("schema_access") | ||
|
|
||
| return or_( | ||
| Database.id.in_(database_ids), | ||
| base_model.perm.in_(perms), | ||
| base_model.schema_perm.in_(schema_perms), | ||
| *args, | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -239,28 +239,47 @@ def test_get_dataset_list_gamma(self): | |
| response = json.loads(rv.data.decode("utf-8")) | ||
| assert response["result"] == [] | ||
|
|
||
| def test_get_dataset_list_gamma_owned(self): | ||
| def test_get_dataset_list_gamma_has_database_access(self): | ||
| """ | ||
| Dataset API: Test get dataset list owned by gamma | ||
| Dataset API: Test get dataset list with database access | ||
| """ | ||
| if backend() == "sqlite": | ||
| return | ||
|
|
||
| self.login(username="gamma") | ||
|
|
||
| # create new dataset | ||
| main_db = get_main_database() | ||
| owned_dataset = self.insert_dataset( | ||
| "ab_user", [self.get_user("gamma").id], main_db | ||
| dataset = self.insert_dataset("ab_user", [], main_db) | ||
|
|
||
| # make sure dataset is not visible due to missing perms | ||
| uri = "api/v1/dataset/" | ||
| rv = self.get_assert_metric(uri, "get_list") | ||
| assert rv.status_code == 200 | ||
| response = json.loads(rv.data.decode("utf-8")) | ||
|
|
||
| assert response["count"] == 0 | ||
|
|
||
| # give database access to main db | ||
| main_db_pvm = security_manager.find_permission_view_menu( | ||
| "database_access", main_db.perm | ||
| ) | ||
| gamma_role = security_manager.find_role("Gamma") | ||
| gamma_role.permissions.append(main_db_pvm) | ||
| db.session.commit() | ||
|
|
||
| self.login(username="gamma") | ||
| # make sure dataset is now visible | ||
| uri = "api/v1/dataset/" | ||
| rv = self.get_assert_metric(uri, "get_list") | ||
| assert rv.status_code == 200 | ||
| response = json.loads(rv.data.decode("utf-8")) | ||
|
|
||
| assert response["count"] == 1 | ||
| assert response["result"][0]["table_name"] == "ab_user" | ||
| tables = {tbl["table_name"] for tbl in response["result"]} | ||
| assert tables == {"ab_user"} | ||
|
|
||
| db.session.delete(owned_dataset) | ||
| # revert gamma permission | ||
| gamma_role.permissions.remove(main_db_pvm) | ||
| db.session.delete(dataset) | ||
| db.session.commit() | ||
|
|
||
| def test_get_dataset_related_database_gamma(self): | ||
|
|
@@ -2255,6 +2274,8 @@ def test_duplicate_virtual_dataset(self): | |
| assert len(new_dataset.columns) == 2 | ||
| assert new_dataset.columns[0].column_name == "id" | ||
| assert new_dataset.columns[1].column_name == "name" | ||
| db.session.delete(new_dataset) | ||
| db.session.commit() | ||
|
Comment on lines
2277
to
2278
Member
Author
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 test didn't clean up after itself, causing the updated test to fail, hence the added cleanup here |
||
|
|
||
| @pytest.mark.usefixtures("create_datasets") | ||
| def test_duplicate_physical_dataset(self): | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.