ChickenBenny opened a new pull request, #77311:
URL: https://github.com/apache/airflow/pull/77311

   ## Why
   
   With `[fab] auth_role_public` set, people who aren't logged in get the 
permissions of that role. If
   the role can read every Dag (`can_read` on `DAGs`), everything works. But if 
it can only read some
   Dags, say `can_read` on `DAG:dag_a`, the Dag list comes back empty, even 
though `dag_a` opens fine
   when you go to it directly:
   
   | Request (not logged in) | Expected | Before this PR |
   |---|---|---|
   | `GET /api/v2/dags` | 200, `dag_a` | 200, empty list |
   | `GET /api/v2/dags/dag_a` | 200 | 200 |
   | `GET /api/v2/dags/dag_b` | 403 | 403 |
   
   So the UI shows no Dags at all, but pasting the URL of `dag_a` works.
   
   The list goes through two checks. `is_authorized_dag` decides whether you 
can list Dags at all, and
   `get_authorized_dag_ids` decides which ones you see. The first one reads 
`user.perms`, finds
   `DAG:dag_a` and lets the request in. The second one just returned `set()` 
for an `AnonymousUser`:
   
   ```python
   if isinstance(user, AnonymousUser):
       return set()
   ```
   
   That shortcut was there because the code after it loads the user's roles by 
`User.id`, and the
   anonymous user has no row in `ab_user`, so it has no `id`. But 
`build_public_user()` now loads the
   public role's permissions into `user.perms` when the user is built, and the 
other checks already
   read them from there. `get_authorized_dag_ids` was the only one that didn't.
   
   ## What
   
   - For the anonymous user, read the permissions from `user.perms` instead of 
returning `set()`.
   - For a logged-in user, the DB query stays the same. Its roles are just 
turned into the same
     `(action, resource)` pairs, so both kinds of user share one loop. Nothing 
changes for them.
   
   I kept the DB lookup for logged-in users on purpose instead of reading 
`user.perms` for them too.
   `deserialize_user` caches the user object for `[fab] cache_ttl` seconds, so 
`user.perms` can be
   older than the DB. Switching them over would change behaviour beyond this 
fix.
   
   ## Testing
   
   - New `test_get_authorized_dag_ids_for_public_user`: it creates a role, sets 
it as
     `AUTH_ROLE_PUBLIC`, builds the user with `build_public_user()` and checks 
the ids for `GET` with
     `can_read` on one Dag and `PUT` with `can_edit` on two Dags. Both cases 
return `set()` without the
     fix.
   - The existing `test_get_authorized_dag_ids` cases for logged-in users pass 
unchanged.
   - Reproduced in Breeze through the API with a real DB and the FAB auth 
manager, not logged in,
     with a public role that can read only `DAG:repro_dag_a`: `GET 
/api/v2/dags` went from `[]` to
     `['repro_dag_a']`. `GET /api/v2/dags/repro_dag_b` still returns 403.
   - `breeze run pytest 
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py 
providers/fab/tests/unit/fab/auth_manager/api_fastapi`:
     318 passed, 1 failed. The failure is 
`test_login.py::TestLogin::test_logout` and is unrelated to
     this change: it fails the same way on `main` when these files run 
together, and passes when
     `test_login.py` runs alone.
   - `breeze run mypy 
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py`: no
     issues.
   - `prek run --stage pre-commit` on the changed files: passed.
   
   ## Related
   
   - #60897: the issue `build_public_user()` was added for. It loads the public 
role's permissions into
     the user, which is what this fix reads.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code (Opus 5.5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


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