dabla commented on code in PR #77007:
URL: https://github.com/apache/airflow/pull/77007#discussion_r4579920613


##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py:
##########
@@ -584,6 +585,10 @@ def check(dag_id: str) -> tuple[str, bool]:
                     details=DagDetails(**details_kwargs),
                 )
 
+            if self._team_level_authorization():

Review Comment:
   Agreed, folded in 38d1e870b3: `_filter_authorized(ids, is_authorized, 
cache_key=...)` now holds the empty guard, the team-level shortcut, the thread 
pool and the cache, and each filter only says how an id becomes the check of a 
single resource. Assets go through it by id as well, so the representative 
resource is picked the same way (`min(ids)`) for all five types. Cache keys and 
the requests sent are unchanged.



##########
providers/keycloak/tests/unit/keycloak/auth_manager/test_keycloak_auth_manager.py:
##########
@@ -1630,5 +1631,100 @@ def test_filter_authorized_dag_ids_caps_max_workers(
 
         mock_executor.assert_called_once_with(max_workers=expected_max_workers)
 
+    @pytest.mark.parametrize(
+        ("filter_name", "check_name", "resources", "expected"),
+        [
+            pytest.param(
+                "filter_authorized_dag_ids",
+                "is_authorized_dag",
+                {"dag_ids": {"dag_0", "dag_1", "dag_2"}},
+                {"dag_0", "dag_1", "dag_2"},
+                id="dags",
+            ),
+            pytest.param(
+                "filter_authorized_assets",
+                "is_authorized_asset",
+                {"assets": [AssetDetails(id="1"), AssetDetails(id="2"), 
AssetDetails(id=None)]},
+                {"1", "2"},
+                id="assets",
+            ),
+            pytest.param(
+                "filter_authorized_connections",
+                "is_authorized_connection",
+                {"conn_ids": {"conn_0", "conn_1", "conn_2"}},
+                {"conn_0", "conn_1", "conn_2"},
+                id="connections",
+            ),
+            pytest.param(
+                "filter_authorized_pools",
+                "is_authorized_pool",
+                {"pool_names": {"pool_0", "pool_1", "pool_2"}},
+                {"pool_0", "pool_1", "pool_2"},
+                id="pools",
+            ),
+            pytest.param(
+                "filter_authorized_variables",
+                "is_authorized_variable",
+                {"variable_keys": {"var_0", "var_1", "var_2"}},
+                {"var_0", "var_1", "var_2"},
+                id="variables",
+            ),
+        ],
+    )
+    @pytest.mark.parametrize("authorized", [True, False])
+    def test_filter_with_team_level_authorization_asks_keycloak_once(
+        self, auth_manager, user, filter_name, check_name, resources, 
expected, authorized
+    ):
+        """One decision of Keycloak applies to every resource of the list."""

Review Comment:
   Applied in c8e4e4585f.



##########
providers/keycloak/tests/unit/keycloak/auth_manager/test_keycloak_auth_manager.py:
##########
@@ -1630,5 +1631,100 @@ def test_filter_authorized_dag_ids_caps_max_workers(
 
         mock_executor.assert_called_once_with(max_workers=expected_max_workers)
 
+    @pytest.mark.parametrize(
+        ("filter_name", "check_name", "resources", "expected"),
+        [
+            pytest.param(
+                "filter_authorized_dag_ids",
+                "is_authorized_dag",
+                {"dag_ids": {"dag_0", "dag_1", "dag_2"}},
+                {"dag_0", "dag_1", "dag_2"},
+                id="dags",
+            ),
+            pytest.param(
+                "filter_authorized_assets",
+                "is_authorized_asset",
+                {"assets": [AssetDetails(id="1"), AssetDetails(id="2"), 
AssetDetails(id=None)]},
+                {"1", "2"},
+                id="assets",
+            ),
+            pytest.param(
+                "filter_authorized_connections",
+                "is_authorized_connection",
+                {"conn_ids": {"conn_0", "conn_1", "conn_2"}},
+                {"conn_0", "conn_1", "conn_2"},
+                id="connections",
+            ),
+            pytest.param(
+                "filter_authorized_pools",
+                "is_authorized_pool",
+                {"pool_names": {"pool_0", "pool_1", "pool_2"}},
+                {"pool_0", "pool_1", "pool_2"},
+                id="pools",
+            ),
+            pytest.param(
+                "filter_authorized_variables",
+                "is_authorized_variable",
+                {"variable_keys": {"var_0", "var_1", "var_2"}},
+                {"var_0", "var_1", "var_2"},
+                id="variables",
+            ),
+        ],
+    )
+    @pytest.mark.parametrize("authorized", [True, False])
+    def test_filter_with_team_level_authorization_asks_keycloak_once(
+        self, auth_manager, user, filter_name, check_name, resources, 
expected, authorized
+    ):
+        """One decision of Keycloak applies to every resource of the list."""
+        with (
+            conf_vars({(CONF_SECTION_NAME, CONF_TEAM_LEVEL_AUTHORIZATION_KEY): 
"True"}),
+            patch.object(
+                KeycloakAuthManager, check_name, autospec=True, 
return_value=authorized
+            ) as mock_is_authorized,
+        ):
+            result = getattr(auth_manager, filter_name)(**resources, 
user=user, method="GET")
+
+        assert result == (expected if authorized else set())
+        mock_is_authorized.assert_called_once()

Review Comment:
   Applied in c8e4e4585f: the parametrized test calls each filter twice, so the 
cached decision is covered for all five filters, granted and denied.



##########
providers/keycloak/tests/unit/keycloak/auth_manager/test_keycloak_auth_manager.py:
##########
@@ -1630,5 +1631,100 @@ def test_filter_authorized_dag_ids_caps_max_workers(
 
         mock_executor.assert_called_once_with(max_workers=expected_max_workers)
 
+    @pytest.mark.parametrize(
+        ("filter_name", "check_name", "resources", "expected"),
+        [
+            pytest.param(
+                "filter_authorized_dag_ids",
+                "is_authorized_dag",
+                {"dag_ids": {"dag_0", "dag_1", "dag_2"}},
+                {"dag_0", "dag_1", "dag_2"},
+                id="dags",
+            ),
+            pytest.param(
+                "filter_authorized_assets",
+                "is_authorized_asset",
+                {"assets": [AssetDetails(id="1"), AssetDetails(id="2"), 
AssetDetails(id=None)]},
+                {"1", "2"},
+                id="assets",
+            ),
+            pytest.param(
+                "filter_authorized_connections",
+                "is_authorized_connection",
+                {"conn_ids": {"conn_0", "conn_1", "conn_2"}},
+                {"conn_0", "conn_1", "conn_2"},
+                id="connections",
+            ),
+            pytest.param(
+                "filter_authorized_pools",
+                "is_authorized_pool",
+                {"pool_names": {"pool_0", "pool_1", "pool_2"}},
+                {"pool_0", "pool_1", "pool_2"},
+                id="pools",
+            ),
+            pytest.param(
+                "filter_authorized_variables",
+                "is_authorized_variable",
+                {"variable_keys": {"var_0", "var_1", "var_2"}},
+                {"var_0", "var_1", "var_2"},
+                id="variables",
+            ),
+        ],
+    )
+    @pytest.mark.parametrize("authorized", [True, False])
+    def test_filter_with_team_level_authorization_asks_keycloak_once(
+        self, auth_manager, user, filter_name, check_name, resources, 
expected, authorized
+    ):
+        """One decision of Keycloak applies to every resource of the list."""
+        with (
+            conf_vars({(CONF_SECTION_NAME, CONF_TEAM_LEVEL_AUTHORIZATION_KEY): 
"True"}),
+            patch.object(
+                KeycloakAuthManager, check_name, autospec=True, 
return_value=authorized
+            ) as mock_is_authorized,
+        ):
+            result = getattr(auth_manager, filter_name)(**resources, 
user=user, method="GET")
+
+        assert result == (expected if authorized else set())
+        mock_is_authorized.assert_called_once()
+
+    @pytest.mark.skipif(not AIRFLOW_V_3_2_PLUS, reason="team_name not 
supported before Airflow 3.2.0")
+    @patch.object(KeycloakAuthManager, "is_authorized_dag", autospec=True, 
return_value=True)
+    def 
test_filter_with_team_level_authorization_asks_for_a_listed_dag_of_the_team(
+        self, mock_is_authorized, auth_manager_multi_team, user
+    ):
+        """The request names a Dag of the list and its team, as a check of 
that single Dag would."""
+        with conf_vars({(CONF_SECTION_NAME, 
CONF_TEAM_LEVEL_AUTHORIZATION_KEY): "True"}):
+            auth_manager_multi_team.filter_authorized_dag_ids(
+                dag_ids={"dag_1", "dag_0"}, user=user, method="PUT", 
team_name="team_a"
+            )
+
+        mock_is_authorized.assert_called_once_with(
+            auth_manager_multi_team,
+            method="PUT",
+            user=user,
+            details=DagDetails(id="dag_0", team_name="team_a"),
+        )
+
+    @patch.object(KeycloakAuthManager, "is_authorized_dag", autospec=True, 
return_value=True)
+    def 
test_filter_with_team_level_authorization_and_no_resources_skips_keycloak(
+        self, mock_is_authorized, auth_manager, user
+    ):
+        with conf_vars({(CONF_SECTION_NAME, 
CONF_TEAM_LEVEL_AUTHORIZATION_KEY): "True"}):
+            assert auth_manager.filter_authorized_dag_ids(dag_ids=set(), 
user=user) == set()
+
+        mock_is_authorized.assert_not_called()
+
+    @patch.object(KeycloakAuthManager, "is_authorized_dag", autospec=True, 
return_value=True)
+    def test_filter_with_team_level_authorization_caches_the_decision(

Review Comment:
   Dropped in c8e4e4585f, together with the change above.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to