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


##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py:
##########
@@ -867,6 +806,38 @@ def _get_teams(self) -> set[str]:
 
         return {r["name"][len(prefix) :] for r in resources_resp.json() if 
r["name"].startswith(prefix)}
 
+    def _filter_authorized(

Review Comment:
   Good catch, fixed in 0dae77bc74: `_filter_authorized` now builds the cache 
key itself and starts it with the resource type, so the filters cannot leave it 
out. Added a test with a pool and a variable of the same name that fails on the 
old key.



##########
providers/keycloak/tests/unit/keycloak/auth_manager/test_keycloak_auth_manager.py:
##########
@@ -1630,5 +1631,80 @@ 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, and 
is cached."""
+        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,
+        ):
+            first = getattr(auth_manager, filter_name)(**resources, user=user, 
method="GET")
+            second = getattr(auth_manager, filter_name)(**resources, 
user=user, method="GET")
+
+        assert first == second == (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"}):

Review Comment:
   Done in 4e12f378cd, for both tests.



-- 
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