gRPC client for sandbox CRUD and command execution.
| 218 | |
| 219 | |
| 220 | class SandboxClient: |
| 221 | """gRPC client for sandbox CRUD and command execution.""" |
| 222 | |
| 223 | def __init__( |
| 224 | self, |
| 225 | endpoint: str, |
| 226 | *, |
| 227 | tls: TlsConfig | None = None, |
| 228 | bearer_token: str | Callable[[], str] | None = None, |
| 229 | timeout: float = 30.0, |
| 230 | cluster_name: str | None = None, |
| 231 | _bearer_close: Callable[[], None] | None = None, |
| 232 | ) -> None: |
| 233 | """Create a SandboxClient. |
| 234 | |
| 235 | Args: |
| 236 | endpoint: host:port for the gateway gRPC service. |
| 237 | tls: mTLS material. None for a plaintext channel. |
| 238 | bearer_token: OIDC access token, or a zero-arg callable |
| 239 | returning the current token (called per RPC; supports |
| 240 | runtime refresh). Combines with `tls` — pass both when |
| 241 | the gateway uses mTLS for transport identity and OIDC |
| 242 | for user identity. |
| 243 | timeout: default per-call timeout in seconds. |
| 244 | cluster_name: optional friendly name for error messages. |
| 245 | _bearer_close: internal — wired by `from_active_cluster` |
| 246 | when an `_OidcRefresher` owns the bearer callable, so |
| 247 | `close()` can release the refresher's HTTP client. |
| 248 | Public callers should not pass this; they own the |
| 249 | lifecycle of any callable they supplied as |
| 250 | `bearer_token`. |
| 251 | """ |
| 252 | self._endpoint = endpoint |
| 253 | self._timeout = timeout |
| 254 | self._cluster_name = cluster_name |
| 255 | self._bearer_close = _bearer_close |
| 256 | if tls is None: |
| 257 | self._channel = grpc.insecure_channel(endpoint) |
| 258 | else: |
| 259 | # Build credentials from whatever subset of mTLS material the |
| 260 | # caller supplied. None for `root_certificates` makes gRPC use |
| 261 | # the system trust store, which is what we want for OIDC |
| 262 | # gateways behind a public CA. |
| 263 | credentials = grpc.ssl_channel_credentials( |
| 264 | root_certificates=(tls.ca_path.read_bytes() if tls.ca_path else None), |
| 265 | private_key=(tls.key_path.read_bytes() if tls.key_path else None), |
| 266 | certificate_chain=( |
| 267 | tls.cert_path.read_bytes() if tls.cert_path else None |
| 268 | ), |
| 269 | ) |
| 270 | self._channel = grpc.secure_channel(endpoint, credentials) |
| 271 | provider = _normalize_bearer(bearer_token) |
| 272 | if provider is not None: |
| 273 | self._channel = grpc.intercept_channel( |
| 274 | self._channel, |
| 275 | _BearerAuthInterceptor(provider), |
| 276 | ) |
| 277 | self._stub = openshell_pb2_grpc.OpenShellStub(self._channel) |
no outgoing calls