You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by am...@apache.org on 2026/10/20 15:23:06 UTC
(superset) 08/08: feat(extensions): give Admin the permissions extension pages declare
This is an automated email from the ASF dual-hosted git repository.
amaannawab923 pushed a commit to branch feat/extension-pages
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 21f64e5b88195b61ba8388ebe6ca8f7fd148f525
Author: amaannawab923 <am...@gmail.com>
AuthorDate: Tue Oct 20 20:45:32 2026 +0530
feat(extensions): give Admin the permissions extension pages declare
An administrator who installed an extension could not open its pages until
they granted themselves access to their own deployment. The permission was
created when the extension loaded, held by nobody.
Granting it to Admin there is not a policy this invents.
`sync_role_definitions` builds the Admin role from every permission that is
not one of the four object-level ones, so `superset init` would hand Admin
this permission anyway -- just not until someone next ran init, which is
not a step installing an extension otherwise requires.
Re-checked on every boot rather than only on creation, so a role that lost
the grant repairs itself at the next start. A boot where Admin already
holds it writes nothing. The role name comes from AUTH_ROLE_ADMIN, since a
deployment may have renamed it and granting a hardcoded "Admin" would
write to a role nobody holds. A deployment with no such role logs and
carries on.
Pages naming one of the four object-level permissions -- database_access,
catalog_access, schema_access, datasource_access -- are now refused at
registration. Those names are exactly what Superset excludes when deciding
the Admin role, so such a page would have its grant stripped the next time
`superset init` ran, leaving a page nobody could open and nothing in the
logs to explain it. Unlike a route, that set is small, fixed and owned by
Superset's own security model, so refusing names from it is a rule that
stays true. The guard reads the set from the security manager rather than
copying it.
---
superset/extensions/view.py | 98 +++++++++++++++++---
tests/unit_tests/extensions/test_pages_view.py | 122 +++++++++++++++++++++++--
2 files changed, 200 insertions(+), 20 deletions(-)
diff --git a/superset/extensions/view.py b/superset/extensions/view.py
index 456c7f54424..7e14b4fe4dc 100644
--- a/superset/extensions/view.py
+++ b/superset/extensions/view.py
@@ -17,7 +17,7 @@
import logging
from typing import Callable
-from flask import abort, Flask, g, render_template
+from flask import abort, current_app, Flask, g, render_template
from flask_appbuilder import expose
from flask_appbuilder.security.decorators import has_access, permission_name
from superset_core.extensions.types import Manifest, Page
@@ -157,6 +157,31 @@ def _first_claimant(app: Flask, path: str) -> str | None:
return None
+def _permission_problem(permission: str | None) -> str | None:
+ """Why this permission cannot be used for a page, or None.
+
+ Superset reserves four permission names for object-level access, and
+ decides what the Admin role holds by excluding exactly those. A page
+ naming one would have its Admin grant stripped the next time
+ ``superset init`` ran, leaving a page nobody could open and nothing in
+ the logs to say why.
+
+ Unlike a route, this set is small, fixed and owned by Superset's own
+ security model, so refusing names from it is a rule that stays true.
+ """
+ if permission is None:
+ return None
+
+ from superset.security.manager import SupersetSecurityManager
+
+ if permission in SupersetSecurityManager.OBJECT_SPEC_PERMISSIONS:
+ return (
+ f"'{permission}' is reserved by Superset for object-level access "
+ f"and cannot be used for a page"
+ )
+ return None
+
+
def register_extension_pages(app: Flask, manifest: Manifest) -> None:
"""Register one URL rule per page an extension declares.
@@ -178,6 +203,17 @@ def register_extension_pages(app: Flask, manifest: Manifest) -> None:
should not stop the server from starting.
"""
for page in manifest.contributions.pages:
+ if problem := _permission_problem(page.permission):
+ logger.error(
+ "Extension '%s' declares a page at '%s' requiring a "
+ "permission that cannot be used: %s. The page has not been "
+ "registered and will not load.",
+ manifest.id,
+ page.path,
+ problem,
+ )
+ continue
+
if claimed := _first_claimant(app, page.path):
logger.error(
"Extension '%s' declares a page at '%s', which is already "
@@ -228,8 +264,48 @@ def registered_page_paths(app: Flask, extension_id: str) -> list[str]:
]
+def _grant_to_admin(permission: str, extension_id: str) -> None:
+ """Give the Admin role a page permission, if it does not already hold it.
+
+ Not a policy this invents. ``sync_role_definitions`` builds the Admin
+ role from every permission that is not one of the four object-level ones,
+ so ``superset init`` would hand Admin this permission anyway. Doing it
+ here means an administrator who installs an extension can open its pages
+ without first running init and without granting themselves access to
+ their own deployment.
+
+ Re-checked on every boot rather than only on creation, so an Admin role
+ that lost the grant -- or a deployment that gained an extension before it
+ gained the role -- repairs itself at the next start.
+ """
+ from superset import security_manager
+
+ role_name = current_app.config.get("AUTH_ROLE_ADMIN", "Admin")
+ role = security_manager.find_role(role_name)
+ if role is None:
+ logger.warning(
+ "No '%s' role to grant '%s' on '%s' to", role_name, permission, extension_id
+ )
+ return
+
+ pvm = security_manager.find_permission_view_menu(permission, extension_id)
+ if pvm is None:
+ return
+
+ if pvm not in role.permissions:
+ # Idempotent and self-committing in FAB; called only when absent so
+ # that an ordinary boot does no write at all.
+ security_manager.add_permission_role(role, pvm)
+ logger.info(
+ "Granted '%s' on '%s' to the '%s' role",
+ permission,
+ extension_id,
+ role_name,
+ )
+
+
def ensure_page_permissions(manifest: Manifest) -> None:
- """Create the permissions this extension's pages require, if missing.
+ """Create the permissions this extension's pages require, and grant Admin.
Created when the extension loads rather than by ``superset init``, because
an extension can be installed long after a deployment was initialised and
@@ -239,7 +315,7 @@ def ensure_page_permissions(manifest: Manifest) -> None:
the Roles screen groups an extension's permissions together and reads as
"can_review on preset.asset-approval". Pages of the same extension may
share a permission or declare different ones; this registers whatever set
- they name.
+ they name, and gives the Admin role each of them.
Failures are logged and swallowed. Every worker runs this at boot, so two
starting together can race between the lookup and the insert; the loser
@@ -256,14 +332,14 @@ def ensure_page_permissions(manifest: Manifest) -> None:
}
for permission in sorted(required):
try:
- if security_manager.find_permission_view_menu(permission, manifest.id):
- continue
- security_manager.add_permission_view_menu(permission, manifest.id)
- logger.info(
- "Created permission '%s' on '%s' for extension pages",
- permission,
- manifest.id,
- )
+ if not security_manager.find_permission_view_menu(permission, manifest.id):
+ security_manager.add_permission_view_menu(permission, manifest.id)
+ logger.info(
+ "Created permission '%s' on '%s' for extension pages",
+ permission,
+ manifest.id,
+ )
+ _grant_to_admin(permission, manifest.id)
except Exception: # pylint: disable=broad-except
# Most often a concurrent worker inserting the same row.
logger.warning(
diff --git a/tests/unit_tests/extensions/test_pages_view.py b/tests/unit_tests/extensions/test_pages_view.py
index d1de537e700..4af7a8daf32 100644
--- a/tests/unit_tests/extensions/test_pages_view.py
+++ b/tests/unit_tests/extensions/test_pages_view.py
@@ -67,6 +67,8 @@ class FakeSecurityManager:
self.created: list[tuple[str, str]] = []
self.asked: list[tuple[str, str]] = []
self.rollbacks = 0
+ self.admin_role = SimpleNamespace(name="Admin", permissions=[])
+ self.roles: dict[str, object] = {"Admin": self.admin_role}
def can_access(self, permission: str, view: str) -> bool:
self.asked.append((permission, view))
@@ -79,6 +81,13 @@ class FakeSecurityManager:
self.existing.add((permission, view))
self.created.append((permission, view))
+ def find_role(self, name: str) -> object | None:
+ return self.roles.get(name)
+
+ def add_permission_role(self, role: object, perm_view: object) -> None:
+ if perm_view not in role.permissions: # type: ignore[attr-defined]
+ role.permissions.append(perm_view) # type: ignore[attr-defined]
+
# Named to match SupersetSecurityManager. A fake that invents its own
# attribute names lets production code call something that is not there.
@property
@@ -335,32 +344,93 @@ def test_the_same_permission_on_another_extension_does_not_carry_over(
def test_permissions_are_created_for_every_page_that_names_one(
- security: FakeSecurityManager,
+ app: Flask, security: FakeSecurityManager
) -> None:
- ensure_page_permissions(manifest("/a", "/b", permission="can_review"))
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", "/b", permission="can_review"))
# One row, not one per page: pages of an extension may share a permission.
assert security.created == [("can_review", "acme.tools")]
+def test_a_created_permission_is_granted_to_admin(
+ app: Flask, security: FakeSecurityManager
+) -> None:
+ """`superset init` would hand Admin this permission; this does it sooner.
+
+ Without it an administrator who installs an extension cannot open its
+ pages until they grant themselves access to their own deployment.
+ """
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", permission="can_review"))
+
+ assert security.admin_role.permissions == [("can_review", "acme.tools")]
+
+
+def test_the_admin_grant_is_repaired_on_a_later_boot(
+ app: Flask, security: FakeSecurityManager
+) -> None:
+ """The permission already exists, but the role lost it."""
+ security.existing.add(("can_review", "acme.tools"))
+
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", permission="can_review"))
+
+ assert security.created == []
+ assert security.admin_role.permissions == [("can_review", "acme.tools")]
+
+
+def test_the_admin_role_is_read_from_configuration(
+ app: Flask, security: FakeSecurityManager
+) -> None:
+ # A deployment may rename it; granting a hardcoded "Admin" would then
+ # write to a role nobody holds.
+ app.config["AUTH_ROLE_ADMIN"] = "Superuser"
+ superuser = SimpleNamespace(name="Superuser", permissions=[])
+ security.roles["Superuser"] = superuser
+
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", permission="can_review"))
+
+ assert superuser.permissions == [("can_review", "acme.tools")]
+ assert security.admin_role.permissions == []
+
+
+def test_a_missing_admin_role_does_not_stop_the_boot(
+ app: Flask, security: FakeSecurityManager
+) -> None:
+ security.roles.clear()
+
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", permission="can_review"))
+
+ assert security.created == [("can_review", "acme.tools")]
+
+
def test_nothing_is_created_for_pages_without_permissions(
- security: FakeSecurityManager,
+ app: Flask, security: FakeSecurityManager
) -> None:
- ensure_page_permissions(manifest("/a", "/b"))
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", "/b"))
assert security.created == []
+ assert security.admin_role.permissions == []
-def test_creating_permissions_is_idempotent(security: FakeSecurityManager) -> None:
+def test_creating_permissions_is_idempotent(
+ app: Flask, security: FakeSecurityManager
+) -> None:
# Every worker runs this at every boot.
- ensure_page_permissions(manifest("/a", permission="can_review"))
- ensure_page_permissions(manifest("/a", permission="can_review"))
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", permission="can_review"))
+ ensure_page_permissions(manifest("/a", permission="can_review"))
assert security.created == [("can_review", "acme.tools")]
+ assert security.admin_role.permissions == [("can_review", "acme.tools")]
def test_a_failure_to_create_a_permission_does_not_stop_the_boot(
- monkeypatch: pytest.MonkeyPatch, security: FakeSecurityManager
+ monkeypatch: pytest.MonkeyPatch, app: Flask, security: FakeSecurityManager
) -> None:
"""Two workers starting together race between the lookup and the insert."""
@@ -369,7 +439,8 @@ def test_a_failure_to_create_a_permission_does_not_stop_the_boot(
monkeypatch.setattr(security, "add_permission_view_menu", explode)
- ensure_page_permissions(manifest("/a", permission="can_review"))
+ with app.app_context():
+ ensure_page_permissions(manifest("/a", permission="can_review"))
# And the session was rolled back, so the next write on this worker is
# not issued against a session left dirty by the failed insert.
@@ -460,6 +531,8 @@ def test_the_fake_security_manager_only_names_real_attributes() -> None:
"can_access",
"find_permission_view_menu",
"add_permission_view_menu",
+ "find_role",
+ "add_permission_role",
"session",
)
missing = [
@@ -475,3 +548,34 @@ def test_the_fake_security_manager_only_names_real_attributes() -> None:
name for name in vars(FakeSecurityManager) if not name.startswith("_")
}
assert fake_surface <= set(used_by_production_code)
+
+
+@pytest.mark.parametrize(
+ "permission",
+ ["database_access", "catalog_access", "schema_access", "datasource_access"],
+)
+def test_a_page_naming_an_object_level_permission_is_refused(
+ app: Flask, caplog: pytest.LogCaptureFixture, permission: str
+) -> None:
+ """These four decide what Admin holds, by being excluded from it.
+
+ A page naming one would have its Admin grant stripped the next time
+ `superset init` ran, leaving a page nobody could open. Refusing it at
+ registration says so while the author can still act on it.
+ """
+ with caplog.at_level(logging.ERROR):
+ register_extension_pages(app, manifest("/a", permission=permission))
+
+ assert "/a" not in paths_in(app)
+ assert "reserved by Superset" in caplog.text
+
+
+def test_the_reserved_set_is_superset_s_own(app: Flask) -> None:
+ """Pins the guard to the list it defends, rather than a copy of it."""
+ from superset.security.manager import SupersetSecurityManager
+
+ for permission in SupersetSecurityManager.OBJECT_SPEC_PERMISSIONS:
+ assert view._permission_problem(permission) is not None
+
+ assert view._permission_problem("can_review") is None
+ assert view._permission_problem(None) is None