vincbeck commented on code in PR #75938: URL: https://github.com/apache/airflow/pull/75938#discussion_r4603878258
########## airflow-ctl/src/airflowctl/ctl/utils/browser_login.py: ########## @@ -0,0 +1,171 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Browser login with the authorization code flow and PKCE (RFC 7636), over a loopback redirect (RFC 8252).""" + +from __future__ import annotations + +import base64 +import hashlib +import secrets +import time +import webbrowser +from dataclasses import dataclass +from http.server import BaseHTTPRequestHandler, HTTPServer +from urllib.parse import parse_qs, urlencode, urljoin, urlsplit + +from rich.console import Console +from rich.markup import escape + +from airflowctl.exceptions import AirflowCtlBrowserLoginException + +CALLBACK_PATH = "/callback" +_MAX_REQUEST_WAIT = 60.0 +# Sent before the code is exchanged for a token, so it must not claim that the login worked. +_CALLBACK_PAGE = ( + b"<!doctype html><title>airflowctl</title>" + b"<p>You can close this window and return to the terminal to see the result of the login.</p>" +) +_CALLBACK_FAILED_PAGE = ( + b"<!doctype html><title>airflowctl</title><p>Login failed. Return to the terminal for details.</p>" +) + + +@dataclass +class AuthorizationCode: + """Authorization code returned to the loopback redirect, with what the token endpoint needs to redeem it.""" + + code: str + code_verifier: str + redirect_uri: str + + +def build_pkce_pair() -> tuple[str, str]: + """Return a PKCE ``(code_verifier, code_challenge)`` pair for the ``S256`` method.""" + code_verifier = secrets.token_urlsafe(64) + digest = hashlib.sha256(code_verifier.encode("ascii")).digest() + code_challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii") + return code_verifier, code_challenge + + +def resolve_endpoint(api_url: str, endpoint: str) -> str: + """Return the URL of a login metadata endpoint, which must be on the API server.""" + url = urljoin(api_url, endpoint) + if urlsplit(url)[:2] != urlsplit(api_url)[:2]: + raise AirflowCtlBrowserLoginException(f"{endpoint} is not an endpoint of {api_url}.") + return url + + +def _has_login_state(params: dict[str, str], state: str) -> bool: + """Return whether the callback parameters carry the ``state`` of this login.""" + # compare_digest raises TypeError on non-ASCII str. + return secrets.compare_digest(params.get("state", "").encode(), state.encode()) + + +def _find_login_error(params: dict[str, str]) -> str | None: + """Return why the callback parameters of this login do not complete it, or ``None`` if they do.""" + if "error" in params: + return f"Login refused: {params.get('error_description') or params['error']}" + if not params.get("code"): + return "The login response contains no authorization code." + return None + + +class _CallbackServer(HTTPServer): + state: str + callback_params: dict[str, str] | None = None + login_error: str | None = None + + +class _CallbackHandler(BaseHTTPRequestHandler): + server: _CallbackServer + # server.timeout only bounds accept(), so an idle connection such as a browser preconnect would block. + timeout = 2 + + def do_GET(self) -> None: + url = urlsplit(self.path) + if url.path != CALLBACK_PATH or self.server.callback_params is not None: + self.send_error(404) + return + params = {key: values[0] for key, values in parse_qs(url.query).items()} + if not _has_login_state(params, self.server.state): + # Any local process can reach this port, so only the real redirect may end the wait. + self._send_page(400, _CALLBACK_FAILED_PAGE) + return + login_error = _find_login_error(params) + self.server.callback_params = params + self.server.login_error = login_error + self._send_page(400 if login_error else 200, _CALLBACK_FAILED_PAGE if login_error else _CALLBACK_PAGE) + + def _send_page(self, status: int, page: bytes) -> None: + self.send_response(status) + self.send_header("Content-Type", "text/html; charset=utf-8") + self.end_headers() + self.wfile.write(page) + + def log_message(self, format, *args) -> None: + # Request lines contain the authorization code. + pass + + +def get_authorization_code( + *, authorization_url: str, open_browser: bool, timeout: float +) -> AuthorizationCode: + """ + Run the browser part of the login and return the authorization code it produced. + + :param authorization_url: URL of the ``authorization_endpoint`` from the CLI login metadata + :param open_browser: open the login page in the default browser, otherwise only print its URL + :param timeout: seconds to wait for the user to finish logging in + """ + code_verifier, code_challenge = build_pkce_pair() + state = secrets.token_urlsafe(32) + + with _CallbackServer(("127.0.0.1", 0), _CallbackHandler) as server: + server.state = state + redirect_uri = f"http://127.0.0.1:{server.server_address[1]}{CALLBACK_PATH}" + query = urlencode( + { + "redirect_uri": redirect_uri, + "state": state, + "code_challenge": code_challenge, + "code_challenge_method": "S256", + } + ) + separator = "&" if urlsplit(authorization_url).query else "?" + login_url = f"{authorization_url}{separator}{query}" + # `auth token --sso` prints the token on stdout. + Console(stderr=True, soft_wrap=True).print( + f"[blue]Log in from your browser:[/blue] {escape(login_url)}" + ) + if open_browser: + webbrowser.open(login_url) Review Comment: I don't think it's ok, `auth token --sso` is made for `TOKEN=$(...)`. On Linux, `webbrowser` starts `xdg-open` with our stdout. A browser started this way keeps the pipe open, so `$(...)` hangs until the browser is closed. When a browser is already running, Chrome's "Opening in existing browser session." can end up in the token. Fix: flush stdout and point fd 1 to fd 2 with `os.dup2` around `webbrowser.open`, then restore it. ########## airflow-ctl/src/airflowctl/ctl/cli_config.py: ########## @@ -345,6 +357,30 @@ def _load_help_texts_yaml() -> dict[str, dict[str, str]]: dest="password", help="The password to use for authentication", ) +ARG_AUTH_SSO = Arg( + flags=("--sso",), + dest="sso", + default=False, + action="store_true", + help="Log in through the browser with the identity provider of the API server, if it offers it", +) +ARG_AUTH_NO_BROWSER = Arg( + flags=("--no-browser",), + dest="no_browser", + default=False, + action="store_true", + help=( + "With --sso, print the login URL instead of opening a browser. The browser must still reach " + "this machine on 127.0.0.1, for example through 'ssh -L' when logged in remotely" Review Comment: The callback port changes on every run, so the `ssh -L` tunnel can only be set up after the URL is printed. Should we add a way to pick the port, or drop the `ssh -L` example until the device code flow lands? ########## airflow-ctl/src/airflowctl/ctl/utils/browser_login.py: ########## @@ -0,0 +1,171 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Browser login with the authorization code flow and PKCE (RFC 7636), over a loopback redirect (RFC 8252).""" + +from __future__ import annotations + +import base64 +import hashlib +import secrets +import time +import webbrowser +from dataclasses import dataclass +from http.server import BaseHTTPRequestHandler, HTTPServer +from urllib.parse import parse_qs, urlencode, urljoin, urlsplit + +from rich.console import Console +from rich.markup import escape + +from airflowctl.exceptions import AirflowCtlBrowserLoginException + +CALLBACK_PATH = "/callback" +_MAX_REQUEST_WAIT = 60.0 +# Sent before the code is exchanged for a token, so it must not claim that the login worked. +_CALLBACK_PAGE = ( + b"<!doctype html><title>airflowctl</title>" + b"<p>You can close this window and return to the terminal to see the result of the login.</p>" +) +_CALLBACK_FAILED_PAGE = ( + b"<!doctype html><title>airflowctl</title><p>Login failed. Return to the terminal for details.</p>" +) + + +@dataclass +class AuthorizationCode: + """Authorization code returned to the loopback redirect, with what the token endpoint needs to redeem it.""" + + code: str + code_verifier: str + redirect_uri: str + + +def build_pkce_pair() -> tuple[str, str]: + """Return a PKCE ``(code_verifier, code_challenge)`` pair for the ``S256`` method.""" + code_verifier = secrets.token_urlsafe(64) + digest = hashlib.sha256(code_verifier.encode("ascii")).digest() + code_challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii") + return code_verifier, code_challenge + + +def resolve_endpoint(api_url: str, endpoint: str) -> str: + """Return the URL of a login metadata endpoint, which must be on the API server.""" + url = urljoin(api_url, endpoint) + if urlsplit(url)[:2] != urlsplit(api_url)[:2]: + raise AirflowCtlBrowserLoginException(f"{endpoint} is not an endpoint of {api_url}.") + return url + + +def _has_login_state(params: dict[str, str], state: str) -> bool: + """Return whether the callback parameters carry the ``state`` of this login.""" + # compare_digest raises TypeError on non-ASCII str. + return secrets.compare_digest(params.get("state", "").encode(), state.encode()) + + +def _find_login_error(params: dict[str, str]) -> str | None: + """Return why the callback parameters of this login do not complete it, or ``None`` if they do.""" + if "error" in params: + return f"Login refused: {params.get('error_description') or params['error']}" + if not params.get("code"): + return "The login response contains no authorization code." + return None + + +class _CallbackServer(HTTPServer): + state: str + callback_params: dict[str, str] | None = None + login_error: str | None = None + + +class _CallbackHandler(BaseHTTPRequestHandler): + server: _CallbackServer + # server.timeout only bounds accept(), so an idle connection such as a browser preconnect would block. + timeout = 2 + + def do_GET(self) -> None: + url = urlsplit(self.path) + if url.path != CALLBACK_PATH or self.server.callback_params is not None: + self.send_error(404) + return + params = {key: values[0] for key, values in parse_qs(url.query).items()} + if not _has_login_state(params, self.server.state): + # Any local process can reach this port, so only the real redirect may end the wait. + self._send_page(400, _CALLBACK_FAILED_PAGE) Review Comment: nit: on a wrong `state` the terminal prints nothing and keeps waiting, so "Return to the terminal for details" sends the user to an empty terminal. Maybe a separate page saying this link is not for the current login? ########## airflow-ctl/src/airflowctl/ctl/cli_config.py: ########## @@ -345,6 +357,30 @@ def _load_help_texts_yaml() -> dict[str, dict[str, str]]: dest="password", help="The password to use for authentication", ) +ARG_AUTH_SSO = Arg( + flags=("--sso",), + dest="sso", + default=False, + action="store_true", + help="Log in through the browser with the identity provider of the API server, if it offers it", +) +ARG_AUTH_NO_BROWSER = Arg( + flags=("--no-browser",), + dest="no_browser", + default=False, + action="store_true", + help=( + "With --sso, print the login URL instead of opening a browser. The browser must still reach " + "this machine on 127.0.0.1, for example through 'ssh -L' when logged in remotely" + ), +) +ARG_AUTH_TIMEOUT = Arg( + flags=("--timeout",), + type=positive_finite_float, + default=None, + dest="timeout", + help="With --sso, seconds to wait for the browser login to complete", Review Comment: nit: the help doesn't say what the default is anymore ```suggestion help="With --sso, seconds to wait for the browser login to complete (default: 300)", ``` ########## airflow-ctl/src/airflowctl/ctl/commands/auth_command.py: ########## @@ -53,6 +94,44 @@ def login(args, api_client=NEW_API_CLIENT) -> None: password = args.password token = args.api_token or os.environ.get("AIRFLOW_CLI_TOKEN") + _exit_on_sso_only_args(args) + if args.sso: + if args.api_token or username or password: + rich.print( + "[red]--sso is not compatible with --api-token, --username or --password.", file=sys.stderr + ) + sys.exit(1) + try: + api_token = _get_token_with_browser(args, api_client, api_url) + except Exception as e: + rich.print(f"[red]Login failed: {escape(str(e))}", file=sys.stderr) + sys.exit(1) + # Debug mode stores the token in a file even with --skip-keyring. + is_stored = not args.skip_keyring or os.getenv("AIRFLOW_CLI_DEBUG_MODE") == "true" Review Comment: nit: this repeats the debug-mode rule from `Credentials.save`. Maybe `save()` could return whether it stored the token? -- 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]
