Sandbox resource access
Give a running sandbox temporary access to an app's database or file bucket.
A running sandbox can query an app’s PostgreSQL database or read and write objects in its bucket. You can use this while building the app, before its first deployment. The sandbox gets a temporary token for the resource you choose. The database password and S3 keys stay outside the sandbox.
Connect a resource
- Connect the database or bucket to an app. The resource and its app connection must be ready and enabled.
- Use a CLI profile with Can administer on that app and permission to use the sandbox. The session must be running and able to reach the workspace’s management HTTPS hostname. If you change the sandbox network policy to allow that hostname, start a new session.
- From your signed-in terminal, run the command for the resource you need. Use the running session’s ID and the ID of the app that holds the connection.
| Resource | Command |
|---|---|
| PostgreSQL | smolcloud sandbox connect SESSION_ID APP_ID database |
| File bucket | smolcloud sandbox connect SESSION_ID APP_ID bucket |
The response gives you an id to revoke access, an expiresAt timestamp and a path inside the sandbox. Each connection has its own file. Keep the file private. Anyone holding its token can use the grant while it is valid.
Use it inside the sandbox
The JSON file at path contains the URL and token for resource requests. The following Python helper reads the file and sends one operation. Run it inside the sandbox with Python 3.
import json
from urllib.request import Request, urlopen
def call(path, operation):
with open(path) as file:
grant = json.load(file)
request = Request(
grant["url"],
data=json.dumps(operation).encode(),
headers={
"Authorization": "Bearer " + grant["token"],
"Content-Type": "application/json",
},
)
with urlopen(request, timeout=20) as response:
return json.load(response)
PostgreSQL. Replace DATABASE_GRANT_PATH with the path returned by the database connect command. SQL runs with the app’s database permissions and returns CSV. To check the connection:
result = call("DATABASE_GRANT_PATH", {"operation": "query", "sql": "SELECT current_user"})
print(result["csv"])
Bucket. Use the path from the bucket connect command. A get reads an existing key. A put writes the supplied bytes at that key. Pick a new key for a new file.
import base64
path = "BUCKET_GRANT_PATH"
key = "notes/first.txt"
call(path, {
"operation": "put", "key": key, "contentType": "text/plain",
"contentBase64": base64.b64encode(b"Hello from the sandbox").decode(),
})
result = call(path, {"operation": "get", "key": key})
contents = base64.b64decode(result["contentBase64"])
End access
From the signed-in terminal, use the id returned by connect:
smolcloud sandbox revoke-resource GRANT_ID
The person or agent who issued the grant, or a workspace owner, can revoke it. Pausing, taking a snapshot or stopping the session ends its grants. Run connect again for a resumed or copied session. Revoking the parent login, losing the required permissions or changing the app’s resource connection also ends access.
Rows and objects already written stay in the database or bucket. If a write is interrupted, check the row or object before retrying. The write may have completed.
Limits and API
- CLI grants last five minutes or until the session deadline, whichever comes first. The create API accepts
lifetimeSecondsfrom 15 to 900, subject to the same deadline. - A session can hold eight active grants and run one resource operation at a time. SQL text is limited to 8 KiB, query output to 256 KiB and each bucket object transferred through a grant to 256 KiB.
- The workspace API publishes the request and response schemas at
GET /api/sandboxes/openapi.json. For bulk transfers, use the connected app’s PostgreSQL driver or S3 client. To manage workspace resources from an agent, use a named agent sign-in.