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_refres
(
cls,
*,
cluster: str | None = None,
timeout: float = 30.0,
auto_refresh: bool = True,
write_back: bool = True,
insecure: bool = False,
)
| 278 | |
| 279 | @classmethod |
| 280 | def from_active_cluster( |
| 281 | cls, |
| 282 | *, |
| 283 | cluster: str | None = None, |
| 284 | timeout: float = 30.0, |
| 285 | auto_refresh: bool = True, |
| 286 | write_back: bool = True, |
| 287 | insecure: bool = False, |
| 288 | ) -> SandboxClient: |
| 289 | """Construct a `SandboxClient` from the active gateway's on-disk state. |
| 290 | |
| 291 | Args: |
| 292 | cluster: explicit gateway name; otherwise reads |
| 293 | `$OPENSHELL_GATEWAY` or `~/.config/openshell/active_gateway`. |
| 294 | timeout: per-call gRPC timeout in seconds. |
| 295 | auto_refresh: when True (default) and the gateway uses OIDC, |
| 296 | lazily refresh the access token via the IdP's token endpoint |
| 297 | if the cached `oidc_token.json` is near expiry. Matches the |
| 298 | lazy-refresh patterns used by `google-auth` and `botocore`. |
| 299 | Set False to keep the SDK as a read-only consumer of the |
| 300 | CLI's cache (fail closed on expiry). |
| 301 | write_back: when True (default, and `auto_refresh=True`), |
| 302 | atomically persist refreshed bundles back to |
| 303 | `oidc_token.json` so other processes — including the |
| 304 | Rust CLI — see the rotation. Required for IdPs with |
| 305 | refresh-token rotation enabled (Keycloak, Entra in |
| 306 | strict mode): an in-memory-only refresh would leave the |
| 307 | on-disk `refresh_token` pointing at an invalidated |
| 308 | value, and any other process starting from that disk |
| 309 | state would fail on its first refresh. Set False only |
| 310 | when you know the SDK is the sole consumer of this |
| 311 | gateway directory. |
| 312 | insecure: when True, disables TLS certificate verification |
| 313 | for OIDC discovery and refresh calls. Mirrors the Rust |
| 314 | CLI's `--insecure` flag for issuers behind self-signed |
| 315 | certs. Off by default. |
| 316 | """ |
| 317 | cluster_name = cluster or _resolve_active_cluster() |
| 318 | gateway_dir = _xdg_config_home() / "openshell" / "gateways" / cluster_name |
| 319 | metadata_path = gateway_dir / "metadata.json" |
| 320 | try: |
| 321 | metadata = json.loads(metadata_path.read_text(encoding="utf-8")) |
| 322 | except FileNotFoundError: |
| 323 | raise SandboxError(f"gateway '{cluster_name}' not found") from None |
| 324 | if "gateway_endpoint" not in metadata: |
| 325 | raise SandboxError(f"gateway '{cluster_name}' metadata missing endpoint") |
| 326 | parsed = urlparse(metadata["gateway_endpoint"]) |
| 327 | host = parsed.hostname or "127.0.0.1" |
| 328 | port = parsed.port or (443 if parsed.scheme == "https" else 80) |
| 329 | endpoint = f"{host}:{port}" |
| 330 | |
| 331 | # TLS transport. Mirror crates/openshell-tui/src/lib.rs |
| 332 | # `build_oidc_channel` — for an https gateway, always build a |
| 333 | # secure channel and pick the strongest available trust profile. |
| 334 | tls: TlsConfig | None = None |
| 335 | if parsed.scheme == "https": |
| 336 | mtls_dir = gateway_dir / "mtls" |
| 337 | ca = mtls_dir / "ca.crt" if (mtls_dir / "ca.crt").exists() else None |