mainsequence 9.0.11__tar.gz → 9.0.12__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mainsequence-9.0.11 → mainsequence-9.0.12}/PKG-INFO +1 -1
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/application_surfaces/api_surfaces/SKILL.md +7 -0
- mainsequence-9.0.12/mainsequence/_request_identity.py +98 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/config.py +3 -1
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/base.py +12 -2
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/models_user.py +33 -3
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/utils.py +8 -22
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/logconf.py +1 -8
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/runtime_credential_exchange.py +28 -36
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/server/caller_assertions.py +2 -1
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/server/fastapi.py +19 -2
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence.egg-info/PKG-INFO +1 -1
- {mainsequence-9.0.11 → mainsequence-9.0.12}/pyproject.toml +1 -1
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_auth_precedence.py +33 -26
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_canonical_api_route_contract.py +1 -4
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_cli_secure_storage.py +8 -10
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_code_repository_runtime_context.py +4 -13
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_credential_repr.py +1 -27
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_fastapi_request_identity.py +141 -1
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_http_retry_budget.py +6 -2
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_runtime_credential_exchange.py +130 -53
- mainsequence-9.0.11/mainsequence/_request_identity.py +0 -44
- {mainsequence-9.0.11 → mainsequence-9.0.12}/LICENSE +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/README.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/AGENTS.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/a2a_sdk_execution/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/mainsequence-inference/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/maintenance/bug_auditor/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/maintenance/code_repository_maintenance/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/maintenance/metatables_transition/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/platform_operations/access_control_and_sharing/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/platform_operations/orchestration_and_releases/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/agent_scaffold/skills/sdk_code_repository_execution/SKILL.md +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/__init__.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/__main__.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/bootstrap.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/__init__.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/api.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/browser_auth.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/cli.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/code_repository_status.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/docker_utils.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/doctor.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/local_ops.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/model_filters.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/pydantic_cli.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/sdk_utils.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/ssh_utils.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/cli/ui.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/__init__.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/_transport.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/agent_runtime_models.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/client.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/compute_validation.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/exceptions.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/github_issues.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/inference.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/models_foundry.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/models_helpers.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/observability.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/client/value_sets.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/code_repository_context.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/code_repository_skills.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/defaults.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/instrumentation/__init__.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/instrumentation/utils.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/repository_identity_security.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/runtime_flags.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/scaffold_skills.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence/server/__init__.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence.egg-info/SOURCES.txt +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence.egg-info/dependency_links.txt +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence.egg-info/entry_points.txt +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence.egg-info/requires.txt +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/mainsequence.egg-info/top_level.txt +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/setup.cfg +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_auth_bootstrap.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_caller_assertions.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_cli_auth_contract.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_cli_browser_auth.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_client.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_code_repository_branch_contract.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_code_repository_documentation_contract.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_code_repository_skills.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_dependency_extras.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_dev_release_version.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_environment_resource_context.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_environment_response_contracts.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_github_issues_client.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_independent_context.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_inference_client.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_instrumentation.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_job_client.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_local_ops.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_logconf.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_models_user_request_bound_auth.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_notification_uid_contract.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_observability_client.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_scaffold_skills.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_secret_client_model.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_source_ownership.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_ssh_utils.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_tolerant_response_reading.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_tolerant_value_sets.py +0 -0
- {mainsequence-9.0.11 → mainsequence-9.0.12}/tests/test_user_application_facts.py +0 -0
|
@@ -147,6 +147,13 @@ person, read `User.get_by_uid(uid)`: a workload identity has `identity_type`
|
|
|
147
147
|
`"workload"`. Caller authentication is not resource authorization; apply the
|
|
148
148
|
endpoint's application policy using the verified user UID.
|
|
149
149
|
|
|
150
|
+
To list the people, Teams and workloads the caller can see, for example to
|
|
151
|
+
offer sharing candidates, read inside `reads_as_caller()` from
|
|
152
|
+
`mainsequence.server.fastapi`: `User.filter`, `User.get_by_uid`, `Team.filter`
|
|
153
|
+
and `Team.get_by_uid` then answer with the caller's visibility, and workload
|
|
154
|
+
rows carry `managed_by_caller`. Outside a signed HTTP request it raises
|
|
155
|
+
`RequestIdentityError`; it never reads as the application instead.
|
|
156
|
+
|
|
150
157
|
WebSockets retain the platform's gateway ticket/header contract rather than
|
|
151
158
|
the HTTP Bearer/assertion flow; the getter is available while the authenticated
|
|
152
159
|
connection is active. See the version-matched
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Private lifecycle shared by the request integration and the public user getter."""
|
|
2
|
+
|
|
3
|
+
from contextlib import contextmanager
|
|
4
|
+
from contextvars import ContextVar
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Any
|
|
7
|
+
from urllib.parse import urlsplit
|
|
8
|
+
|
|
9
|
+
CALLER_ASSERTION_HEADER = "X-MainSequence-Caller-Assertion"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class RequestIdentityError(RuntimeError):
|
|
13
|
+
"""No authenticated caller is bound to the current request."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class _RequestScope:
|
|
18
|
+
user: Any = None
|
|
19
|
+
active: bool = True
|
|
20
|
+
# The verified assertion of a hosted HTTP request, kept only so that
|
|
21
|
+
# reads_as_caller() can present it (ADR-0036, amended for #198).
|
|
22
|
+
caller_assertion: str | None = field(default=None, repr=False)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
_current_scope: ContextVar[_RequestScope | None] = ContextVar(
|
|
26
|
+
"mainsequence_request_identity", default=None
|
|
27
|
+
)
|
|
28
|
+
# The request scope whose caller the directory reads answer as, inside reads_as_caller().
|
|
29
|
+
_caller_reads: ContextVar[_RequestScope | None] = ContextVar(
|
|
30
|
+
"mainsequence_caller_reads", default=None
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@contextmanager
|
|
35
|
+
def _request_scope():
|
|
36
|
+
scope = _RequestScope()
|
|
37
|
+
token = _current_scope.set(scope)
|
|
38
|
+
try:
|
|
39
|
+
yield scope
|
|
40
|
+
finally:
|
|
41
|
+
# Copied child-task contexts hold this same frame and must also expire.
|
|
42
|
+
scope.active = False
|
|
43
|
+
scope.user = None
|
|
44
|
+
scope.caller_assertion = None
|
|
45
|
+
_current_scope.reset(token)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _get_request_identity():
|
|
49
|
+
scope = _current_scope.get()
|
|
50
|
+
if scope is None or not scope.active or scope.user is None:
|
|
51
|
+
raise RequestIdentityError(
|
|
52
|
+
"No authenticated request user is available. Install request identity "
|
|
53
|
+
"once in the application and call User.get_logged_user() inside a request."
|
|
54
|
+
)
|
|
55
|
+
return scope.user
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@contextmanager
|
|
59
|
+
def _reads_as_caller():
|
|
60
|
+
scope = _current_scope.get()
|
|
61
|
+
if scope is None or not scope.active or scope.user is None:
|
|
62
|
+
raise RequestIdentityError(
|
|
63
|
+
"Reading as the caller needs an authenticated request. Install request "
|
|
64
|
+
"identity once in the application and call reads_as_caller() inside a request."
|
|
65
|
+
)
|
|
66
|
+
if scope.caller_assertion is None:
|
|
67
|
+
raise RequestIdentityError(
|
|
68
|
+
"This request carries no caller assertion. Reading as the caller needs a "
|
|
69
|
+
"hosted HTTP request in assertion mode, not local mode or a WebSocket."
|
|
70
|
+
)
|
|
71
|
+
token = _caller_reads.set(scope)
|
|
72
|
+
try:
|
|
73
|
+
yield
|
|
74
|
+
finally:
|
|
75
|
+
_caller_reads.reset(token)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _origin(url: str) -> tuple[str, str | None, int | None]:
|
|
79
|
+
parts = urlsplit(url)
|
|
80
|
+
scheme = parts.scheme.lower()
|
|
81
|
+
return scheme, parts.hostname, parts.port or {"http": 80, "https": 443}.get(scheme)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _caller_read_headers(url: str, endpoint_url: str) -> dict[str, str]:
|
|
85
|
+
"""The header a directory read sends to ``url``: none outside reads_as_caller()."""
|
|
86
|
+
scope = _caller_reads.get()
|
|
87
|
+
if scope is None:
|
|
88
|
+
return {}
|
|
89
|
+
if not scope.active or scope.caller_assertion is None:
|
|
90
|
+
raise RequestIdentityError(
|
|
91
|
+
"The request that reads_as_caller() belongs to has ended; its caller "
|
|
92
|
+
"assertion is no longer available."
|
|
93
|
+
)
|
|
94
|
+
if _origin(url) != _origin(endpoint_url):
|
|
95
|
+
raise RequestIdentityError(
|
|
96
|
+
"The caller assertion is sent only to the configured platform endpoint."
|
|
97
|
+
)
|
|
98
|
+
return {CALLER_ASSERTION_HEADER: scope.caller_assertion}
|
|
@@ -75,6 +75,8 @@ KEYCHAIN_ACCOUNT = "default"
|
|
|
75
75
|
AUTH_RECORD_VERSION = 1
|
|
76
76
|
# Credential entries a project `.env` must not hold. The CLI removes them and
|
|
77
77
|
# never writes them; the session lives in the operating system credential store.
|
|
78
|
+
# The runtime credential secret and `MAINSEQUENCE_TOKEN` are no longer read: they
|
|
79
|
+
# stay listed so that an entry an earlier version left in a checkout is removed.
|
|
78
80
|
PROJECT_ENV_CREDENTIAL_KEYS = (
|
|
79
81
|
"MAINSEQUENCE_ACCESS_TOKEN",
|
|
80
82
|
"MAINSEQUENCE_REFRESH_TOKEN",
|
|
@@ -805,7 +807,7 @@ def session_report() -> dict:
|
|
|
805
807
|
refresh_expires_at = token_expiry(refresh)
|
|
806
808
|
|
|
807
809
|
if auth_mode == "runtime_credential":
|
|
808
|
-
# The credential is an ID with
|
|
810
|
+
# The credential is an ID with the identity token file that proves it.
|
|
809
811
|
authenticated = bool(access or runtime_credential_configured())
|
|
810
812
|
session_expires_at = None
|
|
811
813
|
elif refresh:
|
|
@@ -180,6 +180,11 @@ class BaseObjectOrm:
|
|
|
180
180
|
def _sdk_owned_query_context(cls, operation: str) -> dict[str, Any]:
|
|
181
181
|
return {}
|
|
182
182
|
|
|
183
|
+
@classmethod
|
|
184
|
+
def _read_request_headers(cls, url: str) -> dict[str, str]:
|
|
185
|
+
"""Headers a filter or detail read adds to its own request to ``url``."""
|
|
186
|
+
return {}
|
|
187
|
+
|
|
183
188
|
@classmethod
|
|
184
189
|
def _sdk_owned_create_context(cls, operation: str) -> dict[str, Any]:
|
|
185
190
|
return {}
|
|
@@ -540,12 +545,13 @@ class BaseObjectOrm:
|
|
|
540
545
|
|
|
541
546
|
while next_url:
|
|
542
547
|
req_payload = {"params": params} if params else {}
|
|
548
|
+
headers = cls._read_request_headers(next_url)
|
|
543
549
|
r = make_request(
|
|
544
550
|
s=cls.build_session(),
|
|
545
551
|
loaders=cls.LOADERS,
|
|
546
552
|
r_type="GET",
|
|
547
553
|
url=next_url,
|
|
548
|
-
payload=req_payload,
|
|
554
|
+
payload={**req_payload, "headers": headers} if headers else req_payload,
|
|
549
555
|
time_out=timeout,
|
|
550
556
|
)
|
|
551
557
|
raise_for_response(r, payload=req_payload)
|
|
@@ -610,12 +616,16 @@ class BaseObjectOrm:
|
|
|
610
616
|
**cls._sdk_owned_query_context(operation),
|
|
611
617
|
}
|
|
612
618
|
|
|
619
|
+
payload = {"params": params} # neede to pass special serializer
|
|
620
|
+
headers = cls._read_request_headers(detail_url)
|
|
621
|
+
if headers:
|
|
622
|
+
payload["headers"] = headers
|
|
613
623
|
r = make_request(
|
|
614
624
|
s=cls.build_session(),
|
|
615
625
|
loaders=cls.LOADERS,
|
|
616
626
|
r_type="GET",
|
|
617
627
|
url=detail_url,
|
|
618
|
-
payload=
|
|
628
|
+
payload=payload,
|
|
619
629
|
time_out=timeout,
|
|
620
630
|
)
|
|
621
631
|
raise_for_response(r)
|
|
@@ -6,7 +6,11 @@ from uuid import UUID
|
|
|
6
6
|
|
|
7
7
|
from pydantic import BaseModel, ConfigDict, Field, StrictBool, field_validator, model_validator
|
|
8
8
|
|
|
9
|
-
from mainsequence._request_identity import
|
|
9
|
+
from mainsequence._request_identity import (
|
|
10
|
+
RequestIdentityError,
|
|
11
|
+
_caller_read_headers,
|
|
12
|
+
_get_request_identity,
|
|
13
|
+
)
|
|
10
14
|
from mainsequence.defaults import STANDARD_BACKEND_URL
|
|
11
15
|
|
|
12
16
|
from .base import (
|
|
@@ -336,7 +340,18 @@ class TeamMembershipUpdateResult(BasePydanticModel):
|
|
|
336
340
|
)
|
|
337
341
|
|
|
338
342
|
|
|
339
|
-
class
|
|
343
|
+
class _CallerDirectoryReadMixin:
|
|
344
|
+
"""Inside ``reads_as_caller()``, ``filter`` and ``get`` reads answer as the
|
|
345
|
+
request's caller (ADR-0036, amended for #198)."""
|
|
346
|
+
|
|
347
|
+
@classmethod
|
|
348
|
+
def _read_request_headers(cls, url: str) -> dict[str, str]:
|
|
349
|
+
return _caller_read_headers(url, cls.get_object_url())
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
class Team(
|
|
353
|
+
_CallerDirectoryReadMixin, PermissionManagedObjectMixin, BasePydanticModel, UserApiBaseObjectOrm
|
|
354
|
+
):
|
|
340
355
|
ENDPOINT: ClassVar[str] = "teams"
|
|
341
356
|
FILTERSET_FIELDS: ClassVar[dict[str, list[str]] | None] = {
|
|
342
357
|
"uid": ["exact", "in"],
|
|
@@ -960,7 +975,7 @@ class ShareableAccessState(BasePydanticModel):
|
|
|
960
975
|
)
|
|
961
976
|
|
|
962
977
|
|
|
963
|
-
class User(UserApiBaseObjectOrm, BasePydanticModel):
|
|
978
|
+
class User(_CallerDirectoryReadMixin, UserApiBaseObjectOrm, BasePydanticModel):
|
|
964
979
|
"""A platform User: a person, or the workload identity a deployed Job,
|
|
965
980
|
FastAPI release or Agent runs as.
|
|
966
981
|
|
|
@@ -974,6 +989,10 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
|
|
|
974
989
|
The reverse direction is ``workload_user_uid`` on ``Job``,
|
|
975
990
|
``ResourceRelease`` and ``Agent``: the UID of the User that workload runs
|
|
976
991
|
as, to pass to ``User.get_by_uid`` and to sharing calls.
|
|
992
|
+
|
|
993
|
+
Inside ``mainsequence.server.fastapi.reads_as_caller()``, ``User.filter``
|
|
994
|
+
and ``User.get_by_uid`` answer with what the request's caller may see, and
|
|
995
|
+
workload rows carry ``managed_by_caller``.
|
|
977
996
|
"""
|
|
978
997
|
|
|
979
998
|
# Platform facts, not application authorization policy. None means the
|
|
@@ -986,6 +1005,7 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
|
|
|
986
1005
|
"uid": ["exact", "in"],
|
|
987
1006
|
"email": ["exact", "contains", "in"],
|
|
988
1007
|
"identity_type": ["exact"],
|
|
1008
|
+
"search": ["exact"],
|
|
989
1009
|
}
|
|
990
1010
|
FILTER_VALUE_NORMALIZERS: ClassVar[dict[str, str]] = {
|
|
991
1011
|
"uid": "uid",
|
|
@@ -994,6 +1014,7 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
|
|
|
994
1014
|
"email__contains": "str",
|
|
995
1015
|
"email__in": "str",
|
|
996
1016
|
"identity_type": "str",
|
|
1017
|
+
"search": "str",
|
|
997
1018
|
}
|
|
998
1019
|
|
|
999
1020
|
id: int | None = Field(
|
|
@@ -1038,6 +1059,15 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
|
|
|
1038
1059
|
description="UID of the Agent a workload identity belongs to; None otherwise.",
|
|
1039
1060
|
examples=[None],
|
|
1040
1061
|
)
|
|
1062
|
+
managed_by_caller: bool | None = Field(
|
|
1063
|
+
None,
|
|
1064
|
+
title="Managed By Caller",
|
|
1065
|
+
description=(
|
|
1066
|
+
"On a workload identity read inside `reads_as_caller()`: whether the "
|
|
1067
|
+
"request's caller manages that workload. None when the row does not carry it."
|
|
1068
|
+
),
|
|
1069
|
+
examples=[True],
|
|
1070
|
+
)
|
|
1041
1071
|
profile_picture: str | None = Field(
|
|
1042
1072
|
None,
|
|
1043
1073
|
title="Profile Picture URL",
|
|
@@ -23,8 +23,6 @@ from mainsequence.defaults import resolve_backend_endpoint
|
|
|
23
23
|
from mainsequence.logconf import logger
|
|
24
24
|
from mainsequence.runtime_credential_exchange import (
|
|
25
25
|
RUNTIME_CREDENTIAL_ID_ENV,
|
|
26
|
-
RUNTIME_CREDENTIAL_SECRET_ENV,
|
|
27
|
-
RUNTIME_IDENTITY_TOKEN_FILE_ENV,
|
|
28
26
|
RuntimeIdentityTokenError,
|
|
29
27
|
exchange_runtime_credential,
|
|
30
28
|
identity_token_file_from_environment,
|
|
@@ -321,17 +319,16 @@ class RuntimeCredentialAuthProvider(BaseAuthProvider):
|
|
|
321
319
|
"""
|
|
322
320
|
Exchange the runtime credential the platform injected for short-lived access tokens.
|
|
323
321
|
|
|
324
|
-
The proof is the projected workload identity token
|
|
325
|
-
`MAINSEQUENCE_RUNTIME_IDENTITY_TOKEN_FILE
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
provider
|
|
329
|
-
(`429`) and temporarily unavailable (`503`) exchanges are retried
|
|
330
|
-
timeout budget; a rejected one (`401`) fails at once.
|
|
322
|
+
The proof is the projected workload identity token in the file named by
|
|
323
|
+
`MAINSEQUENCE_RUNTIME_IDENTITY_TOKEN_FILE`, read for every exchange. It is the
|
|
324
|
+
only proof: an unset variable, or a missing, unreadable or empty file, raises
|
|
325
|
+
`AuthError` instead of sending the exchange. The token is never kept on the
|
|
326
|
+
provider.
|
|
327
|
+
Throttled (`429`) and temporarily unavailable (`503`) exchanges are retried
|
|
328
|
+
within the timeout budget; a rejected one (`401`) fails at once.
|
|
331
329
|
"""
|
|
332
330
|
|
|
333
331
|
credential_id: str | None = None
|
|
334
|
-
credential_secret: str | None = field(default=None, repr=False)
|
|
335
332
|
token_url: str | None = None
|
|
336
333
|
token_type: str = "Bearer"
|
|
337
334
|
refresh_skew_seconds: int = 30
|
|
@@ -347,8 +344,6 @@ class RuntimeCredentialAuthProvider(BaseAuthProvider):
|
|
|
347
344
|
self.identity_token_file = identity_token_file_from_environment()
|
|
348
345
|
else:
|
|
349
346
|
self.identity_token_file = str(self.identity_token_file).strip() or None
|
|
350
|
-
if self.credential_secret is None and not self.identity_token_file:
|
|
351
|
-
self.credential_secret = os.getenv(RUNTIME_CREDENTIAL_SECRET_ENV)
|
|
352
347
|
if not self.token_url:
|
|
353
348
|
self.token_url = f"{API_ENDPOINT}/runtime-credentials/token/"
|
|
354
349
|
|
|
@@ -376,19 +371,11 @@ class RuntimeCredentialAuthProvider(BaseAuthProvider):
|
|
|
376
371
|
|
|
377
372
|
def _exchange(self) -> requests.Response:
|
|
378
373
|
credential_id = (self.credential_id or "").strip()
|
|
379
|
-
identity_token_file = self.identity_token_file
|
|
380
|
-
credential_secret = None if identity_token_file else (self.credential_secret or "").strip()
|
|
381
374
|
if not credential_id:
|
|
382
375
|
raise AuthError(
|
|
383
376
|
"MAINSEQUENCE_RUNTIME_CREDENTIAL_ID is required when "
|
|
384
377
|
"MAINSEQUENCE_AUTH_MODE=runtime_credential."
|
|
385
378
|
)
|
|
386
|
-
if not identity_token_file and not credential_secret:
|
|
387
|
-
raise AuthError(
|
|
388
|
-
"MAINSEQUENCE_RUNTIME_CREDENTIAL_SECRET is required when "
|
|
389
|
-
"MAINSEQUENCE_AUTH_MODE=runtime_credential and "
|
|
390
|
-
f"{RUNTIME_IDENTITY_TOKEN_FILE_ENV} is not set."
|
|
391
|
-
)
|
|
392
379
|
try:
|
|
393
380
|
# One budget for every attempt and wait, inside the caller's budget if any.
|
|
394
381
|
with request_budget(self.timeout) as budget:
|
|
@@ -405,8 +392,7 @@ class RuntimeCredentialAuthProvider(BaseAuthProvider):
|
|
|
405
392
|
return exchange_runtime_credential(
|
|
406
393
|
post,
|
|
407
394
|
credential_id=credential_id,
|
|
408
|
-
|
|
409
|
-
identity_token_file=identity_token_file,
|
|
395
|
+
identity_token_file=self.identity_token_file,
|
|
410
396
|
deadline=budget.deadline,
|
|
411
397
|
on_retry=self._log_exchange_retry,
|
|
412
398
|
)
|
|
@@ -25,7 +25,6 @@ from .instrumentation import OTelJSONRenderer
|
|
|
25
25
|
from .repository_identity_security import UNSUPPORTED_SOURCE_IDENTITY_ENV_NAMES
|
|
26
26
|
from .runtime_credential_exchange import (
|
|
27
27
|
RUNTIME_CREDENTIAL_ID_ENV,
|
|
28
|
-
RUNTIME_CREDENTIAL_SECRET_ENV,
|
|
29
28
|
exchange_runtime_credential,
|
|
30
29
|
identity_token_file_from_environment,
|
|
31
30
|
runtime_credential_configured,
|
|
@@ -104,7 +103,6 @@ def _request_job_startup_state(*, timeout_s: float = 10.0) -> dict[str, Any]:
|
|
|
104
103
|
def _exchange_runtime_credential() -> bool:
|
|
105
104
|
if not runtime_credential_configured():
|
|
106
105
|
return False
|
|
107
|
-
identity_token_file = identity_token_file_from_environment()
|
|
108
106
|
token_url = f"{_backend_base_url()}/api/v1/runtime-credentials/token/"
|
|
109
107
|
|
|
110
108
|
def _post(body: dict[str, str]) -> requests.Response:
|
|
@@ -120,12 +118,7 @@ def _request_job_startup_state(*, timeout_s: float = 10.0) -> dict[str, Any]:
|
|
|
120
118
|
token_resp = exchange_runtime_credential(
|
|
121
119
|
_post,
|
|
122
120
|
credential_id=(os.getenv(RUNTIME_CREDENTIAL_ID_ENV) or "").strip(),
|
|
123
|
-
|
|
124
|
-
None
|
|
125
|
-
if identity_token_file
|
|
126
|
-
else (os.getenv(RUNTIME_CREDENTIAL_SECRET_ENV) or "").strip()
|
|
127
|
-
),
|
|
128
|
-
identity_token_file=identity_token_file,
|
|
121
|
+
identity_token_file=identity_token_file_from_environment(),
|
|
129
122
|
deadline=time.monotonic() + timeout_s,
|
|
130
123
|
)
|
|
131
124
|
except Exception:
|
|
@@ -3,13 +3,11 @@ The runtime credential exchange, shared by the auth provider and the logger.
|
|
|
3
3
|
|
|
4
4
|
A runtime started with ``MAINSEQUENCE_AUTH_MODE=runtime_credential`` exchanges its
|
|
5
5
|
credential at ``POST /api/v1/runtime-credentials/token/`` for a short-lived access
|
|
6
|
-
token. The request carries ``credential_id`` and
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
unreadable or empty file is an error, never a fallback to the secret.
|
|
12
|
-
* ``credential_secret`` otherwise: ``MAINSEQUENCE_RUNTIME_CREDENTIAL_SECRET``.
|
|
6
|
+
token. The request carries exactly ``credential_id`` and ``workload_identity_token``:
|
|
7
|
+
the projected token read from the file named by
|
|
8
|
+
``MAINSEQUENCE_RUNTIME_IDENTITY_TOKEN_FILE`` for every exchange, because the file is
|
|
9
|
+
rotated. That token is the only proof. An unset variable and a missing, unreadable
|
|
10
|
+
or empty file are errors, raised instead of sending the request.
|
|
13
11
|
|
|
14
12
|
The token exists only inside one exchange request. It is not kept on any object,
|
|
15
13
|
written to the environment or a file, logged, or included in an error message.
|
|
@@ -29,7 +27,6 @@ from urllib3.exceptions import InvalidHeader
|
|
|
29
27
|
from urllib3.util import Retry
|
|
30
28
|
|
|
31
29
|
RUNTIME_CREDENTIAL_ID_ENV = "MAINSEQUENCE_RUNTIME_CREDENTIAL_ID"
|
|
32
|
-
RUNTIME_CREDENTIAL_SECRET_ENV = "MAINSEQUENCE_RUNTIME_CREDENTIAL_SECRET"
|
|
33
30
|
RUNTIME_IDENTITY_TOKEN_FILE_ENV = "MAINSEQUENCE_RUNTIME_IDENTITY_TOKEN_FILE"
|
|
34
31
|
|
|
35
32
|
# 429: the exchange is throttled. 503: verification is temporarily unavailable.
|
|
@@ -42,7 +39,7 @@ _RETRY_AFTER = Retry(total=0)
|
|
|
42
39
|
|
|
43
40
|
|
|
44
41
|
class RuntimeIdentityTokenError(Exception):
|
|
45
|
-
"""The
|
|
42
|
+
"""The identity token file is unset or cannot be used. The message never holds its content."""
|
|
46
43
|
|
|
47
44
|
|
|
48
45
|
def _environment_value(name: str) -> str:
|
|
@@ -58,18 +55,22 @@ def runtime_credential_configured() -> bool:
|
|
|
58
55
|
"""
|
|
59
56
|
Say whether the environment holds a runtime credential to exchange.
|
|
60
57
|
|
|
61
|
-
That is a credential ID and
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
That is a credential ID and the identity token file that proves it. Only the
|
|
59
|
+
presence of both settings is checked. The token file is read by the exchange
|
|
60
|
+
itself, which reports a missing or empty file.
|
|
64
61
|
"""
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return True
|
|
69
|
-
return bool(_environment_value(RUNTIME_CREDENTIAL_SECRET_ENV))
|
|
62
|
+
return bool(
|
|
63
|
+
_environment_value(RUNTIME_CREDENTIAL_ID_ENV) and identity_token_file_from_environment()
|
|
64
|
+
)
|
|
70
65
|
|
|
71
66
|
|
|
72
|
-
def _read_identity_token(path: str) -> str:
|
|
67
|
+
def _read_identity_token(path: str | None) -> str:
|
|
68
|
+
if not path:
|
|
69
|
+
raise RuntimeIdentityTokenError(
|
|
70
|
+
f"{RUNTIME_IDENTITY_TOKEN_FILE_ENV} is required when "
|
|
71
|
+
"MAINSEQUENCE_AUTH_MODE=runtime_credential: a runtime credential is exchanged "
|
|
72
|
+
"only with the projected workload identity token in that file."
|
|
73
|
+
)
|
|
73
74
|
source = f"The runtime identity token file {path} ({RUNTIME_IDENTITY_TOKEN_FILE_ENV})"
|
|
74
75
|
try:
|
|
75
76
|
with open(path, "rb") as token_file:
|
|
@@ -92,17 +93,11 @@ def _read_identity_token(path: str) -> str:
|
|
|
92
93
|
return token
|
|
93
94
|
|
|
94
95
|
|
|
95
|
-
def _request_body(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if identity_token_file:
|
|
101
|
-
return {
|
|
102
|
-
"credential_id": credential_id,
|
|
103
|
-
"workload_identity_token": _read_identity_token(identity_token_file),
|
|
104
|
-
}
|
|
105
|
-
return {"credential_id": credential_id, "credential_secret": credential_secret or ""}
|
|
96
|
+
def _request_body(credential_id: str, identity_token_file: str | None) -> dict[str, str]:
|
|
97
|
+
return {
|
|
98
|
+
"credential_id": credential_id,
|
|
99
|
+
"workload_identity_token": _read_identity_token(identity_token_file),
|
|
100
|
+
}
|
|
106
101
|
|
|
107
102
|
|
|
108
103
|
def _retry_delay(response: Any, attempt: int) -> float:
|
|
@@ -120,7 +115,6 @@ def exchange_runtime_credential(
|
|
|
120
115
|
send: Callable[[dict[str, str]], Any],
|
|
121
116
|
*,
|
|
122
117
|
credential_id: str,
|
|
123
|
-
credential_secret: str | None,
|
|
124
118
|
identity_token_file: str | None,
|
|
125
119
|
deadline: float,
|
|
126
120
|
on_retry: Callable[[int, float], None] | None = None,
|
|
@@ -135,8 +129,6 @@ def exchange_runtime_credential(
|
|
|
135
129
|
response. It is called once per attempt.
|
|
136
130
|
credential_id:
|
|
137
131
|
The runtime credential ID.
|
|
138
|
-
credential_secret:
|
|
139
|
-
The bootstrap secret. Ignored when ``identity_token_file`` is set.
|
|
140
132
|
identity_token_file:
|
|
141
133
|
The projected token file. It is read again for every attempt.
|
|
142
134
|
deadline:
|
|
@@ -147,12 +139,12 @@ def exchange_runtime_credential(
|
|
|
147
139
|
A ``429`` or ``503`` answer is retried up to ``EXCHANGE_RETRIES`` times. The
|
|
148
140
|
wait is the exponential backoff or the answer's ``Retry-After``, whichever is
|
|
149
141
|
longer; a wait that would not end before ``deadline`` ends the retries. Any
|
|
150
|
-
other answer, ``401`` included, is returned at once
|
|
151
|
-
|
|
152
|
-
be used.
|
|
142
|
+
other answer, ``401`` included, is returned at once. ``RuntimeIdentityTokenError``
|
|
143
|
+
is raised instead of sending an attempt when ``identity_token_file`` is not set
|
|
144
|
+
or the file cannot be used.
|
|
153
145
|
"""
|
|
154
146
|
for attempt in range(EXCHANGE_RETRIES + 1):
|
|
155
|
-
response = send(_request_body(credential_id,
|
|
147
|
+
response = send(_request_body(credential_id, identity_token_file))
|
|
156
148
|
status = response.status_code
|
|
157
149
|
if status not in RETRYABLE_EXCHANGE_STATUSES or attempt == EXCHANGE_RETRIES:
|
|
158
150
|
return response
|
|
@@ -16,7 +16,8 @@ import requests
|
|
|
16
16
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
|
|
17
17
|
from jwt import InvalidKeyError, InvalidTokenError
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
from mainsequence._request_identity import CALLER_ASSERTION_HEADER as ASSERTION_HEADER
|
|
20
|
+
|
|
20
21
|
ASSERTION_TYPE = "mainsequence-caller-assertion+jwt"
|
|
21
22
|
ASSERTION_ALGORITHM = "EdDSA"
|
|
22
23
|
MAX_ASSERTION_SECONDS = 300
|
|
@@ -6,11 +6,12 @@ import asyncio
|
|
|
6
6
|
import json
|
|
7
7
|
import os
|
|
8
8
|
import re
|
|
9
|
+
from contextlib import contextmanager
|
|
9
10
|
from uuid import UUID
|
|
10
11
|
|
|
11
12
|
import requests
|
|
12
13
|
|
|
13
|
-
from mainsequence._request_identity import _request_scope
|
|
14
|
+
from mainsequence._request_identity import _reads_as_caller, _request_scope
|
|
14
15
|
from mainsequence.client.models_user import RequestUserIdentity
|
|
15
16
|
|
|
16
17
|
from .caller_assertions import (
|
|
@@ -220,6 +221,7 @@ class _RequestIdentityMiddleware:
|
|
|
220
221
|
)
|
|
221
222
|
state["resource_release_uid"] = caller.release_uid
|
|
222
223
|
state["organization_environment_uid"] = caller.environment_uid
|
|
224
|
+
context.caller_assertion = assertion
|
|
223
225
|
else:
|
|
224
226
|
user = await asyncio.to_thread(_local_user, scope)
|
|
225
227
|
context.user = user
|
|
@@ -267,4 +269,19 @@ def install_request_identity(app):
|
|
|
267
269
|
)
|
|
268
270
|
|
|
269
271
|
|
|
270
|
-
|
|
272
|
+
@contextmanager
|
|
273
|
+
def reads_as_caller():
|
|
274
|
+
"""Read the directory as the request's caller instead of as the application.
|
|
275
|
+
|
|
276
|
+
Inside the block, ``User.filter``, ``User.get_by_uid``, ``Team.filter`` and
|
|
277
|
+
``Team.get_by_uid`` present the caller assertion the request arrived with,
|
|
278
|
+
and the platform answers with what the caller may see. No other call sends
|
|
279
|
+
it. Raises ``RequestIdentityError`` outside an authenticated request or when
|
|
280
|
+
the request carries no assertion (local mode, WebSockets); it never reads as
|
|
281
|
+
the application instead. See ADR-0036.
|
|
282
|
+
"""
|
|
283
|
+
with _reads_as_caller():
|
|
284
|
+
yield
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
__all__ = ["install_request_identity", "reads_as_caller"]
|