Config#
Snowflake connection resolution, credential deployment, OAuth flow, and token proxy.
Connection Resolver#
inspect_coco.config.connection
#
Snowflake connection resolution from existing TOML configuration files.
SnowflakeConnectionConfig(account, user, host, role=None, warehouse=None, database=None, schema=None, private_key_path=None, token=None, oauth_access_token=None)
dataclass
#
Resolved Snowflake connection configuration.
ConnectionResolutionError
#
Bases: Exception
Raised when a Snowflake connection cannot be resolved.
snowflake_home()
#
Return the Snowflake config directory, honouring SNOWFLAKE_HOME.
resolve_connection(connection_name=None)
#
Resolve a Snowflake connection from existing TOML files.
Resolution order for connection name
- Explicit connection_name parameter
- INSPECT_COCO_SNOWFLAKE_CONNECTION environment variable
- default_connection_name field in TOML file
- Fallback to "default"
File lookup order (using SNOWFLAKE_HOME or ~/.snowflake): 1. connections.toml (newer format) — top-level [connection_name] 2. config.toml (older format) — nested [connections.connection_name]
Source code in src/inspect_coco/config/connection.py
OAuth#
inspect_coco.config.oauth
#
Snowflake Local OAuth flow using SNOWFLAKE$LOCAL_APPLICATION.
Implements the OAuth Authorization Code flow with PKCE for local development. The host machine handles the browser interaction and token management. Containers receive only short-lived access tokens via the token proxy.
OAuthTokens(access_token, refresh_token, expires_at, account, role=None)
dataclass
#
Cached OAuth tokens.
PkceCodes(verifier, challenge)
dataclass
#
PKCE verifier and challenge pair.
OAuthError
#
Bases: Exception
Raised when OAuth authorization fails.
generate_pkce()
#
Generate a PKCE code verifier and S256 challenge.
Source code in src/inspect_coco/config/oauth.py
load_cached_tokens(account=None)
#
Load cached tokens from OS keyring (preferred) or file fallback.
Source code in src/inspect_coco/config/oauth.py
save_cached_tokens(tokens)
#
Save tokens to OS keyring (preferred) or file fallback.
Source code in src/inspect_coco/config/oauth.py
clear_cached_tokens(account=None)
#
Remove cached tokens. Returns True if tokens were found and removed.
exchange_code_for_tokens(account, code, pkce, redirect_uri)
#
Exchange an authorization code for access and refresh tokens.
Source code in src/inspect_coco/config/oauth.py
refresh_access_token(account, refresh_token)
#
Use a refresh token to obtain a new access token.
Source code in src/inspect_coco/config/oauth.py
get_valid_token(tokens)
#
Return tokens with a valid (non-expired) access token, refreshing if needed.
Source code in src/inspect_coco/config/oauth.py
authorize(account, role=None)
#
Run the full OAuth authorization code flow with PKCE.
Opens the user's browser to Snowflake's authorize endpoint, captures the callback on localhost, exchanges the code for tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
account
|
str
|
Snowflake account identifier (e.g., "myorg-myaccount"). |
required |
role
|
str | None
|
Optional Snowflake role to request. |
None
|
Returns:
| Type | Description |
|---|---|
OAuthTokens
|
OAuthTokens with access and refresh tokens. |
Raises:
| Type | Description |
|---|---|
OAuthError
|
If authorization fails or times out. |
Source code in src/inspect_coco/config/oauth.py
Credential Deployer#
inspect_coco.config.deployer
#
Deploy Snowflake credentials into a Docker container.
SandboxExec
#
Bases: Protocol
Protocol for executing commands in a sandbox container.
ExecResult(returncode=0, stdout='', stderr='')
#
deploy_credentials(config, exec_fn, connection_name='default')
async
#
Deploy Snowflake credentials into a Docker sandbox container.
Performs
- Creates ~/.snowflake/cortex directory structure
- Deploys private key (JWT) via base64 transport + chmod 0600
- Generates config.toml with correct authenticator
- Generates cortex settings.json
- Returns env vars dict for subsequent exec() calls
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SnowflakeConnectionConfig
|
Resolved Snowflake connection configuration. |
required |
exec_fn
|
SandboxExec
|
Callable to execute commands in the sandbox. |
required |
connection_name
|
str
|
Name for the connection entry in config.toml. |
'default'
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict of environment variables to pass to subsequent exec() calls. |
Source code in src/inspect_coco/config/deployer.py
Token Proxy#
inspect_coco.proxy.server
#
OAuth token proxy server (host-process mode).
A lightweight HTTP server that runs as a thread in the inspect-coco process, serves short-lived access tokens to Docker sandbox containers via extra_hosts (host-gateway). Reads refresh tokens from the OS keyring, handles automatic refresh, and triggers browser re-auth if needed.
The proxy binds to 127.0.0.1 on a random available port. The assigned port is passed to Docker compose via TOKEN_PROXY_PORT env var.
TokenState(account, role=None)
#
Thread-safe token state backed by keyring.
Source code in src/inspect_coco/proxy/server.py
get_access_token()
#
Return a valid access token, refreshing if needed.
Source code in src/inspect_coco/proxy/server.py
TokenProxyError
#
Bases: Exception
Raised when the proxy cannot serve a token.
ProxyHandler
#
Bases: BaseHTTPRequestHandler
HTTP handler serving token and health endpoints.