ChickenBenny opened a new pull request, #77538:
URL: https://github.com/apache/airflow/pull/77538
* related: #65685
* related: #69773
---
Public access in the FAB auth manager breaks in the API server in two cases.
Both happen because the FastAPI side reads Flask's `current_app`, and FastAPI
requests and startup run with no Flask app context.
### 1. The legacy `AUTH_ROLE_PUBLIC` in `webserver_config.py` is ignored
The FAB docs describe only this setting, with no mention of `[fab]
auth_role_public`, so it is what users following them set. When
`AUTH_ROLE_PUBLIC` is set only in `webserver_config.py`, and `[fab]
auth_role_public` is not set, anonymous requests get a 401, and nothing is
logged.
`_get_auth_role_public` falls back to `self.appbuilder.app.config`. Since
the FAB 5 upgrade (#50960), `appbuilder.app` returns `current_app` instead of
the app it was initialised with. With no app context, it raises `RuntimeError`,
and the `except` returns `None`. So `get_fastapi_middlewares()` does not
register `FabAuthRolePublicMiddleware`, and `build_public_user()` builds no
user.
The value is already in the config of the Flask app that `get_fastapi_app()`
builds and keeps on `self.flask_app`. The fallback now reads it from there. It
keeps `current_app` only when there is an app context and no such app.
The docs now recommend `[fab] auth_role_public`, and describe the
`webserver_config.py` entry as legacy. This PR does not deprecate the legacy
setting.
### 2. Anonymous requests fail with a 500 once the public role is missing
The API server creates the public role at startup. If the role is deleted
while the server runs, for example through `DELETE /auth/fab/v1/roles/{name}`,
every anonymous request that checks permissions fails with a 500 until the
server restarts.
`build_public_user()` left `_roles` and `_perms` at their class default
`set()`. `AnonymousUser` treats an empty set as "not loaded yet", so reading
`perms` falls back to `current_app` and raises `RuntimeError`. Now:
- `None` means "not loaded yet".
- `build_public_user()` sets both to empty sets when the role does not exist.
- It logs a warning once, until the role exists again. Every anonymous
request builds the public user, health checks included, so a warning on each
one would flood the logs.
Anonymous requests then get a 403, the same as when the public role exists
but lacks the permission. With public access configured, an anonymous user is
identified, so this is a missing permission, not a missing login.
The same lazy path in Flask views no longer builds `{None}` for a missing
role, which failed with `AttributeError`.
`build_public_user()` still queries the role on every anonymous request, as
before this PR.
### Before / after
| Anonymous `GET /api/v2/pools` | Before | After |
|---|---|---|
| `AUTH_ROLE_PUBLIC = 'Viewer'` only in `webserver_config.py` | 401 | 200
(and `DELETE` gives 403) |
| `[fab] auth_role_public` set, role deleted while the server runs | 500 |
403 |
### Validation
- New tests in `test_public_access.py` start the API server with
`airflow.api_fastapi.app.create_app` and send anonymous requests, with nothing
mocked:
- one for the legacy setting
- one that deletes the public role while the server runs
- Unit tests cover:
- the Flask view path for a missing role
- the warning being logged once until the role exists again, counted from
the structured log entries
- Each of these tests fails without its fix.
- `real_app_client` now restores the auth manager's `flask_app` when it
finishes. The auth manager is shared across tests, and the app this fixture
left on it made later tests read its config.
- `providers/fab/tests/unit/fab`: 803 passed, 1 failed. The failure is
`test_cleanup_dag_permissions_removes_specific_dag_resources`, which fails the
same way locally on `main` without these changes.
- `breeze build-docs common.compat fab` passed.
- `prek run --stage pre-commit` passed. `prek run --stage manual` passed
except `mypy-providers`, which reports only errors in
`providers/cncf/kubernetes`, which this PR does not change.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes — Claude Code (Opus 5.5)
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]