You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "dabla (via GitHub)" <gi...@apache.org> on 2025/12/12 12:03:00 UTC

[I] Internal Server Error in Airflow API server with keycloak provider when token is not active [airflow]

dabla opened a new issue, #59359:
URL: https://github.com/apache/airflow/issues/59359

   ### Apache Airflow Provider(s)
   
   keycloak
   
   ### Versions of Apache Airflow Providers
   
   We are using the latest airflow providers shipped with Airflow 3.1.4 constraints.
   
   The issue arised with Airflow 3.1.4 and apache-airflow-providers-keycloak 0.3.0.
   
   ### Apache Airflow version
   
   3.1.4
   
   ### Operating System
   
   Linux
   
   ### Deployment
   
   Other 3rd-party Helm chart
   
   ### Deployment details
   
   _No response_
   
   ### What happened
   
   When we keep a browser open for a long time, like overnight, and we want to refresh the Airflow dags page, the Airflow api server crashes with following exception:
   
   ```
   INFO:     172.31.52.95:0 - "GET /favicon.ico HTTP/1.1" 500 Internal Server Error
   ERROR:    Exception in ASGI application
     + Exception Group Traceback (most recent call last):
     |   File "/usr/local/lib/python3.13/site-packages/starlette/_utils.py", line 79, in collapse_excgroups
     |     yield
     |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 183, in __call__
     |     async with anyio.create_task_group() as task_group:
     |                ~~~~~~~~~~~~~~~~~~~~~~~^^
     |   File "/usr/local/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 783, in __aexit__
     |     raise BaseExceptionGroup(
     |         "unhandled errors in a TaskGroup", self._exceptions
     |     ) from None
     | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
     +-+---------------- 1 ----------------
       | Traceback (most recent call last):
       |   File "/usr/local/lib/python3.13/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
       |     result = await app(  # type: ignore[func-returns-value]
       |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |         self.scope, self.receive, self.send
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |     )
       |     ^
       |   File "/usr/local/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
       |     return await self.app(scope, receive, send)
       |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |   File "/usr/local/lib/python3.13/site-packages/fastapi/applications.py", line 1082, in __call__
       |     await super().__call__(scope, receive, send)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/applications.py", line 113, in __call__
       |     await self.middleware_stack(scope, receive, send)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 186, in __call__
       |     raise exc
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 164, in __call__
       |     await self.app(scope, receive, _send)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 29, in __call__
       |     await responder(scope, receive, send)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 130, in __call__
       |     await super().__call__(scope, receive, send)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 46, in __call__
       |     await self.app(scope, receive, self.send_with_compression)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/cors.py", line 85, in __call__
       |     await self.app(scope, receive, send)
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 182, in __call__
       |     with recv_stream, send_stream, collapse_excgroups():
       |                                    ~~~~~~~~~~~~~~~~~~^^
       |   File "/usr/lib64/python3.13/contextlib.py", line 162, in __exit__
       |     self.gen.throw(value)
       |     ~~~~~~~~~~~~~~^^^^^^^
       |   File "/usr/local/lib/python3.13/site-packages/starlette/_utils.py", line 85, in collapse_excgroups
       |     raise exc
       |   File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 184, in __call__
       |     response = await self.dispatch_func(request, call_next)
       |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |   File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 45, in dispatch
       |     new_user = await self._refresh_user(current_token)
       |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |   File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 68, in _refresh_user
       |     return get_auth_manager().refresh_user(user=user)
       |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
       |   File "/usr/local/lib/python3.13/site-packages/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py", line 121, in refresh_user
       |     tokens = client.refresh_token(user.refresh_token)
       |   File "/usr/local/lib/python3.13/site-packages/keycloak/keycloak_openid.py", line 410, in refresh_token
       |     return raise_error_from_response(data_raw, KeycloakPostError)
       |   File "/usr/local/lib/python3.13/site-packages/keycloak/exceptions.py", line 195, in raise_error_from_response
       |     raise error(
       |     ...<3 lines>...
       |     )
       | keycloak.exceptions.KeycloakPostError: 400: b'{"error":"invalid_grant","error_description":"Token is not active"}'
       +------------------------------------
   
   During handling of the above exception, another exception occurred:
   
   Traceback (most recent call last):
     File "/usr/local/lib/python3.13/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
       result = await app(  # type: ignore[func-returns-value]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           self.scope, self.receive, self.send
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       )
       ^
     File "/usr/local/lib/python3.13/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
       return await self.app(scope, receive, send)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/usr/local/lib/python3.13/site-packages/fastapi/applications.py", line 1082, in __call__
       await super().__call__(scope, receive, send)
     File "/usr/local/lib/python3.13/site-packages/starlette/applications.py", line 113, in __call__
       await self.middleware_stack(scope, receive, send)
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 186, in __call__
       raise exc
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/errors.py", line 164, in __call__
       await self.app(scope, receive, _send)
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 29, in __call__
       await responder(scope, receive, send)
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 130, in __call__
       await super().__call__(scope, receive, send)
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/gzip.py", line 46, in __call__
       await self.app(scope, receive, self.send_with_compression)
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/cors.py", line 85, in __call__
       await self.app(scope, receive, send)
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 182, in __call__
       with recv_stream, send_stream, collapse_excgroups():
                                      ~~~~~~~~~~~~~~~~~~^^
     File "/usr/lib64/python3.13/contextlib.py", line 162, in __exit__
       self.gen.throw(value)
       ~~~~~~~~~~~~~~^^^^^^^
     File "/usr/local/lib/python3.13/site-packages/starlette/_utils.py", line 85, in collapse_excgroups
       raise exc
     File "/usr/local/lib/python3.13/site-packages/starlette/middleware/base.py", line 184, in __call__
       response = await self.dispatch_func(request, call_next)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 45, in dispatch
       new_user = await self._refresh_user(current_token)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/usr/local/lib/python3.13/site-packages/airflow/api_fastapi/auth/middlewares/refresh_token.py", line 68, in _refresh_user
       return get_auth_manager().refresh_user(user=user)
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
     File "/usr/local/lib/python3.13/site-packages/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py", line 121, in refresh_user
       tokens = client.refresh_token(user.refresh_token)
     File "/usr/local/lib/python3.13/site-packages/keycloak/keycloak_openid.py", line 410, in refresh_token
       return raise_error_from_response(data_raw, KeycloakPostError)
     File "/usr/local/lib/python3.13/site-packages/keycloak/exceptions.py", line 195, in raise_error_from_response
       raise error(
       ...<3 lines>...
       )
   keycloak.exceptions.KeycloakPostError: 400: b'{"error":"invalid_grant","error_description":"Token is not active"}'
   ```
   
   
   ### What you think should happen instead
   
   When the KeycloakAuthManager tries to refresh the user and thus calls the refresh_token method from the Keycloak client and the later one fails with an KeycloakPostError, it should catch that exception and return None instead of propagating that exception as this makes the API server crash with HTTP 500 Internal Server Error.
   
   Without that patch, the only way to fix this problem is to clear the cookies related to the API server and then refresh the page in the browser.
   
   The above fix or clearing the cookies as work around will force Keycloak to redo an authentication and thus refresh the token successfully instead of relying on an expired token in the cookie.
   
   ### How to reproduce
   
   Just keep the Airflow DAG's page open for a long time until the token expires on Keycloak side, then refresh it and you'll get an Internal Server Error page from the Airflow API server.
   
   <img width="577" height="250" alt="Image" src="https://github.com/user-attachments/assets/679a2242-f3fc-4cc1-8624-74e131ab59ce" />
   
   ### Anything else
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
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: commits-unsubscribe@airflow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "dabla (via GitHub)" <gi...@apache.org>.
dabla commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3649141653

   There is still an issue with the logout route which is overriden in the keycloak provider, I'm also fixing it and create a PR for it.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3769708693

   Fix: https://github.com/apache/airflow/pull/60781


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3756450149

   Multiple things in this issue:
   - As mentioned by @Ilardi, the refresh token works as long as the refresh token is valid. That's how the refresh token flow works in general. You have a JWT token that expires, when this token expires it is automatically refreshed using another token: `refresh_token`. However, when this token expire, there is not much we can do, the session expired and as a result you get some access denied exceptions. If you want to increase the refresh token lifetime you can do it in Keycloak by configuring `Client Session Idle `
   - We should handle that scenario in the refresh_token middleware so that we no longer have 500s. I'll take that one
   - We should also probably redirect automatically the user to the login page when that happens. I'll take that one too


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3780447303

   Everything has been backported so I can confirm this will be available at the next Airflow version 3.1.7 and next Keycloak provider version


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck closed issue #59359: Internal Server Error in Airflow API server with Keycloak provider when token is not active
URL: https://github.com/apache/airflow/issues/59359


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "ecodina (via GitHub)" <gi...@apache.org>.
ecodina commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3773809055

   It's great to see this is fixed! Many thanks @vincbeck 
   
   Right now we are clearing cookies twice a day when the token expires. Is the fix planned for the next release of KC provider + 3.1.7, or will it be available on 3.2? I see some changes in airflow-core


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3780229942

   Backport: https://github.com/apache/airflow/pull/60881


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3785534798

   Next one (that is released next week). So given the current one is `0.5.0`, it will be either `0.5.1` or `0.6.0`


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 closed issue #59359: Internal Server Error in Airflow API server with Keycloak provider when token is not active
URL: https://github.com/apache/airflow/issues/59359


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "Ilardi (via GitHub)" <gi...@apache.org>.
Ilardi commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3748530473

   I just wanted to add that I have the same issue in airflow 3.1.4 but it only seems to happen when the refresh_token is expired. If the access_token is expired but the refresh_token is valid, upon reloading the page, a new airflow token is created so it appears to work correctly.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "nassereddinebelghith (via GitHub)" <gi...@apache.org>.
nassereddinebelghith commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3784427566

   Which version of the provider will include these changes 


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "ecodina (via GitHub)" <gi...@apache.org>.
ecodina commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3780612270

   Many thanks!


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "nassereddinebelghith (via GitHub)" <gi...@apache.org>.
nassereddinebelghith commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3708349824

   I am facing the same issue with airflow 3.1.5 and keycloak auth manager 0.4.1 , I tried to override the refresh tokens method and the refresh user but I am getting 403 instead of 500 error , the issue is that refresh token method is returning an empty dict {}. 


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "dabla (via GitHub)" <gi...@apache.org>.
dabla commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3743548708

   > I am facing the same issue with airflow 3.1.5 and keycloak auth manager 0.4.1 , I tried to override the refresh tokens method and the refresh user but I am getting 403 instead of 500 error , the issue is that refresh token method is returning an empty dict {}.
   
   You have this error I suppose?
   
   <img width="660" height="362" alt="Image" src="https://github.com/user-attachments/assets/f4e1bf40-aea6-4b59-b19c-513e129178f0" />
   
   I think we need to refactor how the id_token_hint is stored today in keycloack provider, I was afraid that was going to be an issue.  Will reopen this one.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "nassereddinebelghith (via GitHub)" <gi...@apache.org>.
nassereddinebelghith commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3743567940

   I was able to manage the access token / refresh token expiration through nginx as I have a L7 layer but I am not so fan of my solution as it is a workaround. My issue was that when the refresh token is expired I got a 403 (after changing the 500 internal server error) that means that airflow is not launching the auth mechanism after a refresh token expired 


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3760865847

   I'll create multiple PRs related to this issue. The first one is fixing the logout flow in Keycloak. Currently, you need to have an active session (an active refresh_token) to logout. It should not be. See https://github.com/apache/airflow/pull/60649


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Internal Server Error in Airflow API server with Keycloak provider when token is not active [airflow]

Posted by "vincbeck (via GitHub)" <gi...@apache.org>.
vincbeck commented on issue #59359:
URL: https://github.com/apache/airflow/issues/59359#issuecomment-3773887785

   We probably should release it as well in 3.1.7. I'll backport it. And yes, changes in Keycloak provider will be released in the next provider release


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org