Credentials¶
A sandbox reaches services with credentials it never holds: the program pushes
them to the daemon through the socket, and the proxy of the sandbox writes
them into the requests that go to their hosts. Inside, the sandbox only sees
placeholders. This page covers custom secrets, the proxied keys that give an
agent an API key of the program's own, the credentials of the services sbx
knows, and their updates.
import os
import fastsandbox
from fastsandbox.models import CredentialsUpdate, CustomSecret
async def main() -> None:
client = await fastsandbox.connect()
async with client.create(kit="shell") as sandbox, sandbox.running():
# The sandbox sees the placeholder; the proxy sends the value to the
# hosts of the secret only.
secret = CustomSecret.new(
["api.example.com"],
sentinel="example-placeholder",
value=os.environ.get("EXAMPLE_TOKEN", "dummy-token"),
)
print(f"as a log prints it: {secret.value}")
await sandbox.push_credentials(CredentialsUpdate().with_custom_secret(secret))
# A credential of a service `sbx` knows, as `sbx secret set` gives it.
await sandbox.push_service_secret("github", os.environ.get("GH_TOKEN", "dummy"))
print(f"recorded: {sorted((await sandbox.inspect()).credential_sources)}")
await sandbox.remove_service_secret("github")
# An empty list removes every custom secret of the sandbox.
await sandbox.push_credentials(CredentialsUpdate().without_custom_secrets())
if __name__ == "__main__":
fastsandbox.run(main())
Custom secrets¶
A CustomSecret is a value, the hosts it is
for, and a placeholder:
CustomSecret.new(targets, sentinel, value, env_name=None). The program in
the sandbox puts the placeholder where the credential goes, in a header for
instance; the proxy replaces it with the value in the requests to the
targets, and leaves it as it is in requests to any other host.
- The value is a Pydantic
SecretStr: a log, arepr()or a JSON dump shows it masked, and only the request sent to the daemon carries it. A program that holds its secret as aSecretStralready gives it as it is, never exposed in between; so doProxiedKey, theKeysof a sandbox spec and the credentials of services. - The sandbox holds no value: not in its environment, not in its files.
env_nameis recorded with the secret, but no such variable is set in the environment of a command. - The secrets stay with the sandbox through a stop and a start, and go with it when it is deleted.
sandbox.push_credentials(update)
sends a CredentialsUpdate. Its
custom secrets replace those of the sandbox, with one exception:
| Update | Custom secrets of the sandbox |
|---|---|
CredentialsUpdate().with_custom_secret(secret) |
replaced by the list of the update |
CredentialsUpdate(), or one with service credentials only |
left as they are (custom_secrets is None) |
CredentialsUpdate().without_custom_secrets() |
all removed |
Proxied keys¶
A ProxiedKey is the custom secret of an API key,
made by docker-sandbox, whose value never comes back to Python: the key of
the program's own, for the hosts it names, or for those the kits of a package
declare for a service. It is the way to give an agent its key.
import os
import fastsandbox
from fastsandbox import ProxiedKey
async def main() -> None:
client = await fastsandbox.connect()
package = await client.package("claude")
# For the hosts the kits of the package inject the key of `anthropic` into.
key = ProxiedKey.for_service(
package, "anthropic", "ANTHROPIC_API_KEY", os.environ["ANTHROPIC_API_KEY"]
)
print(f"{key.env} is written for {', '.join(key.hosts)}")
# The sandbox holds the placeholder, in the variable the agent reads its key from.
request = package.request(fastsandbox.unique_name()).with_proxied_key(key)
async with client.create(kit=package, request=request) as sandbox, sandbox.running():
# The proxy writes the key in place of the placeholder; its value never comes back.
await sandbox.push_proxied_keys(key)
print(f"{sandbox.name} holds {key.placeholder[:7]}…, never the key")
if __name__ == "__main__":
fastsandbox.run(main())
ProxiedKey(env, hosts, value), its value text or aSecretStr, writes the key forhosts, and raisesInvalidRequestErrorfor a variable that is not one, or no host;ProxiedKey.for_service(package, service, env, value)for the hosts the kits ofpackageinject the key ofserviceinto, and raisesInvalidRequestErrorwhen no kit declares the service.CreateRequest.with_proxied_key(key)sets its generated placeholder,sbx-cs-…, in the variableenv, for the sandbox and every command run in it.sandbox.push_proxied_keys(*keys)gives the proxy the keys, once the sandbox exists: they replace the custom secrets of the sandbox, so one call carries every key. The key is written in the requests to its hosts only, whatever the network rules of the sandbox allow.
Unlike the credential of a service, a proxied key does not depend on the daemon authorizing the credential a kit declares (see Withheld credentials). A sandbox spec gives its keys this way, and FastSandbox the keys of the agents it runs.
Credentials of services¶
The services sbx knows, github or openai for instance, have credentials
of their own, which the daemon stores (sbx secret set) and the proxy injects
for the credentials the kits of a sandbox declare, when the daemon authorizes
them. push_service_secret(service, value)
gives one to a sandbox as sbx secret set SERVICE --sandbox does, its value
text or a SecretStr, and
remove_service_secret(service)
takes it back as sbx secret rm does. The daemon records where each credential
comes from, in SandboxInfo.credential_sources;
the sandbox keeps the fixed placeholders it was created with, GH_TOKEN for
GitHub, never a value.
The same by hand, with the methods of
CredentialsUpdate: a value, text or
a SecretStr, and the source the daemon records for it, an environment
variable name as the CLI declares the credentials it stores:
from fastsandbox.models import CredentialSource, CredentialsUpdate
update = (
CredentialsUpdate()
.with_service_credential("openai", value)
.with_credential_source("openai", CredentialSource.env("MY_OPENAI_SOURCE"))
)
await sandbox.push_credentials(update)
with_sandbox_secret() and without_sandbox_secret() build the updates of
push_service_secret() and remove_service_secret(); a cleared
ServiceCredential and
without_credential_source() take a credential back.
Withheld credentials¶
The daemon accepts an update even when it withholds what it carries. Each of
these calls returns a
CredentialsOutcome, whose
withheld names the services whose credential, declared by a kit of the
sandbox, the daemon does not authorize: the proxy injects nothing for them, a
value just pushed included. The creation reports the same in
Created.withheld, for the credentials the
kits declare:
outcome = await sandbox.push_service_secret("openai", value)
if "openai" in outcome.withheld:
print("the proxy gives the sandbox no openai credential")
A value pushed for a withheld service reaches nothing: give the key of the program's own as a proxied key instead.
Tip
Push only the credentials a sandbox needs: they are scoped to it and go with it. FastSandbox gives the agents it runs the keys their profile names, from the environment of the host, as proxied keys (see Agents).