vincbeck commented on code in PR #77538:
URL: https://github.com/apache/airflow/pull/77538#discussion_r4681248854


##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -392,23 +393,22 @@ def _get_auth_role_public(self) -> str | None:
         """
         Return the role granted to anonymous users, or ``None`` if public 
access is disabled.
 
-        Reads ``[fab] auth_role_public`` from the Airflow config first so this 
works
-        during FastAPI startup, before any Flask application context exists. 
Falls back
-        to ``AUTH_ROLE_PUBLIC`` on the Flask app config when an app context is 
available,
-        which covers the legacy ``AUTH_ROLE_PUBLIC`` entry in 
``webserver_config.py``.
+        Reads ``[fab] auth_role_public`` from the Airflow config first, then 
falls back to
+        ``AUTH_ROLE_PUBLIC`` on the Flask app config, which covers the legacy 
entry in
+        ``webserver_config.py``.
         """
         role_public = conf.get("fab", "auth_role_public", fallback="") or None
         if role_public:
             return role_public
         if self.appbuilder is None:
             return None
-        try:
-            return self.appbuilder.app.config.get("AUTH_ROLE_PUBLIC", None)
-        except RuntimeError:
-            # ``appbuilder.app`` is a werkzeug ``LocalProxy``; accessing it 
outside a
-            # Flask application context raises ``RuntimeError``. This happens 
during
-            # FastAPI startup when ``get_fastapi_middlewares`` is invoked.
-            return None
+        # The API server runs FastAPI requests and startup without a Flask app 
context, where
+        # ``current_app`` raises, so read the Flask app it built in 
``get_fastapi_app``.
+        if self.flask_app is not None:

Review Comment:
   Why not check `has_app_context()` first? Flask code would keep reading its 
own app like before, and FastAPI would still fall back to `self.flask_app`. I 
think it would also remove the need to restore `flask_app` in the test fixtures.



##########
providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py:
##########
@@ -534,6 +535,41 @@ def 
test_build_public_user_with_unknown_role_returns_user_with_empty_perms(
         # Role not found in the DB, so no pre-populated perms.
         assert user._perms == set()
 
+    def 
test_build_public_user_warns_once_about_a_missing_role_until_it_exists_again(
+        self, flask_app, auth_manager_with_appbuilder, caplog
+    ):
+        def count_warnings():
+            event = "The public role 'DoesNotExist' does not exist, so 
anonymous users get no permissions"
+            return sum(entry["event"] == event for entry in caplog.entries)
+
+        def build_public_user_with_role(role_name):
+            flask_app.config["AUTH_ROLE_PUBLIC"] = role_name
+            auth_manager_with_appbuilder.build_public_user()
+
+        previous = flask_app.config.get("AUTH_ROLE_PUBLIC")
+        try:
+            build_public_user_with_role("Viewer")
+            build_public_user_with_role("DoesNotExist")
+            build_public_user_with_role("DoesNotExist")
+            assert count_warnings() == 1
+
+            build_public_user_with_role("Viewer")
+            build_public_user_with_role("DoesNotExist")
+            assert count_warnings() == 2
+        finally:
+            flask_app.config["AUTH_ROLE_PUBLIC"] = previous
+
+    def 
test_anonymous_user_with_unknown_role_has_no_perms_in_an_app_context(self, 
flask_app):

Review Comment:
   nit: this test only covers `AnonymousUser`, it should be in 
`tests/unit/fab/auth_manager/models/test_anonymous_user.py`



##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -429,10 +429,22 @@ def build_public_user(self, *, session: Session = 
NEW_SESSION) -> AnonymousUser
 
         user = AnonymousUser()
         role = session.scalar(select(Role).where(Role.name == 
public_role_name))
-        if role is not None:
-            # ``AnonymousUser.roles``/``perms`` normally resolve lazily 
through Flask's
-            # ``current_app``. Writing ``_roles``/``_perms`` directly freezes 
a snapshot for
-            # the lifetime of a single FastAPI authorization check.
+        # ``AnonymousUser.roles``/``perms`` normally resolve lazily through 
Flask's
+        # ``current_app``. Writing ``_roles``/``_perms`` directly freezes a 
snapshot for
+        # the lifetime of a single FastAPI authorization check.
+        if role is None:
+            # Every anonymous request builds the public user, health checks 
included, so warn once
+            # until the role exists again.
+            if self._missing_public_role != public_role_name:

Review Comment:
   Is that needed? The role is created at startup, so this only happens if 
someone deletes it while the server runs. I would keep it simple: no state on 
the auth manager, just a `log.debug`.



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