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


##########
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:
   Agreed. 809b8f76bd removes the state from the auth manager and logs the 
missing role at debug level, and drops the warn-once test.



##########
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:
   Moved to `tests/unit/fab/auth_manager/models/test_anonymous_user.py` in 
74a6e842bf.



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