MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / _make_cluster_bearer_provider

Function _make_cluster_bearer_provider

python/openshell/sandbox.py:1316–1368  ·  view source on GitHub ↗

Build a per-RPC token provider for a gateway directory. Returns `(token_provider, close_fn_or_none)`. `close_fn` is non-None only when an `_OidcRefresher` was constructed; callers that own the provider's lifecycle (e.g. `SandboxClient.close()`) should invoke it during teardown so th

(
    gateway_dir: pathlib.Path,
    cluster_name: str,
    *,
    auto_refresh: bool = True,
    write_back: bool = True,
    insecure: bool = False,
)

Source from the content-addressed store, hash-verified

1314
1315
1316def _make_cluster_bearer_provider(
1317 gateway_dir: pathlib.Path,
1318 cluster_name: str,
1319 *,
1320 auto_refresh: bool = True,
1321 write_back: bool = True,
1322 insecure: bool = False,
1323) -> tuple[Callable[[], str], Callable[[], None] | None]:
1324 """Build a per-RPC token provider for a gateway directory.
1325
1326 Returns `(token_provider, close_fn_or_none)`. `close_fn` is non-None
1327 only when an `_OidcRefresher` was constructed; callers that own the
1328 provider's lifecycle (e.g. `SandboxClient.close()`) should invoke
1329 it during teardown so the underlying httpx.Client is released
1330 rather than relying on `__del__`.
1331
1332 With `auto_refresh=True` (the default), returns an `_OidcRefresher`-
1333 backed callable that lazily refreshes against the IdP's token endpoint
1334 when the cached bundle is stale. This mirrors the lazy-refresh pattern
1335 used by `google.oauth2.credentials.Credentials` and
1336 `botocore.tokens.SSOTokenProvider` and lets long-running scripts
1337 survive token rotation without intervention.
1338
1339 With `auto_refresh=False`, falls back to the read-only / fail-closed
1340 behavior: the SDK consumes whatever the CLI most recently wrote and
1341 raises `SandboxError` when the token expires. Useful for tests or
1342 callers that don't want the SDK to make outbound HTTP calls to the
1343 IdP. No close_fn is returned in this case.
1344
1345 `write_back=True` (only meaningful when `auto_refresh=True`) makes the
1346 refresher atomically persist the rotated bundle back to
1347 `oidc_token.json` so other processes — including the Rust CLI — see
1348 the new token. Defaults to True because OIDC providers with
1349 refresh-token rotation (Keycloak, Entra) invalidate the old
1350 refresh_token on rotation; an in-memory-only refresh would leave the
1351 on-disk bundle pointing at an invalidated value, and any other
1352 process starting from that disk state would fail on its first
1353 refresh.
1354
1355 `insecure=True` disables TLS certificate verification for both the
1356 OIDC discovery document fetch and the refresh-token POST. Mirrors
1357 the Rust CLI's `--insecure` flag for OIDC issuers behind self-signed
1358 certs.
1359 """
1360 if not auto_refresh:
1361 return _make_fail_closed_bearer_provider(gateway_dir, cluster_name), None
1362 refresher = _OidcRefresher(
1363 gateway_dir,
1364 cluster_name,
1365 write_back=write_back,
1366 insecure=insecure,
1367 )
1368 return refresher.current_access_token, refresher.close
1369
1370
1371def _resolve_active_cluster() -> str:

Calls 2

_OidcRefresherClass · 0.85