vincbeck commented on code in PR #75913:
URL: https://github.com/apache/airflow/pull/75913#discussion_r4634957573
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/services/token.py:
##########
@@ -34,45 +41,128 @@
CONF_SECTION_NAME,
CONF_SERVER_URL_KEY,
)
-from airflow.providers.keycloak.auth_manager.keycloak_auth_manager import
KeycloakAuthManager
+from airflow.providers.keycloak.auth_manager.exceptions import (
+ AuthManagerRefreshTokenExpiredException,
+ is_keycloak_outage,
+)
+from airflow.providers.keycloak.auth_manager.keycloak_auth_manager import (
+ KeycloakAuthManager,
+ is_cli_authorization_code_enabled,
+)
from airflow.providers.keycloak.auth_manager.user import
KeycloakAuthManagerUser
+from airflow.providers.keycloak.version_compat import AIRFLOW_V_3_2_PLUS
+# RFC 8252 section 7.3 loopback redirects. ``localhost`` is left out because
it can resolve
+# to a non-loopback interface.
+CLI_LOOPBACK_HOSTS = frozenset({"127.0.0.1", "::1"})
+CLI_CALLBACK_PATH = "/callback"
-@cache
-def _get_jwks_client(issuer: str) -> PyJWKClient:
- """Return the cached JWK client for a Keycloak realm issuer."""
- return PyJWKClient(f"{issuer}/protocol/openid-connect/certs")
+log = logging.getLogger(__name__)
-def create_token_for(
- username: str,
- password: str,
- expiration_time_in_seconds: int = conf.getint("api_auth",
"jwt_expiration_time"),
-) -> str:
- client = KeycloakAuthManager.get_keycloak_client()
+def build_keycloak_unavailable_error(error: Exception) -> HTTPException:
+ log.warning(
+ "Keycloak is unavailable (%s, response code %s)",
+ type(error).__name__,
+ getattr(error, "response_code", None),
+ )
+ return HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Keycloak is unavailable")
+
+
+@contextmanager
+def translate_keycloak_outage(rejected_detail: str | None = None) ->
Iterator[None]:
+ """
+ Raise a ``503`` instead of a Keycloak error that
:func:`is_keycloak_outage` matches.
+ With ``rejected_detail``, any other Keycloak error becomes a ``403`` with
that detail.
+ """
try:
- tokens = client.token(username, password)
- except KeycloakAuthenticationError:
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN,
- detail="Invalid credentials",
- )
+ yield
+ except KeycloakError as e:
+ if is_keycloak_outage(e):
+ raise build_keycloak_unavailable_error(e) from e
+ if rejected_detail is None:
+ raise
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
detail=rejected_detail)
+
+def is_cli_loopback_redirect_uri(redirect_uri: str) -> bool:
+ """Return whether ``redirect_uri`` is a loopback callback a command-line
client listens on."""
+ try:
+ parts = urlsplit(redirect_uri)
+ port = parts.port
+ except ValueError:
+ return False
+ return (
+ parts.scheme == "http"
+ and parts.hostname in CLI_LOOPBACK_HOSTS
+ and port is not None
+ # Reject userinfo: ``:[email protected]:1`` still has a loopback hostname.
+ and "@" not in parts.netloc
+ and parts.path == CLI_CALLBACK_PATH
Review Comment:
The core contract in `auth-manager/index.rst` only asks for an `http`
loopback `redirect_uri`, so a client using another path than `/callback` gets a
400 here. Should the contract mention `/callback`?
--
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]