Skip to content

Fix FAB public access in the API server without a Flask app context - #77538

Merged
vincbeck merged 7 commits into
apache:mainfrom
ChickenBenny:fab-public-user-without-app-context
Nov 25, 2026
Merged

vincbeck merged 7 commits into
apache:mainfrom
ChickenBenny:fab-public-user-without-app-context

Conversation

@ChickenBenny

@ChickenBenny ChickenBenny commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 still reads current_app inside a Flask app context, like the other places that read AUTH_ROLE_PUBLIC, and reads self.flask_app outside one.

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, and logs it at debug level.

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

    They are skipped before Airflow 3.3, because the API server only registers auth manager middlewares through get_fastapi_middlewares since 3.3.0 (Honor AUTH_ROLE_PUBLIC in FastAPI API server #65685). Before that, anonymous requests always get a 401 from the API server.

  • A unit test covers the Flask view path for a missing role.

  • Each of these tests fails without its fix.

  • providers/fab/tests/unit/fab, before rebasing on the latest main: 802 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?
  • Yes — Claude Code (Opus 5.5)

Generated-by: Claude Code (Opus 5.5) following the guidelines

@ChickenBenny
ChickenBenny force-pushed the fab-public-user-without-app-context branch from 1668769 to dd98154 Compare November 25, 2026 09:33
@ChickenBenny

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All comments are addressed and CI is green now.

The compat failures on Airflow 3.0.6, 3.1.8 and 3.2.2 came from the new API server tests in test_public_access.py. The API server only registers auth manager middlewares through get_fastapi_middlewares since 3.3.0 (#65685), so before that anonymous requests never reach build_public_user and always get a 401. 841dda44ab skips these two tests before 3.3. The AnonymousUser unit test still runs on every version.

I also rebased on the latest main to resolve the conflict in test_fab_auth_manager.py.

@vincbeck
vincbeck enabled auto-merge (squash) November 25, 2026 14:59
@vincbeck
vincbeck merged commit 64afc26 into apache:main Nov 25, 2026
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants