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


##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py:
##########
@@ -525,6 +525,19 @@ def get_keycloak_client(client_id: str | None = None, 
client_secret: str | None
             realm_name=realm,
         )
 
+    @staticmethod
+    def _end_keycloak_session(refresh_token: str) -> None:
+        client = KeycloakAuthManager.get_keycloak_client()
+        client.connection.timeout = 5

Review Comment:
   This does not cap the logout at 5 seconds. `KeycloakOpenID` uses 
`max_retries=1` by default and python-keycloak adds POST to the retry list, so 
when Keycloak hangs `logout` waits 5s, retries, and waits again (the 5s also 
applies separately to connect and read). Fix: build the client with `timeout=5` 
and `max_retries=0`, for example via new parameters on `get_keycloak_client`.



##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py:
##########
@@ -525,6 +525,19 @@ def get_keycloak_client(client_id: str | None = None, 
client_secret: str | None
             realm_name=realm,
         )
 
+    @staticmethod
+    def _end_keycloak_session(refresh_token: str) -> None:
+        client = KeycloakAuthManager.get_keycloak_client()
+        client.connection.timeout = 5
+        try:
+            client.logout(refresh_token)
+        except KeycloakError as e:
+            log.warning(

Review Comment:
   nit: a 400 `invalid_grant` is expected here when the Keycloak session 
already expired (user idle longer than the SSO idle timeout), so this would 
warn on many normal logouts. Maybe use `is_keycloak_outage(e)` to log a warning 
only on outages and debug otherwise?



##########
providers/keycloak/tests/unit/keycloak/auth_manager/test_middleware.py:
##########
@@ -409,6 +410,59 @@ 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
+        if expected_calls:
+            assert mock_get_keycloak_client.return_value.connection.timeout == 
5
+
+    @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, caplog
+    ):
+        mock_get_keycloak_client.return_value.logout.side_effect = 
KeycloakPostError(

Review Comment:
   nit: can you parametrize this with `KeycloakConnectionError` too? That is 
the unreachable Keycloak case from the description, and today the test would 
still pass if the `except` were narrowed to `KeycloakPostError`.



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