dabla commented on code in PR #76353:
URL: https://github.com/apache/airflow/pull/76353#discussion_r4579907975
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/middleware.py:
##########
@@ -82,6 +82,11 @@ async def dispatch(self, request: Request, call_next):
HTTPException,
):
new_token = ""
+ # The session ends here and the response clears the Airflow
JWT. Hide it
+ # from the inner middlewares and the route too: resolving it
again without
+ # the Keycloak tokens yields no user, which ``get_user`` would
hand to the
+ # authorization checks instead of answering 401.
+ self._drop_cookie(request, COOKIE_NAME_JWT_TOKEN)
Review Comment:
Good catch, thanks. Both reproduce through the real middleware stack: with
an expired Keycloak refresh token (or no Keycloak cookies)
`collect_request_tokens` returned `['token']` on `main` and `[]` on this
branch, and a rejected JWT went from 403 to 401.
Fixed in 4306de457c:
- The middleware now revokes the Airflow JWT before hiding it, on every path
where it ends the session, not only on logout. It skips that when the JWT
itself was rejected (expired, invalid or already revoked): there is nothing to
revoke then, and `JWTValidator.revoke_token` would log a warning with a
traceback on each such request. To tell the cases apart, `_refresh_user` now
chains its 401/403 `from` the JWT error.
- `revoke_token` validates the token and writes to the database, so it runs
through `run_in_threadpool`. It only exists from Airflow 3.2, so it is looked
up with `getattr`; the provider still supports 3.0.
- Tests, all through the middlewares as `init_middlewares` installs them: an
expired Keycloak session and a session without Keycloak cookies revoke the
token (both fail on the previous commit), a healthy session does not, and a
rejected JWT answers 401, clears the cookie and revokes nothing.
On 403 → 401: I kept the 401, since the UI redirects to login on it and the
route has no credential left to call invalid. It is in the PR description now.
--
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]