This is an automated email from the ASF dual-hosted git repository.
vincbeck pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new d0cd1700c47 Fix FAB role rename to an existing name returning 200
(#76972)
d0cd1700c47 is described below
commit d0cd1700c4728419e04652212c9fd254197cfd9c
Author: ChickenBenny <[email protected]>
AuthorDate: Thu Nov 19 00:05:02 2026 +0800
Fix FAB role rename to an existing name returning 200 (#76972)
---
.../openapi/v2-fab-auth-manager-generated.yaml | 6 +
.../fab/auth_manager/api_fastapi/routes/roles.py | 1 +
.../fab/auth_manager/api_fastapi/services/roles.py | 33 ++++--
.../api_fastapi/services/test_roles.py | 126 ++++++++++++++++++++-
4 files changed, 151 insertions(+), 15 deletions(-)
diff --git
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/openapi/v2-fab-auth-manager-generated.yaml
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/openapi/v2-fab-auth-manager-generated.yaml
index f56de9cccb6..faf2688e66f 100644
---
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/openapi/v2-fab-auth-manager-generated.yaml
+++
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/openapi/v2-fab-auth-manager-generated.yaml
@@ -392,6 +392,12 @@ paths:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Not Found
+ '409':
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/HTTPExceptionResponse'
+ description: Conflict
'422':
description: Validation Error
content:
diff --git
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
index b467b6a9515..ae298ac7fde 100644
---
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
+++
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/routes/roles.py
@@ -116,6 +116,7 @@ def get_role(name: str = Path(..., min_length=1)) ->
RoleResponse:
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
+ status.HTTP_409_CONFLICT,
]
),
dependencies=[Depends(requires_fab_custom_view("PUT",
permissions.RESOURCE_ROLE))],
diff --git
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/services/roles.py
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/services/roles.py
index 6cff8e22314..15095633bbb 100644
---
a/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/services/roles.py
+++
b/providers/fab/src/airflow/providers/fab/auth_manager/api_fastapi/services/roles.py
@@ -155,14 +155,30 @@ class FABAuthManagerRoles:
fields_to_update &= normalized_fields
update_data = RoleResponse.model_validate(existing)
+ permissions = body.permissions or []
+ target_pairs = {(ar.action.name, ar.resource.name) for ar in
permissions}
if "permissions" in fields_to_update:
- cls._replace_role_permissions(security_manager, existing,
body.permissions or [])
- update_data.permissions = body.permissions or []
- if "name" in fields_to_update:
+ cls._check_action_and_resource(security_manager,
list(target_pairs))
+
+ # Rename before touching permissions: each permission change is
committed on its own,
+ # so a rename rejected afterwards would leave the role half-updated.
+ if "name" in fields_to_update and body.name != existing.name:
+ conflict = security_manager.find_role(name=body.name)
+ if conflict and conflict.id != existing.id:
+ raise HTTPException(
+ status_code=status.HTTP_409_CONFLICT,
+ detail=f"Role with name {body.name!r} already exists",
+ )
+ if security_manager.update_role(role_id=existing.id,
name=body.name) is None:
+ raise HTTPException(
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
+ detail="Role was not renamed due to an unexpected error.",
+ )
update_data.name = body.name
- if update_data.name != existing.name:
- security_manager.update_role(role_id=existing.id,
name=update_data.name)
+ if "permissions" in fields_to_update:
+ cls._replace_role_permissions(security_manager, existing,
target_pairs)
+ update_data.permissions = permissions
return update_data
@classmethod
@@ -170,18 +186,15 @@ class FABAuthManagerRoles:
cls,
security_manager: FabAirflowSecurityManagerOverride,
role: Role,
- permissions: list[ActionResource],
+ target_pairs: set[tuple[str, str]],
) -> None:
"""
- Make the role's permissions match `permissions` exactly.
+ Make the role's permissions match the `(action, resource)` pairs in
`target_pairs` exactly.
Unlike the additive sync used on role creation, a PATCH that touches
the permission
set must also revoke permissions currently on the role that are absent
from the
request -- otherwise permissions could be added but never removed via
the API.
"""
- target_pairs = {(ar.action.name, ar.resource.name) for ar in
permissions}
- cls._check_action_and_resource(security_manager, list(target_pairs))
-
current_permissions = {(p.action.name, p.resource.name): p for p in
role.permissions}
for action_name, resource_name in target_pairs -
current_permissions.keys():
diff --git
a/providers/fab/tests/unit/fab/auth_manager/api_fastapi/services/test_roles.py
b/providers/fab/tests/unit/fab/auth_manager/api_fastapi/services/test_roles.py
index be2d0700138..a0af305844c 100644
---
a/providers/fab/tests/unit/fab/auth_manager/api_fastapi/services/test_roles.py
+++
b/providers/fab/tests/unit/fab/auth_manager/api_fastapi/services/test_roles.py
@@ -34,6 +34,10 @@ from
airflow.providers.fab.auth_manager.api_fastapi.datamodels.roles import (
from airflow.providers.fab.auth_manager.api_fastapi.services.roles import (
FABAuthManagerRoles,
)
+from airflow.providers.fab.auth_manager.cli_commands.utils import
get_application_builder
+from airflow.providers.fab.auth_manager.fab_auth_manager import FabAuthManager
+
+from tests_common.test_utils.config import conf_vars
@pytest.fixture
@@ -276,26 +280,38 @@ class TestRolesService:
# PATCH /roles/{name}
- def test_patch_role_rename_success(self, get_fab_auth_manager,
fab_auth_manager, security_manager):
+ @pytest.mark.parametrize(
+ ("new_name", "new_name_resolves_to_self"),
+ [
+ pytest.param("editor", False, id="new-name-unused"),
+ # A case-insensitive collation (e.g. MySQL) finds the role being
renamed itself.
+ pytest.param("Viewer", True, id="case-only-rename"),
+ ],
+ )
+ def test_patch_role_rename_success(
+ self, get_fab_auth_manager, new_name, new_name_resolves_to_self,
fab_auth_manager, security_manager
+ ):
role = _make_role_obj("viewer", [("can_edit", "DAG")])
- security_manager.find_role.return_value = role
+ security_manager.find_role.side_effect = lambda name: (
+ role if name == "viewer" or new_name_resolves_to_self else None
+ )
security_manager.get_permission.return_value = types.SimpleNamespace(
action=types.SimpleNamespace(name="can_edit"),
resource=types.SimpleNamespace(name="DAG")
)
fab_auth_manager.security_manager = security_manager
get_fab_auth_manager.return_value = fab_auth_manager
- body = _make_role_body("editor", [("can_edit", "DAG")])
+ body = _make_role_body(new_name, [("can_edit", "DAG")])
out = FABAuthManagerRoles.patch_role(body=body, name="viewer")
- assert out.name == "editor"
+ assert out.name == new_name
assert out.permissions
assert out.permissions[0].action.name == "can_edit"
assert out.permissions[0].resource.name == "DAG"
# The permission set is unchanged, so nothing should be added or
removed.
security_manager.add_permission_to_role.assert_not_called()
security_manager.remove_permission_from_role.assert_not_called()
- security_manager.update_role.assert_called_once_with(role_id=role.id,
name="editor")
+ security_manager.update_role.assert_called_once_with(role_id=role.id,
name=new_name)
def test_patch_role_adds_missing_permission(
self, get_fab_auth_manager, fab_auth_manager, security_manager
@@ -496,6 +512,41 @@ class TestRolesService:
FABAuthManagerRoles.patch_role(body=body, name="viewer")
assert ex.value.status_code == 404
+ def test_patch_role_rename_to_existing_name_conflicts(
+ self, get_fab_auth_manager, fab_auth_manager, security_manager
+ ):
+ role = _make_role_obj("viewer", [("can_read", "DAG")])
+ other = types.SimpleNamespace(id=2, name="editor", permissions=[])
+ security_manager.find_role.side_effect = lambda name: {"viewer": role,
"editor": other}.get(name)
+ fab_auth_manager.security_manager = security_manager
+ get_fab_auth_manager.return_value = fab_auth_manager
+ body = _make_role_body("editor", [("can_edit", "DAG")])
+
+ with pytest.raises(HTTPException) as ex:
+ FABAuthManagerRoles.patch_role(body=body, name="viewer")
+
+ assert ex.value.status_code == 409
+ security_manager.update_role.assert_not_called()
+ security_manager.add_permission_to_role.assert_not_called()
+ security_manager.remove_permission_from_role.assert_not_called()
+
+ def
test_patch_role_rename_failure_returns_500_and_leaves_permissions_untouched(
+ self, get_fab_auth_manager, fab_auth_manager, security_manager
+ ):
+ role = _make_role_obj("viewer", [("can_read", "DAG")])
+ security_manager.find_role.side_effect = lambda name: {"viewer":
role}.get(name)
+ security_manager.update_role.return_value = None
+ fab_auth_manager.security_manager = security_manager
+ get_fab_auth_manager.return_value = fab_auth_manager
+ body = _make_role_body("editor", [("can_edit", "DAG")])
+
+ with pytest.raises(HTTPException) as ex:
+ FABAuthManagerRoles.patch_role(body=body, name="viewer")
+
+ assert ex.value.status_code == 500
+ security_manager.add_permission_to_role.assert_not_called()
+ security_manager.remove_permission_from_role.assert_not_called()
+
@patch("airflow.providers.fab.auth_manager.api_fastapi.services.roles.create_session")
def test_get_permissions_success(self, create_session,
get_fab_auth_manager):
session = MagicMock()
@@ -609,3 +660,68 @@ class TestRolesService:
assert ex.value.status_code == 400
create_session.assert_called_once_with(scoped=False)
create_session.return_value.__exit__.assert_called_once()
+
+
[email protected]_test
+class TestPatchRoleWithDatabase:
+ ROLE_A = "FakeTeamA"
+ ROLE_B = "FakeTeamB"
+ ACTION = "fake_can_read"
+ RESOURCE_A = "FakeResourceA"
+ RESOURCE_B = "FakeResourceB"
+
+ @pytest.fixture
+ def db_security_manager(self):
+ with (
+ conf_vars(
+ {
+ (
+ "core",
+ "auth_manager",
+ ):
"airflow.providers.fab.auth_manager.fab_auth_manager.FabAuthManager",
+ ("fab", "UPDATE_FAB_PERMS"): "False",
+ }
+ ),
+ get_application_builder() as appbuilder,
+ ):
+ sm = appbuilder.sm
+ self._cleanup(sm)
+ yield sm
+ sm.session.rollback()
+ self._cleanup(sm)
+
+ def _cleanup(self, sm):
+ for role_name in (self.ROLE_A, self.ROLE_B):
+ if sm.find_role(role_name):
+ sm.delete_role(role_name)
+ # delete_permission also drops the action once no permission uses it.
+ for resource_name in (self.RESOURCE_A, self.RESOURCE_B):
+ sm.delete_permission(self.ACTION, resource_name)
+ if resource := sm.get_resource(resource_name):
+ sm.session.delete(resource)
+ sm.session.commit()
+
+ @patch(
+
"airflow.providers.fab.auth_manager.api_fastapi.services.roles.get_fab_auth_manager",
autospec=True
+ )
+ def test_rename_to_existing_role_leaves_both_roles_unchanged(
+ self, get_fab_auth_manager, db_security_manager
+ ):
+ sm = db_security_manager
+ get_fab_auth_manager.return_value = MagicMock(spec=FabAuthManager,
security_manager=sm)
+ role_a = sm.add_role(self.ROLE_A)
+ sm.add_permission_to_role(role_a, sm.create_permission(self.ACTION,
self.RESOURCE_A))
+ sm.add_role(self.ROLE_B)
+ sm.create_permission(self.ACTION, self.RESOURCE_B)
+ body = _make_role_body(self.ROLE_B, [(self.ACTION, self.RESOURCE_B)])
+
+ with pytest.raises(HTTPException) as ex:
+ FABAuthManagerRoles.patch_role(body=body, name=self.ROLE_A)
+
+ assert ex.value.status_code == 409
+ sm.session.expire_all()
+ role_a = sm.find_role(self.ROLE_A)
+ assert {(p.action.name, p.resource.name) for p in role_a.permissions}
== {
+ (self.ACTION, self.RESOURCE_A)
+ }
+ assert sm.find_role(self.ROLE_B).permissions == []