Credential injection
Credential injection keeps real credentials out of the sandbox. Choose one of the three injection methods by use case, configure the sandbox, and apply the common conventions.
Overview
Code in a sandbox often needs to access external services, such as Alibaba Cloud Object Storage Service (OSS), third-party model APIs, DingTalk, and Feishu. All of these requests must carry credentials, such as an AccessKey ID (AK), an API key, or an OAuth token. Real credentials are stored outside the sandbox, either on the client side under your control or in Alibaba Cloud Agent Identity. Code in the sandbox sends requests as usual with a placeholder credential. When a request leaves the sandbox, the egress gateway replaces the placeholder with the real credential and forwards the request. Real credentials never enter sandbox processes, environment variables, or files.
Credential isolation does not replace least-privilege authorization. Even if code in the sandbox cannot read the real credentials, it can still use the permissions already granted to those credentials through the egress gateway. Grant only the operation and resource permissions that the current task requires. Do not configure high-privilege identities, and do not grant the sandbox direct access to sensitive resources such as shared databases. If data from a shared database or other sensitive resource is required, expose it through a trusted business service. Provide only the data or operations that the current task needs.
Select an injection method based on the credential type and how the credentials are managed:
-
Bring your own token or API key: Configure the real header value, or configure a mapping between a placeholder (the placeholder credential used in the sandbox) and the real value. The platform injects the credential. For more information, see Inject your own credentials.
-
Bring your own AccessKey ID (AK), AccessKey Secret (SK), or Security Token Service (STS) credentials: Multiple accounts are supported. You provide and rotate the credentials. For more information, see Inject your own Alibaba Cloud STS credentials.
-
Credentials managed by Agent Identity: Specify the identity name in
metadata, and write a reference expression innetwork.rules. The platform obtains and refreshes the real credentials from Agent Identity. User-delegated OAuth also isolates credentials by logged-on user. See Managed: header injection and Managed: access Alibaba Cloud services (re-signing) below.
If you only need to write a real credential into a specific header, without placeholder replacement or request re-signing, the bring-your-own token or API key method is sufficient. For more information, see Inject your own credentials.
Configure the sandbox
If you already hold a real credential such as an API token, declare the injection in network.rules when you create the sandbox. Specify the domain and the header into which the credential is injected. Code in the sandbox then sends requests as usual, and the egress gateway injects the real credential when each request leaves the sandbox.
Node.js
import { Sandbox } from "e2b";
const token = process.env.API_TOKEN;
const sandbox = await Sandbox.create({
network: {
allowOut: ["api.example.com"],
denyOut: ["0.0.0.0/0"],
rules: {
"api.example.com": [{
transform: {
headers: { Authorization: `Bearer ${token}` },
},
}],
},
},
});
Python
import os
from e2b import Sandbox
sandbox = Sandbox.create(
network={
"allow_out": ["api.example.com"],
"deny_out": ["0.0.0.0/0"],
"rules": {
"api.example.com": [{
"transform": {
"headers": {
"Authorization": f"Bearer {os.environ['API_TOKEN']}"
}
}
}]
},
},
)
For placeholder replacement and other usage, see Inject your own credentials. For the common limits, see Credential usage limits.
If you need the platform to obtain and refresh credentials, or to isolate credentials by user, see Managed: header injection or Managed: access Alibaba Cloud services (re-signing).
Managed: header injection
The following three header injection methods use the same injection mechanism as bring-your-own credentials. All of them inject the credential into the transform.headers field of network.rules. In addition, specify the identity name in metadata and replace the real value with a reference expression that points to Agent Identity. Select a method based on the type of destination service:
|
Destination service |
Injection method |
Document |
|
Services that use API key authentication (model services, search services, and internal APIs) |
API key injection |
|
|
OAuth services accessed with the application identity (DingTalk enterprise applications, Microsoft Graph, and Auth0) |
M2M (machine-to-machine) OAuth |
|
|
Access to personal resources on behalf of a logged-on user (the user's DingTalk, Feishu, and Microsoft 365 files) |
User-delegated OAuth |
The three header injection methods have the following meanings:
-
API key injection: References an API key stored in Agent Identity. The platform retrieves the key and injects it into the header.
-
M2M OAuth: The platform obtains and refreshes an OAuth token by using the identity of the application itself.
-
User-delegated OAuth: The platform obtains a token by using the identity of a specific logged-on user. Tokens are isolated by user, and the user must complete authorization first.
Using an API key as an example, managed credentials and bring-your-own credentials have the same effect on the destination service, and only the storage location of the key differs: the client side or Agent Identity. The additional benefit of managed credentials lies in the lifecycle: the tokens of M2M OAuth and user-delegated OAuth have validity periods, and the platform refreshes them automatically. User-delegated OAuth also isolates credentials by logged-on user.
Managed: access Alibaba Cloud services (re-signing)
The platform obtains and automatically refreshes STS credentials through a Workload Identity. When you create the sandbox, specify the identity name in metadata by using the key fc.sandbox.identity.name. Code in the sandbox then calls Alibaba Cloud SDKs with placeholder credentials. This method does not use network.rules. For more information, see Managed Alibaba Cloud STS injection. To bring your own credentials, use Inject your own Alibaba Cloud STS credentials.
How it works
Regardless of the method, the process has the following three steps:
1. Prepare credentials: bring your own, or host them in Agent Identity
↓
2. When you create the sandbox, configure the header injection rules or the credentials used for request re-signing
↓
3. Code in the sandbox sends a request → the egress gateway injects the credential or re-signs the request → forwards it to the destination service
Common conventions
Regardless of the method, both of the following statements are true:
-
Allowing network access and injecting credentials are two independent configurations. To allow the sandbox to access a domain, configure
network.allowOut(the network policy). To make requests to that domain carry real credentials, the managed methods rely on the identity name inmetadata, with the injection rules configured innetwork.rules. The managed header injection methods require both the identity name inmetadataand the reference expression innetwork.rules. The managed request re-signing method requires only the identity name inmetadata. The bring-your-own credential method requires onlynetwork.rules. If you configure injection but do not allow network access, requests are still blocked by the network policy. If you allow network access but do not configure injection, requests do not automatically carry credentials either. -
Real credentials never enter the sandbox. Real credentials are stored outside the sandbox and injected by the egress gateway when requests leave the sandbox, so they cannot be read from inside the sandbox. Use placeholder credentials in the sandbox so that SDKs generate requests as usual. For the managed methods, the platform obtains and caches the real credentials, and automatically refreshes the tokens that have validity periods. Queries about sandbox information return only reference expressions, such as
${fc.sandbox.identity.apiKey:<provider-name>}. For the bring-your-own credential method, queries return the value you configured in plaintext. Therefore, do not write query responses to logs.
Update the configuration
Modify the network access scope and the credential injection rules while the sandbox is running. For the procedure, see Update credential configuration.
Limits
For the common limits of credential injection, see Credential usage limits.