stegololz commented on code in PR #77007:
URL: https://github.com/apache/airflow/pull/77007#discussion_r4578137347
##########
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:
On your open point: I'd fold it. This block decides access for the whole
list from one answer, and it now exists five times (here, 718, 755, 792, 829).
A `_filter_with(ids, check)` helper holding both this shortcut and the existing
`ThreadPoolExecutor` path keeps the rule in one place if it ever needs to
change (e.g. how the representative resource is picked).
##########
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:
```suggestion
"""One decision of Keycloak applies to every resource of the list,
and is cached."""
```
##########
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:
With the cache check folded into the parametrized test above, this one can
go: it only covers Dags, and the caching itself goes through the unchanged
`single_flight` path, already tested by
`test_filter_authorized_dag_ids_cache_hit`.
##########
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(
Review Comment:
This one passes without the PR: the `if not dag_ids: return set()` guard was
already there before the new branch, and `test_filter_authorized_dag_ids_empty`
covers it. I'd drop it.
##########
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:
How about folding the cache check in here? Calling each filter twice covers
caching for all five filters, granted and denied, and makes
`test_filter_with_team_level_authorization_caches_the_decision` below redundant.
```suggestion
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()
```
--
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]