From 34fb23c6bded2f2aea9dcf949242e55c39565736 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Mon, 16 Nov 2026 23:22:11 +0100 Subject: [PATCH 01/10] feat: re-read the environment access token on every platform request Co-Authored-By: Claude Fable 5.1 --- packages/uipath-platform/CLAUDE.md | 1 + .../src/uipath/platform/_uipath.py | 6 + .../src/uipath/platform/common/_api_client.py | 8 +- .../uipath/platform/common/_base_service.py | 50 ++++- .../src/uipath/platform/common/_config.py | 15 +- .../tests/services/test_env_token_reread.py | 209 ++++++++++++++++++ 6 files changed, 282 insertions(+), 7 deletions(-) create mode 100644 packages/uipath-platform/tests/services/test_env_token_reread.py diff --git a/packages/uipath-platform/CLAUDE.md b/packages/uipath-platform/CLAUDE.md index 327ac1eee..f1a577d6b 100644 --- a/packages/uipath-platform/CLAUDE.md +++ b/packages/uipath-platform/CLAUDE.md @@ -57,6 +57,7 @@ Credentials can be provided via constructor parameters or environment variables - **User authentication** — provide `base_url` and `secret` (access token), or set them via env vars - **S2S (service-to-service)** — provide `client_id` with either `client_secret` or `client_assertion` (an OIDC token for federated credentials), optionally with `scope`; uses `ExternalApplicationService` internally to exchange for an access token +- **Token renewal** — a `UiPath()` whose secret came from the environment (`UiPathApiConfig.secret_from_environment`) re-reads `UNATTENDED_USER_ACCESS_TOKEN` / `UIPATH_ACCESS_TOKEN` on every request, so a job runtime that renews the token there (`uipath/_cli/_job_token.py`) is picked up by existing clients. An explicit `secret=` is sent as is. Configuration is validated with Pydantic; missing `base_url` raises `BaseUrlMissingError`, missing `secret` raises `SecretMissingError`. diff --git a/packages/uipath-platform/src/uipath/platform/_uipath.py b/packages/uipath-platform/src/uipath/platform/_uipath.py index bfcb1312b..139ba8fe5 100644 --- a/packages/uipath-platform/src/uipath/platform/_uipath.py +++ b/packages/uipath-platform/src/uipath/platform/_uipath.py @@ -66,6 +66,9 @@ def __init__( scope: Optional[str] = None, debug: bool = False, ) -> None: + # A token read from the environment is re-read on every request (a long job's + # runtime may renew it there); an explicit secret is sent as is. + secret_from_environment = False try: if _has_valid_client_credentials( client_id, client_secret, client_assertion @@ -77,12 +80,15 @@ def __init__( ) base_url, secret = service._base_url, token_data.access_token else: + secret_from_environment = not secret base_url, secret = resolve_config_from_env(base_url, secret) self._config = UiPathApiConfig( base_url=base_url, secret=secret, ) + if secret_from_environment: + self._config.mark_secret_from_environment() except ValidationError as e: for error in e.errors(): if error["loc"][0] == "base_url": diff --git a/packages/uipath-platform/src/uipath/platform/common/_api_client.py b/packages/uipath-platform/src/uipath/platform/common/_api_client.py index da9b91f4a..556647b02 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_api_client.py +++ b/packages/uipath-platform/src/uipath/platform/common/_api_client.py @@ -30,8 +30,10 @@ def request( **kwargs: Any, ) -> Response: if kwargs.get("include_folder_headers", False): + # The client's default headers are merged by httpx; copying them here would + # make the default Authorization look caller-supplied to BaseService. kwargs["headers"] = { - **kwargs.get("headers", self._client.headers), + **kwargs.get("headers", {}), **self.folder_headers, } @@ -48,8 +50,10 @@ async def request_async( **kwargs: Any, ) -> Response: if kwargs.get("include_folder_headers", False): + # The client's default headers are merged by httpx; copying them here would + # make the default Authorization look caller-supplied to BaseService. kwargs["headers"] = { - **kwargs.get("headers", self._client_async.headers), + **kwargs.get("headers", {}), **self.folder_headers, } diff --git a/packages/uipath-platform/src/uipath/platform/common/_base_service.py b/packages/uipath-platform/src/uipath/platform/common/_base_service.py index 959d135f7..ad23aafd9 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_base_service.py +++ b/packages/uipath-platform/src/uipath/platform/common/_base_service.py @@ -1,5 +1,7 @@ +import os import sys import types +from collections.abc import Mapping, MutableMapping from logging import getLogger from typing import Any, Literal, Union @@ -21,7 +23,12 @@ stop_after_attempt, ) -from uipath.platform.constants import HEADER_TRACEPARENT_ID, HEADER_USER_AGENT +from uipath.platform.constants import ( + ENV_UIPATH_ACCESS_TOKEN, + ENV_UNATTENDED_USER_ACCESS_TOKEN, + HEADER_TRACEPARENT_ID, + HEADER_USER_AGENT, +) from ..errors import EnrichedException from ._config import UiPathApiConfig @@ -41,6 +48,10 @@ _MAX_CALLER_FRAMES = 5 +def _has_authorization_header(headers: Mapping[str, Any]) -> bool: + return any(str(name).lower() == "authorization" for name in headers) + + def _get_caller_component() -> str: try: current: types.FrameType | None = sys._getframe(1) @@ -204,9 +215,11 @@ def request( specific_component = _get_caller_component() - kwargs.setdefault("headers", {}) + # A copy: the caller's mapping must not pick up the headers added here. + kwargs["headers"] = dict(kwargs.get("headers") or {}) kwargs["headers"][HEADER_USER_AGENT] = user_agent_value(specific_component) _inject_trace_context(kwargs["headers"]) + self._add_authorization(kwargs["headers"]) override = resolve_service_url(str(url)) if override: @@ -247,11 +260,12 @@ async def request_async( f"HEADERS: {kwargs.get('headers', self._client_async.headers)}" ) - kwargs.setdefault("headers", {}) + kwargs["headers"] = dict(kwargs.get("headers") or {}) kwargs["headers"][HEADER_USER_AGENT] = user_agent_value( self._specific_component ) _inject_trace_context(kwargs["headers"]) + self._add_authorization(kwargs["headers"]) override = resolve_service_url(str(url)) if override: @@ -279,9 +293,37 @@ def default_headers(self) -> dict[str, str]: @property def auth_headers(self) -> dict[str, str]: - header = f"Bearer {self._config.secret}" + header = f"Bearer {self._current_secret()}" return {"Authorization": header} + def _current_secret(self) -> str: + """The bearer token to send now. + + A secret that came from the environment is re-read on every call: a long-running + job's runtime renews the token there (see ``uipath._cli._job_token``). Any other + secret is sent as is. + """ + if self._config.secret_from_environment: + renewed = os.environ.get( + ENV_UNATTENDED_USER_ACCESS_TOKEN + ) or os.environ.get(ENV_UIPATH_ACCESS_TOKEN) + if renewed: + self._config.secret = renewed + return renewed + return self._config.secret + + def _add_authorization(self, headers: MutableMapping[str, Any]) -> None: + """Put the current environment token on a request, when the secret came from there. + + Otherwise the client default header set in ``__init__`` is used, as before. A header + the caller supplied is left alone. + """ + if not self._config.secret_from_environment or _has_authorization_header( + headers + ): + return + headers["Authorization"] = f"Bearer {self._current_secret()}" + @property def custom_headers(self) -> dict[str, str]: return {} diff --git a/packages/uipath-platform/src/uipath/platform/common/_config.py b/packages/uipath-platform/src/uipath/platform/common/_config.py index f0d62604e..f079e7686 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_config.py +++ b/packages/uipath-platform/src/uipath/platform/common/_config.py @@ -3,13 +3,26 @@ from pathlib import Path from typing import Any -from pydantic import BaseModel +from pydantic import BaseModel, PrivateAttr class UiPathApiConfig(BaseModel): base_url: str secret: str + # Set by UiPath() when the secret came from the token environment variables. A long + # job's runtime renews the token there, so services re-read it on every request. + _secret_from_environment: bool = PrivateAttr(default=False) + + @property + def secret_from_environment(self) -> bool: + """Whether ``secret`` was read from the token environment variables.""" + return self._secret_from_environment + + def mark_secret_from_environment(self) -> None: + """Record that ``secret`` came from the environment and may be renewed there.""" + self._secret_from_environment = True + class ConfigurationManager: _instance = None diff --git a/packages/uipath-platform/tests/services/test_env_token_reread.py b/packages/uipath-platform/tests/services/test_env_token_reread.py new file mode 100644 index 000000000..7017de3d5 --- /dev/null +++ b/packages/uipath-platform/tests/services/test_env_token_reread.py @@ -0,0 +1,209 @@ +"""A UiPath() configured from the environment re-reads the token on every request. + +The job runtime renews the token in the process environment (``uipath._cli._job_token``); +re-reading it is what lets long-lived clients pick the new token up. A secret given +explicitly is sent as is. +""" + +from unittest.mock import patch + +import pytest +from pytest_httpx import HTTPXMock + +from uipath.platform import UiPath, UiPathApiConfig +from uipath.platform.common import ApiClient +from uipath.platform.common._base_service import BaseService +from uipath.platform.common.auth import TokenData + +UNATTENDED = "UNATTENDED_USER_ACCESS_TOKEN" +ACCESS = "UIPATH_ACCESS_TOKEN" +URL = "https://cloud.uipath.com/org/tenant" + + +@pytest.fixture +def url(base_url: str, org: str, tenant: str) -> str: + return f"{base_url}{org}{tenant}/endpoint" + + +@pytest.fixture +def env_config(config: UiPathApiConfig, monkeypatch) -> UiPathApiConfig: + """The fixture config, marked as UiPath() marks one built from the environment.""" + monkeypatch.setenv(ACCESS, config.secret) + monkeypatch.delenv(UNATTENDED, raising=False) + config.mark_secret_from_environment() + return config + + +def _sent_auth(httpx_mock: HTTPXMock) -> list[str]: + return [request.headers["Authorization"] for request in httpx_mock.get_requests()] + + +class TestEnvironmentSecret: + def test_each_request_reads_the_current_environment_token( + self, + httpx_mock: HTTPXMock, + env_config, + execution_context, + url, + monkeypatch, + secret, + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + httpx_mock.add_response(url=url, status_code=200, json={}) + service = BaseService(config=env_config, execution_context=execution_context) + + service.request("GET", "/endpoint") + monkeypatch.setenv(ACCESS, "renewed") + service.request("GET", "/endpoint") + + assert _sent_auth(httpx_mock) == [f"Bearer {secret}", "Bearer renewed"] + assert env_config.secret == "renewed" + + @pytest.mark.anyio + async def test_each_async_request_reads_the_current_environment_token( + self, + httpx_mock: HTTPXMock, + env_config, + execution_context, + url, + monkeypatch, + secret, + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + httpx_mock.add_response(url=url, status_code=200, json={}) + service = BaseService(config=env_config, execution_context=execution_context) + + await service.request_async("GET", "/endpoint") + monkeypatch.setenv(ACCESS, "renewed") + await service.request_async("GET", "/endpoint") + + assert _sent_auth(httpx_mock) == [f"Bearer {secret}", "Bearer renewed"] + assert env_config.secret == "renewed" + + def test_the_unattended_token_wins_over_the_access_token( + self, httpx_mock: HTTPXMock, env_config, execution_context, url, monkeypatch + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + monkeypatch.setenv(UNATTENDED, "unattended") + monkeypatch.setenv(ACCESS, "access") + service = BaseService(config=env_config, execution_context=execution_context) + + service.request("GET", "/endpoint") + + assert _sent_auth(httpx_mock) == ["Bearer unattended"] + + def test_a_missing_variable_falls_back_to_the_stored_secret( + self, + httpx_mock: HTTPXMock, + env_config, + execution_context, + url, + monkeypatch, + secret, + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + service = BaseService(config=env_config, execution_context=execution_context) + monkeypatch.delenv(ACCESS) + + service.request("GET", "/endpoint") + + assert _sent_auth(httpx_mock) == [f"Bearer {secret}"] + + def test_a_caller_supplied_authorization_header_is_left_alone( + self, httpx_mock: HTTPXMock, env_config, execution_context, url, monkeypatch + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + monkeypatch.setenv(ACCESS, "renewed") + service = BaseService(config=env_config, execution_context=execution_context) + + service.request("GET", "/endpoint", headers={"authorization": "Bearer mine"}) + + assert _sent_auth(httpx_mock) == ["Bearer mine"] + + def test_the_callers_headers_mapping_is_not_modified( + self, httpx_mock: HTTPXMock, env_config, execution_context, url + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + service = BaseService(config=env_config, execution_context=execution_context) + headers = {"X-Test": "1"} + + service.request("GET", "/endpoint", headers=headers) + + assert headers == {"X-Test": "1"} + assert httpx_mock.get_requests()[0].headers["X-Test"] == "1" + + def test_api_client_folder_headers_do_not_pin_the_old_token( + self, httpx_mock: HTTPXMock, env_config, execution_context, url, monkeypatch + ): + """include_folder_headers used to copy the client defaults, Authorization included.""" + monkeypatch.setenv("UIPATH_FOLDER_KEY", "folder-1") + httpx_mock.add_response(url=url, status_code=200, json={}) + client = ApiClient(env_config, execution_context) + monkeypatch.setenv(ACCESS, "renewed") + + client.request("GET", "/endpoint", include_folder_headers=True) + + sent = httpx_mock.get_requests()[0] + assert sent.headers["Authorization"] == "Bearer renewed" + assert sent.headers["x-uipath-folderkey"] == "folder-1" + assert sent.headers["Accept"] == "application/json" + + +class TestStaticSecret: + def test_an_explicit_secret_ignores_the_environment( + self, httpx_mock: HTTPXMock, config, execution_context, url, monkeypatch, secret + ): + httpx_mock.add_response(url=url, status_code=200, json={}) + monkeypatch.setenv(ACCESS, "renewed") + service = BaseService(config=config, execution_context=execution_context) + + service.request("GET", "/endpoint") + + assert config.secret_from_environment is False + assert _sent_auth(httpx_mock) == [f"Bearer {secret}"] + assert config.secret == secret + + def test_the_flag_is_outside_validation_and_serialization(self): + config = UiPathApiConfig(base_url="https://x", secret="s") + assert config.secret_from_environment is False + + config.mark_secret_from_environment() + + assert config.secret_from_environment is True + assert config.model_dump() == {"base_url": "https://x", "secret": "s"} + + +class TestUiPathFactory: + def test_a_client_built_from_the_environment_follows_it(self, monkeypatch): + monkeypatch.setenv("UIPATH_URL", URL) + monkeypatch.setenv(ACCESS, "env-token") + monkeypatch.delenv(UNATTENDED, raising=False) + sdk = UiPath() + assert sdk._config.secret_from_environment is True + assert sdk._config.secret == "env-token" + + monkeypatch.setenv(ACCESS, "renewed") + + assert sdk.api_client.default_headers["Authorization"] == "Bearer renewed" + assert sdk._config.secret == "renewed" + + def test_an_explicit_secret_does_not(self, monkeypatch): + monkeypatch.setenv("UIPATH_URL", URL) + monkeypatch.setenv(ACCESS, "env-token") + + sdk = UiPath(secret="mine") + monkeypatch.setenv(ACCESS, "renewed") + + assert sdk._config.secret_from_environment is False + assert sdk.api_client.default_headers["Authorization"] == "Bearer mine" + + def test_client_credentials_do_not(self, monkeypatch): + monkeypatch.setenv(ACCESS, "env-token") + with patch( + "uipath.platform._uipath.ExternalApplicationService.get_token_data", + return_value=TokenData(access_token="app-token"), + ): + sdk = UiPath(base_url=URL, client_id="app", client_secret="s3cret") + + assert sdk._config.secret_from_environment is False + assert sdk._config.secret == "app-token" From 415664601bf311c9bd8b0eedbb5ec106eb893c35 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Mon, 16 Nov 2026 23:22:11 +0100 Subject: [PATCH 02/10] feat: renew the job user access token over the handler IPC pipe Co-Authored-By: Claude Fable 5.1 --- .../uipath/docs/core/environment_variables.md | 7 +- packages/uipath/src/uipath/_cli/_job_api.py | 39 +- packages/uipath/src/uipath/_cli/_job_token.py | 286 +++++++++ packages/uipath/src/uipath/_cli/cli_run.py | 20 +- .../uipath/src/uipath/_cli/cli_server_ipc.py | 49 +- packages/uipath/tests/cli/test_job_token.py | 580 ++++++++++++++++++ packages/uipath/tests/cli/test_run.py | 89 ++- packages/uipath/tests/cli/test_server_ipc.py | 187 ++++++ 8 files changed, 1238 insertions(+), 19 deletions(-) create mode 100644 packages/uipath/src/uipath/_cli/_job_token.py create mode 100644 packages/uipath/tests/cli/test_job_token.py diff --git a/packages/uipath/docs/core/environment_variables.md b/packages/uipath/docs/core/environment_variables.md index bcf1bcc8b..625527764 100644 --- a/packages/uipath/docs/core/environment_variables.md +++ b/packages/uipath/docs/core/environment_variables.md @@ -112,4 +112,9 @@ The `LOG_LEVEL` environment variable controls the verbosity of logging in the Or The default value is `INFORMATION` ### Builtin Variables -The runtime environment automatically includes certain variables (such as `UIPATH_FOLDER_KEY`, `UIPATH_ROBOT_KEY`), eliminating the need for manual configuration. \ No newline at end of file +The runtime environment automatically includes certain variables (such as `UIPATH_FOLDER_KEY`, `UIPATH_ROBOT_KEY`), eliminating the need for manual configuration. + +### Access Token Renewal +A job receives its user access token once, through `UNATTENDED_USER_ACCESS_TOKEN` (or `UIPATH_ACCESS_TOKEN`). A job that runs longer than that token lives would otherwise start failing with `401` responses. When the job executor asks for it (it starts the job with `uipath run --handler-ipc-pipe ... --handler-ipc-token-refresh`, or sets `refreshAccessTokenOverIpc` on a pooled run request), the runtime asks the executor for a new token over the job's IPC channel at about 80% of the current token's lifetime and writes it back to the token environment variables that are set. + +`UiPath()` clients configured from the environment read the token variable on every request, so they pick the renewed token up without any change; code that reads the variables itself sees it too. A client created with an explicit `secret=` keeps that secret. When the executor does not ask for renewal, nothing changes. If a renewal fails, the runtime logs a warning once, keeps the current token and tries again later. diff --git a/packages/uipath/src/uipath/_cli/_job_api.py b/packages/uipath/src/uipath/_cli/_job_api.py index 7f911ad56..9c7e4f61e 100644 --- a/packages/uipath/src/uipath/_cli/_job_api.py +++ b/packages/uipath/src/uipath/_cli/_job_api.py @@ -342,14 +342,22 @@ def __init__( loop: asyncio.AbstractEventLoop, thread: threading.Thread, log_handler: "_IpcLogHandler | None" = None, + token_refresher: Any = None, ) -> None: self._client = client self._loop = loop self._thread = thread self._log_handler = log_handler + self._token_refresher = token_refresher def _shutdown(self) -> None: """Close the client and stop its loop/thread.""" + if self._token_refresher is not None: + # First: its refresh task lives on this loop, and nothing may be published to + # the environment once teardown has begun. + from ._job_token import uninstall_job_token_refresher + + uninstall_job_token_refresher(self._token_refresher) if self._log_handler is not None: self._log_handler.flush_pending() try: @@ -372,9 +380,17 @@ def is_wire_job_key(job_key: str | None) -> TypeGuard[str]: def connect_handler_ipc( - pipe: str, job_key: str | None, resume_version: int | None + pipe: str, + job_key: str | None, + resume_version: int | None, + *, + refresh_token: bool = False, ) -> _HandlerIpcConnection: - """Dial ``pipe`` on a dedicated loop/thread and install the sinks (see ``_HandlerIpcConnection``).""" + """Dial ``pipe`` on a dedicated loop/thread and install the sinks (see ``_HandlerIpcConnection``). + + With ``refresh_token`` the host has said it serves ``GetUserAccessToken``: the job's user + access token is then renewed over the same connection (see ``_job_token``). + """ if not is_wire_job_key(job_key): raise RuntimeError( f"--handler-ipc-pipe needs UIPATH_JOB_KEY to be a job key; got {job_key!r}." @@ -415,7 +431,14 @@ async def _build() -> Any: raise # Install in the caller's context: the sinks are contextvars, read in the job's own context at teardown. handler = install_runtime_sinks(job_key, resume_version, proxy, loop) - return _HandlerIpcConnection(client, loop, thread, handler) + token_refresher = None + if refresh_token: + from ._job_token import install_job_token_refresher + + token_refresher = install_job_token_refresher( + job_key, resume_version, proxy, loop + ) + return _HandlerIpcConnection(client, loop, thread, handler, token_refresher) async def disconnect_handler_ipc(conn: _HandlerIpcConnection) -> None: @@ -426,11 +449,17 @@ async def disconnect_handler_ipc(conn: _HandlerIpcConnection) -> None: @contextlib.asynccontextmanager async def handler_ipc_connection( - pipe: str | None, job_key: str | None, resume_version: int | None + pipe: str | None, + job_key: str | None, + resume_version: int | None, + *, + refresh_token: bool = False, ) -> AsyncIterator[Any]: """Connect (if ``pipe`` is set) and always disconnect on exit; yields the connection or None.""" conn = ( - connect_handler_ipc(pipe, job_key, resume_version) if pipe is not None else None + connect_handler_ipc(pipe, job_key, resume_version, refresh_token=refresh_token) + if pipe is not None + else None ) try: yield conn diff --git a/packages/uipath/src/uipath/_cli/_job_token.py b/packages/uipath/src/uipath/_cli/_job_token.py new file mode 100644 index 000000000..9e7d8c387 --- /dev/null +++ b/packages/uipath/src/uipath/_cli/_job_token.py @@ -0,0 +1,286 @@ +"""Renew the job's user access token over the job-api IPC channel. + +The executor hands the token to the process once, as an environment variable, and a job that +outlives it starts failing with 401. ``JobTokenRefresher`` asks the host for a new token +(``IPythonJobApi.GetUserAccessToken``) shortly before the current one expires and writes it back +to the token environment variables. ``UiPath()`` clients built from the environment re-read those +on every request (see ``uipath.platform``), so they pick the new token up without a change of +their own, and so does user code that reads the variables itself. + +The host opts in (``uipath run --handler-ipc-token-refresh``, or ``refreshAccessTokenOverIpc`` on +a pooled run request); a host without the method is never asked. +""" + +from __future__ import annotations + +import asyncio +import logging +import os +import threading +import time +from collections.abc import Callable +from concurrent.futures import Future +from dataclasses import dataclass +from typing import Any + +from uipath._utils._auth import parse_access_token +from uipath.platform.constants import ( + ENV_UIPATH_ACCESS_TOKEN, + ENV_UNATTENDED_USER_ACCESS_TOKEN, +) + +from ._job_api import PythonUserAccessTokenRequestDto + +logger = logging.getLogger(__name__) + +# A healthy host answers in well under a second (serverless hands back the job's own token, the +# Robot asks Orchestrator). +_TOKEN_REQUEST_TIMEOUT_S = 30.0 +# Renew when this share of the lifetime is left (so at 80 percent of it), and never later than +# this many seconds before expiry. +_REFRESH_MARGIN_FRACTION = 0.2 +_MIN_REFRESH_MARGIN_S = 120.0 +# After a failed renewal, or one that returned a token already due, leave the host alone for +# this long before asking again. +_RETRY_AFTER_FAILURE_S = 60.0 +_TOKEN_ENV_VARS = (ENV_UNATTENDED_USER_ACCESS_TOKEN, ENV_UIPATH_ACCESS_TOKEN) +# .NET exception type names a host answers with when it cannot serve the method at all. +_UNSUPPORTED_MARKERS = ( + "MethodNotFound", + "EndpointNotFound", + "NotSupported", + "MissingMethod", +) + + +def _claims(token: str) -> dict[str, Any]: + try: + claims = parse_access_token(token) + except Exception: + return {} + return claims if isinstance(claims, dict) else {} + + +def _claim_seconds(claims: dict[str, Any], name: str) -> float | None: + value = claims.get(name) + if isinstance(value, bool) or not isinstance(value, (int, float)): + return None + return float(value) + + +@dataclass(frozen=True) +class _TokenState: + """A token with the moments that drive its renewal (epoch seconds).""" + + token: str + issued_at: float + expires_at: float | None + + @classmethod + def from_token(cls, token: str, now: float) -> _TokenState: + """Read ``iat``/``exp`` from the JWT; a token without ``exp`` is never renewed early.""" + claims = _claims(token) + issued_at = _claim_seconds(claims, "iat") + return cls( + token=token, + issued_at=now if issued_at is None else min(issued_at, now), + expires_at=_claim_seconds(claims, "exp"), + ) + + @property + def refresh_at(self) -> float | None: + """When to renew: 80 percent into the lifetime, at least two minutes before expiry.""" + if self.expires_at is None: + return None + lifetime = max(self.expires_at - self.issued_at, 0.0) + margin = max(_MIN_REFRESH_MARGIN_S, _REFRESH_MARGIN_FRACTION * lifetime) + return self.expires_at - margin + + def is_fresh(self, now: float) -> bool: + refresh_at = self.refresh_at + return refresh_at is None or now < refresh_at + + +def publish_token_to_environ(token: str) -> None: + """Republish a token under whichever token variables the process environment has set.""" + for name in _TOKEN_ENV_VARS: + if name in os.environ: + os.environ[name] = token + + +def _is_unsupported(error: BaseException) -> bool: + """True for a host that does not implement GetUserAccessToken; asking again cannot help.""" + if isinstance(error, (AttributeError, NotImplementedError)): + return True + type_name = getattr(error, "type_name", None) or "" + return any(marker in type_name for marker in _UNSUPPORTED_MARKERS) + + +class JobTokenRefresher: + """Renews the job's user access token on the IPC loop and publishes it to the environment. + + One task, started by :meth:`start`, sleeps until 80 percent of the current token's lifetime + has passed (at least two minutes before expiry), asks the host, stores and publishes the + answer, and reschedules from the new token. A failure is logged once, the current token is + kept and the host is left alone for a minute; a host that does not implement the call ends + the task. :meth:`stop` cancels the task, a request in flight included, and a stopped + refresher never writes to the environment again. + """ + + def __init__( + self, + proxy: Any, + loop: asyncio.AbstractEventLoop, + job_key: str, + resume_version: int | None, + *, + initial_token: str | None = None, + timeout: float = _TOKEN_REQUEST_TIMEOUT_S, + publish: Callable[[str], None] | None = publish_token_to_environ, + clock: Callable[[], float] = time.time, + ) -> None: + self._proxy = proxy + self._loop = loop + self._job_key = job_key + self._resume_version = resume_version + self._timeout = timeout + self._publish = publish + self._clock = clock + self._lock = threading.Lock() + self._state: _TokenState | None = ( + _TokenState.from_token(initial_token, clock()) if initial_token else None + ) + self._task: Future[None] | None = None + self._retry_not_before = 0.0 + self._stopped = False + self._unsupported = False + self._reported_failure = False + + def start(self) -> None: + """Schedule the renewal task on the IPC loop (once).""" + if self._task is None and not self._stopped: + self._task = asyncio.run_coroutine_threadsafe(self._run(), self._loop) + + def stop(self) -> None: + """Cancel the renewal task; nothing is published to the environment after this.""" + with self._lock: + self._stopped = True + task = self._task + if task is not None: + task.cancel() + + @property + def task(self) -> Future[None] | None: + """The renewal task's handle, for callers that need to await its end.""" + return self._task + + @property + def current_token(self) -> str | None: + """The token last seeded or renewed, or None.""" + state = self._state + return None if state is None else state.token + + @property + def expires_at(self) -> float | None: + """The current token's ``exp`` (epoch seconds), or None when unknown.""" + state = self._state + return None if state is None else state.expires_at + + async def _run(self) -> None: + while not self._stopped and not self._unsupported: + state = self._state + if state is None or state.refresh_at is None: + return # no token, or one without exp: nothing to time against + now = self._clock() + due_at = max(state.refresh_at, self._retry_not_before) + if now < due_at: + await asyncio.sleep(due_at - now) + continue + await self._refresh_once() + + async def _refresh_once(self) -> None: + request = PythonUserAccessTokenRequestDto( + jobKey=self._job_key, resumeVersion=self._resume_version, scopes=None + ) + try: + token = await asyncio.wait_for( + self._proxy.GetUserAccessToken(request), timeout=self._timeout + ) + if not isinstance(token, str) or not token: + raise RuntimeError(f"the host returned no token ({token!r})") + except asyncio.CancelledError: + raise + except Exception as e: + self._note_failure(e) + return + self._store(token) + + def _store(self, token: str) -> None: + now = self._clock() + state = _TokenState.from_token(token, now) + with self._lock: + if self._stopped: + return # a late answer after teardown must not touch the environment + self._state = state + # A token that is already due (serverless handing back the same soon-to-expire + # token) would otherwise make the loop spin: wait the backoff before asking again. + self._retry_not_before = ( + 0.0 if state.is_fresh(now) else now + _RETRY_AFTER_FAILURE_S + ) + if self._publish is None: + return + try: + self._publish(token) + except Exception: + logger.debug( + "Could not republish the renewed access token", exc_info=True + ) + + def _note_failure(self, error: BaseException) -> None: + if self._stopped: + return # teardown cancels in-flight work; nothing worth reporting + with self._lock: + self._retry_not_before = self._clock() + _RETRY_AFTER_FAILURE_S + if _is_unsupported(error): + self._unsupported = True + first = not self._reported_failure + self._reported_failure = True + if not first: + logger.debug("Renewing the job's user access token failed again: %r", error) + elif self._unsupported: + logger.warning( + "The job host does not support renewing the user access token (%r); " + "keeping the token the job started with.", + error, + ) + else: + logger.warning( + "Could not renew the job's user access token over IPC (%r); keeping the " + "current token and retrying later. Further failures are logged at debug level.", + error, + ) + + +def install_job_token_refresher( + job_key: str, + resume_version: int | None, + proxy: Any, + loop: asyncio.AbstractEventLoop, +) -> JobTokenRefresher: + """Start renewing the job's token, seeded with the one in the environment. + + Pair with :func:`uninstall_job_token_refresher` when the job ends. + """ + initial = os.environ.get(ENV_UNATTENDED_USER_ACCESS_TOKEN) or os.environ.get( + ENV_UIPATH_ACCESS_TOKEN + ) + refresher = JobTokenRefresher( + proxy, loop, job_key, resume_version, initial_token=initial + ) + refresher.start() + return refresher + + +def uninstall_job_token_refresher(refresher: JobTokenRefresher) -> None: + """Stop the renewal task.""" + refresher.stop() diff --git a/packages/uipath/src/uipath/_cli/cli_run.py b/packages/uipath/src/uipath/_cli/cli_run.py index 13ff381a7..3e3ee31e1 100644 --- a/packages/uipath/src/uipath/_cli/cli_run.py +++ b/packages/uipath/src/uipath/_cli/cli_run.py @@ -124,8 +124,14 @@ def get_usage_help(self) -> list[str]: hidden=True, # set by the job executor, never by a person help="Named pipe to stream this job's logs and result over uipath-ipc instead of writing them to files.", ) +@click.option( + "--handler-ipc-token-refresh", + is_flag=True, + hidden=True, # set by the job executor once its job api serves GetUserAccessToken + help="Renew this job's user access token over the handler IPC pipe before it expires. Needs --handler-ipc-pipe.", +) @track_command("run") -def run( +def run( # NOSONAR: one parameter per click option entrypoint: str | None, input: str | None, resume: bool, @@ -139,6 +145,7 @@ def run( keep_state_file: bool, simulation: str | None, handler_ipc_pipe: str | None, + handler_ipc_token_refresh: bool, ) -> None: """Execute the project.""" input_file = file or input_file @@ -155,6 +162,10 @@ def run( console.error(f"Invalid --simulation config: {e}") return + if handler_ipc_token_refresh and handler_ipc_pipe is None: + console.error("--handler-ipc-token-refresh needs --handler-ipc-pipe.") + return + result = Middlewares.next( "run", entrypoint, @@ -232,7 +243,12 @@ async def execute() -> None: from ._job_api import handler_ipc_connection async with ( - handler_ipc_connection(handler_ipc_pipe, ctx.job_id, None), + handler_ipc_connection( + handler_ipc_pipe, + ctx.job_id, + None, + refresh_token=handler_ipc_token_refresh, + ), ResourceOverwritesContext( lambda: read_resource_overwrites_from_file(ctx.runtime_dir) ), diff --git a/packages/uipath/src/uipath/_cli/cli_server_ipc.py b/packages/uipath/src/uipath/_cli/cli_server_ipc.py index 586680274..77ce24aee 100644 --- a/packages/uipath/src/uipath/_cli/cli_server_ipc.py +++ b/packages/uipath/src/uipath/_cli/cli_server_ipc.py @@ -36,6 +36,9 @@ class PythonServerRunRequest: workingDirectory: str | None = None environmentVariables: dict[str, str] = field(default_factory=dict) streamOutputOverIpc: bool = False + # The host sets this once its job api serves GetUserAccessToken; a host without the + # method is never asked. + refreshAccessTokenOverIpc: bool = False @dataclass @@ -103,7 +106,10 @@ async def RunJob( on_run_start: "Any" = None on_run_end: "Any" = None installed: "list[Any]" = [] - if request.streamOutputOverIpc: + token_refreshers: "list[Any]" = [] + stream_output = request.streamOutputOverIpc + refresh_token = request.refreshAccessTokenOverIpc + if stream_output or refresh_token: # Never stored: a captured callback goes stale on reconnect/restart. from ._job_api import ( IPythonJobApi, @@ -112,16 +118,24 @@ async def RunJob( is_wire_job_key, ) + flags = " and ".join( + name + for name, on in ( + ("streamOutputOverIpc", stream_output), + ("refreshAccessTokenOverIpc", refresh_token), + ) + if on + ) if not is_wire_job_key(request.jobKey): return PythonServerRunJobResult( exitCode=1, - error=f"streamOutputOverIpc needs a 'jobKey' that is a job key (Guid); got {request.jobKey!r}", + error=f"{flags} needs a 'jobKey' that is a job key (Guid); got {request.jobKey!r}", ) if message is None or message.client is None: return PythonServerRunJobResult( exitCode=1, - error="streamOutputOverIpc is only available when RunJob is invoked over IPC", + error=f"{flags} is only available when RunJob is invoked over IPC", ) # get_callback only wraps the connection, so this cannot tell us whether the peer @@ -133,12 +147,33 @@ async def RunJob( resume_version = request.resumeVersion def _install() -> None: - installed.append( - install_runtime_sinks(job_key, resume_version, callback, loop) - ) + if stream_output: + installed.append( + install_runtime_sinks(job_key, resume_version, callback, loop) + ) + if refresh_token: + # Runs after the job's env vars are applied, so the seed token is this + # job's. Jobs are serialized by the server lock, so one refresher at a + # time writes os.environ. + from ._job_token import install_job_token_refresher + + token_refreshers.append( + install_job_token_refresher( + job_key, resume_version, callback, loop + ) + ) + + def _uninstall() -> None: + if stream_output: + clear_runtime_sinks() + if token_refreshers: + from ._job_token import uninstall_job_token_refresher + + for refresher in token_refreshers: + uninstall_job_token_refresher(refresher) on_run_start = _install - on_run_end = clear_runtime_sinks + on_run_end = _uninstall result = await _run_command_isolated( cmd, diff --git a/packages/uipath/tests/cli/test_job_token.py b/packages/uipath/tests/cli/test_job_token.py new file mode 100644 index 000000000..50181be47 --- /dev/null +++ b/packages/uipath/tests/cli/test_job_token.py @@ -0,0 +1,580 @@ +"""The job's user access token refresher: renewal over the job-api IPC proxy, published to the environment. + +Fakes stand in for the IPC proxy (``GetUserAccessToken``) except where the wire itself is the +point; tokens are unsigned JWTs built here so ``iat``/``exp`` can be placed precisely. +""" + +import asyncio +import base64 +import json +import logging +import os +import threading +import time +from contextvars import ContextVar +from typing import Any + +import pytest + +from uipath._cli import _job_api, _job_token +from uipath._cli._job_token import ( + JobTokenRefresher, + install_job_token_refresher, + publish_token_to_environ, + uninstall_job_token_refresher, +) + +JOB_ID = "3f2504e0-4f89-11d3-9a0c-0305e82c3301" +UNATTENDED = "UNATTENDED_USER_ACCESS_TOKEN" +ACCESS = "UIPATH_ACCESS_TOKEN" +LOGGER = "uipath._cli._job_token" + + +def _b64(obj: Any) -> str: + return base64.urlsafe_b64encode(json.dumps(obj).encode()).decode().rstrip("=") + + +def jwt(**claims: Any) -> str: + return f"{_b64({'alg': 'none'})}.{_b64(claims)}.sig" + + +def token_with_lifetime(lifetime: float, *, remaining: float, tag: str = "t") -> str: + """A JWT expiring ``remaining`` seconds from now, issued ``lifetime`` seconds before that.""" + exp = time.time() + remaining + return jwt(iat=exp - lifetime, exp=exp, tag=tag) + + +class FakeJobApi: + """Stands in for the ``IPythonJobApi`` proxy; hands out ``tokens`` in order, then "fresh".""" + + def __init__(self, *tokens: Any, error: BaseException | None = None) -> None: + self.tokens = list(tokens) + self.error = error + self.requests: list[Any] = [] + self.called = threading.Event() + # When set, every call blocks until the event is set (from the loop's thread). + self.release: asyncio.Event | None = None + + async def GetUserAccessToken(self, request: Any) -> Any: + self.requests.append(request) + self.called.set() + if self.release is not None: + await self.release.wait() + if self.error is not None: + raise self.error + return self.tokens.pop(0) if self.tokens else "fresh" + + +class RemoteError(Exception): + """Shaped like uipath_ipc's RemoteException: carries the .NET type name.""" + + def __init__(self, type_name: str) -> None: + super().__init__(type_name) + self.type_name = type_name + + +@pytest.fixture +def ipc_loop(): + loop = asyncio.new_event_loop() + thread = threading.Thread( + target=loop.run_forever, name="test-ipc-loop", daemon=True + ) + thread.start() + yield loop + loop.call_soon_threadsafe(loop.stop) + thread.join(timeout=5) + loop.close() + + +@pytest.fixture +def records() -> Any: + captured: list[logging.LogRecord] = [] + + class _Capture(logging.Handler): + def emit(self, record: logging.LogRecord) -> None: + captured.append(record) + + api_logger = logging.getLogger(LOGGER) + handler = _Capture() + api_logger.addHandler(handler) + prior_level = api_logger.level + api_logger.setLevel(logging.DEBUG) + try: + yield captured + finally: + api_logger.removeHandler(handler) + api_logger.setLevel(prior_level) + + +def _refresher( + api: Any, loop: asyncio.AbstractEventLoop, token: str | None, **kwargs: Any +) -> JobTokenRefresher: + kwargs.setdefault("publish", None) + return JobTokenRefresher(api, loop, JOB_ID, 2, initial_token=token, **kwargs) + + +def _wait(predicate: Any, timeout: float = 5.0) -> bool: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + if predicate(): + return True + time.sleep(0.02) + return bool(predicate()) + + +def _pending_tasks(loop: asyncio.AbstractEventLoop) -> int: + async def count() -> int: + me = asyncio.current_task() + return len([t for t in asyncio.all_tasks() if t is not me and not t.done()]) + + return asyncio.run_coroutine_threadsafe(count(), loop).result(timeout=5) + + +def _warnings(records: list[logging.LogRecord]) -> list[str]: + return [r.getMessage() for r in records if r.levelno == logging.WARNING] + + +class TestTokenState: + def test_exp_and_iat_drive_the_renewal_point(self): + now = 1_000_000.0 + state = _job_token._TokenState.from_token + + # 10 min lifetime: 20 percent is exactly the two-minute floor. + ten_min = state(jwt(iat=now, exp=now + 600), now) + assert ten_min.refresh_at == now + 480 + assert ten_min.is_fresh(now + 479) + assert not ten_min.is_fresh(now + 480) + # Short-lived: the floor wins over 20 percent. + assert state(jwt(iat=now, exp=now + 300), now).refresh_at == now + 180 + # Long-lived: 20 percent wins (8 h: renew after 6.4 h). + eight_h = state(jwt(iat=now, exp=now + 8 * 3600), now) + assert eight_h.refresh_at == now + 8 * 3600 * 0.8 + # No iat: the lifetime is measured from when the token arrived. + no_iat = state(jwt(exp=now + 600), now) + assert no_iat.issued_at == now + assert no_iat.refresh_at == now + 480 + # An iat in the future (clock skew) is clamped to now. + assert state(jwt(iat=now + 50, exp=now + 650), now).issued_at == now + + def test_a_token_without_exp_is_never_due(self): + now = 1_000_000.0 + none = _job_token._TokenState.from_token(jwt(sub="x"), now) + assert none.expires_at is None + assert none.refresh_at is None + assert none.is_fresh(now + 10**9) + + def test_an_opaque_token_is_kept(self): + state = _job_token._TokenState.from_token("opaque", 1.0) + assert state.token == "opaque" + assert state.expires_at is None + + +class TestRenewal: + def test_renews_at_80_percent_and_republishes(self, ipc_loop, monkeypatch): + monkeypatch.setenv(ACCESS, "old") + monkeypatch.delenv(UNATTENDED, raising=False) + renewed = token_with_lifetime(3600, remaining=3600, tag="renewed") + api = FakeJobApi(renewed) + # 10 min lifetime with 120.5 s left: the two-minute margin puts renewal ~0.5 s away. + refresher = JobTokenRefresher( + api, + ipc_loop, + JOB_ID, + 7, + initial_token=token_with_lifetime(600, remaining=120.5), + ) + + refresher.start() + + assert api.called.wait(timeout=5) + assert _wait(lambda: os.environ[ACCESS] == renewed) + assert UNATTENDED not in os.environ + request = api.requests[0] + assert isinstance(request, _job_api.PythonUserAccessTokenRequestDto) + assert (request.jobKey, request.resumeVersion, request.scopes) == ( + JOB_ID, + 7, + None, + ) + assert refresher.current_token == renewed + assert refresher.expires_at == pytest.approx(time.time() + 3600, abs=5) + # Rescheduled against the new token, hours away. + task = refresher.task + assert task is not None + assert not task.done() + assert len(api.requests) == 1 + + refresher.stop() + assert _wait(lambda: _pending_tasks(ipc_loop) == 0) + assert task.cancelled() + + def test_a_fresh_token_only_waits_and_stop_cancels_the_wait(self, ipc_loop): + api = FakeJobApi() + refresher = _refresher(api, ipc_loop, token_with_lifetime(3600, remaining=3000)) + + refresher.start() + assert _wait(lambda: _pending_tasks(ipc_loop) == 1) + time.sleep(0.2) + assert api.requests == [] + + refresher.stop() + assert _wait(lambda: _pending_tasks(ipc_loop) == 0) + # Idempotent, and the token is still there to read. + refresher.stop() + assert refresher.current_token is not None + + def test_a_late_answer_after_stop_is_dropped(self, ipc_loop, monkeypatch): + monkeypatch.setenv(ACCESS, "old") + api = FakeJobApi("late") + api.release = asyncio.Event() + refresher = JobTokenRefresher( + api, + ipc_loop, + JOB_ID, + None, + initial_token=token_with_lifetime(600, remaining=60), # due now + ) + refresher.start() + assert api.called.wait(timeout=5) + + refresher.stop() + ipc_loop.call_soon_threadsafe(api.release.set) + + assert _wait(lambda: _pending_tasks(ipc_loop) == 0) + assert os.environ[ACCESS] == "old" + assert refresher.current_token != "late" + + def test_store_after_stop_publishes_nothing(self, ipc_loop, monkeypatch): + """The race the cancellation may miss: the answer arrived, stop() landed before the store.""" + monkeypatch.setenv(ACCESS, "old") + refresher = JobTokenRefresher( + FakeJobApi(), ipc_loop, JOB_ID, None, initial_token="old" + ) + + refresher.stop() + refresher._store("late") + + assert os.environ[ACCESS] == "old" + assert refresher.current_token == "old" + + def test_missing_exp_means_no_timer(self, ipc_loop): + api = FakeJobApi() + refresher = _refresher(api, ipc_loop, jwt(sub="no-exp")) + + refresher.start() + + task = refresher.task + assert task is not None + task.result(timeout=5) # returns at once: nothing to time against + assert api.requests == [] + assert refresher.expires_at is None + + def test_no_token_at_all_means_no_timer(self, ipc_loop): + api = FakeJobApi() + refresher = _refresher(api, ipc_loop, None) + + refresher.start() + + task = refresher.task + assert task is not None + task.result(timeout=5) + assert api.requests == [] + assert refresher.current_token is None + + +class TestFailures: + def test_a_failed_renewal_warns_once_and_retries_after_the_backoff( + self, ipc_loop, monkeypatch, records + ): + monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) + api = FakeJobApi(error=RuntimeError("pipe down")) + token = token_with_lifetime(600, remaining=120.2) + refresher = _refresher(api, ipc_loop, token) + + refresher.start() + + assert api.called.wait(timeout=5) + assert _wait(lambda: len(api.requests) >= 2) # retried, after the backoff + refresher.stop() + assert len(api.requests) < 10 # ... not hammered + assert refresher.current_token == token + warnings = _warnings(records) + assert len(warnings) == 1 + assert "pipe down" in warnings[0] + + def test_an_unsupported_host_ends_the_task(self, ipc_loop, records): + api = FakeJobApi(error=RemoteError("UiPath.Ipc.MethodNotFoundException")) + token = token_with_lifetime(600, remaining=60) # due now + refresher = _refresher(api, ipc_loop, token) + + refresher.start() + + task = refresher.task + assert task is not None + task.result(timeout=5) # the loop exits on its own + assert len(api.requests) == 1 + assert refresher.current_token == token + warnings = _warnings(records) + assert len(warnings) == 1 + assert "does not support" in warnings[0] + + @pytest.mark.parametrize("bad", ["", None, 42]) + def test_an_empty_answer_is_a_failure(self, ipc_loop, monkeypatch, records, bad): + monkeypatch.setenv(ACCESS, "old") + api = FakeJobApi(bad) + refresher = JobTokenRefresher( + api, + ipc_loop, + JOB_ID, + None, + initial_token=token_with_lifetime(600, remaining=60), + ) + + refresher.start() + + assert api.called.wait(timeout=5) + assert _wait(lambda: len(_warnings(records)) == 1) + assert os.environ[ACCESS] == "old" + refresher.stop() + + def test_a_token_already_due_is_kept_but_not_refetched_at_once( + self, ipc_loop, monkeypatch, records + ): + """Serverless hands back the job's own token; when the handler lags, it is still due.""" + monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) + api = FakeJobApi( + *[token_with_lifetime(600, remaining=60, tag=str(i)) for i in range(50)] + ) + refresher = _refresher(api, ipc_loop, token_with_lifetime(600, remaining=60)) + + refresher.start() + assert api.called.wait(timeout=5) + time.sleep(1.0) + refresher.stop() + + assert 2 <= len(api.requests) <= 8 # once per backoff period, not a spin + assert refresher.current_token in api.tokens or refresher.current_token != "" + assert _warnings(records) == [] + + def test_a_timed_out_host_is_a_failure(self, ipc_loop, records): + api = FakeJobApi("late") + api.release = asyncio.Event() # never released + refresher = _refresher( + api, ipc_loop, token_with_lifetime(600, remaining=60), timeout=0.2 + ) + + refresher.start() + + assert _wait(lambda: len(_warnings(records)) == 1) + assert "TimeoutError" in _warnings(records)[0] + refresher.stop() + + +class TestPublish: + def test_publish_writes_only_the_variables_that_are_set(self, monkeypatch): + monkeypatch.setenv(UNATTENDED, "old") + monkeypatch.setenv(ACCESS, "old") + publish_token_to_environ("new") + assert (os.environ[UNATTENDED], os.environ[ACCESS]) == ("new", "new") + + monkeypatch.delenv(UNATTENDED) + publish_token_to_environ("newer") + assert UNATTENDED not in os.environ + assert os.environ[ACCESS] == "newer" + + monkeypatch.delenv(ACCESS) + publish_token_to_environ("unseen") + assert UNATTENDED not in os.environ + assert ACCESS not in os.environ + + def test_a_failing_publish_keeps_the_token(self, ipc_loop, records): + def boom(token: str) -> None: + raise RuntimeError("env is read-only") + + refresher = _refresher(FakeJobApi(), ipc_loop, "old", publish=boom) + + refresher._store("renewed") + + assert refresher.current_token == "renewed" + assert _warnings(records) == [] + + +class TestInstall: + def test_install_seeds_from_the_environment_and_starts(self, ipc_loop, monkeypatch): + unattended = token_with_lifetime(3600, remaining=3000, tag="unattended") + monkeypatch.setenv(UNATTENDED, unattended) + monkeypatch.setenv(ACCESS, token_with_lifetime(3600, remaining=3000, tag="a")) + api = FakeJobApi() + + refresher = install_job_token_refresher(JOB_ID, 3, api, ipc_loop) + try: + # UNATTENDED_USER_ACCESS_TOKEN wins, as in resolve_config_from_env. + assert refresher.current_token == unattended + assert _wait(lambda: _pending_tasks(ipc_loop) == 1) + assert api.requests == [] + finally: + uninstall_job_token_refresher(refresher) + + assert _wait(lambda: _pending_tasks(ipc_loop) == 0) + + def test_shutdown_stops_the_refresher(self, monkeypatch): + class _Client: + async def aclose(self) -> None: + return None + + monkeypatch.setenv(ACCESS, token_with_lifetime(3600, remaining=3000)) + loop = asyncio.new_event_loop() + thread = threading.Thread(target=loop.run_forever, daemon=True) + thread.start() + refresher = install_job_token_refresher(JOB_ID, None, FakeJobApi(), loop) + assert _wait(lambda: _pending_tasks(loop) == 1) + conn = _job_api._HandlerIpcConnection(_Client(), loop, thread, None, refresher) + + conn._shutdown() + + task = refresher.task + assert task is not None + assert task.cancelled() + assert not thread.is_alive() + + +def test_token_request_dto_wire_keys_are_pinned(): + serialization = pytest.importorskip("uipath_ipc.wire.serialization") + request = _job_api.PythonUserAccessTokenRequestDto(jobKey="j") + assert set(serialization.to_wire(request)) == {"jobKey", "resumeVersion", "scopes"} + + +# -- over a real pipe --------------------------------------------------------------------- + +_pipe_counter = 0 + + +def _unique_pipe() -> str: + global _pipe_counter + _pipe_counter += 1 + return f"uipath-jobtoken-test-{os.getpid()}-{_pipe_counter}" + + +def _isolate_output_sinks(monkeypatch) -> None: + from uipath.runtime import output_sinks + + monkeypatch.setattr( + output_sinks, "_log_handler", ContextVar("test_log_handler", default=None) + ) + monkeypatch.setattr( + output_sinks, "_result_sink", ContextVar("test_result_sink", default=None) + ) + + +def _serve_jobapi_in_background(pipe: str, api: Any): + """Host ``api`` as IPythonJobApi on ``pipe`` on its own loop/thread; returns stop().""" + from uipath_ipc import IpcServer, NamedPipeServerTransport + + loop = asyncio.new_event_loop() + ready = threading.Event() + holder: dict[str, Any] = {} + + async def _serve() -> None: + server = IpcServer( + transport=NamedPipeServerTransport(pipe), + services={_job_api.IPythonJobApi: api}, + request_timeout=None, + ) + holder["server"] = server + async with server: + ready.set() + await server.serve_forever() + + def _run() -> None: + asyncio.set_event_loop(loop) + try: + loop.run_until_complete(_serve()) + except Exception: + pass + finally: + loop.close() + + thread = threading.Thread(target=_run, name="jobtoken-test-server", daemon=True) + thread.start() + if not ready.wait(timeout=10): + raise TimeoutError(f"job-api test server on {pipe!r} did not start") + + def stop() -> None: + server = holder.get("server") + if server is not None: + try: + asyncio.run_coroutine_threadsafe(server.aclose(), loop).result( + timeout=10 + ) + except Exception: + pass + thread.join(timeout=10) + + return stop + + +class _WireApi(_job_api.IPythonJobApi): + def __init__(self) -> None: + self.received: list[Any] = [] + + async def SendLog(self, log: Any) -> None: + return None + + async def SetResult(self, result: Any) -> bool: + return True + + async def GetUserAccessToken(self, request: Any) -> str: + self.received.append(request) + return f"wire-token-{len(self.received)}" + + +def test_connect_with_refresh_renews_over_the_wire(monkeypatch): + pytest.importorskip("uipath_ipc") + _isolate_output_sinks(monkeypatch) + monkeypatch.setenv(ACCESS, token_with_lifetime(600, remaining=60)) # due now + monkeypatch.delenv(UNATTENDED, raising=False) + api = _WireApi() + pipe = _unique_pipe() + stop = _serve_jobapi_in_background(pipe, api) + try: + + async def scenario() -> None: + async with _job_api.handler_ipc_connection( + pipe, JOB_ID, 5, refresh_token=True + ) as conn: + assert conn._token_refresher is not None + # The renewal crosses the pipe and lands in the environment. + assert await asyncio.to_thread( + _wait, lambda: os.environ[ACCESS] == "wire-token-1" + ) + # Teardown cancelled the task on the connection's loop. + task = conn._token_refresher.task + assert task is not None + assert task.done() + + asyncio.run(scenario()) + finally: + stop() + + assert len(api.received) == 1 + request = api.received[0] + # The server deserializes against the contract, so this is the typed DTO after a round trip. + assert (request.jobKey, request.resumeVersion, request.scopes) == (JOB_ID, 5, None) + + +def test_connect_without_refresh_installs_no_refresher(monkeypatch): + pytest.importorskip("uipath_ipc") + _isolate_output_sinks(monkeypatch) + api = _WireApi() + pipe = _unique_pipe() + stop = _serve_jobapi_in_background(pipe, api) + try: + + async def scenario() -> None: + async with _job_api.handler_ipc_connection(pipe, JOB_ID, None) as conn: + assert conn._token_refresher is None + + asyncio.run(scenario()) + finally: + stop() + assert api.received == [] diff --git a/packages/uipath/tests/cli/test_run.py b/packages/uipath/tests/cli/test_run.py index a47e5a4d7..b7166c6de 100644 --- a/packages/uipath/tests/cli/test_run.py +++ b/packages/uipath/tests/cli/test_run.py @@ -227,6 +227,85 @@ def test_the_flag_is_hidden_but_still_accepted(self, runner: CliRunner): ).output ) + def test_the_token_refresh_flag_needs_the_pipe( + self, runner: CliRunner, temp_dir: str, entrypoint: str + ): + with runner.isolated_filesystem(temp_dir=temp_dir): + result = runner.invoke( + cli, ["run", entrypoint, "--handler-ipc-token-refresh"] + ) + assert result.exit_code == 1 + assert "needs --handler-ipc-pipe" in result.output + assert "Successful execution." not in result.output + + def test_the_token_refresh_flag_is_hidden_but_still_accepted( + self, runner: CliRunner + ): + assert ( + "handler-ipc-token-refresh" + not in runner.invoke(cli, ["run", "--help"]).output + ) + assert ( + "No such option" + not in runner.invoke( + cli, + [ + "run", + "x", + "--handler-ipc-pipe", + "some-pipe", + "--handler-ipc-token-refresh", + ], + ).output + ) + + def test_the_token_refresh_flag_reaches_the_connection( + self, runner: CliRunner, temp_dir: str, monkeypatch + ): + """The executor's opt-in has to arrive where the provider is installed.""" + job_key = "3f2504e0-4f89-11d3-9a0c-0305e82c3301" + monkeypatch.setenv("UIPATH_JOB_KEY", job_key) + seen: list[tuple[str, str, int | None, bool]] = [] + + @asynccontextmanager + async def _fake_connection( + pipe, job_key, resume_version, *, refresh_token=False + ): + seen.append((pipe, job_key, resume_version, refresh_token)) + yield None + + factory = _make_mock_factory(["my_agent"]) + with runner.isolated_filesystem(temp_dir=temp_dir): + with ( + patch( + "uipath._cli.cli_run.Middlewares.next", + return_value=_middleware_continue(), + ), + patch( + "uipath._cli.cli_run.UiPathRuntimeFactoryRegistry.get", + return_value=factory, + ), + patch( + "uipath._cli.cli_run.ResourceOverwritesContext", + side_effect=_mock_resource_overwrites_context, + ), + patch( + "uipath._cli._job_api.handler_ipc_connection", _fake_connection + ), + ): + result = runner.invoke( + cli, + [ + "run", + "--handler-ipc-pipe", + "some-pipe", + "--handler-ipc-token-refresh", + ], + ) + + assert result.exit_code == 0, f"output: {result.output!r}" + assert seen == [("some-pipe", job_key, None, True)] + def test_the_job_runs_inside_the_open_connection( self, runner: CliRunner, temp_dir: str, monkeypatch ): @@ -235,7 +314,7 @@ def test_the_job_runs_inside_the_open_connection( monkeypatch.setenv("UIPATH_JOB_KEY", job_key) events: list[str] = [] - seen: list[tuple[str, str, int | None]] = [] + seen: list[tuple[str, str, int | None, bool]] = [] snapshots: list[object] = [] from uipath.runtime import context as runtime_context @@ -244,8 +323,10 @@ def test_the_job_runs_inside_the_open_connection( sentinel = logging.Handler() @asynccontextmanager - async def _fake_connection(pipe, job_key, resume_version): - seen.append((pipe, job_key, resume_version)) + async def _fake_connection( + pipe, job_key, resume_version, *, refresh_token=False + ): + seen.append((pipe, job_key, resume_version, refresh_token)) events.append("open") # Install for real: the ordering that matters is against the runtime context's # one-shot snapshot, not against runtime.execute. @@ -303,7 +384,7 @@ async def _execute(*a, **k): # The whole point: the connection must still be open when the job runs. assert events == ["open", "run", "close"], events # And it must be told which job, not None. - assert seen == [("some-pipe", job_key, None)] + assert seen == [("some-pipe", job_key, None, False)] # The connection must be open BEFORE the runtime context snapshots the sinks: move it # inside and __enter__ would capture None, losing every log line and the result. assert snapshots == [sentinel], ( diff --git a/packages/uipath/tests/cli/test_server_ipc.py b/packages/uipath/tests/cli/test_server_ipc.py index 2d7d1b8ae..3fab762b0 100644 --- a/packages/uipath/tests/cli/test_server_ipc.py +++ b/packages/uipath/tests/cli/test_server_ipc.py @@ -638,3 +638,190 @@ async def scenario() -> None: asyncio.run(scenario()) assert events == [("run",)] + + +class TestPooledTokenRefresh: + """refreshAccessTokenOverIpc is its own opt-in, set by a host that serves GetUserAccessToken.""" + + @staticmethod + def _fake_run(events: list[Any]) -> Any: + async def run(cmd, args, env, wd, on_run_start=None, on_run_end=None): + if on_run_start: + on_run_start() + events.append(("run",)) + if on_run_end: + on_run_end() + return {"ExitCode": 0, "Error": None} + + return run + + @staticmethod + def _spy_token_refresher(monkeypatch, events: list[Any]) -> None: + from uipath._cli import _job_token + + def install(jid: str, rv: int | None, cb: Any, loop: Any) -> str: + events.append(("install-token", jid, rv, cb)) + return "REFRESHER" + + def uninstall(refresher: Any) -> None: + events.append(("uninstall-token", refresher)) + + monkeypatch.setattr(_job_token, "install_job_token_refresher", install) + monkeypatch.setattr(_job_token, "uninstall_job_token_refresher", uninstall) + + def test_the_opt_in_defaults_to_off(self): + from uipath._cli.cli_server_ipc import PythonServerRunRequest + + assert ( + PythonServerRunRequest( + jobKey=JOB_ID, command="run" + ).refreshAccessTokenOverIpc + is False + ) + + def test_runjob_installs_the_refresher_inside_the_run_and_removes_it_after( + self, monkeypatch + ): + from uipath._cli import _job_api, cli_server_ipc + from uipath._cli.cli_server_ipc import PythonServerRunRequest + + events: list[Any] = [] + self._spy_token_refresher(monkeypatch, events) + monkeypatch.setattr( + _job_api, + "install_runtime_sinks", + lambda *a: events.append(("install-sinks",)), + ) + monkeypatch.setattr( + cli_server_ipc, "_run_command_isolated", self._fake_run(events) + ) + + class _Client: + def get_callback(self, contract: Any) -> Any: + return "CALLBACK" + + service = PythonRuntimeService() + request = PythonServerRunRequest( + jobKey=JOB_ID, + resumeVersion=4, + command="run", + args=[], + refreshAccessTokenOverIpc=True, + ) + + async def scenario() -> Any: + return await service.RunJob( + request, message=Message(client=cast(Any, _Client())) + ) + + result = asyncio.run(scenario()) + assert result.exitCode == 0, result.error + # No sinks without streamOutputOverIpc; the refresher lives exactly as long as the run. + assert events == [ + ("install-token", JOB_ID, 4, "CALLBACK"), + ("run",), + ("uninstall-token", "REFRESHER"), + ] + + def test_both_opt_ins_install_both(self, monkeypatch): + from uipath._cli import _job_api, cli_server_ipc + from uipath._cli.cli_server_ipc import PythonServerRunRequest + + events: list[Any] = [] + self._spy_token_refresher(monkeypatch, events) + monkeypatch.setattr( + _job_api, + "install_runtime_sinks", + lambda *a: events.append(("install-sinks",)), + ) + monkeypatch.setattr( + _job_api, "clear_runtime_sinks", lambda: events.append(("clear",)) + ) + monkeypatch.setattr( + cli_server_ipc, "_run_command_isolated", self._fake_run(events) + ) + + class _Client: + def get_callback(self, contract: Any) -> Any: + return "CALLBACK" + + service = PythonRuntimeService() + request = PythonServerRunRequest( + jobKey=JOB_ID, + command="run", + args=[], + streamOutputOverIpc=True, + refreshAccessTokenOverIpc=True, + ) + + async def scenario() -> Any: + return await service.RunJob( + request, message=Message(client=cast(Any, _Client())) + ) + + result = asyncio.run(scenario()) + assert result.exitCode == 0, result.error + assert events == [ + ("install-sinks",), + ("install-token", JOB_ID, None, "CALLBACK"), + ("run",), + ("clear",), + ("uninstall-token", "REFRESHER"), + ] + + @pytest.mark.parametrize( + "job_key", ["", "job-9", "00000000-0000-0000-0000-000000000000"] + ) + def test_token_refresh_needs_a_real_job_key(self, monkeypatch, job_key): + from uipath._cli import cli_server_ipc + from uipath._cli.cli_server_ipc import PythonServerRunRequest + + events: list[Any] = [] + self._spy_token_refresher(monkeypatch, events) + monkeypatch.setattr( + cli_server_ipc, "_run_command_isolated", self._fake_run(events) + ) + + class _Client: + def get_callback(self, contract: Any) -> Any: + return "CALLBACK" + + service = PythonRuntimeService() + request = PythonServerRunRequest( + jobKey=job_key, command="run", args=[], refreshAccessTokenOverIpc=True + ) + + async def scenario() -> Any: + return await service.RunJob( + request, message=Message(client=cast(Any, _Client())) + ) + + result = asyncio.run(scenario()) + assert result.exitCode == 1 + assert "refreshAccessTokenOverIpc" in (result.error or "") + assert "jobKey" in (result.error or "") + assert events == [] + + def test_token_refresh_needs_ipc(self, monkeypatch): + from uipath._cli import cli_server_ipc + from uipath._cli.cli_server_ipc import PythonServerRunRequest + + events: list[Any] = [] + self._spy_token_refresher(monkeypatch, events) + monkeypatch.setattr( + cli_server_ipc, "_run_command_isolated", self._fake_run(events) + ) + + service = PythonRuntimeService() + request = PythonServerRunRequest( + jobKey=JOB_ID, command="run", args=[], refreshAccessTokenOverIpc=True + ) + + async def scenario() -> Any: + return await service.RunJob(request, message=Message()) + + result = asyncio.run(scenario()) + assert result.exitCode == 1 + assert "refreshAccessTokenOverIpc" in (result.error or "") + assert "over IPC" in (result.error or "") + assert events == [] From ecddebd3132c59bc2cdcb255ed76655357ac1f93 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Mon, 16 Nov 2026 23:22:12 +0100 Subject: [PATCH 03/10] chore: bump uipath-platform to 0.3.8 and uipath to 2.15.3 Co-Authored-By: Claude Fable 5.1 --- packages/uipath-platform/pyproject.toml | 2 +- packages/uipath-platform/uv.lock | 2 +- packages/uipath/pyproject.toml | 4 ++-- packages/uipath/uv.lock | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/uipath-platform/pyproject.toml b/packages/uipath-platform/pyproject.toml index 413a6c63c..1fa8ed8c6 100644 --- a/packages/uipath-platform/pyproject.toml +++ b/packages/uipath-platform/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "uipath-platform" -version = "0.3.7" +version = "0.3.8" description = "HTTP client library for programmatic access to UiPath Platform" readme = { file = "README.md", content-type = "text/markdown" } requires-python = ">=3.11" diff --git a/packages/uipath-platform/uv.lock b/packages/uipath-platform/uv.lock index 4530e2ed5..525de4ee2 100644 --- a/packages/uipath-platform/uv.lock +++ b/packages/uipath-platform/uv.lock @@ -1095,7 +1095,7 @@ dev = [ [[package]] name = "uipath-platform" -version = "0.3.7" +version = "0.3.8" source = { editable = "." } dependencies = [ { name = "anyio" }, diff --git a/packages/uipath/pyproject.toml b/packages/uipath/pyproject.toml index 032d8b139..049ff1d32 100644 --- a/packages/uipath/pyproject.toml +++ b/packages/uipath/pyproject.toml @@ -1,13 +1,13 @@ [project] name = "uipath" -version = "2.15.2" +version = "2.15.3" description = "Python SDK and CLI for UiPath Platform, enabling programmatic interaction with automation services, process management, and deployment tools." readme = { file = "README.md", content-type = "text/markdown" } requires-python = ">=3.11" dependencies = [ "uipath-core>=0.5.33, <0.6.0", "uipath-runtime>=0.13.5, <0.14.0", - "uipath-platform>=0.3.3, <0.4.0", + "uipath-platform>=0.3.8, <0.4.0", "uipath-ipc>=2.5.1, <2.6.0", "click>=8.3.3, <9.0.0", "httpx>=0.28.1", diff --git a/packages/uipath/uv.lock b/packages/uipath/uv.lock index 6b4d34ce2..3c3d61cf6 100644 --- a/packages/uipath/uv.lock +++ b/packages/uipath/uv.lock @@ -2599,7 +2599,7 @@ wheels = [ [[package]] name = "uipath" -version = "2.15.2" +version = "2.15.3" source = { editable = "." } dependencies = [ { name = "applicationinsights" }, @@ -2762,7 +2762,7 @@ wheels = [ [[package]] name = "uipath-platform" -version = "0.3.7" +version = "0.3.8" source = { editable = "../uipath-platform" } dependencies = [ { name = "anyio" }, From 1fa7bb33daf5a925b5fa7596c3804be9963baf63 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Tue, 17 Nov 2026 11:35:40 +0100 Subject: [PATCH 04/10] refactor: make secret_from_environment a plain config field Co-Authored-By: Claude Fable 5.1 --- .../src/uipath/platform/_uipath.py | 3 +-- .../uipath/platform/common/_base_service.py | 12 ++++++---- .../src/uipath/platform/common/_config.py | 16 ++----------- .../tests/services/test_env_token_reread.py | 23 +++++++++++-------- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/packages/uipath-platform/src/uipath/platform/_uipath.py b/packages/uipath-platform/src/uipath/platform/_uipath.py index 139ba8fe5..3da1e79e5 100644 --- a/packages/uipath-platform/src/uipath/platform/_uipath.py +++ b/packages/uipath-platform/src/uipath/platform/_uipath.py @@ -86,9 +86,8 @@ def __init__( self._config = UiPathApiConfig( base_url=base_url, secret=secret, + secret_from_environment=secret_from_environment, ) - if secret_from_environment: - self._config.mark_secret_from_environment() except ValidationError as e: for error in e.errors(): if error["loc"][0] == "base_url": diff --git a/packages/uipath-platform/src/uipath/platform/common/_base_service.py b/packages/uipath-platform/src/uipath/platform/common/_base_service.py index ad23aafd9..f6528ee60 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_base_service.py +++ b/packages/uipath-platform/src/uipath/platform/common/_base_service.py @@ -215,7 +215,10 @@ def request( specific_component = _get_caller_component() - # A copy: the caller's mapping must not pick up the headers added here. + # Work on a copy: the headers added below must not be written into the dict the + # caller passed in. That dict is reused on retries and often across calls, and an + # Authorization header left in it would later look like one the caller chose, so + # the token would never be re-read. kwargs["headers"] = dict(kwargs.get("headers") or {}) kwargs["headers"][HEADER_USER_AGENT] = user_agent_value(specific_component) _inject_trace_context(kwargs["headers"]) @@ -260,6 +263,7 @@ async def request_async( f"HEADERS: {kwargs.get('headers', self._client_async.headers)}" ) + # A copy, for the same reason as in request(). kwargs["headers"] = dict(kwargs.get("headers") or {}) kwargs["headers"][HEADER_USER_AGENT] = user_agent_value( self._specific_component @@ -318,10 +322,10 @@ def _add_authorization(self, headers: MutableMapping[str, Any]) -> None: Otherwise the client default header set in ``__init__`` is used, as before. A header the caller supplied is left alone. """ - if not self._config.secret_from_environment or _has_authorization_header( - headers - ): + from_environment = self._config.secret_from_environment + if not from_environment or _has_authorization_header(headers): return + headers["Authorization"] = f"Bearer {self._current_secret()}" @property diff --git a/packages/uipath-platform/src/uipath/platform/common/_config.py b/packages/uipath-platform/src/uipath/platform/common/_config.py index f079e7686..70cccadb9 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_config.py +++ b/packages/uipath-platform/src/uipath/platform/common/_config.py @@ -3,25 +3,13 @@ from pathlib import Path from typing import Any -from pydantic import BaseModel, PrivateAttr +from pydantic import BaseModel class UiPathApiConfig(BaseModel): base_url: str secret: str - - # Set by UiPath() when the secret came from the token environment variables. A long - # job's runtime renews the token there, so services re-read it on every request. - _secret_from_environment: bool = PrivateAttr(default=False) - - @property - def secret_from_environment(self) -> bool: - """Whether ``secret`` was read from the token environment variables.""" - return self._secret_from_environment - - def mark_secret_from_environment(self) -> None: - """Record that ``secret`` came from the environment and may be renewed there.""" - self._secret_from_environment = True + secret_from_environment: bool = False class ConfigurationManager: diff --git a/packages/uipath-platform/tests/services/test_env_token_reread.py b/packages/uipath-platform/tests/services/test_env_token_reread.py index 7017de3d5..95a5a7063 100644 --- a/packages/uipath-platform/tests/services/test_env_token_reread.py +++ b/packages/uipath-platform/tests/services/test_env_token_reread.py @@ -27,10 +27,10 @@ def url(base_url: str, org: str, tenant: str) -> str: @pytest.fixture def env_config(config: UiPathApiConfig, monkeypatch) -> UiPathApiConfig: - """The fixture config, marked as UiPath() marks one built from the environment.""" + """The fixture config, flagged as UiPath() flags one built from the environment.""" monkeypatch.setenv(ACCESS, config.secret) monkeypatch.delenv(UNATTENDED, raising=False) - config.mark_secret_from_environment() + config.secret_from_environment = True return config @@ -163,14 +163,17 @@ def test_an_explicit_secret_ignores_the_environment( assert _sent_auth(httpx_mock) == [f"Bearer {secret}"] assert config.secret == secret - def test_the_flag_is_outside_validation_and_serialization(self): - config = UiPathApiConfig(base_url="https://x", secret="s") - assert config.secret_from_environment is False - - config.mark_secret_from_environment() - - assert config.secret_from_environment is True - assert config.model_dump() == {"base_url": "https://x", "secret": "s"} + def test_the_flag_defaults_to_false(self): + assert ( + UiPathApiConfig(base_url="https://x", secret="s").secret_from_environment + is False + ) + assert ( + UiPathApiConfig( + base_url="https://x", secret="s", secret_from_environment=True + ).secret_from_environment + is True + ) class TestUiPathFactory: From b37f01b8ad998a16a1b7f2895cc196949d90f9d2 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Wed, 18 Nov 2026 12:09:33 +0100 Subject: [PATCH 05/10] feat: re-read the environment token before each trace upload Co-Authored-By: Claude Fable 5.1 --- .../uipath/docs/core/environment_variables.md | 2 +- .../src/uipath/tracing/_otel_exporters.py | 11 +++++++ .../tests/tracing/test_otel_exporters.py | 29 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/uipath/docs/core/environment_variables.md b/packages/uipath/docs/core/environment_variables.md index 625527764..546e1e307 100644 --- a/packages/uipath/docs/core/environment_variables.md +++ b/packages/uipath/docs/core/environment_variables.md @@ -117,4 +117,4 @@ The runtime environment automatically includes certain variables (such as `UIPAT ### Access Token Renewal A job receives its user access token once, through `UNATTENDED_USER_ACCESS_TOKEN` (or `UIPATH_ACCESS_TOKEN`). A job that runs longer than that token lives would otherwise start failing with `401` responses. When the job executor asks for it (it starts the job with `uipath run --handler-ipc-pipe ... --handler-ipc-token-refresh`, or sets `refreshAccessTokenOverIpc` on a pooled run request), the runtime asks the executor for a new token over the job's IPC channel at about 80% of the current token's lifetime and writes it back to the token environment variables that are set. -`UiPath()` clients configured from the environment read the token variable on every request, so they pick the renewed token up without any change; code that reads the variables itself sees it too. A client created with an explicit `secret=` keeps that secret. When the executor does not ask for renewal, nothing changes. If a renewal fails, the runtime logs a warning once, keeps the current token and tries again later. +`UiPath()` clients configured from the environment read the token variable on every request, so they pick the renewed token up without any change; code that reads the variables itself sees it too, and the trace exporter re-reads `UIPATH_ACCESS_TOKEN` before each upload. A client created with an explicit `secret=` keeps that secret. When the executor does not ask for renewal, nothing changes. If a renewal fails, the runtime logs a warning once, keeps the current token and tries again later. diff --git a/packages/uipath/src/uipath/tracing/_otel_exporters.py b/packages/uipath/src/uipath/tracing/_otel_exporters.py index 6796c9629..b45269482 100644 --- a/packages/uipath/src/uipath/tracing/_otel_exporters.py +++ b/packages/uipath/src/uipath/tracing/_otel_exporters.py @@ -395,10 +395,21 @@ def _build_url(self, span_list: list[Dict[str, Any]]) -> str: source = str(span_list[0].get("Source") or SpanSource.CODED_AGENTS) return f"{self.base_url}/api/Traces/v3/spans?traceId={trace_id}&source={source}" + def _refresh_auth_token(self) -> None: + """Follow the token environment variable: a long job's runtime renews it there.""" + token = os.environ.get(ENV_UIPATH_ACCESS_TOKEN) + if not token or token == self.auth_token: + return + + self.auth_token = token + self.headers["Authorization"] = f"Bearer {token}" + self.http_client.headers["Authorization"] = f"Bearer {token}" + def _send_with_retries( self, url: str, payload: list[Dict[str, Any]], max_retries: int = 4 ) -> SpanExportResult: """Send the HTTP request with retry logic.""" + self._refresh_auth_token() for attempt in range(max_retries): try: response = self.http_client.post(url, json=payload) diff --git a/packages/uipath/tests/tracing/test_otel_exporters.py b/packages/uipath/tests/tracing/test_otel_exporters.py index c8630b539..d930fb6ba 100644 --- a/packages/uipath/tests/tracing/test_otel_exporters.py +++ b/packages/uipath/tests/tracing/test_otel_exporters.py @@ -3,10 +3,12 @@ import unittest from unittest.mock import MagicMock, patch +import httpx import pytest from opentelemetry.sdk.trace import ReadableSpan from opentelemetry.sdk.trace.export import SpanExportResult +from uipath._utils._ssl_context import get_httpx_client_kwargs from uipath.platform.common._span_utils import SpanSource, SpanStatus from uipath.platform.constants import ( HEADER_INTERNAL_ACCOUNT_ID, @@ -161,6 +163,33 @@ def test_force_flush(exporter): assert exporter.force_flush() is True +def test_send_uses_the_token_currently_in_the_environment(mock_env_vars): + """A token renewed in the environment after construction goes out on the next upload.""" + seen: list[str | None] = [] + + def handler(request: httpx.Request) -> httpx.Response: + seen.append(request.headers.get("Authorization")) + return httpx.Response(200) + + exporter = LlmOpsHttpExporter() + exporter.http_client.close() + exporter.http_client = httpx.Client( + **get_httpx_client_kwargs(headers=exporter.headers), + transport=httpx.MockTransport(handler), + ) + url = "https://test.uipath.com/org/tenant/llmopstenant_/api/Traces/v3/spans" + + exporter._send_with_retries(url, []) + os.environ["UIPATH_ACCESS_TOKEN"] = "renewed-token" + exporter._send_with_retries(url, []) + del os.environ["UIPATH_ACCESS_TOKEN"] + exporter._send_with_retries(url, []) + + assert seen == ["Bearer test-token", "Bearer renewed-token", "Bearer renewed-token"] + assert exporter.auth_token == "renewed-token" + assert exporter.headers["Authorization"] == "Bearer renewed-token" + + def test_shutdown_closes_http_client(mock_env_vars): with patch("uipath.tracing._otel_exporters.httpx.Client") as mock_client_class: mock_client = MagicMock() From a6d35089e4214939faa0ce88a61bd6fb206eb8da Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Thu, 19 Nov 2026 10:58:13 +0100 Subject: [PATCH 06/10] feat: send the current environment token when a bridge connects Co-Authored-By: Claude Fable 5.1 --- .../uipath/docs/core/environment_variables.md | 2 +- .../uipath/src/uipath/_cli/_chat/_bridge.py | 4 +- .../src/uipath/_cli/_chat/_voice_bridge.py | 4 +- .../uipath/src/uipath/_cli/_debug/_bridge.py | 3 ++ .../src/uipath/_cli/_utils/_auth_header.py | 21 +++++++++ packages/uipath/tests/cli/chat/test_bridge.py | 42 ++++++++++++++++++ .../tests/cli/chat/test_voice_bridge.py | 32 ++++++++++++++ .../tests/cli/test_signalr_debug_bridge.py | 44 +++++++++++++++++++ .../uipath/tests/cli/unit/test_auth_header.py | 33 ++++++++++++++ 9 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 packages/uipath/src/uipath/_cli/_utils/_auth_header.py create mode 100644 packages/uipath/tests/cli/unit/test_auth_header.py diff --git a/packages/uipath/docs/core/environment_variables.md b/packages/uipath/docs/core/environment_variables.md index 546e1e307..d333f876c 100644 --- a/packages/uipath/docs/core/environment_variables.md +++ b/packages/uipath/docs/core/environment_variables.md @@ -117,4 +117,4 @@ The runtime environment automatically includes certain variables (such as `UIPAT ### Access Token Renewal A job receives its user access token once, through `UNATTENDED_USER_ACCESS_TOKEN` (or `UIPATH_ACCESS_TOKEN`). A job that runs longer than that token lives would otherwise start failing with `401` responses. When the job executor asks for it (it starts the job with `uipath run --handler-ipc-pipe ... --handler-ipc-token-refresh`, or sets `refreshAccessTokenOverIpc` on a pooled run request), the runtime asks the executor for a new token over the job's IPC channel at about 80% of the current token's lifetime and writes it back to the token environment variables that are set. -`UiPath()` clients configured from the environment read the token variable on every request, so they pick the renewed token up without any change; code that reads the variables itself sees it too, and the trace exporter re-reads `UIPATH_ACCESS_TOKEN` before each upload. A client created with an explicit `secret=` keeps that secret. When the executor does not ask for renewal, nothing changes. If a renewal fails, the runtime logs a warning once, keeps the current token and tries again later. +`UiPath()` clients configured from the environment read the token variable on every request, so they pick the renewed token up without any change; code that reads the variables itself sees it too, and the trace exporter re-reads `UIPATH_ACCESS_TOKEN` before each upload, as do the chat, voice and debug bridges whenever they connect. A client created with an explicit `secret=` keeps that secret. When the executor does not ask for renewal, nothing changes. If a renewal fails, the runtime logs a warning once, keeps the current token and tries again later. diff --git a/packages/uipath/src/uipath/_cli/_chat/_bridge.py b/packages/uipath/src/uipath/_cli/_chat/_bridge.py index 397b7454e..005e01619 100644 --- a/packages/uipath/src/uipath/_cli/_chat/_bridge.py +++ b/packages/uipath/src/uipath/_cli/_chat/_bridge.py @@ -32,6 +32,8 @@ from uipath.runtime.chat import UiPathChatProtocol from uipath.runtime.context import UiPathRuntimeContext +from .._utils._auth_header import with_current_access_token + logger = logging.getLogger(__name__) # Type for tool call resume values (confirmToolCall or endToolCall payloads) @@ -222,7 +224,7 @@ async def connect(self, timeout: float = 10.0) -> None: self._client.connect( url=self.websocket_url, socketio_path=self.websocket_path, - headers=self.headers, + headers=with_current_access_token(self.headers), auth=self.auth, transports=["websocket"], ), diff --git a/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py b/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py index 4240f28c0..fe61c63dc 100644 --- a/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py +++ b/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py @@ -28,6 +28,8 @@ ) from uipath.runtime.context import UiPathRuntimeContext +from .._utils._auth_header import with_current_access_token + logger = logging.getLogger(__name__) @@ -109,7 +111,7 @@ async def run(self) -> VoiceSessionEndReason: self._client.connect( url=self._url, socketio_path=self._socketio_path, - headers=self._headers, + headers=with_current_access_token(self._headers), transports=["websocket"], ), timeout=_ATTEMPT_CAS_SOCKET_CONNECTION_TIMEOUT_SECONDS, diff --git a/packages/uipath/src/uipath/_cli/_debug/_bridge.py b/packages/uipath/src/uipath/_cli/_debug/_bridge.py index 4571ec1a1..f4867ab52 100644 --- a/packages/uipath/src/uipath/_cli/_debug/_bridge.py +++ b/packages/uipath/src/uipath/_cli/_debug/_bridge.py @@ -34,6 +34,8 @@ ) from uipath.runtime.events import UiPathRuntimeStateEvent, UiPathRuntimeStatePhase +from .._utils._auth_header import with_current_access_token + DebugAttachMode = Literal["signalr", "console", "none"] logger = logging.getLogger(__name__) @@ -512,6 +514,7 @@ async def connect(self) -> None: all_headers = {**self.headers} if self.access_token: all_headers["Authorization"] = f"Bearer {self.access_token}" + all_headers = with_current_access_token(all_headers) # Lazy import to avoid dependency if not used, improve startup time from pysignalr.client import SignalRClient diff --git a/packages/uipath/src/uipath/_cli/_utils/_auth_header.py b/packages/uipath/src/uipath/_cli/_utils/_auth_header.py new file mode 100644 index 000000000..6016f429b --- /dev/null +++ b/packages/uipath/src/uipath/_cli/_utils/_auth_header.py @@ -0,0 +1,21 @@ +"""The bearer header for connections the CLI opens on behalf of a job.""" + +import os +from collections.abc import Mapping + +from uipath.platform.constants import ENV_UIPATH_ACCESS_TOKEN + + +def with_current_access_token(headers: Mapping[str, str]) -> dict[str, str]: + """A copy of ``headers`` whose ``Authorization`` carries the token now in the environment. + + A long job's runtime renews the token in the environment, so a connection opened late + in the job must send the renewed token, not the one read when the headers were built. + Only an ``Authorization`` header that is already present is replaced, and only when the + variable is set. + """ + result = dict(headers) + token = os.environ.get(ENV_UIPATH_ACCESS_TOKEN) + if token and "Authorization" in result: + result["Authorization"] = f"Bearer {token}" + return result diff --git a/packages/uipath/tests/cli/chat/test_bridge.py b/packages/uipath/tests/cli/chat/test_bridge.py index 2c4aedbe9..3ec6152f9 100644 --- a/packages/uipath/tests/cli/chat/test_bridge.py +++ b/packages/uipath/tests/cli/chat/test_bridge.py @@ -1200,3 +1200,45 @@ async def test_storage_cleaned_up_after_consumption(self) -> None: assert len(bridge._tool_resume_results) == 0 assert len(bridge._tool_resume_pending) == 0 assert len(bridge._expected_tool_call_ids) == 0 + + +class TestConnectUsesTheCurrentToken: + """The bearer sent on connect is the token in the environment at that moment.""" + + @pytest.mark.anyio + async def test_connect_sends_the_renewed_token( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + from unittest.mock import patch + + monkeypatch.delenv("CAS_WEBSOCKET_DISABLED", raising=False) + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "renewed-token") + bridge = SocketIOChatBridge( + websocket_url="wss://test.example.com", + websocket_path="/socket.io", + conversation_id="conv-123", + exchange_id="exch-456", + headers={"Authorization": "Bearer startup-token", "X-Keep": "yes"}, + ) + seen: dict[str, Any] = {} + + class FakeClient: + def __init__(self, **_: Any) -> None: + self.handlers: dict[str, Any] = {} + + def on(self, event: str, handler: Any) -> None: + self.handlers[event] = handler + + async def connect(self, **kwargs: Any) -> None: + seen.update(kwargs) + await self.handlers["connect"]() + + async def disconnect(self) -> None: + pass + + with patch("socketio.AsyncClient", FakeClient): + await bridge.connect(timeout=1.0) + + assert seen["headers"]["Authorization"] == "Bearer renewed-token" + assert seen["headers"]["X-Keep"] == "yes" + assert bridge.headers["Authorization"] == "Bearer startup-token" diff --git a/packages/uipath/tests/cli/chat/test_voice_bridge.py b/packages/uipath/tests/cli/chat/test_voice_bridge.py index c4e704922..2bc5c0b51 100644 --- a/packages/uipath/tests/cli/chat/test_voice_bridge.py +++ b/packages/uipath/tests/cli/chat/test_voice_bridge.py @@ -233,3 +233,35 @@ def test_omits_conversational_user_id_header_when_none( bridge = get_voice_bridge(ctx, AsyncMock()) assert "X-UiPath-Internal-ConversationalUserId" not in bridge._headers + + +async def test_run_connects_with_the_token_currently_in_the_environment( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from unittest.mock import patch + + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "renewed-token") + session = _make_session() + session._headers = {"Authorization": "Bearer startup-token", "X-Keep": "yes"} + seen: dict[str, Any] = {} + + class FakeClient: + def __init__(self, **_: Any) -> None: + pass + + def on(self, event: str, handler: Any) -> None: + pass + + async def connect(self, **kwargs: Any) -> None: + seen.update(kwargs) + session._end_session(VoiceSessionEndReason.COMPLETED) + + async def disconnect(self) -> None: + pass + + with patch("socketio.AsyncClient", FakeClient): + reason = await session.run() + + assert reason == VoiceSessionEndReason.COMPLETED + assert seen["headers"]["Authorization"] == "Bearer renewed-token" + assert seen["headers"]["X-Keep"] == "yes" diff --git a/packages/uipath/tests/cli/test_signalr_debug_bridge.py b/packages/uipath/tests/cli/test_signalr_debug_bridge.py index fc08f2668..43605c274 100644 --- a/packages/uipath/tests/cli/test_signalr_debug_bridge.py +++ b/packages/uipath/tests/cli/test_signalr_debug_bridge.py @@ -2,6 +2,8 @@ from typing import Any from unittest.mock import AsyncMock +import pytest + from uipath._cli._debug._bridge import SignalRDebugBridge from uipath.runtime import UiPathBreakpointResult @@ -61,3 +63,45 @@ async def test_breakpoint_hit_without_a_function_start_line_keeps_its_shape() -> "nextNodes": ["route"], }, ) + + +async def test_connect_sends_the_token_currently_in_the_environment( + monkeypatch: pytest.MonkeyPatch, +) -> None: + from unittest.mock import patch + + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "renewed-token") + bridge = SignalRDebugBridge( + hub_url="https://hub.example/signalr/robotdebug", + access_token="startup-token", + headers={"X-Keep": "yes"}, + ) + seen: dict[str, Any] = {} + + class FakeSignalRClient: + def __init__(self, url: str, headers: dict[str, str]) -> None: + seen["url"] = url + seen["headers"] = headers + self._on_open: Any = None + + def on(self, event: str, handler: Any) -> None: + pass + + def on_open(self, handler: Any) -> None: + self._on_open = handler + + def on_close(self, handler: Any) -> None: + pass + + def on_error(self, handler: Any) -> None: + pass + + async def run(self) -> None: + await self._on_open() + + with patch("pysignalr.client.SignalRClient", FakeSignalRClient): + await bridge.connect() + + assert seen["headers"]["Authorization"] == "Bearer renewed-token" + assert seen["headers"]["X-Keep"] == "yes" + assert bridge.access_token == "startup-token" diff --git a/packages/uipath/tests/cli/unit/test_auth_header.py b/packages/uipath/tests/cli/unit/test_auth_header.py new file mode 100644 index 000000000..8591b0913 --- /dev/null +++ b/packages/uipath/tests/cli/unit/test_auth_header.py @@ -0,0 +1,33 @@ +import pytest + +from uipath._cli._utils._auth_header import with_current_access_token + + +def test_replaces_the_bearer_with_the_environment_token( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "renewed") + headers = {"Authorization": "Bearer old", "X-Other": "kept"} + + result = with_current_access_token(headers) + + assert result == {"Authorization": "Bearer renewed", "X-Other": "kept"} + assert headers["Authorization"] == "Bearer old", "the caller's dict is untouched" + + +def test_keeps_the_bearer_when_the_variable_is_unset( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.delenv("UIPATH_ACCESS_TOKEN", raising=False) + + assert with_current_access_token({"Authorization": "Bearer old"}) == { + "Authorization": "Bearer old" + } + + +def test_adds_nothing_when_no_bearer_was_given( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "renewed") + + assert with_current_access_token({"X-Other": "kept"}) == {"X-Other": "kept"} From df0c820a5308ae231e34c1e91d2ac8b6f694bbbf Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Fri, 20 Nov 2026 10:41:16 +0100 Subject: [PATCH 07/10] fix: retry the token renewal after 30 seconds instead of 60 The same wait covers a failed call and a host that hands back a token already due. Halving it picks up the serverless handler's renewed token sooner and gives a short-lived token more tries before it expires. Co-Authored-By: Claude Fable 5.2 --- packages/uipath/src/uipath/_cli/_job_token.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/uipath/src/uipath/_cli/_job_token.py b/packages/uipath/src/uipath/_cli/_job_token.py index 9e7d8c387..faf202daa 100644 --- a/packages/uipath/src/uipath/_cli/_job_token.py +++ b/packages/uipath/src/uipath/_cli/_job_token.py @@ -42,7 +42,7 @@ _MIN_REFRESH_MARGIN_S = 120.0 # After a failed renewal, or one that returned a token already due, leave the host alone for # this long before asking again. -_RETRY_AFTER_FAILURE_S = 60.0 +_RETRY_AFTER_FAILURE_S = 30.0 _TOKEN_ENV_VARS = (ENV_UNATTENDED_USER_ACCESS_TOKEN, ENV_UIPATH_ACCESS_TOKEN) # .NET exception type names a host answers with when it cannot serve the method at all. _UNSUPPORTED_MARKERS = ( @@ -122,7 +122,7 @@ class JobTokenRefresher: One task, started by :meth:`start`, sleeps until 80 percent of the current token's lifetime has passed (at least two minutes before expiry), asks the host, stores and publishes the answer, and reschedules from the new token. A failure is logged once, the current token is - kept and the host is left alone for a minute; a host that does not implement the call ends + kept and the host is left alone for 30 seconds; a host that does not implement the call ends the task. :meth:`stop` cancels the task, a request in flight included, and a stopped refresher never writes to the environment again. """ From 628b8d62702f40efd5206c4348bf0b6aca626261 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Fri, 20 Nov 2026 10:50:37 +0100 Subject: [PATCH 08/10] refactor: treat every token renewal failure the same way The host opts in on the command line, so the refresher no longer reads the error's type to decide the host is "unsupported" and stop asking. Any failure keeps the current token and is retried after the backoff. Only the first failure is logged. Its warning now says so: later failures to renew the job's token are not logged. Co-Authored-By: Claude Fable 5.2 --- packages/uipath/src/uipath/_cli/_job_token.py | 50 ++++++------------- packages/uipath/tests/cli/test_job_token.py | 17 +++++-- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/packages/uipath/src/uipath/_cli/_job_token.py b/packages/uipath/src/uipath/_cli/_job_token.py index faf202daa..ce4672eb7 100644 --- a/packages/uipath/src/uipath/_cli/_job_token.py +++ b/packages/uipath/src/uipath/_cli/_job_token.py @@ -44,13 +44,6 @@ # this long before asking again. _RETRY_AFTER_FAILURE_S = 30.0 _TOKEN_ENV_VARS = (ENV_UNATTENDED_USER_ACCESS_TOKEN, ENV_UIPATH_ACCESS_TOKEN) -# .NET exception type names a host answers with when it cannot serve the method at all. -_UNSUPPORTED_MARKERS = ( - "MethodNotFound", - "EndpointNotFound", - "NotSupported", - "MissingMethod", -) def _claims(token: str) -> dict[str, Any]: @@ -108,23 +101,16 @@ def publish_token_to_environ(token: str) -> None: os.environ[name] = token -def _is_unsupported(error: BaseException) -> bool: - """True for a host that does not implement GetUserAccessToken; asking again cannot help.""" - if isinstance(error, (AttributeError, NotImplementedError)): - return True - type_name = getattr(error, "type_name", None) or "" - return any(marker in type_name for marker in _UNSUPPORTED_MARKERS) - - class JobTokenRefresher: """Renews the job's user access token on the IPC loop and publishes it to the environment. One task, started by :meth:`start`, sleeps until 80 percent of the current token's lifetime has passed (at least two minutes before expiry), asks the host, stores and publishes the - answer, and reschedules from the new token. A failure is logged once, the current token is - kept and the host is left alone for 30 seconds; a host that does not implement the call ends - the task. :meth:`stop` cancels the task, a request in flight included, and a stopped - refresher never writes to the environment again. + answer, and reschedules from the new token. The host opted in, so every failure is handled + the same way, whatever its type: the current token is kept and the host is asked again after + 30 seconds. Only the first failure is logged, as a warning that says no more will follow. + :meth:`stop` cancels the task, a request in flight included, and a stopped refresher never + writes to the environment again. """ def __init__( @@ -153,7 +139,6 @@ def __init__( self._task: Future[None] | None = None self._retry_not_before = 0.0 self._stopped = False - self._unsupported = False self._reported_failure = False def start(self) -> None: @@ -187,7 +172,7 @@ def expires_at(self) -> float | None: return None if state is None else state.expires_at async def _run(self) -> None: - while not self._stopped and not self._unsupported: + while not self._stopped: state = self._state if state is None or state.refresh_at is None: return # no token, or one without exp: nothing to time against @@ -241,24 +226,17 @@ def _note_failure(self, error: BaseException) -> None: return # teardown cancels in-flight work; nothing worth reporting with self._lock: self._retry_not_before = self._clock() + _RETRY_AFTER_FAILURE_S - if _is_unsupported(error): - self._unsupported = True first = not self._reported_failure self._reported_failure = True if not first: - logger.debug("Renewing the job's user access token failed again: %r", error) - elif self._unsupported: - logger.warning( - "The job host does not support renewing the user access token (%r); " - "keeping the token the job started with.", - error, - ) - else: - logger.warning( - "Could not renew the job's user access token over IPC (%r); keeping the " - "current token and retrying later. Further failures are logged at debug level.", - error, - ) + return # the one warning already said that later failures are not logged + logger.warning( + "Could not renew the job's user access token over IPC (%r). Keeping the current " + "token and retrying every %g seconds. This is logged only once: later failures " + "to renew this job's token are not logged.", + error, + _RETRY_AFTER_FAILURE_S, + ) def install_job_token_refresher( diff --git a/packages/uipath/tests/cli/test_job_token.py b/packages/uipath/tests/cli/test_job_token.py index 50181be47..8c4faa492 100644 --- a/packages/uipath/tests/cli/test_job_token.py +++ b/packages/uipath/tests/cli/test_job_token.py @@ -301,22 +301,29 @@ def test_a_failed_renewal_warns_once_and_retries_after_the_backoff( warnings = _warnings(records) assert len(warnings) == 1 assert "pipe down" in warnings[0] + # the silence that follows is announced + assert "logged only once" in warnings[0] - def test_an_unsupported_host_ends_the_task(self, ipc_loop, records): + def test_a_host_without_the_method_is_retried_like_any_other_failure( + self, ipc_loop, monkeypatch, records + ): + # The host opted in, so no error type is read as "unsupported": a missing method is a + # host fault like any other, and the refresher keeps asking. + monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) api = FakeJobApi(error=RemoteError("UiPath.Ipc.MethodNotFoundException")) token = token_with_lifetime(600, remaining=60) # due now refresher = _refresher(api, ipc_loop, token) refresher.start() + assert _wait(lambda: len(api.requests) >= 2) # still asking task = refresher.task - assert task is not None - task.result(timeout=5) # the loop exits on its own - assert len(api.requests) == 1 + assert task is not None and not task.done() + refresher.stop() assert refresher.current_token == token warnings = _warnings(records) assert len(warnings) == 1 - assert "does not support" in warnings[0] + assert "MethodNotFoundException" in warnings[0] @pytest.mark.parametrize("bad", ["", None, 42]) def test_an_empty_answer_is_a_failure(self, ipc_loop, monkeypatch, records, bad): From 5b9d987726d1061987b2e0f47393e06ecf8d8902 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Fri, 20 Nov 2026 11:39:35 +0100 Subject: [PATCH 09/10] chore: slim the comments and docstrings of the token renewal change Comments and docstrings only; no code change. The environment variables page now describes what a job sees instead of how renewal is wired. Co-Authored-By: Claude Fable 5.2 --- packages/uipath-platform/CLAUDE.md | 2 +- .../src/uipath/platform/_uipath.py | 3 +- .../src/uipath/platform/common/_api_client.py | 6 +- .../uipath/platform/common/_base_service.py | 20 ++---- .../tests/services/test_env_token_reread.py | 7 +- .../uipath/docs/core/environment_variables.md | 4 +- packages/uipath/src/uipath/_cli/_job_api.py | 5 +- packages/uipath/src/uipath/_cli/_job_token.py | 68 ++++++------------- .../src/uipath/_cli/_utils/_auth_header.py | 10 +-- packages/uipath/src/uipath/_cli/cli_run.py | 2 +- .../uipath/src/uipath/_cli/cli_server_ipc.py | 6 +- .../src/uipath/tracing/_otel_exporters.py | 2 +- packages/uipath/tests/cli/test_job_token.py | 15 ++-- packages/uipath/tests/cli/test_run.py | 2 +- packages/uipath/tests/cli/test_server_ipc.py | 2 +- 15 files changed, 45 insertions(+), 109 deletions(-) diff --git a/packages/uipath-platform/CLAUDE.md b/packages/uipath-platform/CLAUDE.md index f1a577d6b..35abe93f2 100644 --- a/packages/uipath-platform/CLAUDE.md +++ b/packages/uipath-platform/CLAUDE.md @@ -57,7 +57,7 @@ Credentials can be provided via constructor parameters or environment variables - **User authentication** — provide `base_url` and `secret` (access token), or set them via env vars - **S2S (service-to-service)** — provide `client_id` with either `client_secret` or `client_assertion` (an OIDC token for federated credentials), optionally with `scope`; uses `ExternalApplicationService` internally to exchange for an access token -- **Token renewal** — a `UiPath()` whose secret came from the environment (`UiPathApiConfig.secret_from_environment`) re-reads `UNATTENDED_USER_ACCESS_TOKEN` / `UIPATH_ACCESS_TOKEN` on every request, so a job runtime that renews the token there (`uipath/_cli/_job_token.py`) is picked up by existing clients. An explicit `secret=` is sent as is. +- **Token renewal** — a `UiPath()` whose secret came from the environment (`UiPathApiConfig.secret_from_environment`) re-reads `UNATTENDED_USER_ACCESS_TOKEN` / `UIPATH_ACCESS_TOKEN` on every request. An explicit `secret=` is sent as is. Configuration is validated with Pydantic; missing `base_url` raises `BaseUrlMissingError`, missing `secret` raises `SecretMissingError`. diff --git a/packages/uipath-platform/src/uipath/platform/_uipath.py b/packages/uipath-platform/src/uipath/platform/_uipath.py index 3da1e79e5..c0de56261 100644 --- a/packages/uipath-platform/src/uipath/platform/_uipath.py +++ b/packages/uipath-platform/src/uipath/platform/_uipath.py @@ -66,8 +66,7 @@ def __init__( scope: Optional[str] = None, debug: bool = False, ) -> None: - # A token read from the environment is re-read on every request (a long job's - # runtime may renew it there); an explicit secret is sent as is. + # A secret read from the environment is re-read on every request. secret_from_environment = False try: if _has_valid_client_credentials( diff --git a/packages/uipath-platform/src/uipath/platform/common/_api_client.py b/packages/uipath-platform/src/uipath/platform/common/_api_client.py index 556647b02..3f5bee633 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_api_client.py +++ b/packages/uipath-platform/src/uipath/platform/common/_api_client.py @@ -30,8 +30,7 @@ def request( **kwargs: Any, ) -> Response: if kwargs.get("include_folder_headers", False): - # The client's default headers are merged by httpx; copying them here would - # make the default Authorization look caller-supplied to BaseService. + # httpx merges the client's default headers; do not copy them here. kwargs["headers"] = { **kwargs.get("headers", {}), **self.folder_headers, @@ -50,8 +49,7 @@ async def request_async( **kwargs: Any, ) -> Response: if kwargs.get("include_folder_headers", False): - # The client's default headers are merged by httpx; copying them here would - # make the default Authorization look caller-supplied to BaseService. + # httpx merges the client's default headers; do not copy them here. kwargs["headers"] = { **kwargs.get("headers", {}), **self.folder_headers, diff --git a/packages/uipath-platform/src/uipath/platform/common/_base_service.py b/packages/uipath-platform/src/uipath/platform/common/_base_service.py index f6528ee60..db64303b8 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_base_service.py +++ b/packages/uipath-platform/src/uipath/platform/common/_base_service.py @@ -215,10 +215,7 @@ def request( specific_component = _get_caller_component() - # Work on a copy: the headers added below must not be written into the dict the - # caller passed in. That dict is reused on retries and often across calls, and an - # Authorization header left in it would later look like one the caller chose, so - # the token would never be re-read. + # Copy: never write into the caller's dict. kwargs["headers"] = dict(kwargs.get("headers") or {}) kwargs["headers"][HEADER_USER_AGENT] = user_agent_value(specific_component) _inject_trace_context(kwargs["headers"]) @@ -263,7 +260,7 @@ async def request_async( f"HEADERS: {kwargs.get('headers', self._client_async.headers)}" ) - # A copy, for the same reason as in request(). + # Copy: never write into the caller's dict. kwargs["headers"] = dict(kwargs.get("headers") or {}) kwargs["headers"][HEADER_USER_AGENT] = user_agent_value( self._specific_component @@ -301,12 +298,7 @@ def auth_headers(self) -> dict[str, str]: return {"Authorization": header} def _current_secret(self) -> str: - """The bearer token to send now. - - A secret that came from the environment is re-read on every call: a long-running - job's runtime renews the token there (see ``uipath._cli._job_token``). Any other - secret is sent as is. - """ + """The bearer token to send now; one from the environment is re-read.""" if self._config.secret_from_environment: renewed = os.environ.get( ENV_UNATTENDED_USER_ACCESS_TOKEN @@ -317,11 +309,7 @@ def _current_secret(self) -> str: return self._config.secret def _add_authorization(self, headers: MutableMapping[str, Any]) -> None: - """Put the current environment token on a request, when the secret came from there. - - Otherwise the client default header set in ``__init__`` is used, as before. A header - the caller supplied is left alone. - """ + """Send the environment's current token, unless the caller set a header.""" from_environment = self._config.secret_from_environment if not from_environment or _has_authorization_header(headers): return diff --git a/packages/uipath-platform/tests/services/test_env_token_reread.py b/packages/uipath-platform/tests/services/test_env_token_reread.py index 95a5a7063..1692ebbaf 100644 --- a/packages/uipath-platform/tests/services/test_env_token_reread.py +++ b/packages/uipath-platform/tests/services/test_env_token_reread.py @@ -1,9 +1,4 @@ -"""A UiPath() configured from the environment re-reads the token on every request. - -The job runtime renews the token in the process environment (``uipath._cli._job_token``); -re-reading it is what lets long-lived clients pick the new token up. A secret given -explicitly is sent as is. -""" +"""A UiPath() configured from the environment re-reads the token on every request.""" from unittest.mock import patch diff --git a/packages/uipath/docs/core/environment_variables.md b/packages/uipath/docs/core/environment_variables.md index d333f876c..c14371f02 100644 --- a/packages/uipath/docs/core/environment_variables.md +++ b/packages/uipath/docs/core/environment_variables.md @@ -115,6 +115,6 @@ The default value is `INFORMATION` The runtime environment automatically includes certain variables (such as `UIPATH_FOLDER_KEY`, `UIPATH_ROBOT_KEY`), eliminating the need for manual configuration. ### Access Token Renewal -A job receives its user access token once, through `UNATTENDED_USER_ACCESS_TOKEN` (or `UIPATH_ACCESS_TOKEN`). A job that runs longer than that token lives would otherwise start failing with `401` responses. When the job executor asks for it (it starts the job with `uipath run --handler-ipc-pipe ... --handler-ipc-token-refresh`, or sets `refreshAccessTokenOverIpc` on a pooled run request), the runtime asks the executor for a new token over the job's IPC channel at about 80% of the current token's lifetime and writes it back to the token environment variables that are set. +In a long-running job, the access token in `UNATTENDED_USER_ACCESS_TOKEN` / `UIPATH_ACCESS_TOKEN` can be renewed while the job runs. `UiPath()` clients configured from the environment read the variable on every request, so they always send the current token. A client created with an explicit `secret=` keeps that secret. -`UiPath()` clients configured from the environment read the token variable on every request, so they pick the renewed token up without any change; code that reads the variables itself sees it too, and the trace exporter re-reads `UIPATH_ACCESS_TOKEN` before each upload, as do the chat, voice and debug bridges whenever they connect. A client created with an explicit `secret=` keeps that secret. When the executor does not ask for renewal, nothing changes. If a renewal fails, the runtime logs a warning once, keeps the current token and tries again later. +If your code reads these variables itself, read them when you need the token instead of keeping a copy. diff --git a/packages/uipath/src/uipath/_cli/_job_api.py b/packages/uipath/src/uipath/_cli/_job_api.py index 9c7e4f61e..ec1000b49 100644 --- a/packages/uipath/src/uipath/_cli/_job_api.py +++ b/packages/uipath/src/uipath/_cli/_job_api.py @@ -353,8 +353,6 @@ def __init__( def _shutdown(self) -> None: """Close the client and stop its loop/thread.""" if self._token_refresher is not None: - # First: its refresh task lives on this loop, and nothing may be published to - # the environment once teardown has begun. from ._job_token import uninstall_job_token_refresher uninstall_job_token_refresher(self._token_refresher) @@ -388,8 +386,7 @@ def connect_handler_ipc( ) -> _HandlerIpcConnection: """Dial ``pipe`` on a dedicated loop/thread and install the sinks (see ``_HandlerIpcConnection``). - With ``refresh_token`` the host has said it serves ``GetUserAccessToken``: the job's user - access token is then renewed over the same connection (see ``_job_token``). + With ``refresh_token``, the job's user access token is renewed over the same connection. """ if not is_wire_job_key(job_key): raise RuntimeError( diff --git a/packages/uipath/src/uipath/_cli/_job_token.py b/packages/uipath/src/uipath/_cli/_job_token.py index ce4672eb7..c9858e497 100644 --- a/packages/uipath/src/uipath/_cli/_job_token.py +++ b/packages/uipath/src/uipath/_cli/_job_token.py @@ -1,15 +1,4 @@ -"""Renew the job's user access token over the job-api IPC channel. - -The executor hands the token to the process once, as an environment variable, and a job that -outlives it starts failing with 401. ``JobTokenRefresher`` asks the host for a new token -(``IPythonJobApi.GetUserAccessToken``) shortly before the current one expires and writes it back -to the token environment variables. ``UiPath()`` clients built from the environment re-read those -on every request (see ``uipath.platform``), so they pick the new token up without a change of -their own, and so does user code that reads the variables itself. - -The host opts in (``uipath run --handler-ipc-token-refresh``, or ``refreshAccessTokenOverIpc`` on -a pooled run request); a host without the method is never asked. -""" +"""Renew a job's user access token before it expires.""" from __future__ import annotations @@ -33,15 +22,10 @@ logger = logging.getLogger(__name__) -# A healthy host answers in well under a second (serverless hands back the job's own token, the -# Robot asks Orchestrator). _TOKEN_REQUEST_TIMEOUT_S = 30.0 -# Renew when this share of the lifetime is left (so at 80 percent of it), and never later than -# this many seconds before expiry. +# Renew when this share of the lifetime is left, at least this many seconds before expiry. _REFRESH_MARGIN_FRACTION = 0.2 _MIN_REFRESH_MARGIN_S = 120.0 -# After a failed renewal, or one that returned a token already due, leave the host alone for -# this long before asking again. _RETRY_AFTER_FAILURE_S = 30.0 _TOKEN_ENV_VARS = (ENV_UNATTENDED_USER_ACCESS_TOKEN, ENV_UIPATH_ACCESS_TOKEN) @@ -63,7 +47,7 @@ def _claim_seconds(claims: dict[str, Any], name: str) -> float | None: @dataclass(frozen=True) class _TokenState: - """A token with the moments that drive its renewal (epoch seconds).""" + """A token and its renewal times (epoch seconds).""" token: str issued_at: float @@ -71,7 +55,7 @@ class _TokenState: @classmethod def from_token(cls, token: str, now: float) -> _TokenState: - """Read ``iat``/``exp`` from the JWT; a token without ``exp`` is never renewed early.""" + """Read ``iat`` and ``exp`` from the JWT.""" claims = _claims(token) issued_at = _claim_seconds(claims, "iat") return cls( @@ -82,7 +66,7 @@ def from_token(cls, token: str, now: float) -> _TokenState: @property def refresh_at(self) -> float | None: - """When to renew: 80 percent into the lifetime, at least two minutes before expiry.""" + """When to renew, or None for a token without ``exp``.""" if self.expires_at is None: return None lifetime = max(self.expires_at - self.issued_at, 0.0) @@ -95,22 +79,16 @@ def is_fresh(self, now: float) -> bool: def publish_token_to_environ(token: str) -> None: - """Republish a token under whichever token variables the process environment has set.""" + """Write the token to the token environment variables that are set.""" for name in _TOKEN_ENV_VARS: if name in os.environ: os.environ[name] = token class JobTokenRefresher: - """Renews the job's user access token on the IPC loop and publishes it to the environment. - - One task, started by :meth:`start`, sleeps until 80 percent of the current token's lifetime - has passed (at least two minutes before expiry), asks the host, stores and publishes the - answer, and reschedules from the new token. The host opted in, so every failure is handled - the same way, whatever its type: the current token is kept and the host is asked again after - 30 seconds. Only the first failure is logged, as a warning that says no more will follow. - :meth:`stop` cancels the task, a request in flight included, and a stopped refresher never - writes to the environment again. + """Renews the job's user access token and publishes it to the environment. + + A failure keeps the current token and is retried; only the first one is logged. """ def __init__( @@ -142,12 +120,12 @@ def __init__( self._reported_failure = False def start(self) -> None: - """Schedule the renewal task on the IPC loop (once).""" + """Start renewing (once).""" if self._task is None and not self._stopped: self._task = asyncio.run_coroutine_threadsafe(self._run(), self._loop) def stop(self) -> None: - """Cancel the renewal task; nothing is published to the environment after this.""" + """Stop renewing; nothing is published after this.""" with self._lock: self._stopped = True task = self._task @@ -156,18 +134,18 @@ def stop(self) -> None: @property def task(self) -> Future[None] | None: - """The renewal task's handle, for callers that need to await its end.""" + """The renewal task, once started.""" return self._task @property def current_token(self) -> str | None: - """The token last seeded or renewed, or None.""" + """The current token, or None.""" state = self._state return None if state is None else state.token @property def expires_at(self) -> float | None: - """The current token's ``exp`` (epoch seconds), or None when unknown.""" + """The current token's expiry (epoch seconds), or None.""" state = self._state return None if state is None else state.expires_at @@ -175,7 +153,7 @@ async def _run(self) -> None: while not self._stopped: state = self._state if state is None or state.refresh_at is None: - return # no token, or one without exp: nothing to time against + return # nothing to time against now = self._clock() due_at = max(state.refresh_at, self._retry_not_before) if now < due_at: @@ -205,10 +183,9 @@ def _store(self, token: str) -> None: state = _TokenState.from_token(token, now) with self._lock: if self._stopped: - return # a late answer after teardown must not touch the environment + return self._state = state - # A token that is already due (serverless handing back the same soon-to-expire - # token) would otherwise make the loop spin: wait the backoff before asking again. + # An answer that is already due must not make the loop spin. self._retry_not_before = ( 0.0 if state.is_fresh(now) else now + _RETRY_AFTER_FAILURE_S ) @@ -223,13 +200,13 @@ def _store(self, token: str) -> None: def _note_failure(self, error: BaseException) -> None: if self._stopped: - return # teardown cancels in-flight work; nothing worth reporting + return with self._lock: self._retry_not_before = self._clock() + _RETRY_AFTER_FAILURE_S first = not self._reported_failure self._reported_failure = True if not first: - return # the one warning already said that later failures are not logged + return logger.warning( "Could not renew the job's user access token over IPC (%r). Keeping the current " "token and retrying every %g seconds. This is logged only once: later failures " @@ -245,10 +222,7 @@ def install_job_token_refresher( proxy: Any, loop: asyncio.AbstractEventLoop, ) -> JobTokenRefresher: - """Start renewing the job's token, seeded with the one in the environment. - - Pair with :func:`uninstall_job_token_refresher` when the job ends. - """ + """Start renewing the job's token, starting from the one in the environment.""" initial = os.environ.get(ENV_UNATTENDED_USER_ACCESS_TOKEN) or os.environ.get( ENV_UIPATH_ACCESS_TOKEN ) @@ -260,5 +234,5 @@ def install_job_token_refresher( def uninstall_job_token_refresher(refresher: JobTokenRefresher) -> None: - """Stop the renewal task.""" + """Stop renewing the job's token.""" refresher.stop() diff --git a/packages/uipath/src/uipath/_cli/_utils/_auth_header.py b/packages/uipath/src/uipath/_cli/_utils/_auth_header.py index 6016f429b..842d4cb67 100644 --- a/packages/uipath/src/uipath/_cli/_utils/_auth_header.py +++ b/packages/uipath/src/uipath/_cli/_utils/_auth_header.py @@ -1,4 +1,4 @@ -"""The bearer header for connections the CLI opens on behalf of a job.""" +"""Bearer header for connections the CLI opens.""" import os from collections.abc import Mapping @@ -7,13 +7,7 @@ def with_current_access_token(headers: Mapping[str, str]) -> dict[str, str]: - """A copy of ``headers`` whose ``Authorization`` carries the token now in the environment. - - A long job's runtime renews the token in the environment, so a connection opened late - in the job must send the renewed token, not the one read when the headers were built. - Only an ``Authorization`` header that is already present is replaced, and only when the - variable is set. - """ + """Copy ``headers``, with an existing ``Authorization`` set to the environment's token.""" result = dict(headers) token = os.environ.get(ENV_UIPATH_ACCESS_TOKEN) if token and "Authorization" in result: diff --git a/packages/uipath/src/uipath/_cli/cli_run.py b/packages/uipath/src/uipath/_cli/cli_run.py index 3e3ee31e1..ba76caf3e 100644 --- a/packages/uipath/src/uipath/_cli/cli_run.py +++ b/packages/uipath/src/uipath/_cli/cli_run.py @@ -127,7 +127,7 @@ def get_usage_help(self) -> list[str]: @click.option( "--handler-ipc-token-refresh", is_flag=True, - hidden=True, # set by the job executor once its job api serves GetUserAccessToken + hidden=True, # set by the job executor, never by a person help="Renew this job's user access token over the handler IPC pipe before it expires. Needs --handler-ipc-pipe.", ) @track_command("run") diff --git a/packages/uipath/src/uipath/_cli/cli_server_ipc.py b/packages/uipath/src/uipath/_cli/cli_server_ipc.py index 77ce24aee..d497a514e 100644 --- a/packages/uipath/src/uipath/_cli/cli_server_ipc.py +++ b/packages/uipath/src/uipath/_cli/cli_server_ipc.py @@ -36,8 +36,6 @@ class PythonServerRunRequest: workingDirectory: str | None = None environmentVariables: dict[str, str] = field(default_factory=dict) streamOutputOverIpc: bool = False - # The host sets this once its job api serves GetUserAccessToken; a host without the - # method is never asked. refreshAccessTokenOverIpc: bool = False @@ -152,9 +150,7 @@ def _install() -> None: install_runtime_sinks(job_key, resume_version, callback, loop) ) if refresh_token: - # Runs after the job's env vars are applied, so the seed token is this - # job's. Jobs are serialized by the server lock, so one refresher at a - # time writes os.environ. + # The job's environment is applied by now; jobs run one at a time. from ._job_token import install_job_token_refresher token_refreshers.append( diff --git a/packages/uipath/src/uipath/tracing/_otel_exporters.py b/packages/uipath/src/uipath/tracing/_otel_exporters.py index b45269482..d46dfc4d9 100644 --- a/packages/uipath/src/uipath/tracing/_otel_exporters.py +++ b/packages/uipath/src/uipath/tracing/_otel_exporters.py @@ -396,7 +396,7 @@ def _build_url(self, span_list: list[Dict[str, Any]]) -> str: return f"{self.base_url}/api/Traces/v3/spans?traceId={trace_id}&source={source}" def _refresh_auth_token(self) -> None: - """Follow the token environment variable: a long job's runtime renews it there.""" + """Pick up a token that changed in the environment.""" token = os.environ.get(ENV_UIPATH_ACCESS_TOKEN) if not token or token == self.auth_token: return diff --git a/packages/uipath/tests/cli/test_job_token.py b/packages/uipath/tests/cli/test_job_token.py index 8c4faa492..15f2fa416 100644 --- a/packages/uipath/tests/cli/test_job_token.py +++ b/packages/uipath/tests/cli/test_job_token.py @@ -1,8 +1,4 @@ -"""The job's user access token refresher: renewal over the job-api IPC proxy, published to the environment. - -Fakes stand in for the IPC proxy (``GetUserAccessToken``) except where the wire itself is the -point; tokens are unsigned JWTs built here so ``iat``/``exp`` can be placed precisely. -""" +"""Tests for the job's user access token refresher.""" import asyncio import base64 @@ -45,7 +41,7 @@ def token_with_lifetime(lifetime: float, *, remaining: float, tag: str = "t") -> class FakeJobApi: - """Stands in for the ``IPythonJobApi`` proxy; hands out ``tokens`` in order, then "fresh".""" + """Fake job API proxy; hands out ``tokens`` in order, then "fresh".""" def __init__(self, *tokens: Any, error: BaseException | None = None) -> None: self.tokens = list(tokens) @@ -66,7 +62,7 @@ async def GetUserAccessToken(self, request: Any) -> Any: class RemoteError(Exception): - """Shaped like uipath_ipc's RemoteException: carries the .NET type name.""" + """Shaped like uipath_ipc's RemoteException.""" def __init__(self, type_name: str) -> None: super().__init__(type_name) @@ -307,8 +303,7 @@ def test_a_failed_renewal_warns_once_and_retries_after_the_backoff( def test_a_host_without_the_method_is_retried_like_any_other_failure( self, ipc_loop, monkeypatch, records ): - # The host opted in, so no error type is read as "unsupported": a missing method is a - # host fault like any other, and the refresher keeps asking. + # No error type stops the retries. monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) api = FakeJobApi(error=RemoteError("UiPath.Ipc.MethodNotFoundException")) token = token_with_lifetime(600, remaining=60) # due now @@ -347,7 +342,7 @@ def test_an_empty_answer_is_a_failure(self, ipc_loop, monkeypatch, records, bad) def test_a_token_already_due_is_kept_but_not_refetched_at_once( self, ipc_loop, monkeypatch, records ): - """Serverless hands back the job's own token; when the handler lags, it is still due.""" + """An answer that is already due waits the backoff instead of spinning.""" monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) api = FakeJobApi( *[token_with_lifetime(600, remaining=60, tag=str(i)) for i in range(50)] diff --git a/packages/uipath/tests/cli/test_run.py b/packages/uipath/tests/cli/test_run.py index b7166c6de..b6d95e15f 100644 --- a/packages/uipath/tests/cli/test_run.py +++ b/packages/uipath/tests/cli/test_run.py @@ -262,7 +262,7 @@ def test_the_token_refresh_flag_is_hidden_but_still_accepted( def test_the_token_refresh_flag_reaches_the_connection( self, runner: CliRunner, temp_dir: str, monkeypatch ): - """The executor's opt-in has to arrive where the provider is installed.""" + """The flag reaches the IPC connection.""" job_key = "3f2504e0-4f89-11d3-9a0c-0305e82c3301" monkeypatch.setenv("UIPATH_JOB_KEY", job_key) seen: list[tuple[str, str, int | None, bool]] = [] diff --git a/packages/uipath/tests/cli/test_server_ipc.py b/packages/uipath/tests/cli/test_server_ipc.py index 3fab762b0..2f8f8414d 100644 --- a/packages/uipath/tests/cli/test_server_ipc.py +++ b/packages/uipath/tests/cli/test_server_ipc.py @@ -641,7 +641,7 @@ async def scenario() -> None: class TestPooledTokenRefresh: - """refreshAccessTokenOverIpc is its own opt-in, set by a host that serves GetUserAccessToken.""" + """refreshAccessTokenOverIpc is its own opt-in.""" @staticmethod def _fake_run(events: list[Any]) -> Any: From a8c1219d881c28eab24f85a8c39a91e510b266a3 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Fri, 20 Nov 2026 13:33:38 +0100 Subject: [PATCH 10/10] fix: address the review findings on token renewal - A client follows the environment's token only while UIPATH_JOB_KEY there still matches the job it was created in; after that it keeps its last token. - The chat and voice bridges give the socket library a callable for the headers, so a reconnect sends the current token. - One warning per failure episode: the flag resets when a renewal succeeds, and the recovery is logged. - An opt-in that cannot be honored warns and the job runs without renewal. - The loop warns when it stops for lack of a token with an expiry, and waits in slices of at most a minute. - Only the variables holding the job's token are rewritten. - Remove properties and a wrapper that only tests used, and annotate the tests this change adds. Co-Authored-By: Claude Fable 5.2 --- .../src/uipath/platform/_uipath.py | 3 + .../uipath/platform/common/_base_service.py | 6 +- .../src/uipath/platform/common/_config.py | 1 + .../tests/services/test_env_token_reread.py | 144 +++++++--- .../uipath/src/uipath/_cli/_chat/_bridge.py | 3 +- .../src/uipath/_cli/_chat/_voice_bridge.py | 3 +- packages/uipath/src/uipath/_cli/_job_api.py | 11 +- packages/uipath/src/uipath/_cli/_job_token.py | 80 +++--- packages/uipath/src/uipath/_cli/cli_run.py | 7 +- .../uipath/src/uipath/_cli/cli_server_ipc.py | 88 +++--- packages/uipath/tests/cli/chat/test_bridge.py | 8 +- .../tests/cli/chat/test_voice_bridge.py | 8 +- packages/uipath/tests/cli/test_job_token.py | 254 +++++++++++++----- packages/uipath/tests/cli/test_run.py | 34 ++- packages/uipath/tests/cli/test_server_ipc.py | 83 ++++-- .../uipath/tests/cli/unit/test_auth_header.py | 4 +- .../tests/tracing/test_otel_exporters.py | 2 +- 17 files changed, 508 insertions(+), 231 deletions(-) diff --git a/packages/uipath-platform/src/uipath/platform/_uipath.py b/packages/uipath-platform/src/uipath/platform/_uipath.py index c0de56261..3a282fe07 100644 --- a/packages/uipath-platform/src/uipath/platform/_uipath.py +++ b/packages/uipath-platform/src/uipath/platform/_uipath.py @@ -1,3 +1,4 @@ +import os from functools import cached_property from typing import Optional @@ -18,6 +19,7 @@ ) from .common.auth import resolve_config_from_env from .connections import ConnectionsService +from .constants import ENV_JOB_KEY from .context_grounding import ContextGroundingService from .documents import DocumentsService from .entities import EntitiesService @@ -86,6 +88,7 @@ def __init__( base_url=base_url, secret=secret, secret_from_environment=secret_from_environment, + environment_job_key=os.environ.get(ENV_JOB_KEY), ) except ValidationError as e: for error in e.errors(): diff --git a/packages/uipath-platform/src/uipath/platform/common/_base_service.py b/packages/uipath-platform/src/uipath/platform/common/_base_service.py index db64303b8..1a86dcd7e 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_base_service.py +++ b/packages/uipath-platform/src/uipath/platform/common/_base_service.py @@ -24,6 +24,7 @@ ) from uipath.platform.constants import ( + ENV_JOB_KEY, ENV_UIPATH_ACCESS_TOKEN, ENV_UNATTENDED_USER_ACCESS_TOKEN, HEADER_TRACEPARENT_ID, @@ -299,7 +300,10 @@ def auth_headers(self) -> dict[str, str]: def _current_secret(self) -> str: """The bearer token to send now; one from the environment is re-read.""" - if self._config.secret_from_environment: + config = self._config + # Follow the environment only while it belongs to the job this config was built in. + same_job = os.environ.get(ENV_JOB_KEY) == config.environment_job_key + if config.secret_from_environment and same_job: renewed = os.environ.get( ENV_UNATTENDED_USER_ACCESS_TOKEN ) or os.environ.get(ENV_UIPATH_ACCESS_TOKEN) diff --git a/packages/uipath-platform/src/uipath/platform/common/_config.py b/packages/uipath-platform/src/uipath/platform/common/_config.py index 70cccadb9..afad358db 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_config.py +++ b/packages/uipath-platform/src/uipath/platform/common/_config.py @@ -10,6 +10,7 @@ class UiPathApiConfig(BaseModel): base_url: str secret: str secret_from_environment: bool = False + environment_job_key: str | None = None class ConfigurationManager: diff --git a/packages/uipath-platform/tests/services/test_env_token_reread.py b/packages/uipath-platform/tests/services/test_env_token_reread.py index 1692ebbaf..cbefb3920 100644 --- a/packages/uipath-platform/tests/services/test_env_token_reread.py +++ b/packages/uipath-platform/tests/services/test_env_token_reread.py @@ -5,13 +5,14 @@ import pytest from pytest_httpx import HTTPXMock -from uipath.platform import UiPath, UiPathApiConfig +from uipath.platform import UiPath, UiPathApiConfig, UiPathExecutionContext from uipath.platform.common import ApiClient from uipath.platform.common._base_service import BaseService from uipath.platform.common.auth import TokenData UNATTENDED = "UNATTENDED_USER_ACCESS_TOKEN" ACCESS = "UIPATH_ACCESS_TOKEN" +JOB_KEY = "UIPATH_JOB_KEY" URL = "https://cloud.uipath.com/org/tenant" @@ -21,10 +22,13 @@ def url(base_url: str, org: str, tenant: str) -> str: @pytest.fixture -def env_config(config: UiPathApiConfig, monkeypatch) -> UiPathApiConfig: +def env_config( + config: UiPathApiConfig, monkeypatch: pytest.MonkeyPatch +) -> UiPathApiConfig: """The fixture config, flagged as UiPath() flags one built from the environment.""" monkeypatch.setenv(ACCESS, config.secret) monkeypatch.delenv(UNATTENDED, raising=False) + monkeypatch.delenv(JOB_KEY, raising=False) config.secret_from_environment = True return config @@ -37,12 +41,12 @@ class TestEnvironmentSecret: def test_each_request_reads_the_current_environment_token( self, httpx_mock: HTTPXMock, - env_config, - execution_context, - url, - monkeypatch, - secret, - ): + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + secret: str, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) httpx_mock.add_response(url=url, status_code=200, json={}) service = BaseService(config=env_config, execution_context=execution_context) @@ -58,12 +62,12 @@ def test_each_request_reads_the_current_environment_token( async def test_each_async_request_reads_the_current_environment_token( self, httpx_mock: HTTPXMock, - env_config, - execution_context, - url, - monkeypatch, - secret, - ): + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + secret: str, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) httpx_mock.add_response(url=url, status_code=200, json={}) service = BaseService(config=env_config, execution_context=execution_context) @@ -76,8 +80,13 @@ async def test_each_async_request_reads_the_current_environment_token( assert env_config.secret == "renewed" def test_the_unattended_token_wins_over_the_access_token( - self, httpx_mock: HTTPXMock, env_config, execution_context, url, monkeypatch - ): + self, + httpx_mock: HTTPXMock, + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) monkeypatch.setenv(UNATTENDED, "unattended") monkeypatch.setenv(ACCESS, "access") @@ -90,12 +99,12 @@ def test_the_unattended_token_wins_over_the_access_token( def test_a_missing_variable_falls_back_to_the_stored_secret( self, httpx_mock: HTTPXMock, - env_config, - execution_context, - url, - monkeypatch, - secret, - ): + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + secret: str, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) service = BaseService(config=env_config, execution_context=execution_context) monkeypatch.delenv(ACCESS) @@ -105,8 +114,13 @@ def test_a_missing_variable_falls_back_to_the_stored_secret( assert _sent_auth(httpx_mock) == [f"Bearer {secret}"] def test_a_caller_supplied_authorization_header_is_left_alone( - self, httpx_mock: HTTPXMock, env_config, execution_context, url, monkeypatch - ): + self, + httpx_mock: HTTPXMock, + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) monkeypatch.setenv(ACCESS, "renewed") service = BaseService(config=env_config, execution_context=execution_context) @@ -116,8 +130,12 @@ def test_a_caller_supplied_authorization_header_is_left_alone( assert _sent_auth(httpx_mock) == ["Bearer mine"] def test_the_callers_headers_mapping_is_not_modified( - self, httpx_mock: HTTPXMock, env_config, execution_context, url - ): + self, + httpx_mock: HTTPXMock, + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) service = BaseService(config=env_config, execution_context=execution_context) headers = {"X-Test": "1"} @@ -128,8 +146,13 @@ def test_the_callers_headers_mapping_is_not_modified( assert httpx_mock.get_requests()[0].headers["X-Test"] == "1" def test_api_client_folder_headers_do_not_pin_the_old_token( - self, httpx_mock: HTTPXMock, env_config, execution_context, url, monkeypatch - ): + self, + httpx_mock: HTTPXMock, + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + ) -> None: """include_folder_headers used to copy the client defaults, Authorization included.""" monkeypatch.setenv("UIPATH_FOLDER_KEY", "folder-1") httpx_mock.add_response(url=url, status_code=200, json={}) @@ -144,10 +167,59 @@ def test_api_client_folder_headers_do_not_pin_the_old_token( assert sent.headers["Accept"] == "application/json" +class TestAnotherJobsEnvironment: + def test_a_client_keeps_its_token_once_the_environment_belongs_to_another_job( + self, + httpx_mock: HTTPXMock, + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + secret: str, + ) -> None: + httpx_mock.add_response(url=url, status_code=200, json={}) + httpx_mock.add_response(url=url, status_code=200, json={}) + monkeypatch.setenv(JOB_KEY, "job-a") + env_config.environment_job_key = "job-a" + service = BaseService(config=env_config, execution_context=execution_context) + + service.request("GET", "/endpoint") + monkeypatch.setenv(JOB_KEY, "job-b") + monkeypatch.setenv(ACCESS, "job-b-token") + service.request("GET", "/endpoint") + + assert _sent_auth(httpx_mock) == [f"Bearer {secret}", f"Bearer {secret}"] + assert env_config.secret == secret + + def test_a_renewal_inside_the_same_job_is_still_followed( + self, + httpx_mock: HTTPXMock, + env_config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + ) -> None: + httpx_mock.add_response(url=url, status_code=200, json={}) + monkeypatch.setenv(JOB_KEY, "job-a") + env_config.environment_job_key = "job-a" + service = BaseService(config=env_config, execution_context=execution_context) + + monkeypatch.setenv(ACCESS, "renewed") + service.request("GET", "/endpoint") + + assert _sent_auth(httpx_mock) == ["Bearer renewed"] + + class TestStaticSecret: def test_an_explicit_secret_ignores_the_environment( - self, httpx_mock: HTTPXMock, config, execution_context, url, monkeypatch, secret - ): + self, + httpx_mock: HTTPXMock, + config: UiPathApiConfig, + execution_context: UiPathExecutionContext, + url: str, + monkeypatch: pytest.MonkeyPatch, + secret: str, + ) -> None: httpx_mock.add_response(url=url, status_code=200, json={}) monkeypatch.setenv(ACCESS, "renewed") service = BaseService(config=config, execution_context=execution_context) @@ -158,7 +230,7 @@ def test_an_explicit_secret_ignores_the_environment( assert _sent_auth(httpx_mock) == [f"Bearer {secret}"] assert config.secret == secret - def test_the_flag_defaults_to_false(self): + def test_the_flag_defaults_to_false(self) -> None: assert ( UiPathApiConfig(base_url="https://x", secret="s").secret_from_environment is False @@ -172,12 +244,16 @@ def test_the_flag_defaults_to_false(self): class TestUiPathFactory: - def test_a_client_built_from_the_environment_follows_it(self, monkeypatch): + def test_a_client_built_from_the_environment_follows_it( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: monkeypatch.setenv("UIPATH_URL", URL) monkeypatch.setenv(ACCESS, "env-token") monkeypatch.delenv(UNATTENDED, raising=False) + monkeypatch.setenv(JOB_KEY, "job-a") sdk = UiPath() assert sdk._config.secret_from_environment is True + assert sdk._config.environment_job_key == "job-a" assert sdk._config.secret == "env-token" monkeypatch.setenv(ACCESS, "renewed") @@ -185,7 +261,7 @@ def test_a_client_built_from_the_environment_follows_it(self, monkeypatch): assert sdk.api_client.default_headers["Authorization"] == "Bearer renewed" assert sdk._config.secret == "renewed" - def test_an_explicit_secret_does_not(self, monkeypatch): + def test_an_explicit_secret_does_not(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("UIPATH_URL", URL) monkeypatch.setenv(ACCESS, "env-token") @@ -195,7 +271,7 @@ def test_an_explicit_secret_does_not(self, monkeypatch): assert sdk._config.secret_from_environment is False assert sdk.api_client.default_headers["Authorization"] == "Bearer mine" - def test_client_credentials_do_not(self, monkeypatch): + def test_client_credentials_do_not(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv(ACCESS, "env-token") with patch( "uipath.platform._uipath.ExternalApplicationService.get_token_data", diff --git a/packages/uipath/src/uipath/_cli/_chat/_bridge.py b/packages/uipath/src/uipath/_cli/_chat/_bridge.py index 005e01619..56fe5e336 100644 --- a/packages/uipath/src/uipath/_cli/_chat/_bridge.py +++ b/packages/uipath/src/uipath/_cli/_chat/_bridge.py @@ -224,7 +224,8 @@ async def connect(self, timeout: float = 10.0) -> None: self._client.connect( url=self.websocket_url, socketio_path=self.websocket_path, - headers=with_current_access_token(self.headers), + # A callable: the library asks again on every reconnect. + headers=lambda: with_current_access_token(self.headers), auth=self.auth, transports=["websocket"], ), diff --git a/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py b/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py index fe61c63dc..f0e642265 100644 --- a/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py +++ b/packages/uipath/src/uipath/_cli/_chat/_voice_bridge.py @@ -111,7 +111,8 @@ async def run(self) -> VoiceSessionEndReason: self._client.connect( url=self._url, socketio_path=self._socketio_path, - headers=with_current_access_token(self._headers), + # A callable: the library asks again on every reconnect. + headers=lambda: with_current_access_token(self._headers), transports=["websocket"], ), timeout=_ATTEMPT_CAS_SOCKET_CONNECTION_TIMEOUT_SECONDS, diff --git a/packages/uipath/src/uipath/_cli/_job_api.py b/packages/uipath/src/uipath/_cli/_job_api.py index ec1000b49..334443900 100644 --- a/packages/uipath/src/uipath/_cli/_job_api.py +++ b/packages/uipath/src/uipath/_cli/_job_api.py @@ -14,7 +14,10 @@ from concurrent.futures import wait as _futures_wait from dataclasses import dataclass from enum import IntEnum -from typing import Any, TypeGuard +from typing import TYPE_CHECKING, Any, TypeGuard + +if TYPE_CHECKING: + from ._job_token import JobTokenRefresher logger = logging.getLogger(__name__) @@ -342,7 +345,7 @@ def __init__( loop: asyncio.AbstractEventLoop, thread: threading.Thread, log_handler: "_IpcLogHandler | None" = None, - token_refresher: Any = None, + token_refresher: JobTokenRefresher | None = None, ) -> None: self._client = client self._loop = loop @@ -353,9 +356,7 @@ def __init__( def _shutdown(self) -> None: """Close the client and stop its loop/thread.""" if self._token_refresher is not None: - from ._job_token import uninstall_job_token_refresher - - uninstall_job_token_refresher(self._token_refresher) + self._token_refresher.stop() if self._log_handler is not None: self._log_handler.flush_pending() try: diff --git a/packages/uipath/src/uipath/_cli/_job_token.py b/packages/uipath/src/uipath/_cli/_job_token.py index c9858e497..ee6b1fa86 100644 --- a/packages/uipath/src/uipath/_cli/_job_token.py +++ b/packages/uipath/src/uipath/_cli/_job_token.py @@ -3,11 +3,12 @@ from __future__ import annotations import asyncio +import functools import logging import os import threading import time -from collections.abc import Callable +from collections.abc import Callable, Iterable from concurrent.futures import Future from dataclasses import dataclass from typing import Any @@ -27,6 +28,8 @@ _REFRESH_MARGIN_FRACTION = 0.2 _MIN_REFRESH_MARGIN_S = 120.0 _RETRY_AFTER_FAILURE_S = 30.0 +# Longest single wait, so a suspended machine or a clock change cannot delay a renewal for long. +_MAX_SLEEP_S = 60.0 _TOKEN_ENV_VARS = (ENV_UNATTENDED_USER_ACCESS_TOKEN, ENV_UIPATH_ACCESS_TOKEN) @@ -78,9 +81,11 @@ def is_fresh(self, now: float) -> bool: return refresh_at is None or now < refresh_at -def publish_token_to_environ(token: str) -> None: - """Write the token to the token environment variables that are set.""" - for name in _TOKEN_ENV_VARS: +def publish_token_to_environ( + token: str, names: Iterable[str] = _TOKEN_ENV_VARS +) -> None: + """Write the token to those of ``names`` that are set in the environment.""" + for name in names: if name in os.environ: os.environ[name] = token @@ -88,7 +93,8 @@ def publish_token_to_environ(token: str) -> None: class JobTokenRefresher: """Renews the job's user access token and publishes it to the environment. - A failure keeps the current token and is retried; only the first one is logged. + A failure keeps the current token and is retried. Only the first failure is logged, + until a renewal succeeds again. """ def __init__( @@ -132,32 +138,19 @@ def stop(self) -> None: if task is not None: task.cancel() - @property - def task(self) -> Future[None] | None: - """The renewal task, once started.""" - return self._task - - @property - def current_token(self) -> str | None: - """The current token, or None.""" - state = self._state - return None if state is None else state.token - - @property - def expires_at(self) -> float | None: - """The current token's expiry (epoch seconds), or None.""" - state = self._state - return None if state is None else state.expires_at - async def _run(self) -> None: while not self._stopped: state = self._state if state is None or state.refresh_at is None: - return # nothing to time against + logger.warning( + "Not renewing the job's user access token: there is no token with an " + "expiry to schedule against." + ) + return now = self._clock() due_at = max(state.refresh_at, self._retry_not_before) if now < due_at: - await asyncio.sleep(due_at - now) + await asyncio.sleep(min(due_at - now, _MAX_SLEEP_S)) continue await self._refresh_once() @@ -189,14 +182,17 @@ def _store(self, token: str) -> None: self._retry_not_before = ( 0.0 if state.is_fresh(now) else now + _RETRY_AFTER_FAILURE_S ) - if self._publish is None: - return - try: - self._publish(token) - except Exception: - logger.debug( - "Could not republish the renewed access token", exc_info=True - ) + recovered = self._reported_failure + self._reported_failure = False + if self._publish is not None: + try: + self._publish(token) + except Exception: + logger.debug( + "Could not republish the renewed access token", exc_info=True + ) + if recovered: + logger.info("Renewed the job's user access token after an earlier failure.") def _note_failure(self, error: BaseException) -> None: if self._stopped: @@ -209,8 +205,8 @@ def _note_failure(self, error: BaseException) -> None: return logger.warning( "Could not renew the job's user access token over IPC (%r). Keeping the current " - "token and retrying every %g seconds. This is logged only once: later failures " - "to renew this job's token are not logged.", + "token and retrying every %g seconds. This is logged only once: further " + "failures are not logged until a renewal succeeds.", error, _RETRY_AFTER_FAILURE_S, ) @@ -226,13 +222,17 @@ def install_job_token_refresher( initial = os.environ.get(ENV_UNATTENDED_USER_ACCESS_TOKEN) or os.environ.get( ENV_UIPATH_ACCESS_TOKEN ) + # Another token under the other variable name is left alone. + names = tuple( + name for name in _TOKEN_ENV_VARS if initial and os.environ.get(name) == initial + ) refresher = JobTokenRefresher( - proxy, loop, job_key, resume_version, initial_token=initial + proxy, + loop, + job_key, + resume_version, + initial_token=initial, + publish=functools.partial(publish_token_to_environ, names=names), ) refresher.start() return refresher - - -def uninstall_job_token_refresher(refresher: JobTokenRefresher) -> None: - """Stop renewing the job's token.""" - refresher.stop() diff --git a/packages/uipath/src/uipath/_cli/cli_run.py b/packages/uipath/src/uipath/_cli/cli_run.py index ba76caf3e..49e8f0b16 100644 --- a/packages/uipath/src/uipath/_cli/cli_run.py +++ b/packages/uipath/src/uipath/_cli/cli_run.py @@ -163,8 +163,11 @@ def run( # NOSONAR: one parameter per click option return if handler_ipc_token_refresh and handler_ipc_pipe is None: - console.error("--handler-ipc-token-refresh needs --handler-ipc-pipe.") - return + # Renewal is optional: the job runs without it. + console.warning( + "--handler-ipc-token-refresh needs --handler-ipc-pipe; running without token renewal." + ) + handler_ipc_token_refresh = False result = Middlewares.next( "run", diff --git a/packages/uipath/src/uipath/_cli/cli_server_ipc.py b/packages/uipath/src/uipath/_cli/cli_server_ipc.py index d497a514e..4f9cfa5af 100644 --- a/packages/uipath/src/uipath/_cli/cli_server_ipc.py +++ b/packages/uipath/src/uipath/_cli/cli_server_ipc.py @@ -116,60 +116,58 @@ async def RunJob( is_wire_job_key, ) - flags = " and ".join( - name - for name, on in ( - ("streamOutputOverIpc", stream_output), - ("refreshAccessTokenOverIpc", refresh_token), - ) - if on - ) + client = message.client if message is not None else None + problem: str | None = None if not is_wire_job_key(request.jobKey): - return PythonServerRunJobResult( - exitCode=1, - error=f"{flags} needs a 'jobKey' that is a job key (Guid); got {request.jobKey!r}", + problem = ( + f"needs a 'jobKey' that is a job key (Guid); got {request.jobKey!r}" ) + elif client is None: + problem = "is only available when RunJob is invoked over IPC" - if message is None or message.client is None: + if problem is not None and stream_output: return PythonServerRunJobResult( - exitCode=1, - error=f"{flags} is only available when RunJob is invoked over IPC", + exitCode=1, error=f"streamOutputOverIpc {problem}" ) - - # get_callback only wraps the connection, so this cannot tell us whether the peer - # actually hosts the contract; a peer that doesn't shows up as a failing send. - callback = message.client.get_callback(IPythonJobApi) # type: ignore[type-abstract] - - loop = asyncio.get_running_loop() - job_key = request.jobKey - resume_version = request.resumeVersion - - def _install() -> None: - if stream_output: - installed.append( - install_runtime_sinks(job_key, resume_version, callback, loop) - ) - if refresh_token: - # The job's environment is applied by now; jobs run one at a time. - from ._job_token import install_job_token_refresher - - token_refreshers.append( - install_job_token_refresher( - job_key, resume_version, callback, loop + if problem is not None: + # Renewal is optional: the job runs without it. + console.warning( + f"refreshAccessTokenOverIpc {problem}; running without token renewal." + ) + elif client is not None: + # get_callback only wraps the connection, so this cannot tell us whether the peer + # actually hosts the contract; a peer that doesn't shows up as a failing send. + callback = client.get_callback(IPythonJobApi) # type: ignore[type-abstract] + + loop = asyncio.get_running_loop() + job_key = request.jobKey + resume_version = request.resumeVersion + + def _install() -> None: + if stream_output: + installed.append( + install_runtime_sinks( + job_key, resume_version, callback, loop + ) + ) + if refresh_token: + # The job's environment is applied by now; jobs run one at a time. + from ._job_token import install_job_token_refresher + + token_refreshers.append( + install_job_token_refresher( + job_key, resume_version, callback, loop + ) ) - ) - - def _uninstall() -> None: - if stream_output: - clear_runtime_sinks() - if token_refreshers: - from ._job_token import uninstall_job_token_refresher + def _uninstall() -> None: + if stream_output: + clear_runtime_sinks() for refresher in token_refreshers: - uninstall_job_token_refresher(refresher) + refresher.stop() - on_run_start = _install - on_run_end = _uninstall + on_run_start = _install + on_run_end = _uninstall result = await _run_command_isolated( cmd, diff --git a/packages/uipath/tests/cli/chat/test_bridge.py b/packages/uipath/tests/cli/chat/test_bridge.py index 3ec6152f9..58a344146 100644 --- a/packages/uipath/tests/cli/chat/test_bridge.py +++ b/packages/uipath/tests/cli/chat/test_bridge.py @@ -1239,6 +1239,10 @@ async def disconnect(self) -> None: with patch("socketio.AsyncClient", FakeClient): await bridge.connect(timeout=1.0) - assert seen["headers"]["Authorization"] == "Bearer renewed-token" - assert seen["headers"]["X-Keep"] == "yes" + headers = seen["headers"]() + assert headers["Authorization"] == "Bearer renewed-token" + assert headers["X-Keep"] == "yes" assert bridge.headers["Authorization"] == "Bearer startup-token" + # Asked again, as on a reconnect, it carries the token of that moment. + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "newer-token") + assert seen["headers"]()["Authorization"] == "Bearer newer-token" diff --git a/packages/uipath/tests/cli/chat/test_voice_bridge.py b/packages/uipath/tests/cli/chat/test_voice_bridge.py index 2bc5c0b51..85e73517c 100644 --- a/packages/uipath/tests/cli/chat/test_voice_bridge.py +++ b/packages/uipath/tests/cli/chat/test_voice_bridge.py @@ -263,5 +263,9 @@ async def disconnect(self) -> None: reason = await session.run() assert reason == VoiceSessionEndReason.COMPLETED - assert seen["headers"]["Authorization"] == "Bearer renewed-token" - assert seen["headers"]["X-Keep"] == "yes" + headers = seen["headers"]() + assert headers["Authorization"] == "Bearer renewed-token" + assert headers["X-Keep"] == "yes" + # Asked again, as on a reconnect, it carries the token of that moment. + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "newer-token") + assert seen["headers"]()["Authorization"] == "Bearer newer-token" diff --git a/packages/uipath/tests/cli/test_job_token.py b/packages/uipath/tests/cli/test_job_token.py index 15f2fa416..7745eb103 100644 --- a/packages/uipath/tests/cli/test_job_token.py +++ b/packages/uipath/tests/cli/test_job_token.py @@ -7,6 +7,8 @@ import os import threading import time +from collections.abc import Callable, Iterator +from concurrent.futures import Future from contextvars import ContextVar from typing import Any @@ -17,7 +19,6 @@ JobTokenRefresher, install_job_token_refresher, publish_token_to_environ, - uninstall_job_token_refresher, ) JOB_ID = "3f2504e0-4f89-11d3-9a0c-0305e82c3301" @@ -41,7 +42,10 @@ def token_with_lifetime(lifetime: float, *, remaining: float, tag: str = "t") -> class FakeJobApi: - """Fake job API proxy; hands out ``tokens`` in order, then "fresh".""" + """Fake job API proxy; hands out ``tokens`` in order, then "fresh". + + An exception among the tokens is raised instead of returned. + """ def __init__(self, *tokens: Any, error: BaseException | None = None) -> None: self.tokens = list(tokens) @@ -58,7 +62,10 @@ async def GetUserAccessToken(self, request: Any) -> Any: await self.release.wait() if self.error is not None: raise self.error - return self.tokens.pop(0) if self.tokens else "fresh" + answer = self.tokens.pop(0) if self.tokens else "fresh" + if isinstance(answer, BaseException): + raise answer + return answer class RemoteError(Exception): @@ -70,7 +77,7 @@ def __init__(self, type_name: str) -> None: @pytest.fixture -def ipc_loop(): +def ipc_loop() -> Iterator[asyncio.AbstractEventLoop]: loop = asyncio.new_event_loop() thread = threading.Thread( target=loop.run_forever, name="test-ipc-loop", daemon=True @@ -83,7 +90,7 @@ def ipc_loop(): @pytest.fixture -def records() -> Any: +def records() -> Iterator[list[logging.LogRecord]]: captured: list[logging.LogRecord] = [] class _Capture(logging.Handler): @@ -130,8 +137,19 @@ def _warnings(records: list[logging.LogRecord]) -> list[str]: return [r.getMessage() for r in records if r.levelno == logging.WARNING] +def _token(refresher: JobTokenRefresher) -> str | None: + state = refresher._state + return None if state is None else state.token + + +def _task(refresher: JobTokenRefresher) -> Future[None]: + task = refresher._task + assert task is not None + return task + + class TestTokenState: - def test_exp_and_iat_drive_the_renewal_point(self): + def test_exp_and_iat_drive_the_renewal_point(self) -> None: now = 1_000_000.0 state = _job_token._TokenState.from_token @@ -152,21 +170,23 @@ def test_exp_and_iat_drive_the_renewal_point(self): # An iat in the future (clock skew) is clamped to now. assert state(jwt(iat=now + 50, exp=now + 650), now).issued_at == now - def test_a_token_without_exp_is_never_due(self): + def test_a_token_without_exp_is_never_due(self) -> None: now = 1_000_000.0 none = _job_token._TokenState.from_token(jwt(sub="x"), now) assert none.expires_at is None assert none.refresh_at is None assert none.is_fresh(now + 10**9) - def test_an_opaque_token_is_kept(self): + def test_an_opaque_token_is_kept(self) -> None: state = _job_token._TokenState.from_token("opaque", 1.0) assert state.token == "opaque" assert state.expires_at is None class TestRenewal: - def test_renews_at_80_percent_and_republishes(self, ipc_loop, monkeypatch): + def test_renews_at_80_percent_and_republishes( + self, ipc_loop: asyncio.AbstractEventLoop, monkeypatch: pytest.MonkeyPatch + ) -> None: monkeypatch.setenv(ACCESS, "old") monkeypatch.delenv(UNATTENDED, raising=False) renewed = token_with_lifetime(3600, remaining=3600, tag="renewed") @@ -192,11 +212,12 @@ def test_renews_at_80_percent_and_republishes(self, ipc_loop, monkeypatch): 7, None, ) - assert refresher.current_token == renewed - assert refresher.expires_at == pytest.approx(time.time() + 3600, abs=5) + assert _token(refresher) == renewed + state = refresher._state + assert state is not None + assert state.expires_at == pytest.approx(time.time() + 3600, abs=5) # Rescheduled against the new token, hours away. - task = refresher.task - assert task is not None + task = _task(refresher) assert not task.done() assert len(api.requests) == 1 @@ -204,7 +225,9 @@ def test_renews_at_80_percent_and_republishes(self, ipc_loop, monkeypatch): assert _wait(lambda: _pending_tasks(ipc_loop) == 0) assert task.cancelled() - def test_a_fresh_token_only_waits_and_stop_cancels_the_wait(self, ipc_loop): + def test_a_fresh_token_only_waits_and_stop_cancels_the_wait( + self, ipc_loop: asyncio.AbstractEventLoop + ) -> None: api = FakeJobApi() refresher = _refresher(api, ipc_loop, token_with_lifetime(3600, remaining=3000)) @@ -217,9 +240,11 @@ def test_a_fresh_token_only_waits_and_stop_cancels_the_wait(self, ipc_loop): assert _wait(lambda: _pending_tasks(ipc_loop) == 0) # Idempotent, and the token is still there to read. refresher.stop() - assert refresher.current_token is not None + assert _token(refresher) is not None - def test_a_late_answer_after_stop_is_dropped(self, ipc_loop, monkeypatch): + def test_a_late_answer_after_stop_is_dropped( + self, ipc_loop: asyncio.AbstractEventLoop, monkeypatch: pytest.MonkeyPatch + ) -> None: monkeypatch.setenv(ACCESS, "old") api = FakeJobApi("late") api.release = asyncio.Event() @@ -238,9 +263,11 @@ def test_a_late_answer_after_stop_is_dropped(self, ipc_loop, monkeypatch): assert _wait(lambda: _pending_tasks(ipc_loop) == 0) assert os.environ[ACCESS] == "old" - assert refresher.current_token != "late" + assert _token(refresher) != "late" - def test_store_after_stop_publishes_nothing(self, ipc_loop, monkeypatch): + def test_store_after_stop_publishes_nothing( + self, ipc_loop: asyncio.AbstractEventLoop, monkeypatch: pytest.MonkeyPatch + ) -> None: """The race the cancellation may miss: the answer arrived, stop() landed before the store.""" monkeypatch.setenv(ACCESS, "old") refresher = JobTokenRefresher( @@ -251,37 +278,64 @@ def test_store_after_stop_publishes_nothing(self, ipc_loop, monkeypatch): refresher._store("late") assert os.environ[ACCESS] == "old" - assert refresher.current_token == "old" + assert _token(refresher) == "old" - def test_missing_exp_means_no_timer(self, ipc_loop): + def test_missing_exp_means_no_timer( + self, ipc_loop: asyncio.AbstractEventLoop, records: list[logging.LogRecord] + ) -> None: api = FakeJobApi() refresher = _refresher(api, ipc_loop, jwt(sub="no-exp")) refresher.start() - task = refresher.task - assert task is not None - task.result(timeout=5) # returns at once: nothing to time against + _task(refresher).result(timeout=5) # returns at once: nothing to time against assert api.requests == [] - assert refresher.expires_at is None + warnings = _warnings(records) + assert len(warnings) == 1 + assert "no token with an expiry" in warnings[0] - def test_no_token_at_all_means_no_timer(self, ipc_loop): + def test_no_token_at_all_means_no_timer( + self, ipc_loop: asyncio.AbstractEventLoop, records: list[logging.LogRecord] + ) -> None: api = FakeJobApi() refresher = _refresher(api, ipc_loop, None) refresher.start() - task = refresher.task - assert task is not None - task.result(timeout=5) + _task(refresher).result(timeout=5) + assert api.requests == [] + assert _token(refresher) is None + assert len(_warnings(records)) == 1 + + def test_a_clock_jump_during_the_wait_is_noticed_within_one_slice( + self, ipc_loop: asyncio.AbstractEventLoop, monkeypatch: pytest.MonkeyPatch + ) -> None: + monkeypatch.setattr(_job_token, "_MAX_SLEEP_S", 0.1) + jump = [0.0] + api = FakeJobApi(token_with_lifetime(3600, remaining=3600)) + refresher = _refresher( + api, + ipc_loop, + token_with_lifetime(3600, remaining=3000), # renewal is ~38 minutes away + clock=lambda: time.time() + jump[0], + ) + + refresher.start() + time.sleep(0.3) assert api.requests == [] - assert refresher.current_token is None + + jump[0] = 2400.0 # the wall clock moves past the renewal point mid-wait + assert api.called.wait(timeout=5) + refresher.stop() class TestFailures: def test_a_failed_renewal_warns_once_and_retries_after_the_backoff( - self, ipc_loop, monkeypatch, records - ): + self, + ipc_loop: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + records: list[logging.LogRecord], + ) -> None: monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) api = FakeJobApi(error=RuntimeError("pipe down")) token = token_with_lifetime(600, remaining=120.2) @@ -293,7 +347,7 @@ def test_a_failed_renewal_warns_once_and_retries_after_the_backoff( assert _wait(lambda: len(api.requests) >= 2) # retried, after the backoff refresher.stop() assert len(api.requests) < 10 # ... not hammered - assert refresher.current_token == token + assert _token(refresher) == token warnings = _warnings(records) assert len(warnings) == 1 assert "pipe down" in warnings[0] @@ -301,8 +355,11 @@ def test_a_failed_renewal_warns_once_and_retries_after_the_backoff( assert "logged only once" in warnings[0] def test_a_host_without_the_method_is_retried_like_any_other_failure( - self, ipc_loop, monkeypatch, records - ): + self, + ipc_loop: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + records: list[logging.LogRecord], + ) -> None: # No error type stops the retries. monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) api = FakeJobApi(error=RemoteError("UiPath.Ipc.MethodNotFoundException")) @@ -312,16 +369,48 @@ def test_a_host_without_the_method_is_retried_like_any_other_failure( refresher.start() assert _wait(lambda: len(api.requests) >= 2) # still asking - task = refresher.task - assert task is not None and not task.done() + assert not _task(refresher).done() refresher.stop() - assert refresher.current_token == token + assert _token(refresher) == token warnings = _warnings(records) assert len(warnings) == 1 assert "MethodNotFoundException" in warnings[0] + def test_a_new_failure_after_a_recovery_warns_again( + self, + ipc_loop: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + records: list[logging.LogRecord], + ) -> None: + monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.2) + api = FakeJobApi( + RuntimeError("down"), + token_with_lifetime(600, remaining=60, tag="still-due"), + RuntimeError("down again"), + token_with_lifetime(3600, remaining=3600, tag="fresh"), + ) + refresher = _refresher(api, ipc_loop, token_with_lifetime(600, remaining=60)) + + refresher.start() + + assert _wait(lambda: len(api.requests) == 4) + refresher.stop() + failures = [w for w in _warnings(records) if "Could not renew" in w] + assert len(failures) == 2 + assert "'down'" in failures[0] and "'down again'" in failures[1] + recoveries = [ + r for r in records if "after an earlier failure" in r.getMessage() + ] + assert len(recoveries) == 2 + @pytest.mark.parametrize("bad", ["", None, 42]) - def test_an_empty_answer_is_a_failure(self, ipc_loop, monkeypatch, records, bad): + def test_an_empty_answer_is_a_failure( + self, + ipc_loop: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + records: list[logging.LogRecord], + bad: Any, + ) -> None: monkeypatch.setenv(ACCESS, "old") api = FakeJobApi(bad) refresher = JobTokenRefresher( @@ -340,8 +429,11 @@ def test_an_empty_answer_is_a_failure(self, ipc_loop, monkeypatch, records, bad) refresher.stop() def test_a_token_already_due_is_kept_but_not_refetched_at_once( - self, ipc_loop, monkeypatch, records - ): + self, + ipc_loop: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + records: list[logging.LogRecord], + ) -> None: """An answer that is already due waits the backoff instead of spinning.""" monkeypatch.setattr(_job_token, "_RETRY_AFTER_FAILURE_S", 0.3) api = FakeJobApi( @@ -355,10 +447,12 @@ def test_a_token_already_due_is_kept_but_not_refetched_at_once( refresher.stop() assert 2 <= len(api.requests) <= 8 # once per backoff period, not a spin - assert refresher.current_token in api.tokens or refresher.current_token != "" + assert _token(refresher) in api.tokens or _token(refresher) != "" assert _warnings(records) == [] - def test_a_timed_out_host_is_a_failure(self, ipc_loop, records): + def test_a_timed_out_host_is_a_failure( + self, ipc_loop: asyncio.AbstractEventLoop, records: list[logging.LogRecord] + ) -> None: api = FakeJobApi("late") api.release = asyncio.Event() # never released refresher = _refresher( @@ -373,7 +467,9 @@ def test_a_timed_out_host_is_a_failure(self, ipc_loop, records): class TestPublish: - def test_publish_writes_only_the_variables_that_are_set(self, monkeypatch): + def test_publish_writes_only_the_variables_that_are_set( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: monkeypatch.setenv(UNATTENDED, "old") monkeypatch.setenv(ACCESS, "old") publish_token_to_environ("new") @@ -389,7 +485,19 @@ def test_publish_writes_only_the_variables_that_are_set(self, monkeypatch): assert UNATTENDED not in os.environ assert ACCESS not in os.environ - def test_a_failing_publish_keeps_the_token(self, ipc_loop, records): + def test_publish_can_be_limited_to_some_variables( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + monkeypatch.setenv(UNATTENDED, "job") + monkeypatch.setenv(ACCESS, "mine") + + publish_token_to_environ("new", names=(UNATTENDED,)) + + assert (os.environ[UNATTENDED], os.environ[ACCESS]) == ("new", "mine") + + def test_a_failing_publish_keeps_the_token( + self, ipc_loop: asyncio.AbstractEventLoop, records: list[logging.LogRecord] + ) -> None: def boom(token: str) -> None: raise RuntimeError("env is read-only") @@ -397,12 +505,14 @@ def boom(token: str) -> None: refresher._store("renewed") - assert refresher.current_token == "renewed" + assert _token(refresher) == "renewed" assert _warnings(records) == [] class TestInstall: - def test_install_seeds_from_the_environment_and_starts(self, ipc_loop, monkeypatch): + def test_install_seeds_from_the_environment_and_starts( + self, ipc_loop: asyncio.AbstractEventLoop, monkeypatch: pytest.MonkeyPatch + ) -> None: unattended = token_with_lifetime(3600, remaining=3000, tag="unattended") monkeypatch.setenv(UNATTENDED, unattended) monkeypatch.setenv(ACCESS, token_with_lifetime(3600, remaining=3000, tag="a")) @@ -411,15 +521,39 @@ def test_install_seeds_from_the_environment_and_starts(self, ipc_loop, monkeypat refresher = install_job_token_refresher(JOB_ID, 3, api, ipc_loop) try: # UNATTENDED_USER_ACCESS_TOKEN wins, as in resolve_config_from_env. - assert refresher.current_token == unattended + assert _token(refresher) == unattended assert _wait(lambda: _pending_tasks(ipc_loop) == 1) assert api.requests == [] finally: - uninstall_job_token_refresher(refresher) + refresher.stop() assert _wait(lambda: _pending_tasks(ipc_loop) == 0) - def test_shutdown_stops_the_refresher(self, monkeypatch): + @pytest.mark.parametrize("same_token", [True, False]) + def test_install_renews_only_the_variables_that_hold_the_seed_token( + self, + ipc_loop: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + same_token: bool, + ) -> None: + seed = token_with_lifetime(600, remaining=60) # due now + renewed = token_with_lifetime(3600, remaining=3600, tag="renewed") + monkeypatch.setenv(UNATTENDED, seed) + monkeypatch.setenv(ACCESS, seed if same_token else "someone-elses") + + refresher = install_job_token_refresher( + JOB_ID, None, FakeJobApi(renewed), ipc_loop + ) + try: + assert _wait(lambda: os.environ[UNATTENDED] == renewed) + finally: + refresher.stop() + + assert os.environ[ACCESS] == (renewed if same_token else "someone-elses") + + def test_shutdown_stops_the_refresher( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: class _Client: async def aclose(self) -> None: return None @@ -434,13 +568,11 @@ async def aclose(self) -> None: conn._shutdown() - task = refresher.task - assert task is not None - assert task.cancelled() + assert _task(refresher).cancelled() assert not thread.is_alive() -def test_token_request_dto_wire_keys_are_pinned(): +def test_token_request_dto_wire_keys_are_pinned() -> None: serialization = pytest.importorskip("uipath_ipc.wire.serialization") request = _job_api.PythonUserAccessTokenRequestDto(jobKey="j") assert set(serialization.to_wire(request)) == {"jobKey", "resumeVersion", "scopes"} @@ -457,7 +589,7 @@ def _unique_pipe() -> str: return f"uipath-jobtoken-test-{os.getpid()}-{_pipe_counter}" -def _isolate_output_sinks(monkeypatch) -> None: +def _isolate_output_sinks(monkeypatch: pytest.MonkeyPatch) -> None: from uipath.runtime import output_sinks monkeypatch.setattr( @@ -468,7 +600,7 @@ def _isolate_output_sinks(monkeypatch) -> None: ) -def _serve_jobapi_in_background(pipe: str, api: Any): +def _serve_jobapi_in_background(pipe: str, api: Any) -> Callable[[], None]: """Host ``api`` as IPythonJobApi on ``pipe`` on its own loop/thread; returns stop().""" from uipath_ipc import IpcServer, NamedPipeServerTransport @@ -530,7 +662,9 @@ async def GetUserAccessToken(self, request: Any) -> str: return f"wire-token-{len(self.received)}" -def test_connect_with_refresh_renews_over_the_wire(monkeypatch): +def test_connect_with_refresh_renews_over_the_wire( + monkeypatch: pytest.MonkeyPatch, +) -> None: pytest.importorskip("uipath_ipc") _isolate_output_sinks(monkeypatch) monkeypatch.setenv(ACCESS, token_with_lifetime(600, remaining=60)) # due now @@ -550,9 +684,7 @@ async def scenario() -> None: _wait, lambda: os.environ[ACCESS] == "wire-token-1" ) # Teardown cancelled the task on the connection's loop. - task = conn._token_refresher.task - assert task is not None - assert task.done() + assert _task(conn._token_refresher).done() asyncio.run(scenario()) finally: @@ -564,7 +696,9 @@ async def scenario() -> None: assert (request.jobKey, request.resumeVersion, request.scopes) == (JOB_ID, 5, None) -def test_connect_without_refresh_installs_no_refresher(monkeypatch): +def test_connect_without_refresh_installs_no_refresher( + monkeypatch: pytest.MonkeyPatch, +) -> None: pytest.importorskip("uipath_ipc") _isolate_output_sinks(monkeypatch) api = _WireApi() diff --git a/packages/uipath/tests/cli/test_run.py b/packages/uipath/tests/cli/test_run.py index b6d95e15f..abf828879 100644 --- a/packages/uipath/tests/cli/test_run.py +++ b/packages/uipath/tests/cli/test_run.py @@ -2,6 +2,7 @@ import json import logging import os +from collections.abc import AsyncIterator from contextlib import asynccontextmanager from unittest.mock import AsyncMock, Mock, patch @@ -227,20 +228,21 @@ def test_the_flag_is_hidden_but_still_accepted(self, runner: CliRunner): ).output ) - def test_the_token_refresh_flag_needs_the_pipe( + def test_the_token_refresh_flag_without_the_pipe_runs_without_renewal( self, runner: CliRunner, temp_dir: str, entrypoint: str - ): - with runner.isolated_filesystem(temp_dir=temp_dir): + ) -> None: + with runner.isolated_filesystem(temp_dir=temp_dir), self._taken_over(): result = runner.invoke( cli, ["run", entrypoint, "--handler-ipc-token-refresh"] ) - assert result.exit_code == 1 + # Renewal is optional: the job runs without it. + assert result.exit_code == 0, f"output: {result.output!r}" assert "needs --handler-ipc-pipe" in result.output - assert "Successful execution." not in result.output + assert "Successful execution." in result.output def test_the_token_refresh_flag_is_hidden_but_still_accepted( self, runner: CliRunner - ): + ) -> None: assert ( "handler-ipc-token-refresh" not in runner.invoke(cli, ["run", "--help"]).output @@ -260,8 +262,8 @@ def test_the_token_refresh_flag_is_hidden_but_still_accepted( ) def test_the_token_refresh_flag_reaches_the_connection( - self, runner: CliRunner, temp_dir: str, monkeypatch - ): + self, runner: CliRunner, temp_dir: str, monkeypatch: pytest.MonkeyPatch + ) -> None: """The flag reaches the IPC connection.""" job_key = "3f2504e0-4f89-11d3-9a0c-0305e82c3301" monkeypatch.setenv("UIPATH_JOB_KEY", job_key) @@ -269,8 +271,12 @@ def test_the_token_refresh_flag_reaches_the_connection( @asynccontextmanager async def _fake_connection( - pipe, job_key, resume_version, *, refresh_token=False - ): + pipe: str | None, + job_key: str | None, + resume_version: int | None, + *, + refresh_token: bool = False, + ) -> AsyncIterator[None]: seen.append((pipe, job_key, resume_version, refresh_token)) yield None @@ -324,8 +330,12 @@ def test_the_job_runs_inside_the_open_connection( @asynccontextmanager async def _fake_connection( - pipe, job_key, resume_version, *, refresh_token=False - ): + pipe: str | None, + job_key: str | None, + resume_version: int | None, + *, + refresh_token: bool = False, + ) -> AsyncIterator[None]: seen.append((pipe, job_key, resume_version, refresh_token)) events.append("open") # Install for real: the ordering that matters is against the runtime context's diff --git a/packages/uipath/tests/cli/test_server_ipc.py b/packages/uipath/tests/cli/test_server_ipc.py index 2f8f8414d..82a700fb5 100644 --- a/packages/uipath/tests/cli/test_server_ipc.py +++ b/packages/uipath/tests/cli/test_server_ipc.py @@ -645,7 +645,14 @@ class TestPooledTokenRefresh: @staticmethod def _fake_run(events: list[Any]) -> Any: - async def run(cmd, args, env, wd, on_run_start=None, on_run_end=None): + async def run( + cmd: Any, + args: Any, + env: Any, + wd: Any, + on_run_start: Any = None, + on_run_end: Any = None, + ) -> dict[str, Any]: if on_run_start: on_run_start() events.append(("run",)) @@ -656,20 +663,22 @@ async def run(cmd, args, env, wd, on_run_start=None, on_run_end=None): return run @staticmethod - def _spy_token_refresher(monkeypatch, events: list[Any]) -> None: + def _spy_token_refresher( + monkeypatch: pytest.MonkeyPatch, events: list[Any] + ) -> None: from uipath._cli import _job_token - def install(jid: str, rv: int | None, cb: Any, loop: Any) -> str: - events.append(("install-token", jid, rv, cb)) - return "REFRESHER" + class _Refresher: + def stop(self) -> None: + events.append(("stop-token",)) - def uninstall(refresher: Any) -> None: - events.append(("uninstall-token", refresher)) + def install(jid: str, rv: int | None, cb: Any, loop: Any) -> _Refresher: + events.append(("install-token", jid, rv, cb)) + return _Refresher() monkeypatch.setattr(_job_token, "install_job_token_refresher", install) - monkeypatch.setattr(_job_token, "uninstall_job_token_refresher", uninstall) - def test_the_opt_in_defaults_to_off(self): + def test_the_opt_in_defaults_to_off(self) -> None: from uipath._cli.cli_server_ipc import PythonServerRunRequest assert ( @@ -680,8 +689,8 @@ def test_the_opt_in_defaults_to_off(self): ) def test_runjob_installs_the_refresher_inside_the_run_and_removes_it_after( - self, monkeypatch - ): + self, monkeypatch: pytest.MonkeyPatch + ) -> None: from uipath._cli import _job_api, cli_server_ipc from uipath._cli.cli_server_ipc import PythonServerRunRequest @@ -720,10 +729,10 @@ async def scenario() -> Any: assert events == [ ("install-token", JOB_ID, 4, "CALLBACK"), ("run",), - ("uninstall-token", "REFRESHER"), + ("stop-token",), ] - def test_both_opt_ins_install_both(self, monkeypatch): + def test_both_opt_ins_install_both(self, monkeypatch: pytest.MonkeyPatch) -> None: from uipath._cli import _job_api, cli_server_ipc from uipath._cli.cli_server_ipc import PythonServerRunRequest @@ -766,13 +775,15 @@ async def scenario() -> Any: ("install-token", JOB_ID, None, "CALLBACK"), ("run",), ("clear",), - ("uninstall-token", "REFRESHER"), + ("stop-token",), ] @pytest.mark.parametrize( "job_key", ["", "job-9", "00000000-0000-0000-0000-000000000000"] ) - def test_token_refresh_needs_a_real_job_key(self, monkeypatch, job_key): + def test_a_bad_job_key_runs_the_job_without_renewal( + self, monkeypatch: pytest.MonkeyPatch, job_key: str + ) -> None: from uipath._cli import cli_server_ipc from uipath._cli.cli_server_ipc import PythonServerRunRequest @@ -797,12 +808,13 @@ async def scenario() -> Any: ) result = asyncio.run(scenario()) - assert result.exitCode == 1 - assert "refreshAccessTokenOverIpc" in (result.error or "") - assert "jobKey" in (result.error or "") - assert events == [] + # Renewal is optional: the job runs, without a refresher. + assert result.exitCode == 0, result.error + assert events == [("run",)] - def test_token_refresh_needs_ipc(self, monkeypatch): + def test_without_ipc_the_job_runs_without_renewal( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: from uipath._cli import cli_server_ipc from uipath._cli.cli_server_ipc import PythonServerRunRequest @@ -817,11 +829,38 @@ def test_token_refresh_needs_ipc(self, monkeypatch): jobKey=JOB_ID, command="run", args=[], refreshAccessTokenOverIpc=True ) + async def scenario() -> Any: + return await service.RunJob(request, message=Message()) + + result = asyncio.run(scenario()) + assert result.exitCode == 0, result.error + assert events == [("run",)] + + def test_stream_output_still_needs_a_real_job_key( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + from uipath._cli import cli_server_ipc + from uipath._cli.cli_server_ipc import PythonServerRunRequest + + events: list[Any] = [] + self._spy_token_refresher(monkeypatch, events) + monkeypatch.setattr( + cli_server_ipc, "_run_command_isolated", self._fake_run(events) + ) + + service = PythonRuntimeService() + request = PythonServerRunRequest( + jobKey="job-9", + command="run", + args=[], + streamOutputOverIpc=True, + refreshAccessTokenOverIpc=True, + ) + async def scenario() -> Any: return await service.RunJob(request, message=Message()) result = asyncio.run(scenario()) assert result.exitCode == 1 - assert "refreshAccessTokenOverIpc" in (result.error or "") - assert "over IPC" in (result.error or "") + assert (result.error or "").startswith("streamOutputOverIpc needs a 'jobKey'") assert events == [] diff --git a/packages/uipath/tests/cli/unit/test_auth_header.py b/packages/uipath/tests/cli/unit/test_auth_header.py index 8591b0913..f3d06d995 100644 --- a/packages/uipath/tests/cli/unit/test_auth_header.py +++ b/packages/uipath/tests/cli/unit/test_auth_header.py @@ -25,9 +25,7 @@ def test_keeps_the_bearer_when_the_variable_is_unset( } -def test_adds_nothing_when_no_bearer_was_given( - monkeypatch: pytest.MonkeyPatch, -) -> None: +def test_adds_nothing_when_no_bearer_was_given(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "renewed") assert with_current_access_token({"X-Other": "kept"}) == {"X-Other": "kept"} diff --git a/packages/uipath/tests/tracing/test_otel_exporters.py b/packages/uipath/tests/tracing/test_otel_exporters.py index d930fb6ba..f7a2d3fbc 100644 --- a/packages/uipath/tests/tracing/test_otel_exporters.py +++ b/packages/uipath/tests/tracing/test_otel_exporters.py @@ -163,7 +163,7 @@ def test_force_flush(exporter): assert exporter.force_flush() is True -def test_send_uses_the_token_currently_in_the_environment(mock_env_vars): +def test_send_uses_the_token_currently_in_the_environment(mock_env_vars: None) -> None: """A token renewed in the environment after construction goes out on the next upload.""" seen: list[str | None] = []