-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsandbox.py
More file actions
2390 lines (2140 loc) · 89 KB
/
Copy pathsandbox.py
File metadata and controls
2390 lines (2140 loc) · 89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import base64
import contextlib
import errno
import ipaddress
import json
import math
import os
import pathlib
import sys
import tempfile
import threading
import time
from collections import namedtuple
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Generic, Never, SupportsIndex, TypeVar, cast
from urllib.parse import urlparse
import grpc
import httpx
from ._proto import (
datamodel_pb2,
openshell_pb2,
openshell_pb2_grpc,
)
from .errors import _error_mapping_channel
from .mutations import DeletionOutcome, DeletionResult
_ClientCallDetailsBase = namedtuple(
"_ClientCallDetailsBase",
("method", "timeout", "metadata", "credentials", "wait_for_ready", "compression"),
)
_OAUTH_MAX_RESPONSE_BYTES = 1 << 20
T = TypeVar("T")
@dataclass(frozen=True)
class Page(Generic[T]):
"""One response page from a list operation."""
items: builtins.list[T]
next_page_token: str
class Pager(Generic[T]):
"""Lazy, single-pass iterator that fetches one RPC page per advance."""
def __init__(self, fetch: Callable[[str], Page[T]], page_token: str = "") -> None:
self._fetch = fetch
self._page_token: str | None = page_token
def __iter__(self) -> Pager[T]:
return self
def __next__(self) -> Page[T]:
if self._page_token is None:
raise StopIteration
page = self._fetch(self._page_token)
self._page_token = page.next_page_token or None
return page
def all(self) -> builtins.list[T]:
"""Consume the pager and collect every remaining item."""
return [item for page in self for item in page.items]
def _workspace_scope(workspace: str) -> datamodel_pb2.WorkspaceSelector:
if not workspace:
raise ValueError("workspace must be non-empty")
return datamodel_pb2.WorkspaceSelector(workspace=workspace)
def _all_workspaces_scope() -> datamodel_pb2.WorkspaceSelector:
return datamodel_pb2.WorkspaceSelector(all_workspaces=datamodel_pb2.AllWorkspaces())
def _service_exposure_messages(
exposures: Sequence[ServiceExposure] | None,
) -> list[openshell_pb2.SandboxServiceExposure]:
return [
openshell_pb2.SandboxServiceExposure(
service=exposure.service,
target_port=exposure.target_port,
)
for exposure in exposures or ()
]
class _ClientCallDetails(_ClientCallDetailsBase, grpc.ClientCallDetails):
pass
if TYPE_CHECKING:
import builtins
from collections.abc import Callable, Iterator, Mapping, Sequence
@dataclass(frozen=True)
class TlsConfig:
"""Channel TLS material.
All three fields are optional so callers can pick the trust profile:
- Full mTLS: pass all three (server trusts client identity).
- CA-only: pass `ca_path` (custom CA, no client identity).
- System roots: pass no fields (`TlsConfig()`) — uses the OS trust
store. Useful for OIDC gateways behind a public CA.
`cert_path` and `key_path` must be set together or not at all.
"""
ca_path: pathlib.Path | None = None
cert_path: pathlib.Path | None = None
key_path: pathlib.Path | None = None
def __post_init__(self) -> None:
if (self.cert_path is None) != (self.key_path is None):
raise ValueError("TlsConfig: cert_path and key_path must be set together")
class _BearerAuthInterceptor(
grpc.UnaryUnaryClientInterceptor,
grpc.UnaryStreamClientInterceptor,
grpc.StreamUnaryClientInterceptor,
grpc.StreamStreamClientInterceptor,
):
"""Add `authorization: Bearer <token>` to every outgoing RPC.
Implemented as an interceptor (not call credentials) so it works on
both plaintext and TLS channels without needing
`grpc.composite_channel_credentials`. The token provider is invoked
per call, so callers can swap tokens at runtime by mutating shared
state or returning a fresh value from the callable.
"""
def __init__(self, token_provider: Callable[[], str]) -> None:
self._token_provider = token_provider
def _attach(self, details: grpc.ClientCallDetails) -> grpc.ClientCallDetails:
original_metadata = getattr(details, "metadata", None)
metadata = list(original_metadata) if original_metadata else []
metadata.append(("authorization", f"Bearer {self._token_provider()}"))
return _ClientCallDetails(
getattr(details, "method", None),
getattr(details, "timeout", None),
metadata,
getattr(details, "credentials", None),
getattr(details, "wait_for_ready", None),
getattr(details, "compression", None),
)
def intercept_unary_unary(self, continuation, client_call_details, request):
return continuation(self._attach(client_call_details), request)
def intercept_unary_stream(self, continuation, client_call_details, request):
return continuation(self._attach(client_call_details), request)
def intercept_stream_unary(
self, continuation, client_call_details, request_iterator
):
return continuation(self._attach(client_call_details), request_iterator)
def intercept_stream_stream(
self, continuation, client_call_details, request_iterator
):
return continuation(self._attach(client_call_details), request_iterator)
def _normalize_bearer(
bearer: str | Callable[[], str] | None,
) -> Callable[[], str] | None:
if bearer is None:
return None
if callable(bearer):
return cast("Callable[[], str]", bearer)
token = bearer
return lambda: token
def _is_loopback_host(hostname: str | None) -> bool:
if hostname is None:
return False
if hostname.lower() == "localhost":
return True
with contextlib.suppress(ValueError):
return ipaddress.ip_address(hostname).is_loopback
return False
def _is_local_grpc_endpoint(endpoint: str) -> bool:
if endpoint.startswith("unix:"):
return True
return _is_loopback_host(urlparse(f"//{endpoint}").hostname)
def _validate_oauth_url(name: str, raw: str) -> str:
"""Validate an OAuth endpoint without reflecting attacker-controlled URLs."""
parsed = urlparse(raw)
if not parsed.scheme or not parsed.hostname or parsed.username or parsed.password:
raise SandboxError(f"invalid OAuth {name} URL")
if parsed.fragment:
raise SandboxError(f"OAuth {name} URL must not contain a fragment")
if parsed.scheme == "https":
return raw
if parsed.scheme == "http" and _is_loopback_host(parsed.hostname):
return raw
raise SandboxError(
f"OAuth {name} URL must use HTTPS (HTTP is allowed only for loopback hosts)"
)
def _oauth_json_request(
client: httpx.Client,
method: str,
url: str,
*,
kind: str,
headers: Mapping[str, str],
data: Mapping[str, str] | None = None,
) -> tuple[int, dict[str, object]]:
"""Read a bounded JSON response without retaining arbitrary error bodies."""
with client.stream(method, url, headers=headers, data=data) as response:
if response.status_code != 200:
return response.status_code, {}
content = bytearray()
for chunk in response.iter_bytes():
content.extend(chunk)
if len(content) > _OAUTH_MAX_RESPONSE_BYTES:
raise SandboxError(f"OAuth {kind} response is too large")
try:
payload = json.loads(content)
except (UnicodeDecodeError, json.JSONDecodeError):
raise SandboxError(f"OAuth {kind} returned invalid JSON") from None
if not isinstance(payload, dict):
raise SandboxError(f"OAuth {kind} returned invalid JSON")
return 200, payload
class ClientCredentialsAuth:
"""Renewable OAuth 2.0 client-credentials bearer provider.
Tokens and client secrets remain in memory. The first RPC lazily performs
discovery and an exchange; later RPCs share the cached token until it is
within 30 seconds of expiry. Concurrent callers share one exchange.
"""
def __init__(
self,
*,
client_secret: str | Callable[[], str],
issuer: str | None = None,
client_id: str | None = None,
scopes: Sequence[str] | None = None,
audience: str | None = None,
insecure: bool = False,
timeout: float = 30.0,
_transport: httpx.BaseTransport | None = None,
) -> None:
if not isinstance(client_secret, str) and not callable(client_secret):
raise TypeError("client_secret must be a string or zero-argument callable")
if isinstance(client_secret, str) and not client_secret:
raise SandboxError("OAuth client secret must not be empty")
self._secret = client_secret
self._issuer = issuer
self._client_id = client_id
self._scopes = tuple(scopes) if scopes is not None else None
self._audience = audience
self._insecure = insecure
self._timeout = timeout
self._transport = _transport
self._lock = threading.Lock()
self._access_token: str | None = None
self._expires_at = 0.0
self._token_endpoint: str | None = None
def __repr__(self) -> str:
return (
"ClientCredentialsAuth(issuer="
f"{self._issuer!r}, client_id={self._client_id!r}, "
f"scopes={self._scopes!r}, audience={self._audience!r})"
)
def _apply_gateway_metadata(
self, metadata: Mapping[str, object], *, insecure: bool = False
) -> None:
"""Fill omitted fields and apply active-gateway transport settings."""
if self._issuer is None:
value = metadata.get("oidc_issuer")
self._issuer = value if isinstance(value, str) and value else None
if self._client_id is None:
value = metadata.get("oidc_client_id")
self._client_id = value if isinstance(value, str) and value else None
if self._audience is None:
value = metadata.get("oidc_audience")
self._audience = value if isinstance(value, str) and value else None
if self._scopes is None:
value = metadata.get("oidc_scopes")
if isinstance(value, str):
self._scopes = tuple(value.split())
# Preserve an explicit provider-level opt-in while also honoring the
# active-gateway construction flag used by the existing OIDC refresher.
self._insecure = self._insecure or insecure
def __call__(self) -> str:
if (
self._access_token is not None
and time.time() + _OIDC_TOKEN_EXPIRY_GRACE_SECONDS < self._expires_at
):
return self._access_token
with self._lock:
if (
self._access_token is not None
and time.time() + _OIDC_TOKEN_EXPIRY_GRACE_SECONDS < self._expires_at
):
return self._access_token
token, expires_at = self._exchange()
self._access_token = token
self._expires_at = expires_at
return token
def _exchange(self) -> tuple[str, float]:
if not self._issuer or not self._client_id:
raise SandboxError("OAuth client credentials require issuer and client_id")
issuer = _validate_oauth_url("issuer", self._issuer).rstrip("/")
headers = {"accept": "application/json"}
try:
with httpx.Client(
verify=not self._insecure,
follow_redirects=False,
timeout=self._timeout,
transport=self._transport,
) as client:
if self._token_endpoint is None:
status, discovery = _oauth_json_request(
client,
"GET",
f"{issuer}/.well-known/openid-configuration",
kind="discovery",
headers=headers,
)
if status != 200:
raise SandboxError(f"OAuth discovery failed with HTTP {status}")
discovered = discovery.get("issuer")
if (
not isinstance(discovered, str)
or discovered.rstrip("/") != issuer
):
raise SandboxError("OAuth discovery issuer mismatch")
endpoint = discovery.get("token_endpoint")
if not isinstance(endpoint, str) or not endpoint:
raise SandboxError(
"OAuth discovery document is missing token_endpoint"
)
self._token_endpoint = _validate_oauth_url(
"token endpoint", endpoint
)
secret = self._resolve_secret()
data = {
"grant_type": "client_credentials",
"client_id": self._client_id,
"client_secret": secret,
}
if self._scopes:
data["scope"] = " ".join(self._scopes)
if self._audience:
data["audience"] = self._audience
status, payload = _oauth_json_request(
client,
"POST",
self._token_endpoint,
kind="client credentials response",
data=data,
headers=headers,
)
except SandboxError:
raise
except httpx.HTTPError:
raise SandboxError("OAuth client credentials request failed") from None
if status != 200:
raise SandboxError(
f"OAuth client credentials exchange failed with HTTP {status}"
)
access_token = payload.get("access_token")
expires_in = payload.get("expires_in")
if not isinstance(access_token, str) or not access_token:
raise SandboxError(
"OAuth client credentials response is missing access_token"
)
if (
isinstance(expires_in, bool)
or not isinstance(expires_in, (int, float))
or not math.isfinite(expires_in)
or expires_in <= 0
):
raise SandboxError(
"OAuth client credentials response requires a positive finite expires_in"
)
return access_token, time.time() + float(expires_in)
def _resolve_secret(self) -> str:
if isinstance(self._secret, str):
return self._secret
try:
value = self._secret()
except Exception:
raise SandboxError("OAuth client secret supplier failed") from None
if not isinstance(value, str) or not value:
raise SandboxError("OAuth client secret supplier returned an empty secret")
return value
@dataclass(frozen=True)
class SandboxStatusRef:
phase: int
current_policy_version: int
exit_code: int | None = None
@dataclass(frozen=True)
class ServiceExposure:
"""A loopback HTTP service to expose during sandbox creation."""
target_port: int
service: str = ""
class _ImmutableLabels(dict[str, str]):
"""A read-only, copy- and pickle-safe label mapping."""
def _deny_mutation(self, *args: object, **kwargs: object) -> Never:
del args, kwargs
raise TypeError("sandbox labels are immutable")
__setitem__ = _deny_mutation
__delitem__ = _deny_mutation
clear = _deny_mutation
pop = _deny_mutation
popitem = _deny_mutation
setdefault = _deny_mutation
update = _deny_mutation
__ior__ = _deny_mutation
def __deepcopy__(self, memo: dict[int, object]) -> _ImmutableLabels:
del memo
return type(self)(self)
def __reduce_ex__(self, protocol: SupportsIndex, /) -> str | tuple[Any, ...]:
del protocol
return type(self), (dict(self),)
@dataclass(frozen=True)
class SandboxWorkloadTemplateProvenanceRef:
name: str
resource_version: str
@dataclass(frozen=True)
class SandboxRef:
id: str
name: str
workspace: str
status: SandboxStatusRef
# Excluded from equality/hash to preserve the original identity while the
# immutable mapping remains safe for deepcopy, pickle, and asdict.
labels: Mapping[str, str] = field(default_factory=_ImmutableLabels, compare=False)
created_from_workload_template: SandboxWorkloadTemplateProvenanceRef | None = None
# Populated by create operations. The empty key identifies the unnamed service.
service_urls: Mapping[str, str] = field(
default_factory=_ImmutableLabels, compare=False
)
def __post_init__(self) -> None:
object.__setattr__(self, "labels", _ImmutableLabels(self.labels))
object.__setattr__(self, "service_urls", _ImmutableLabels(self.service_urls))
@property
def phase(self) -> int:
return self.status.phase
@property
def current_policy_version(self) -> int:
return self.status.current_policy_version
@dataclass(frozen=True)
class ExecChunk:
stream: str
data: bytes
@dataclass(frozen=True)
class ExecResult:
exit_code: int
stdout: str
stderr: str
class SandboxError(RuntimeError):
pass
class SandboxSession:
def __init__(self, client: SandboxClient, sandbox: SandboxRef) -> None:
self._client = client
self.sandbox = sandbox
self._workspace = sandbox.workspace
@property
def id(self) -> str:
return self.sandbox.id
def exec(
self,
command: Sequence[str],
*,
stream_output: bool = False,
workdir: str | None = None,
env: Mapping[str, str] | None = None,
stdin: bytes | None = None,
timeout_seconds: int | None = None,
no_login_shell: bool = False,
) -> ExecResult:
return self._client.exec(
self.sandbox.name,
command,
workspace=self._workspace,
stream_output=stream_output,
workdir=workdir,
env=env,
stdin=stdin,
timeout_seconds=timeout_seconds,
no_login_shell=no_login_shell,
)
def exec_python(
self,
function: Callable[..., object],
*,
args: Sequence[object] = (),
kwargs: Mapping[str, object] | None = None,
stream_output: bool = False,
workdir: str | None = None,
env: Mapping[str, str] | None = None,
timeout_seconds: int | None = None,
) -> ExecResult:
return self._client.exec_python(
self.sandbox.name,
function,
workspace=self._workspace,
args=args,
kwargs=kwargs,
stream_output=stream_output,
workdir=workdir,
env=env,
timeout_seconds=timeout_seconds,
)
def delete(self, *, allow_missing: bool = False) -> DeletionResult:
return self._client.delete(
self.sandbox.name, workspace=self._workspace, allow_missing=allow_missing
)
def stop(self) -> SandboxRef:
self.sandbox = self._client.stop(self.sandbox.name, workspace=self._workspace)
return self.sandbox
def start(self) -> SandboxRef:
self.sandbox = self._client.start(self.sandbox.name, workspace=self._workspace)
return self.sandbox
class SandboxClient:
"""gRPC client for sandbox CRUD and command execution."""
def __init__(
self,
endpoint: str,
*,
tls: TlsConfig | None = None,
bearer_token: str | Callable[[], str] | None = None,
client_credentials: ClientCredentialsAuth | None = None,
timeout: float = 30.0,
cluster_name: str | None = None,
_bearer_close: Callable[[], None] | None = None,
) -> None:
"""Create a SandboxClient.
Args:
endpoint: host:port for the gateway gRPC service.
tls: mTLS material. None for a plaintext channel.
bearer_token: OIDC access token, or a zero-arg callable
returning the current token (called per RPC; supports
runtime refresh). Combines with `tls` — pass both when
the gateway uses mTLS for transport identity and OIDC
for user identity.
client_credentials: renewable OAuth client-credentials provider.
Mutually exclusive with `bearer_token`. A non-loopback endpoint
requires `tls` so the acquired bearer is never sent in cleartext.
timeout: default per-call timeout in seconds.
cluster_name: optional friendly name for error messages.
_bearer_close: internal — wired by `from_active_cluster`
when an `_OidcRefresher` owns the bearer callable, so
`close()` can release the refresher's HTTP client.
Public callers should not pass this; they own the
lifecycle of any callable they supplied as
`bearer_token`.
"""
if bearer_token is not None and client_credentials is not None:
raise SandboxError(
"bearer_token and client_credentials are mutually exclusive"
)
if (
client_credentials is not None
and tls is None
and not _is_local_grpc_endpoint(endpoint)
):
raise SandboxError(
"OAuth client credentials require TLS for non-loopback gateway endpoints"
)
self._endpoint = endpoint
self._timeout = timeout
self._cluster_name = cluster_name
self._bearer_close = _bearer_close
if tls is None:
self._channel = grpc.insecure_channel(endpoint)
else:
# Build credentials from whatever subset of mTLS material the
# caller supplied. None for `root_certificates` makes gRPC use
# the system trust store, which is what we want for OIDC
# gateways behind a public CA.
credentials = grpc.ssl_channel_credentials(
root_certificates=(tls.ca_path.read_bytes() if tls.ca_path else None),
private_key=(tls.key_path.read_bytes() if tls.key_path else None),
certificate_chain=(
tls.cert_path.read_bytes() if tls.cert_path else None
),
)
self._channel = grpc.secure_channel(endpoint, credentials)
provider = _normalize_bearer(client_credentials or bearer_token)
if provider is not None:
self._channel = grpc.intercept_channel(
self._channel,
_BearerAuthInterceptor(provider),
)
self._stub = openshell_pb2_grpc.OpenShellStub(
_error_mapping_channel(self._channel)
)
@classmethod
def from_active_cluster(
cls,
*,
cluster: str | None = None,
timeout: float = 30.0,
auto_refresh: bool = True,
write_back: bool = True,
insecure: bool = False,
client_credentials: ClientCredentialsAuth | None = None,
) -> SandboxClient:
"""Construct a `SandboxClient` from the active gateway's on-disk state.
Args:
cluster: explicit gateway name; otherwise reads
`$OPENSHELL_GATEWAY` or `~/.config/openshell/active_gateway`.
timeout: per-call gRPC timeout in seconds.
auto_refresh: when True (default) and the gateway uses OIDC,
lazily refresh the access token via the IdP's token endpoint
if the cached `oidc_token.json` is near expiry. Matches the
lazy-refresh patterns used by `google-auth` and `botocore`.
Set False to keep the SDK as a read-only consumer of the
CLI's cache (fail closed on expiry).
write_back: when True (default, and `auto_refresh=True`),
atomically persist refreshed bundles back to
`oidc_token.json` so other processes — including the
Rust CLI — see the rotation. Required for IdPs with
refresh-token rotation enabled (Keycloak, Entra in
strict mode): an in-memory-only refresh would leave the
on-disk `refresh_token` pointing at an invalidated
value, and any other process starting from that disk
state would fail on its first refresh. Set False only
when you know the SDK is the sole consumer of this
gateway directory.
insecure: when True, disables TLS certificate verification
for OIDC discovery and refresh calls. Mirrors the Rust
CLI's `--insecure` flag for issuers behind self-signed
certs. Off by default.
client_credentials: renewable OAuth client-credentials provider.
Omitted issuer, client ID, audience, and scopes are filled from
the registered gateway metadata. This provider does not read or
write `oidc_token.json`. Remote plaintext gateways are rejected.
"""
cluster_name = cluster or _resolve_active_cluster()
gateway_dir = _xdg_config_home() / "openshell" / "gateways" / cluster_name
metadata_path = gateway_dir / "metadata.json"
try:
metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
except FileNotFoundError:
raise SandboxError(f"gateway '{cluster_name}' not found") from None
if "gateway_endpoint" not in metadata:
raise SandboxError(f"gateway '{cluster_name}' metadata missing endpoint")
parsed = urlparse(metadata["gateway_endpoint"])
host = parsed.hostname or "127.0.0.1"
port = parsed.port or (443 if parsed.scheme == "https" else 80)
endpoint = f"{host}:{port}"
# TLS transport. Mirror crates/openshell-tui/src/lib.rs
# `build_oidc_channel` — for an https gateway, always build a
# secure channel and pick the strongest available trust profile.
tls: TlsConfig | None = None
if parsed.scheme == "https":
mtls_dir = gateway_dir / "mtls"
ca = mtls_dir / "ca.crt" if (mtls_dir / "ca.crt").exists() else None
cert = mtls_dir / "tls.crt" if (mtls_dir / "tls.crt").exists() else None
key = mtls_dir / "tls.key" if (mtls_dir / "tls.key").exists() else None
if ca is not None and cert is not None and key is not None:
# Full mTLS.
tls = TlsConfig(ca_path=ca, cert_path=cert, key_path=key)
elif ca is not None:
# CA-only trust (no client identity).
tls = TlsConfig(ca_path=ca)
else:
# System roots (e.g. OIDC gateway behind a public CA).
tls = TlsConfig()
# OIDC bearer. Mirror the Rust CLI/TUI: the gateway metadata's
# `auth_mode` is authoritative — a stale oidc_token.json next to
# a non-OIDC gateway should NOT cause us to attach a bearer.
bearer_token: Callable[[], str] | None = None
bearer_close: Callable[[], None] | None = None
if client_credentials is not None:
if metadata.get("auth_mode") != "oidc":
raise SandboxError(
f"gateway '{cluster_name}' is not configured for OIDC"
)
client_credentials._apply_gateway_metadata(metadata, insecure=insecure)
elif metadata.get("auth_mode") == "oidc":
bearer_token, bearer_close = _make_cluster_bearer_provider(
gateway_dir,
cluster_name,
auto_refresh=auto_refresh,
write_back=write_back,
insecure=insecure,
)
return cls(
endpoint,
tls=tls,
bearer_token=bearer_token,
client_credentials=client_credentials,
timeout=timeout,
cluster_name=cluster_name,
_bearer_close=bearer_close,
)
def close(self) -> None:
"""Release the gRPC channel and any bearer-auth resources.
Idempotent. If `from_active_cluster` wired up an OIDC refresher
for this client, the refresher's underlying httpx.Client is
closed here too — otherwise long-lived services that churn
clients would leak sockets / file descriptors until GC.
"""
self._channel.close()
if self._bearer_close is not None:
with contextlib.suppress(Exception):
self._bearer_close()
self._bearer_close = None
def __enter__(self) -> SandboxClient:
return self
def __exit__(self, *args: object) -> None:
self.close()
def health(self) -> openshell_pb2.HealthResponse:
return self._stub.Health(openshell_pb2.HealthRequest(), timeout=self._timeout)
def create(
self,
*,
workspace: str,
spec: openshell_pb2.SandboxSpec | None = None,
name: str | None = None,
labels: Mapping[str, str] | None = None,
service_exposures: Sequence[ServiceExposure] | None = None,
) -> SandboxRef:
request_spec = spec if spec is not None else _default_spec()
response = self._stub.CreateSandbox(
openshell_pb2.CreateSandboxRequest(
workspace_scope=_workspace_scope(workspace),
spec=request_spec,
name=name or "",
labels=dict(labels) if labels else {},
service_exposures=_service_exposure_messages(service_exposures),
),
timeout=self._timeout,
)
sandbox_ref = _sandbox_ref(response.sandbox, response.service_urls)
if sandbox_ref.id == "":
raise SandboxError("CreateSandbox returned empty sandbox id")
return sandbox_ref
def create_from_template(
self,
*,
workspace: str,
workload_template: str,
spec: openshell_pb2.SandboxSpec | None = None,
name: str | None = None,
labels: Mapping[str, str] | None = None,
service_exposures: Sequence[ServiceExposure] | None = None,
) -> SandboxRef:
if not workload_template.strip():
raise SandboxError("workload_template is required")
request_spec = spec if spec is not None else openshell_pb2.SandboxSpec()
response = self._stub.CreateSandbox(
openshell_pb2.CreateSandboxRequest(
workspace_scope=_workspace_scope(workspace),
spec=request_spec,
name=name or "",
labels=dict(labels) if labels else {},
workload_template=workload_template,
service_exposures=_service_exposure_messages(service_exposures),
),
timeout=self._timeout,
)
sandbox_ref = _sandbox_ref(response.sandbox, response.service_urls)
if sandbox_ref.id == "":
raise SandboxError("CreateSandbox returned empty sandbox id")
return sandbox_ref
def create_session(
self,
*,
workspace: str,
spec: openshell_pb2.SandboxSpec | None = None,
name: str | None = None,
labels: Mapping[str, str] | None = None,
service_exposures: Sequence[ServiceExposure] | None = None,
) -> SandboxSession:
return SandboxSession(
self,
self.create(
workspace=workspace,
spec=spec,
name=name,
labels=labels,
service_exposures=service_exposures,
),
)
def create_session_from_template(
self,
*,
workspace: str,
workload_template: str,
spec: openshell_pb2.SandboxSpec | None = None,
name: str | None = None,
labels: Mapping[str, str] | None = None,
service_exposures: Sequence[ServiceExposure] | None = None,
) -> SandboxSession:
return SandboxSession(
self,
self.create_from_template(
workspace=workspace,
workload_template=workload_template,
spec=spec,
name=name,
labels=labels,
service_exposures=service_exposures,
),
)
def sandbox_templates(self) -> SandboxTemplateClient:
return SandboxTemplateClient(self._channel, timeout=self._timeout)
def get(self, name: str, *, workspace: str) -> SandboxRef:
response = self._stub.GetSandbox(
openshell_pb2.GetSandboxRequest(
workspace_scope=_workspace_scope(workspace),
name=name,
),
timeout=self._timeout,
)
return _sandbox_ref(response.sandbox)
def get_session(self, name: str, *, workspace: str) -> SandboxSession:
return SandboxSession(self, self.get(name, workspace=workspace))
def list(
self,
*,
workspace: str,
page_size: int = 100,
page_token: str = "",
label_selector: str | None = None,
) -> Pager[SandboxRef]:
def fetch(token: str) -> Page[SandboxRef]:
response = self._stub.ListSandboxes(
openshell_pb2.ListSandboxesRequest(
workspace_scope=_workspace_scope(workspace),
page_size=page_size,
page_token=token,
label_selector=label_selector or "",
),
timeout=self._timeout,
)
return Page(
items=[_sandbox_ref(item) for item in response.sandboxes],
next_page_token=getattr(response, "next_page_token", ""),
)
return Pager(fetch, page_token)
def list_all(
self,
*,
workspace: str,
page_size: int = 100,
page_token: str = "",
label_selector: str | None = None,
) -> builtins.list[SandboxRef]:
return self.list(
workspace=workspace,
page_size=page_size,
page_token=page_token,
label_selector=label_selector,
).all()
def list_for_all_workspaces(
self,
*,
page_size: int = 100,
page_token: str = "",
label_selector: str | None = None,
) -> Pager[SandboxRef]:
def fetch(token: str) -> Page[SandboxRef]:
response = self._stub.ListSandboxes(
openshell_pb2.ListSandboxesRequest(
workspace_scope=_all_workspaces_scope(),
page_size=page_size,
page_token=token,
label_selector=label_selector or "",
),
timeout=self._timeout,
)
return Page(
items=[_sandbox_ref(item) for item in response.sandboxes],
next_page_token=getattr(response, "next_page_token", ""),
)
return Pager(fetch, page_token)
def list_all_for_all_workspaces(
self,
*,
page_size: int = 100,
page_token: str = "",
label_selector: str | None = None,
) -> builtins.list[SandboxRef]:
return self.list_for_all_workspaces(
page_size=page_size,
page_token=page_token,
label_selector=label_selector,
).all()
def list_ids(
self,
*,
workspace: str,
page_size: int = 100,
label_selector: str | None = None,
) -> builtins.list[str]:
return [
item.id
for item in self.list_all(
workspace=workspace,
page_size=page_size,
label_selector=label_selector,
)
]
def list_ids_for_all_workspaces(
self,
*,
page_size: int = 100,
label_selector: str | None = None,
) -> builtins.list[str]:
return [
item.id
for item in self.list_all_for_all_workspaces(
page_size=page_size,
label_selector=label_selector,