You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jl...@apache.org on 2026/09/07 04:51:24 UTC

(superset) 04/06: fix(database): sync schema permissions when a bundle references an existing database

This is an automated email from the ASF dual-hosted git repository.

sadpandajoe pushed a commit to branch fix-dar-import-schema-permissions
in repository https://gitbox.apache.org/repos/asf/superset.git

commit d58726db001f98e6049b5e808d19f946efa1645a
Author: sadpandajoe <jc...@gmail.com>
AuthorDate: Sat Sep 5 21:43:16 2026 +0000

    fix(database): sync schema permissions when a bundle references an existing database
    
    Chart, dataset, and saved-query bundle imports call import_database(config,
    overwrite=False) unconditionally to resolve a bundle's parent database. The
    dashboard importer reaches the same branch too: it passes
    overwrite=(overwrite and overwrite_all), which is False whenever either
    flag is -- the default. In all four cases, when the referenced database
    already exists in the target (e.g. it was created by a prior import), that
    branch returned the existing database without ever calling
    add_permissions(), so a schema added to the live connection since the
    database was first imported was never synced through this path either --
    independent of the transient-failure gap fixed separately in
    add_permissions() itself.
    
    Call add_permissions(existing) before returning, gated behind the same
    can_write capability check this function already requires for every other
    permission-view creation, so this doesn't introduce a new way to trigger it
    without that capability.
---
 superset/commands/database/importers/v1/utils.py   | 12 ++++
 .../databases/commands/importers/v1/import_test.py | 82 ++++++++++++++++++++++
 2 files changed, 94 insertions(+)

diff --git a/superset/commands/database/importers/v1/utils.py b/superset/commands/database/importers/v1/utils.py
index 973aaad8b80..f7b6acdb1b3 100644
--- a/superset/commands/database/importers/v1/utils.py
+++ b/superset/commands/database/importers/v1/utils.py
@@ -118,6 +118,18 @@ def import_database(  # noqa: C901
     existing = db.session.query(Database).filter_by(uuid=config["uuid"]).first()
     if existing:
         if not overwrite or not can_write:
+            if can_write:
+                # Chart/dataset/saved-query/dashboard bundles that reference
+                # an already-imported database reach this branch; without
+                # this, a schema added to the live connection since the
+                # database was first imported would never get a first-time
+                # grant through this path either. ``add_permissions()`` does
+                # a live, uncached metadata scan, so this can be slow for
+                # cross-catalog-enabled engines -- see its own comment.
+                try:
+                    add_permissions(existing)
+                except (SupersetDBAPIConnectionError, OAuth2RedirectError) as ex:
+                    logger.warning(ex.message)
             return existing
         config["id"] = existing.id
         # Stored secrets must not be rebound to a different endpoint: without
diff --git a/tests/unit_tests/databases/commands/importers/v1/import_test.py b/tests/unit_tests/databases/commands/importers/v1/import_test.py
index 2ccf0cc1fb2..ad49a2dff23 100644
--- a/tests/unit_tests/databases/commands/importers/v1/import_test.py
+++ b/tests/unit_tests/databases/commands/importers/v1/import_test.py
@@ -344,6 +344,88 @@ def test_import_database_with_masked_encrypted_extra_existing_db(
     assert encrypted["credentials_info"]["private_key"] != PASSWORD_MASK
 
 
+def test_import_database_existing_no_overwrite_backfills_permissions(
+    mocker: MockerFixture,
+    session: Session,
+) -> None:
+    """
+    ``import_database(config, overwrite=False)`` is called unconditionally by
+    the chart, dataset, and saved-query bundle importers
+    (``superset/commands/chart/importers/v1/__init__.py``,
+    ``superset/commands/dataset/importers/v1/__init__.py``, and
+    ``superset/commands/query/importers/v1/__init__.py``), and by the
+    dashboard importer (``superset/commands/dashboard/importers/v1/__init__.py``)
+    whenever both ``overwrite``/``overwrite_all`` aren't simultaneously
+    ``True`` -- the common case -- whenever the bundle references a
+    database, including one that already exists in the target, eg because a
+    prior import already created it. Before this fix, the early return for
+    an existing, non-overwritten database skipped ``add_permissions()``
+    entirely, so a schema added to the live connection since that database
+    was first imported would never get a first-time grant through this path
+    either.
+    """
+    from superset import security_manager
+    from superset.commands.database.importers.v1.utils import import_database
+    from superset.models.core import Database
+    from tests.integration_tests.fixtures.importexport import database_config
+
+    mocker.patch.object(security_manager, "can_access", return_value=True)
+    mock_add_permissions = mocker.patch(
+        "superset.commands.database.importers.v1.utils.add_permissions"
+    )
+
+    engine = db.session.get_bind()
+    Database.metadata.create_all(engine)  # pylint: disable=no-member
+
+    config = copy.deepcopy(database_config)
+    existing = import_database(config)
+    mock_add_permissions.reset_mock()
+
+    again = copy.deepcopy(database_config)
+    result = import_database(again, overwrite=False)
+
+    assert result.id == existing.id
+    mock_add_permissions.assert_called_once_with(existing)
+
+
+def test_import_database_existing_no_overwrite_no_permission_skips_backfill(
+    mocker: MockerFixture,
+    session: Session,
+) -> None:
+    """
+    The permission backfill added for the existing/no-overwrite branch must
+    stay behind the same ``can_write`` gate as every other permission-view
+    creation in ``import_database()`` -- a principal without database-write
+    access must not trigger it just by importing a
+    chart/dataset/saved-query/dashboard bundle that happens to reference an
+    existing database.
+    """
+    from superset import security_manager
+    from superset.commands.database.importers.v1.utils import import_database
+    from superset.models.core import Database
+    from tests.integration_tests.fixtures.importexport import database_config
+
+    mocker.patch.object(security_manager, "can_access", return_value=True)
+    mock_add_permissions = mocker.patch(
+        "superset.commands.database.importers.v1.utils.add_permissions"
+    )
+
+    engine = db.session.get_bind()
+    Database.metadata.create_all(engine)  # pylint: disable=no-member
+
+    config = copy.deepcopy(database_config)
+    existing = import_database(config)
+    mock_add_permissions.reset_mock()
+
+    mocker.patch.object(security_manager, "can_access", return_value=False)
+
+    again = copy.deepcopy(database_config)
+    result = import_database(again, overwrite=False)
+
+    assert result.id == existing.id
+    mock_add_permissions.assert_not_called()
+
+
 def test_import_database_oauth2_redirect_is_nonfatal(
     mocker: MockerFixture,
     session: Session,