vincbeck commented on code in PR #77007:
URL: https://github.com/apache/airflow/pull/77007#discussion_r4591948748
##########
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:
nit: the value is fixed, so `conf_vars` can be a decorator here. Same for
the parametrized test above.
##########
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:
Not introduced by this PR, but now that all five filters go through this
helper, it is a good place to fix it. The cache in `cache.py` is shared by all
filters and the key has no resource type: `(user_id, method, team_name,
frozenset(ids))`. Example: a pool and a variable both named `default_pool`,
with no team. `get_authorized_pools` caches `{"default_pool"}`, then within the
TTL `get_authorized_variables` gets the same key and returns the pool decision
without asking Keycloak. Fix: add the resource type to the cache key inside
`_filter_authorized`.
--
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]