diff --git a/packages/uipath-platform/CLAUDE.md b/packages/uipath-platform/CLAUDE.md index f695ef911..26afa62f9 100644 --- a/packages/uipath-platform/CLAUDE.md +++ b/packages/uipath-platform/CLAUDE.md @@ -59,6 +59,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. 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/pyproject.toml b/packages/uipath-platform/pyproject.toml index 78f8bc068..fdafe30cd 100644 --- a/packages/uipath-platform/pyproject.toml +++ b/packages/uipath-platform/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "uipath-platform" -version = "0.3.12" +version = "0.3.13" 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/src/uipath/platform/_uipath.py b/packages/uipath-platform/src/uipath/platform/_uipath.py index dddbc2c37..257e613d4 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, DocumentsServiceV2 from .entities import EntitiesService @@ -67,6 +69,8 @@ def __init__( scope: Optional[str] = None, debug: bool = False, ) -> None: + # A secret read from the environment is re-read on every request. + secret_from_environment = False try: if _has_valid_client_credentials( client_id, client_secret, client_assertion @@ -78,11 +82,14 @@ 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, + 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/_api_client.py b/packages/uipath-platform/src/uipath/platform/common/_api_client.py index da9b91f4a..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,9 @@ def request( **kwargs: Any, ) -> Response: if kwargs.get("include_folder_headers", False): + # httpx merges the client's default headers; do not copy them here. kwargs["headers"] = { - **kwargs.get("headers", self._client.headers), + **kwargs.get("headers", {}), **self.folder_headers, } @@ -48,8 +49,9 @@ async def request_async( **kwargs: Any, ) -> Response: if kwargs.get("include_folder_headers", False): + # httpx merges the client's default headers; do not copy them here. 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..1a86dcd7e 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,13 @@ stop_after_attempt, ) -from uipath.platform.constants import HEADER_TRACEPARENT_ID, HEADER_USER_AGENT +from uipath.platform.constants import ( + ENV_JOB_KEY, + 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 +49,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 +216,11 @@ def request( specific_component = _get_caller_component() - kwargs.setdefault("headers", {}) + # 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"]) + self._add_authorization(kwargs["headers"]) override = resolve_service_url(str(url)) if override: @@ -247,11 +261,13 @@ async def request_async( f"HEADERS: {kwargs.get('headers', self._client_async.headers)}" ) - kwargs.setdefault("headers", {}) + # 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 ) _inject_trace_context(kwargs["headers"]) + self._add_authorization(kwargs["headers"]) override = resolve_service_url(str(url)) if override: @@ -279,9 +295,31 @@ 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; one from the environment is re-read.""" + 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) + if renewed: + self._config.secret = renewed + return renewed + return self._config.secret + + def _add_authorization(self, headers: MutableMapping[str, Any]) -> None: + """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 + + 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..afad358db 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_config.py +++ b/packages/uipath-platform/src/uipath/platform/common/_config.py @@ -9,6 +9,8 @@ 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 new file mode 100644 index 000000000..cbefb3920 --- /dev/null +++ b/packages/uipath-platform/tests/services/test_env_token_reread.py @@ -0,0 +1,283 @@ +"""A UiPath() configured from the environment re-reads the token on every request.""" + +from unittest.mock import patch + +import pytest +from pytest_httpx import HTTPXMock + +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" + + +@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: 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 + + +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: 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) + + 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: 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) + + 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: 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") + 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: 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) + + 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: 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) + + 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: 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"} + + 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: 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={}) + 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 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: 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) + + 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_defaults_to_false(self) -> None: + 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: + 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") + + assert sdk.api_client.default_headers["Authorization"] == "Bearer renewed" + assert sdk._config.secret == "renewed" + + def test_an_explicit_secret_does_not(self, monkeypatch: pytest.MonkeyPatch) -> None: + 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: pytest.MonkeyPatch) -> None: + 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" diff --git a/packages/uipath-platform/uv.lock b/packages/uipath-platform/uv.lock index 479b3bdca..17d0ca82d 100644 --- a/packages/uipath-platform/uv.lock +++ b/packages/uipath-platform/uv.lock @@ -1082,7 +1082,7 @@ dev = [ [[package]] name = "uipath-platform" -version = "0.3.12" +version = "0.3.13" source = { editable = "." } dependencies = [ { name = "anyio" }, diff --git a/packages/uipath/docs/core/environment_variables.md b/packages/uipath/docs/core/environment_variables.md index bcf1bcc8b..c14371f02 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 +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. + +If your code reads these variables itself, read them when you need the token instead of keeping a copy. diff --git a/packages/uipath/pyproject.toml b/packages/uipath/pyproject.toml index 9f3c76d5e..4ef78779e 100644 --- a/packages/uipath/pyproject.toml +++ b/packages/uipath/pyproject.toml @@ -1,13 +1,13 @@ [project] name = "uipath" -version = "2.15.4" +version = "2.15.5" 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.35, <0.6.0", "uipath-runtime>=0.13.5, <0.14.0", - "uipath-platform>=0.3.12, <0.4.0", + "uipath-platform>=0.3.13, <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/src/uipath/_cli/_chat/_bridge.py b/packages/uipath/src/uipath/_cli/_chat/_bridge.py index 397b7454e..56fe5e336 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,8 @@ async def connect(self, timeout: float = 10.0) -> None: self._client.connect( url=self.websocket_url, socketio_path=self.websocket_path, - headers=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 4240f28c0..f0e642265 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,8 @@ async def run(self) -> VoiceSessionEndReason: self._client.connect( url=self._url, socketio_path=self._socketio_path, - headers=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/_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/_job_api.py b/packages/uipath/src/uipath/_cli/_job_api.py index 7f911ad56..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,14 +345,18 @@ def __init__( loop: asyncio.AbstractEventLoop, thread: threading.Thread, log_handler: "_IpcLogHandler | None" = None, + token_refresher: JobTokenRefresher | None = 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: + self._token_refresher.stop() if self._log_handler is not None: self._log_handler.flush_pending() try: @@ -372,9 +379,16 @@ 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 job's user access token is renewed over the same connection. + """ 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 +429,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 +447,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..ee6b1fa86 --- /dev/null +++ b/packages/uipath/src/uipath/_cli/_job_token.py @@ -0,0 +1,238 @@ +"""Renew a job's user access token before it expires.""" + +from __future__ import annotations + +import asyncio +import functools +import logging +import os +import threading +import time +from collections.abc import Callable, Iterable +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__) + +_TOKEN_REQUEST_TIMEOUT_S = 30.0 +# 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 +_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) + + +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 and its renewal times (epoch seconds).""" + + token: str + issued_at: float + expires_at: float | None + + @classmethod + def from_token(cls, token: str, now: float) -> _TokenState: + """Read ``iat`` and ``exp`` from the JWT.""" + 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, 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) + 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, 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 + + +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 failure is logged, + until a renewal succeeds 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._reported_failure = False + + def start(self) -> None: + """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: + """Stop renewing; nothing is published after this.""" + with self._lock: + self._stopped = True + task = self._task + if task is not None: + task.cancel() + + async def _run(self) -> None: + while not self._stopped: + state = self._state + if state is None or state.refresh_at is None: + 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(min(due_at - now, _MAX_SLEEP_S)) + 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 + self._state = state + # 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 + ) + 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: + 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 + 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: further " + "failures are not logged until a renewal succeeds.", + error, + _RETRY_AFTER_FAILURE_S, + ) + + +def install_job_token_refresher( + job_key: str, + resume_version: int | None, + proxy: Any, + loop: asyncio.AbstractEventLoop, +) -> JobTokenRefresher: + """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 + ) + # 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, + publish=functools.partial(publish_token_to_environ, names=names), + ) + refresher.start() + return refresher 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..842d4cb67 --- /dev/null +++ b/packages/uipath/src/uipath/_cli/_utils/_auth_header.py @@ -0,0 +1,15 @@ +"""Bearer header for connections the CLI opens.""" + +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]: + """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: + result["Authorization"] = f"Bearer {token}" + return result diff --git a/packages/uipath/src/uipath/_cli/cli_run.py b/packages/uipath/src/uipath/_cli/cli_run.py index 13ff381a7..49e8f0b16 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, 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") -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,13 @@ def run( console.error(f"Invalid --simulation config: {e}") return + if handler_ipc_token_refresh and handler_ipc_pipe is None: + # 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", entrypoint, @@ -232,7 +246,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..4f9cfa5af 100644 --- a/packages/uipath/src/uipath/_cli/cli_server_ipc.py +++ b/packages/uipath/src/uipath/_cli/cli_server_ipc.py @@ -36,6 +36,7 @@ class PythonServerRunRequest: workingDirectory: str | None = None environmentVariables: dict[str, str] = field(default_factory=dict) streamOutputOverIpc: bool = False + refreshAccessTokenOverIpc: bool = False @dataclass @@ -103,7 +104,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,33 +116,58 @@ async def RunJob( is_wire_job_key, ) + 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"streamOutputOverIpc 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="streamOutputOverIpc 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: - installed.append( - install_runtime_sinks(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." ) - - on_run_start = _install - on_run_end = clear_runtime_sinks + 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() + for refresher in token_refreshers: + refresher.stop() + + on_run_start = _install + on_run_end = _uninstall result = await _run_command_isolated( cmd, diff --git a/packages/uipath/src/uipath/tracing/_otel_exporters.py b/packages/uipath/src/uipath/tracing/_otel_exporters.py index 6796c9629..d46dfc4d9 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: + """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 + + 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/cli/chat/test_bridge.py b/packages/uipath/tests/cli/chat/test_bridge.py index 2c4aedbe9..58a344146 100644 --- a/packages/uipath/tests/cli/chat/test_bridge.py +++ b/packages/uipath/tests/cli/chat/test_bridge.py @@ -1200,3 +1200,49 @@ 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) + + 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 c4e704922..85e73517c 100644 --- a/packages/uipath/tests/cli/chat/test_voice_bridge.py +++ b/packages/uipath/tests/cli/chat/test_voice_bridge.py @@ -233,3 +233,39 @@ 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 + 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 new file mode 100644 index 000000000..7745eb103 --- /dev/null +++ b/packages/uipath/tests/cli/test_job_token.py @@ -0,0 +1,716 @@ +"""Tests for the job's user access token refresher.""" + +import asyncio +import base64 +import json +import logging +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 + +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, +) + +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: + """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) + 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 + answer = self.tokens.pop(0) if self.tokens else "fresh" + if isinstance(answer, BaseException): + raise answer + return answer + + +class RemoteError(Exception): + """Shaped like uipath_ipc's RemoteException.""" + + def __init__(self, type_name: str) -> None: + super().__init__(type_name) + self.type_name = type_name + + +@pytest.fixture +def ipc_loop() -> Iterator[asyncio.AbstractEventLoop]: + 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() -> Iterator[list[logging.LogRecord]]: + 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] + + +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) -> None: + 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) -> 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) -> 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: asyncio.AbstractEventLoop, monkeypatch: pytest.MonkeyPatch + ) -> None: + 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 _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 = _task(refresher) + 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: asyncio.AbstractEventLoop + ) -> None: + 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 _token(refresher) is not None + + 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() + 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 _token(refresher) != "late" + + 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( + FakeJobApi(), ipc_loop, JOB_ID, None, initial_token="old" + ) + + refresher.stop() + refresher._store("late") + + assert os.environ[ACCESS] == "old" + assert _token(refresher) == "old" + + 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).result(timeout=5) # returns at once: nothing to time against + assert api.requests == [] + 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: asyncio.AbstractEventLoop, records: list[logging.LogRecord] + ) -> None: + api = FakeJobApi() + refresher = _refresher(api, ipc_loop, None) + + refresher.start() + + _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 == [] + + 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: 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) + 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 _token(refresher) == token + 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_a_host_without_the_method_is_retried_like_any_other_failure( + 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")) + 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 + assert not _task(refresher).done() + refresher.stop() + 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: asyncio.AbstractEventLoop, + monkeypatch: pytest.MonkeyPatch, + records: list[logging.LogRecord], + bad: Any, + ) -> None: + 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: 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( + *[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 _token(refresher) in api.tokens or _token(refresher) != "" + assert _warnings(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( + 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: pytest.MonkeyPatch + ) -> None: + 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_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") + + refresher = _refresher(FakeJobApi(), ipc_loop, "old", publish=boom) + + refresher._store("renewed") + + assert _token(refresher) == "renewed" + assert _warnings(records) == [] + + +class TestInstall: + 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")) + 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 _token(refresher) == unattended + assert _wait(lambda: _pending_tasks(ipc_loop) == 1) + assert api.requests == [] + finally: + refresher.stop() + + assert _wait(lambda: _pending_tasks(ipc_loop) == 0) + + @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 + + 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() + + assert _task(refresher).cancelled() + assert not thread.is_alive() + + +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"} + + +# -- 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: pytest.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) -> Callable[[], None]: + """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.MonkeyPatch, +) -> None: + 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. + assert _task(conn._token_refresher).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.MonkeyPatch, +) -> None: + 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..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,6 +228,90 @@ def test_the_flag_is_hidden_but_still_accepted(self, runner: CliRunner): ).output ) + def test_the_token_refresh_flag_without_the_pipe_runs_without_renewal( + self, runner: CliRunner, temp_dir: str, entrypoint: str + ) -> None: + with runner.isolated_filesystem(temp_dir=temp_dir), self._taken_over(): + result = runner.invoke( + cli, ["run", entrypoint, "--handler-ipc-token-refresh"] + ) + # 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." 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 + ) + 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: pytest.MonkeyPatch + ) -> None: + """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]] = [] + + @asynccontextmanager + async def _fake_connection( + 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 + + 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 +320,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 +329,14 @@ 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: 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 # one-shot snapshot, not against runtime.execute. @@ -303,7 +394,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..82a700fb5 100644 --- a/packages/uipath/tests/cli/test_server_ipc.py +++ b/packages/uipath/tests/cli/test_server_ipc.py @@ -638,3 +638,229 @@ async def scenario() -> None: asyncio.run(scenario()) assert events == [("run",)] + + +class TestPooledTokenRefresh: + """refreshAccessTokenOverIpc is its own opt-in.""" + + @staticmethod + def _fake_run(events: list[Any]) -> Any: + 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",)) + if on_run_end: + on_run_end() + return {"ExitCode": 0, "Error": None} + + return run + + @staticmethod + def _spy_token_refresher( + monkeypatch: pytest.MonkeyPatch, events: list[Any] + ) -> None: + from uipath._cli import _job_token + + class _Refresher: + def stop(self) -> None: + events.append(("stop-token",)) + + 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) + + def test_the_opt_in_defaults_to_off(self) -> None: + 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: pytest.MonkeyPatch + ) -> None: + 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",), + ("stop-token",), + ] + + 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 + + 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",), + ("stop-token",), + ] + + @pytest.mark.parametrize( + "job_key", ["", "job-9", "00000000-0000-0000-0000-000000000000"] + ) + 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 + + 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()) + # Renewal is optional: the job runs, without a refresher. + assert result.exitCode == 0, result.error + assert events == [("run",)] + + 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 + + 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 == 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 (result.error or "").startswith("streamOutputOverIpc needs a 'jobKey'") + assert events == [] 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..f3d06d995 --- /dev/null +++ b/packages/uipath/tests/cli/unit/test_auth_header.py @@ -0,0 +1,31 @@ +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"} diff --git a/packages/uipath/tests/tracing/test_otel_exporters.py b/packages/uipath/tests/tracing/test_otel_exporters.py index c8630b539..f7a2d3fbc 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: None) -> None: + """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() diff --git a/packages/uipath/uv.lock b/packages/uipath/uv.lock index ab608bcd9..85cca848a 100644 --- a/packages/uipath/uv.lock +++ b/packages/uipath/uv.lock @@ -2720,7 +2720,7 @@ wheels = [ [[package]] name = "uipath" -version = "2.15.4" +version = "2.15.5" source = { editable = "." } dependencies = [ { name = "applicationinsights" }, @@ -2883,7 +2883,7 @@ wheels = [ [[package]] name = "uipath-platform" -version = "0.3.12" +version = "0.3.13" source = { editable = "../uipath-platform" } dependencies = [ { name = "anyio" },