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


##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/middleware.py:
##########
@@ -308,6 +315,18 @@ async def generate_new_user_token(request, new_user):
             return ""
         return get_auth_manager().generate_jwt(new_user)
 
+    @staticmethod
+    def _end_keycloak_session(refresh_token: str) -> None:
+        """End the Keycloak session of ``refresh_token``."""
+        try:
+            KeycloakAuthManager.get_keycloak_client().logout(refresh_token)

Review Comment:
   Logout can hang for up to 60 seconds when Keycloak is slow or unreachable. 
`get_keycloak_client()` builds a `KeycloakOpenID` with python-keycloak's 
default timeout (60s), and the logout response waits on this call. The other 
Keycloak calls in this provider use `timeout=5`. Fix: use a short timeout here, 
for example by adding a `timeout` parameter to `get_keycloak_client`.



##########
providers/keycloak/tests/unit/keycloak/auth_manager/test_middleware.py:
##########
@@ -409,6 +410,53 @@ def test_refreshed_token_does_not_override_logout(self, 
mock_auth_manager):
             for refreshed_token in ["new_token", "new_refresh_token", 
access_token]
         )
 
+    @pytest.mark.parametrize(
+        ("refresh_token", "expected_calls"),
+        [
+            pytest.param("refresh_token", [call("refresh_token")], 
id="with_refresh_token"),
+            pytest.param(None, [], id="without_refresh_token"),
+        ],
+    )
+    @patch(f"{MODULE}.KeycloakAuthManager.get_keycloak_client", autospec=True)
+    def test_logout_ends_keycloak_session(
+        self, mock_get_keycloak_client, mock_auth_manager, refresh_token, 
expected_calls
+    ):
+        cookies = {COOKIE_NAME_JWT_TOKEN: "valid_token", 
COOKIE_NAME_ACCESS_TOKEN: keycloak_token("user_id")}
+        if refresh_token:
+            cookies[COOKIE_NAME_REFRESH_TOKEN] = refresh_token
+        app, _ = _build_app()
+        app.state.auth_manager = mock_auth_manager
+
+        TestClient(app, cookies=cookies).get("/api/v2/auth/logout", 
follow_redirects=False)
+
+        assert mock_get_keycloak_client.return_value.logout.call_args_list == 
expected_calls
+
+    @patch(f"{MODULE}.KeycloakAuthManager.get_keycloak_client", autospec=True)
+    def test_logout_completes_when_keycloak_cannot_end_the_session(
+        self, mock_get_keycloak_client, mock_auth_manager
+    ):
+        mock_get_keycloak_client.return_value.logout.side_effect = 
KeycloakPostError(

Review Comment:
   nit: this test also passes without your change, since `logout` was never 
called before. Can you assert that `logout` was called and that the warning is 
logged?



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