Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/uipath-platform/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "uipath-platform"
version = "0.3.23"
version = "0.3.24"
description = "HTTP client library for programmatic access to UiPath Platform"
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
from ..orchestrator._folder_service import FolderService
from ._execution_service import (
_ORIGINATOR_VALUE,
EXECUTABLE_HEADERS,
HEADER_ACTIVITY_JOB_ID,
HEADER_IS_EXECUTABLE_ID,
HEADER_IS_EXECUTABLE_TYPE,
HEADER_ORIGINATOR,
HEADER_SOURCE,
ConnectionExecutionMixin,
executable_headers,
)
from .connections import (
ActivityMetadata,
Expand All @@ -33,6 +37,8 @@

__all__ = [
"HEADER_ACTIVITY_JOB_ID",
"HEADER_IS_EXECUTABLE_ID",
"HEADER_IS_EXECUTABLE_TYPE",
"HEADER_ORIGINATOR",
"HEADER_SOURCE",
"ConnectionsService",
Expand Down Expand Up @@ -779,7 +785,9 @@ def _build_activity_request_spec(
elif param_name in activity_metadata.parameter_location_info.path_params:
path_params[param_name] = value_str
elif param_name in activity_metadata.parameter_location_info.header_params:
header_params[param_name] = value_str
# Only the job's environment sets the executable headers, never an input.
if param_name.lower() not in EXECUTABLE_HEADERS:
header_params[param_name] = value_str
elif (
param_name in activity_metadata.parameter_location_info.multipart_params
):
Expand All @@ -800,6 +808,7 @@ def _build_activity_request_spec(
}
if job_key := UiPathConfig.job_key:
headers[HEADER_ACTIVITY_JOB_ID] = job_key
headers.update(executable_headers())

# body and files handling
json_data: Dict[str, Any] | None = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Running a connector activity or a vendor HTTP request through a connection."""

import time
import uuid
from collections.abc import Sequence
from typing import Any
from urllib.parse import urlsplit
Expand All @@ -11,12 +12,13 @@

from ..common._base_service import BaseService, _inject_trace_context
from ..common._bindings import resource_override
from ..common._config import UiPathConfig
from ..common._config import UiPathConfig, resolve_execution_type
from ..common._folder_context import header_folder
from ..common._service_url_overrides import (
inject_routing_headers,
resolve_service_url,
)
from ..common._span_utils import ExecutionType
from ..common._user_agent import user_agent_value
from ..common.retry import (
RETRYABLE_STATUS_CODES,
Expand All @@ -39,6 +41,36 @@
HEADER_ACTIVITY_JOB_ID = "x-uipath-job-id"
_ORIGINATOR_VALUE = "uipath-python"

# The automation that makes an Integration Service call. Integration Service applies
# its access policies to this automation (an Agent and its process key).
HEADER_IS_EXECUTABLE_TYPE = "x-uipath-is-executable-type"
HEADER_IS_EXECUTABLE_ID = "x-uipath-is-executable-id"
EXECUTABLE_HEADERS = frozenset({HEADER_IS_EXECUTABLE_TYPE, HEADER_IS_EXECUTABLE_ID})
_EXECUTABLE_TYPE_AGENT = "Agent"


def executable_headers() -> dict[str, str]:
"""The executable headers of an Integration Service call from a deployed job.

The SDK sends both headers or neither. It sends them only for a deployed
Orchestrator job: the run has a job key, it is not a debug run, and its
process key is a GUID. The values come from the job's environment, never
from the caller's input. A local run or a debug run sends no headers, as
Maestro and agents-api do.
"""
if not UiPathConfig.job_key or resolve_execution_type() != ExecutionType.RUNTIME:
return {}
try:
process_key = uuid.UUID((UiPathConfig.process_uuid or "").strip())
except ValueError:
return {}
return {
HEADER_IS_EXECUTABLE_TYPE: _EXECUTABLE_TYPE_AGENT,
# The canonical form: Integration Service reads only a hyphenated GUID.
HEADER_IS_EXECUTABLE_ID: str(process_key),
}


# Statuses of the Integration Service call that an opted-in retry sends again.
_EXECUTE_RETRYABLE_STATUS_CODES = RETRYABLE_STATUS_CODES | {500}
_EXECUTE_INITIAL_BACKOFF = 0.5
Expand Down Expand Up @@ -723,6 +755,7 @@ def _execute_call(
}
if job_key := UiPathConfig.job_key:
headers[HEADER_ACTIVITY_JOB_ID] = job_key
headers.update(executable_headers())
_inject_trace_context(headers)
# The token of the moment: a renewed one replaces the client's own
self._add_authorization(headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pytest_httpx import HTTPXMock

from uipath.platform import UiPathApiConfig, UiPathExecutionContext
from uipath.platform.common import UiPathConfig
from uipath.platform.common._bindings import (
ConnectionResourceOverwrite,
_resource_overwrites,
Expand Down Expand Up @@ -1527,3 +1528,94 @@ def test_object_name_dots_inside_a_segment_are_allowed(
httpx_mock.add_response(url=f"{instance_url}/files/report..v1.pdf", json={})

assert service.execute_activity(CONNECTION_ID, "files/report..v1.pdf").ok


class TestExecutableHeaders:
"""The access policy headers on an execute call: the automation that makes the call."""

PROCESS_KEY = "6f1c2a3b-4d5e-4f60-8a7b-9c0d1e2f3a4b"

@pytest.fixture(autouse=True)
def _deployed_job(self, monkeypatch: pytest.MonkeyPatch, tmp_path) -> Any:
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("UIPATH_JOB_KEY", "job-key-abc")
monkeypatch.setenv("UIPATH_PROCESS_UUID", self.PROCESS_KEY)
monkeypatch.delenv("UIPATH_IS_DEBUG", raising=False)
UiPathConfig.reset()
yield
UiPathConfig.reset()

def test_execute_activity_in_a_job(
self, httpx_mock: HTTPXMock, service: ConnectionsService, instance_url: str
) -> None:
httpx_mock.add_response(url=f"{instance_url}/tickets", json=[])

service.execute_activity(CONNECTION_ID, "tickets")

request = sent(httpx_mock)
assert request.headers["x-uipath-is-executable-type"] == "Agent"
assert request.headers["x-uipath-is-executable-id"] == self.PROCESS_KEY

def test_execute_http_in_a_job_keeps_vendor_headers_in_the_body(
self, httpx_mock: HTTPXMock, service: ConnectionsService, instance_url: str
) -> None:
httpx_mock.add_response(
url=f"{instance_url}/http-request",
json={"code": 200, "statusText": "OK", "headers": {}, "body": {}},
)

service.execute_http(
CONNECTION_ID,
"/v2/tickets",
"GET",
headers={"x-uipath-is-executable-type": "AgenticProcess"},
)

request = sent(httpx_mock)
assert request.headers["x-uipath-is-executable-type"] == "Agent"
assert request.headers["x-uipath-is-executable-id"] == self.PROCESS_KEY
# A vendor header goes to the vendor in the proxy body, not to Integration Service.
assert json.loads(request.content)["headers"] == {
"x-uipath-is-executable-type": "AgenticProcess"
}

async def test_execute_activity_async_in_a_job(
self, httpx_mock: HTTPXMock, service: ConnectionsService, instance_url: str
) -> None:
httpx_mock.add_response(url=f"{instance_url}/tickets", json=[])

await service.execute_activity_async(CONNECTION_ID, "tickets")

request = sent(httpx_mock)
assert request.headers["x-uipath-is-executable-id"] == self.PROCESS_KEY

def test_debug_run_sends_no_headers(
self,
httpx_mock: HTTPXMock,
service: ConnectionsService,
instance_url: str,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("UIPATH_IS_DEBUG", "true")
httpx_mock.add_response(url=f"{instance_url}/tickets", json=[])

service.execute_activity(CONNECTION_ID, "tickets")

request = sent(httpx_mock)
assert "x-uipath-is-executable-type" not in request.headers
assert "x-uipath-is-executable-id" not in request.headers

def test_local_run_sends_no_headers(
self,
httpx_mock: HTTPXMock,
service: ConnectionsService,
instance_url: str,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("UIPATH_JOB_KEY", raising=False)
httpx_mock.add_response(url=f"{instance_url}/tickets", json=[])

service.execute_activity(CONNECTION_ID, "tickets")

request = sent(httpx_mock)
assert "x-uipath-is-executable-type" not in request.headers
Loading
Loading