diff --git a/airflow/api_fastapi/auth/managers/utils/__init__.py b/airflow/api_fastapi/auth/managers/utils/__init__.py deleted file mode 100644 index 217e5db960782..0000000000000 --- a/airflow/api_fastapi/auth/managers/utils/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -# -# 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. diff --git a/airflow/api_fastapi/auth/managers/utils/fab.py b/airflow/api_fastapi/auth/managers/utils/fab.py deleted file mode 100644 index 82a1c9344ba48..0000000000000 --- a/airflow/api_fastapi/auth/managers/utils/fab.py +++ /dev/null @@ -1,52 +0,0 @@ -# -# 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 - -from typing import TYPE_CHECKING - -from airflow.security.permissions import ( - ACTION_CAN_ACCESS_MENU, - ACTION_CAN_CREATE, - ACTION_CAN_DELETE, - ACTION_CAN_EDIT, - ACTION_CAN_READ, -) - -if TYPE_CHECKING: - from airflow.api_fastapi.auth.managers.base_auth_manager import ResourceMethod - -# Convert methods to FAB action name -_MAP_METHOD_NAME_TO_FAB_ACTION_NAME: dict[ResourceMethod, str] = { - "POST": ACTION_CAN_CREATE, - "GET": ACTION_CAN_READ, - "PUT": ACTION_CAN_EDIT, - "DELETE": ACTION_CAN_DELETE, - "MENU": ACTION_CAN_ACCESS_MENU, -} - - -def get_fab_action_from_method_map(): - """Return the map associating a method to a FAB action.""" - return _MAP_METHOD_NAME_TO_FAB_ACTION_NAME - - -def get_method_from_fab_action_map(): - """Return the map associating a FAB action to a method.""" - return { - **{v: k for k, v in _MAP_METHOD_NAME_TO_FAB_ACTION_NAME.items()}, - } diff --git a/providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py b/providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py index 0c721e713a199..61a77abdf801e 100644 --- a/providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py +++ b/providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py @@ -41,10 +41,6 @@ PoolDetails, VariableDetails, ) -from airflow.api_fastapi.auth.managers.utils.fab import ( - get_fab_action_from_method_map, - get_method_from_fab_action_map, -) from airflow.cli.cli_config import ( DefaultHelpParser, GroupCommand, @@ -89,6 +85,7 @@ RESOURCE_WEBSITE, RESOURCE_XCOM, ) +from airflow.providers.fab.www.utils import get_fab_action_from_method_map, get_method_from_fab_action_map from airflow.utils.session import NEW_SESSION, create_session, provide_session from airflow.utils.yaml import safe_load diff --git a/providers/fab/src/airflow/providers/fab/www/security_manager.py b/providers/fab/src/airflow/providers/fab/www/security_manager.py index a20c1308eda15..b41bcd8bec56b 100644 --- a/providers/fab/src/airflow/providers/fab/www/security_manager.py +++ b/providers/fab/src/airflow/providers/fab/www/security_manager.py @@ -23,10 +23,7 @@ from flask_limiter.util import get_remote_address from airflow.api_fastapi.app import get_auth_manager -from airflow.api_fastapi.auth.managers.utils.fab import ( - get_method_from_fab_action_map, -) -from airflow.providers.fab.www.utils import CustomSQLAInterface +from airflow.providers.fab.www.utils import CustomSQLAInterface, get_method_from_fab_action_map from airflow.utils.log.logging_mixin import LoggingMixin EXISTING_ROLES = { diff --git a/providers/fab/src/airflow/providers/fab/www/utils.py b/providers/fab/src/airflow/providers/fab/www/utils.py index b14da6448642e..1b9e6d0309d27 100644 --- a/providers/fab/src/airflow/providers/fab/www/utils.py +++ b/providers/fab/src/airflow/providers/fab/www/utils.py @@ -28,13 +28,30 @@ from sqlalchemy.ext.associationproxy import AssociationProxy from airflow.api_fastapi.app import get_auth_manager +from airflow.providers.fab.www.security.permissions import ( + ACTION_CAN_ACCESS_MENU, + ACTION_CAN_CREATE, + ACTION_CAN_DELETE, + ACTION_CAN_EDIT, + ACTION_CAN_READ, +) from airflow.utils import timezone if TYPE_CHECKING: from sqlalchemy.orm.session import Session + from airflow.api_fastapi.auth.managers.base_auth_manager import ResourceMethod from airflow.providers.fab.auth_manager.fab_auth_manager import FabAuthManager +# Convert methods to FAB action name +_MAP_METHOD_NAME_TO_FAB_ACTION_NAME: dict[ResourceMethod, str] = { + "POST": ACTION_CAN_CREATE, + "GET": ACTION_CAN_READ, + "PUT": ACTION_CAN_EDIT, + "DELETE": ACTION_CAN_DELETE, + "MENU": ACTION_CAN_ACCESS_MENU, +} + def get_fab_auth_manager() -> FabAuthManager: from airflow.providers.fab.auth_manager.fab_auth_manager import FabAuthManager @@ -47,6 +64,18 @@ def get_fab_auth_manager() -> FabAuthManager: return auth_manager +def get_fab_action_from_method_map(): + """Return the map associating a method to a FAB action.""" + return _MAP_METHOD_NAME_TO_FAB_ACTION_NAME + + +def get_method_from_fab_action_map(): + """Return the map associating a FAB action to a method.""" + return { + **{v: k for k, v in _MAP_METHOD_NAME_TO_FAB_ACTION_NAME.items()}, + } + + class UtcAwareFilterMixin: """Mixin for filter for UTC time.""" diff --git a/providers/fab/tests/unit/fab/www/views/test_views_custom_user_views.py b/providers/fab/tests/unit/fab/www/views/test_views_custom_user_views.py index 6851db3700c95..315f64afd42ea 100644 --- a/providers/fab/tests/unit/fab/www/views/test_views_custom_user_views.py +++ b/providers/fab/tests/unit/fab/www/views/test_views_custom_user_views.py @@ -26,7 +26,7 @@ from airflow import settings from airflow.providers.fab.www import app as application -from airflow.security import permissions +from airflow.providers.fab.www.security import permissions from unit.fab.auth_manager.api_endpoints.api_connexion_utils import ( create_user, delete_role,