You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@airflow.apache.org by "vincbeck (via GitHub)" <gi...@apache.org> on 2025/09/05 17:28:56 UTC
[PR] Make `batch_is_authorized_` methods team aware [airflow]
vincbeck opened a new pull request, #55306:
URL: https://github.com/apache/airflow/pull/55306
The same way some `is_authorized_` methods are team aware, the `batch_is_authorized_` methods need to be as well.
<!--
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.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Make `batch_is_authorized_` methods team aware [airflow]
Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas commented on code in PR #55306:
URL: https://github.com/apache/airflow/pull/55306#discussion_r2326108782
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -255,21 +255,28 @@ def inner(
request: BulkBody[PoolBody],
user: GetUserDep,
) -> None:
+ existing_pool_names = [
+ cast("str", entity) if action.action == BulkAction.DELETE else cast("PoolBody", entity).pool
+ for action in request.actions
+ for entity in action.entities
+ if action.action != BulkAction.CREATE
+ ]
+ teams = Pool.get_bulk_team_name(existing_pool_names)
+
requests: list[IsAuthorizedPoolRequest] = []
for action in request.actions:
- requests.extend(
- [
- {
- "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
- "details": PoolDetails(
- name=cast("str", pool)
- if action.action == BulkAction.DELETE
- else cast("PoolBody", pool).pool
- ),
- }
- for pool in action.entities
- ]
- )
+ for pool in action.entities:
+ pool_name = (
+ cast("str", pool) if action.action == BulkAction.DELETE else cast("PoolBody", pool).pool
+ )
+ req: IsAuthorizedPoolRequest = {
+ "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
+ "details": PoolDetails(
+ name=pool_name,
+ team_name=teams.get(pool_name),
+ ),
+ }
+ requests.append(req)
Review Comment:
I know this logic is probably quite mechanical and obvious if you're used to this area of the codebase. But I find it a bit hard and obscure to review/read. I wonder if it's worth adding more comments to this logic going forward?
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
session.execute(import_errors_select), itemgetter(0)
)
import_errors = []
for import_error, file_dag_ids in import_errors_result:
+ dag_ids = [dag_id for _, dag_id in file_dag_ids]
+ teams = DagModel.get_bulk_team_name(dag_ids, session=session)
Review Comment:
This is really like a `dag_id_to_team` mapping right? I think a better name (I know, naming is hard 🙁) would make this and the other instances below more readable.
Also, every dag from the same dag bundle that is owned by a team will be accessible from that team. Do we need to worry about it at the dag file level? Or is that hard to change at this point?
##########
airflow-core/src/airflow/models/connection.py:
##########
@@ -598,3 +598,13 @@ def get_team_name(connection_id: str, session=NEW_SESSION) -> str | None:
.where(Connection.conn_id == connection_id)
)
return session.scalar(stmt)
+
+ @staticmethod
+ @provide_session
+ def get_bulk_team_name(connection_ids: list[str], session=NEW_SESSION) -> dict[str, str | None]:
Review Comment:
I think this is a confusing method name. It's really getting a connection to team name mapping. It could be more generally useful in other instances, and the current name is somewhat hard coded to the current usage.
##########
airflow-core/tests/unit/api_fastapi/core_api/test_security.py:
##########
@@ -106,7 +106,7 @@ async def test_get_user_expired_token(self, mock_get_auth_manager):
@pytest.mark.db_test
@patch("airflow.api_fastapi.core_api.security.get_auth_manager")
- async def test_requires_access_dag_authorized(self, mock_get_auth_manager):
Review Comment:
Why did async get dropped in favour of def in all these cases? Is that related to the primary changes in this PR?
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -255,21 +255,28 @@ def inner(
request: BulkBody[PoolBody],
user: GetUserDep,
) -> None:
+ existing_pool_names = [
+ cast("str", entity) if action.action == BulkAction.DELETE else cast("PoolBody", entity).pool
+ for action in request.actions
+ for entity in action.entities
+ if action.action != BulkAction.CREATE
+ ]
+ teams = Pool.get_bulk_team_name(existing_pool_names)
+
requests: list[IsAuthorizedPoolRequest] = []
for action in request.actions:
- requests.extend(
- [
- {
- "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
- "details": PoolDetails(
- name=cast("str", pool)
- if action.action == BulkAction.DELETE
- else cast("PoolBody", pool).pool
- ),
- }
- for pool in action.entities
- ]
- )
+ for pool in action.entities:
+ pool_name = (
+ cast("str", pool) if action.action == BulkAction.DELETE else cast("PoolBody", pool).pool
+ )
+ req: IsAuthorizedPoolRequest = {
+ "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
+ "details": PoolDetails(
+ name=pool_name,
+ team_name=teams.get(pool_name),
Review Comment:
Users should also have access to all connections, pools, dags, etc that are _not_ owned by a team (i.e. are global). Is that case still covered by this code? I see that the methods that produce the mappings do not include pools that are not owned by a team (i.e. `None` team name). So I'm guessing no?
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Make `batch_is_authorized_` methods team aware [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on code in PR #55306:
URL: https://github.com/apache/airflow/pull/55306#discussion_r2330654604
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -255,21 +255,28 @@ def inner(
request: BulkBody[PoolBody],
user: GetUserDep,
) -> None:
+ existing_pool_names = [
+ cast("str", entity) if action.action == BulkAction.DELETE else cast("PoolBody", entity).pool
+ for action in request.actions
+ for entity in action.entities
+ if action.action != BulkAction.CREATE
+ ]
+ teams = Pool.get_bulk_team_name(existing_pool_names)
+
requests: list[IsAuthorizedPoolRequest] = []
for action in request.actions:
- requests.extend(
- [
- {
- "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
- "details": PoolDetails(
- name=cast("str", pool)
- if action.action == BulkAction.DELETE
- else cast("PoolBody", pool).pool
- ),
- }
- for pool in action.entities
- ]
- )
+ for pool in action.entities:
+ pool_name = (
+ cast("str", pool) if action.action == BulkAction.DELETE else cast("PoolBody", pool).pool
+ )
+ req: IsAuthorizedPoolRequest = {
+ "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
+ "details": PoolDetails(
+ name=pool_name,
+ team_name=teams.get(pool_name),
+ ),
+ }
+ requests.append(req)
Review Comment:
Fair point, I can add some
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
session.execute(import_errors_select), itemgetter(0)
)
import_errors = []
for import_error, file_dag_ids in import_errors_result:
+ dag_ids = [dag_id for _, dag_id in file_dag_ids]
+ teams = DagModel.get_bulk_team_name(dag_ids, session=session)
Review Comment:
Fair point for the renaming.
Regarding the second point, this API has always been working on the dag file level. This API returns the error per dag file. I guess changing this behavior would be breaking and would have many consequences
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
session.execute(import_errors_select), itemgetter(0)
)
import_errors = []
for import_error, file_dag_ids in import_errors_result:
+ dag_ids = [dag_id for _, dag_id in file_dag_ids]
Review Comment:
Nop, see my comment above
##########
airflow-core/src/airflow/models/connection.py:
##########
@@ -598,3 +598,13 @@ def get_team_name(connection_id: str, session=NEW_SESSION) -> str | None:
.where(Connection.conn_id == connection_id)
)
return session.scalar(stmt)
+
+ @staticmethod
+ @provide_session
+ def get_bulk_team_name(connection_ids: list[str], session=NEW_SESSION) -> dict[str, str | None]:
Review Comment:
Agree
##########
airflow-core/tests/unit/api_fastapi/core_api/test_security.py:
##########
@@ -106,7 +106,7 @@ async def test_get_user_expired_token(self, mock_get_auth_manager):
@pytest.mark.db_test
@patch("airflow.api_fastapi.core_api.security.get_auth_manager")
- async def test_requires_access_dag_authorized(self, mock_get_auth_manager):
Review Comment:
It is not related, this is something I noticed when working on the tests. The `async` is not necessary here, I am pretty sure it comes from copy paste from tests testing `resolve_user_from_token`, that is an async function
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
Review Comment:
Good catch! It should have called it out actually. When working on this issue I found out that bug. `file_dag_ids` (the second element of `import_errors_result`) is not a `str` but a `tuple`, so today we are passing a `tuple` as `str` as Dag ID to the method `batch_is_authorized_dag` (needless to say, garbage data). That is also why I added this test in `airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_import_error.py`:
```
mock_batch_is_authorized_dag.assert_called_once_with(
[
{
"method": "GET",
"details": DagDetails(id=permitted_dag_model.dag_id, team_name=team),
}
],
user=mock.ANY,
)
```
To catch this bug
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -255,21 +255,28 @@ def inner(
request: BulkBody[PoolBody],
user: GetUserDep,
) -> None:
+ existing_pool_names = [
+ cast("str", entity) if action.action == BulkAction.DELETE else cast("PoolBody", entity).pool
+ for action in request.actions
+ for entity in action.entities
+ if action.action != BulkAction.CREATE
+ ]
+ teams = Pool.get_bulk_team_name(existing_pool_names)
+
requests: list[IsAuthorizedPoolRequest] = []
for action in request.actions:
- requests.extend(
- [
- {
- "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
- "details": PoolDetails(
- name=cast("str", pool)
- if action.action == BulkAction.DELETE
- else cast("PoolBody", pool).pool
- ),
- }
- for pool in action.entities
- ]
- )
+ for pool in action.entities:
+ pool_name = (
+ cast("str", pool) if action.action == BulkAction.DELETE else cast("PoolBody", pool).pool
+ )
+ req: IsAuthorizedPoolRequest = {
+ "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
+ "details": PoolDetails(
+ name=pool_name,
+ team_name=teams.get(pool_name),
Review Comment:
Indeed, `existing_pool_names` contains only pool names that are associated with one team. But the authorization request we are sending contain all entities (here pool). Look at line `267`: `for action in request.actions:`. We are looping over the entire list of entities. In case the pool is not associated to a team, then `team_name` is `False` in the authorization request. All we do here is passing information to the auth manager so that it can make authorization decision.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Make `batch_is_authorized_` methods team aware [airflow]
Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #55306:
URL: https://github.com/apache/airflow/pull/55306#discussion_r2327647815
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
Review Comment:
May I ask why do we need to change this?
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
session.execute(import_errors_select), itemgetter(0)
)
import_errors = []
for import_error, file_dag_ids in import_errors_result:
+ dag_ids = [dag_id for _, dag_id in file_dag_ids]
Review Comment:
It seems like the `file_dag_ids` is still `Iterable[str]` ?
```suggestion
dag_ids = [dag_id for dag_id in file_dag_ids]
```
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Make `batch_is_authorized_` methods team aware [airflow]
Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck merged PR #55306:
URL: https://github.com/apache/airflow/pull/55306
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Make `batch_is_authorized_` methods team aware [airflow]
Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas commented on code in PR #55306:
URL: https://github.com/apache/airflow/pull/55306#discussion_r2331045324
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -255,21 +255,28 @@ def inner(
request: BulkBody[PoolBody],
user: GetUserDep,
) -> None:
+ existing_pool_names = [
+ cast("str", entity) if action.action == BulkAction.DELETE else cast("PoolBody", entity).pool
+ for action in request.actions
+ for entity in action.entities
+ if action.action != BulkAction.CREATE
+ ]
+ teams = Pool.get_bulk_team_name(existing_pool_names)
+
requests: list[IsAuthorizedPoolRequest] = []
for action in request.actions:
- requests.extend(
- [
- {
- "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
- "details": PoolDetails(
- name=cast("str", pool)
- if action.action == BulkAction.DELETE
- else cast("PoolBody", pool).pool
- ),
- }
- for pool in action.entities
- ]
- )
+ for pool in action.entities:
+ pool_name = (
+ cast("str", pool) if action.action == BulkAction.DELETE else cast("PoolBody", pool).pool
+ )
+ req: IsAuthorizedPoolRequest = {
+ "method": MAP_BULK_ACTION_TO_AUTH_METHOD[action.action],
+ "details": PoolDetails(
+ name=pool_name,
+ team_name=teams.get(pool_name),
Review Comment:
Got it, thanks!
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Make `batch_is_authorized_` methods team aware [airflow]
Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #55306:
URL: https://github.com/apache/airflow/pull/55306#discussion_r2331606915
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/import_error.py:
##########
@@ -179,19 +179,21 @@ def get_import_errors(
limit=limit,
session=session,
)
- import_errors_result: Iterable[tuple[ParseImportError, Iterable[str]]] = groupby(
+ import_errors_result: Iterable[tuple[ParseImportError, Iterable]] = groupby(
Review Comment:
Thanks for the clarification Vincent! I see, then the change LGTM.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org