forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(database import): Gracefully handle error to get catalog schemas (a…
- Loading branch information
1 parent
e1f98e2
commit 21e794a
Showing
5 changed files
with
147 additions
and
109 deletions.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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 __future__ import annotations | ||
|
||
import logging | ||
|
||
from superset import security_manager | ||
from superset.databases.ssh_tunnel.models import SSHTunnel | ||
from superset.db_engine_specs.base import GenericDBException | ||
from superset.models.core import Database | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def add_permissions(database: Database, ssh_tunnel: SSHTunnel | None) -> None: | ||
""" | ||
Add DAR for catalogs and schemas. | ||
""" | ||
if database.db_engine_spec.supports_catalog: | ||
catalogs = database.get_all_catalog_names( | ||
cache=False, | ||
ssh_tunnel=ssh_tunnel, | ||
) | ||
|
||
for catalog in catalogs: | ||
security_manager.add_permission_view_menu( | ||
"catalog_access", | ||
security_manager.get_catalog_perm( | ||
database.database_name, | ||
catalog, | ||
), | ||
) | ||
else: | ||
catalogs = [None] | ||
|
||
for catalog in catalogs: | ||
try: | ||
for schema in database.get_all_schema_names( | ||
catalog=catalog, | ||
cache=False, | ||
ssh_tunnel=ssh_tunnel, | ||
): | ||
security_manager.add_permission_view_menu( | ||
"schema_access", | ||
security_manager.get_schema_perm( | ||
database.database_name, | ||
catalog, | ||
schema, | ||
), | ||
) | ||
except GenericDBException: # pylint: disable=broad-except | ||
logger.warning("Error processing catalog '%s'", catalog) | ||
continue |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# 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 pytest_mock import MockerFixture | ||
|
||
from superset.commands.database.utils import add_permissions | ||
|
||
|
||
def test_add_permissions(mocker: MockerFixture) -> None: | ||
""" | ||
Test adding permissions to a database when it's created. | ||
""" | ||
database = mocker.MagicMock() | ||
database.database_name = "my_db" | ||
database.db_engine_spec.supports_catalog = True | ||
database.get_all_catalog_names.return_value = ["catalog1", "catalog2"] | ||
database.get_all_schema_names.side_effect = [["schema1"], ["schema2"]] | ||
ssh_tunnel = mocker.MagicMock() | ||
add_permission_view_menu = mocker.patch( | ||
"superset.commands.database.importers.v1.utils.security_manager." | ||
"add_permission_view_menu" | ||
) | ||
|
||
add_permissions(database, ssh_tunnel) | ||
|
||
add_permission_view_menu.assert_has_calls( | ||
[ | ||
mocker.call("catalog_access", "[my_db].[catalog1]"), | ||
mocker.call("catalog_access", "[my_db].[catalog2]"), | ||
mocker.call("schema_access", "[my_db].[catalog1].[schema1]"), | ||
mocker.call("schema_access", "[my_db].[catalog2].[schema2]"), | ||
] | ||
) | ||
|
||
|
||
def test_add_permissions_handle_failures(mocker: MockerFixture) -> None: | ||
""" | ||
Test adding permissions to a database when it's created in case | ||
the request to get all schemas for one fo the catalogs fail. | ||
""" | ||
database = mocker.MagicMock() | ||
database.database_name = "my_db" | ||
database.db_engine_spec.supports_catalog = True | ||
database.get_all_catalog_names.return_value = ["catalog1", "catalog2", "catalog3"] | ||
database.get_all_schema_names.side_effect = [["schema1"], Exception, ["schema3"]] | ||
ssh_tunnel = mocker.MagicMock() | ||
add_permission_view_menu = mocker.patch( | ||
"superset.commands.database.importers.v1.utils.security_manager." | ||
"add_permission_view_menu" | ||
) | ||
|
||
add_permissions(database, ssh_tunnel) | ||
|
||
add_permission_view_menu.assert_has_calls( | ||
[ | ||
mocker.call("catalog_access", "[my_db].[catalog1]"), | ||
mocker.call("catalog_access", "[my_db].[catalog2]"), | ||
mocker.call("catalog_access", "[my_db].[catalog3]"), | ||
mocker.call("schema_access", "[my_db].[catalog1].[schema1]"), | ||
mocker.call("schema_access", "[my_db].[catalog3].[schema3]"), | ||
] | ||
) |