SandboxGateway separates control plane traffic from data plane traffic for Agent Sandbox, and supports routing rules, ingress traffic authentication, and other capabilities. This enables access control for ingress traffic and helps prevent control plane component issues from affecting business communication.
Background
Starting with ack-sandbox-manager add-on v0.5.2, the SandboxGateway feature is available.
By default, Ingress forwards both control plane (api.{domain}) and data plane (*.{domain}) traffic to the sandbox-manager service. Because the control plane and data plane are not physically separated, an issue with a control plane component can affect the agent's business communication.
Once you switch the traffic, the sandbox-gateway service forwards data plane traffic. This decoupling reduces the impact of control plane failures on business data transfer.
Applicable scope
-
On the cluster Add-ons page, the ack-sandbox-manager add-on is upgraded to v0.5.2 or later.
-
In the
sandbox-systemnamespace, thesandbox-gatewayworkload has at least one replica and its pods are in the ready state. -
Inbound traffic on Transmission Control Protocol (TCP) port
7788is allowed in the Control Plane Security Group, so that Ingress can forward traffic tosandbox-gateway. -
If you use a TrafficPolicy to manage the network policy of the Sandbox, allow inbound traffic from the
sandbox-gatewayService in the Sandbox-side policy. For more information, see Manage agent network access with TrafficPolicy.
How it works
After you switch the traffic, Ingress distributes traffic based on domain name matching rules:
-
Control plane traffic: Traffic accessed through
api.{domain}is processed bysandbox-manager. -
Data plane traffic: Traffic accessed through
*.{domain}is handled bysandbox-gateway. Thesandbox-gatewayservice queries its local routing registry and forwards requests to the target Sandbox Pod based on the routing rules.
The following table describes the traffic forwarding paths:
|
Domain name |
Path |
Forwarding service |
Description |
|
|
|
|
Forwards control plane traffic. |
|
|
|
|
Forwards data plane traffic. |
Preparation
Verify the connectivity of SandboxGateway.
-
Evaluate your business traffic and adjust the SandboxGateway instance configuration as needed to meet your workload requirements. For more information, see Limitations.
-
On the details page of the target cluster, go to Workloads > Deployments. Switch to the
sandbox-systemnamespace, and then click thesandbox-gatewayworkload name to open its details page. Verify that the replica count is 1 or greater and that the pods are in a ready state. -
Obtain the ClusterIP of the sandbox-gateway Service:
kubectl get svc -n sandbox-system sandbox-gateway -o jsonpath='{.spec.clusterIP}' -
Send a request based on the following template. Replace the placeholders before you run the command:
curl -v \ --resolve "<PORT>-<NAMESPACE>--<NAME>.<DOMAIN>:7788:<SANDBOX-GATEWAY-SVC-IP>" \ -H "e2b-sandbox-id: <NAMESPACE>--<NAME>" \ -H "e2b-sandbox-port: <PORT>" \ -H "X-Access-Token: <SANDBOX-TOKEN>" \ http://<PORT>-<NAMESPACE>--<NAME>.<DOMAIN>:7788/healthExpected output: HTTP/1.1 204 No Content, which indicates that sandbox-gateway has forwarded the request to the health check endpoint of envd in the Sandbox Pod.
The placeholders are described as follows:
-
<PORT>: the port on which the Sandbox provides services. If no other service is enabled, use the default port of envd, which is49983. -
<NAMESPACE>: the namespace of the Sandbox that you want to verify. -
<NAME>: the name of the Sandbox that you want to verify. -
<DOMAIN>: the wildcard domain name that points to the gateway. -
<SANDBOX-GATEWAY-SVC-IP>: the ClusterIP of thesandbox-gatewayService that you obtained in step 3. -
<SANDBOX-TOKEN>: if Ingress traffic authentication is enabled, specify theenvdAccessTokenof the Sandbox. Otherwise, omit this request header.
-
This verification covers only the connectivity between sandbox-gateway and the Sandbox Pod. You must allow traffic from Ingress to sandbox-gateway in the security group.
Switching traffic
Switching data plane traffic may interrupt your services. Perform the switch during off-peak hours. Before the switch, evaluate the traffic impact and complete the pre-switch verification described in Preparation to avoid network issues.
-
In the console, go to the Add-ons page, select Configuration for
ack-sandbox-manager, and switch dataplaneService tosandbox-gateway. -
(Optional) To access
sandbox-managerover HTTP only (no Transport Layer Security (TLS) certificate is required), clear the Whether to enable TLS for ingress checkbox on the same configuration page. -
Verify that the forwarding path is switched to
sandbox-gatewayin the console of the associated load balancer.-
Find the associated Application Load Balancer (ALB) instance name in the Ingress resource:
Console
On the details page of the target cluster, go to Network > Ingresses. Switch to the
sandbox-systemnamespace and view the Endpoint of thesandbox-managerIngress. The prefix of the endpoint (for example, alb-xxx) is the name of the ALB instance.kubectl
kubectl get ingress -nsandbox-system sandbox-manager -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' | sed -E 's/^(alb-[^.]+).*/\1/' -
Go to the ALB console. Click the target instance name, and then go to Listener > ingress-auto-listener-443 > Forwarding Rules. Confirm that the forwarding action is changed to
sandbox-system-sandbox-gateway-7788.
-
-
In the left navigation bar of the target cluster, select , and switch to the Application Logs tab.
-
On the Application Logs page, set the Logstore to
ack-sandbox-gateway. Check the access log and runtime log for any abnormal status codes, such asresponse_codevalues outside the2xxor1xxrange. To collect metrics yourself, see Metric reporting.To roll back the change, see How to roll back on service failure.
Limitations
By default, SandboxGateway is deployed with three replicas, each using an instance specification of 4 vCPU and 8 GiB. A single replica instance provides a total inbound and outbound bandwidth of 1.5 Gbit/s, which can support approximately 93.75 MB/s of service traffic. The total inbound and outbound bandwidth supported by a single ACS instance is subject to the ACS instance specification. For higher performance, see Adjust the SandboxGateway specification.
The capacity of a single 4 vCPU and 8 GiB instance is calculated as follows: 1.5 Gbit/s / 2 (for both inbound and outbound traffic) / 8 (for unit conversion) ≈ 93.75 MB/s.
The preceding data represents a theoretical maximum. Alibaba Cloud does not guarantee this performance level, which may vary based on your specific use case. Monitor your services for packet loss or check the gateway logs for 502 status codes to assess the actual performance.
Advanced operations
Configure routing rules
When processing a request, SandboxGateway first attempts a header-based match and then falls back to a domain-based match.
Header-based routing (matched first)
|
Header |
Default name |
Required |
Description |
|
Sandbox ID header |
|
Yes |
The Sandbox ID. |
|
Sandbox port header |
|
No |
The target port within the Sandbox Pod. The default is |
Example:
curl https://sandbox.domain/sandboxes \
-H "e2b-sandbox-id: default--my-sandbox" \
-H "e2b-sandbox-port: 49983"
Domain-based routing
If the Sandbox ID header is missing, SandboxGateway attempts to match the Host or authority field by using the {port}-{namespace}--{name}.{domain} format.
Due to the DNS name format limits, the leftmost label of a domain name, {port}-{namespace}--{name}, cannot exceed 63 characters in total length. Otherwise, domain-based routing does not take effect.
|
Field |
Description |
Example |
|
|
The target port on the Sandbox Pod. |
|
|
|
The Kubernetes namespace of the Sandbox. |
|
|
|
The name of the Sandbox custom resource (CR). |
|
|
|
The wildcard domain name that points to the gateway. |
|
Example:
curl https://3000-default--my-sandbox.domain \
-H "Host: 3000-default--my-sandbox.domain"
Metric reporting
The sandbox-gateway service exposes a metrics endpoint on port 9902. This endpoint provides monitoring metrics in the Prometheus standard format. Collect metrics from the http://<pod_ip>:9902/metrics address (accessible only within the cluster).
Graceful shutdown
SandboxGateway supports graceful shutdown of instances. When an instance is shut down, the preStop hook calls POST http://127.0.0.1:9901/drain_listeners?graceful, which allows in-flight requests to complete as much as possible before the container exits. This reduces the impact of instance scale-in and upgrade operations on your services.
Ingress traffic authentication
Starting with ack-sandbox-manager add-on v0.6.6, sandbox-gateway can validate the access token of inbound requests. sandbox-gateway provides the following two authentication methods. Select a method based on how you manage business credentials:
-
Static access token authentication (v0.6.6 and later): Validates the
X-Access-Tokenrequest header. The token value is theenvdAccessTokenreturned in the response body when the Sandbox is created. This method applies if the credential is static and shares the same lifecycle as the Sandbox. -
JWT-based dynamic token authentication (v0.6.8 and later) : Validates the
E2B-Traffic-Access-Tokenrequest header. The token value is a JSON Web Token (JWT) issued by theack-agent-identityadd-on. This method applies if you need replay protection and multi-tenant isolation, and want credentials to expire within a short validity period configured in the issuance parameters.
Static access token authentication
Procedure
After you enable ingress traffic authentication, existing traffic that does not carry the authentication request header is rejected with HTTP 401. Make sure that all clients send the authentication request header before you enable this feature. Make the change during off-peak hours.
-
On the cluster Add-ons page, click Configuration for
ack-sandbox-manager, select Whether to enable ingress traffic authentication, and then click Save.sandbox-gatewaymust restart to apply the change. During the restart, established persistent connections are disconnected. Service resumes after clients reconnect. Evaluate the impact of the change in advance. -
Create a Sandbox by using the E2B SDK, and then obtain
_envd_access_tokenfrom the Sandbox object. The following example uses the Python SDK:Important_envd_access_tokenis a critical security credential for accessing the Agent Sandbox data plane. If the token is leaked, your business data and sandbox environment may be exposed to security risks. Store the token in encrypted form and never write it in plaintext to code or logs.from e2b_code_interpreter import Sandbox sbx = Sandbox.create(template="code-interpreter") # Obtain the token from the private attribute of the Sandbox instance (the Python SDK does not mask the token). access_token = sbx._envd_access_tokenWhen you call native SDK methods such as
sbx.run_code,sbx.commands.run, andsbx.files.*, the E2B SDK automatically attaches_envd_access_tokento theX-Access-Tokenrequest header. Manually include this request header only when you access a self-hosted HTTP service inside the Sandbox, such as a service on port 3000, as described in Step 3. -
When you access a self-hosted HTTP service inside the Sandbox, include the
X-Access-Tokenrequest header. Example:curl https://<YOUR-DOMAIN>/ \ -H "X-Access-Token: <ENVD-ACCESS-TOKEN>" \ -H "e2b-sandbox-id: <NAMESPACE>--<NAME>" \ -H "e2b-sandbox-port: <PORT>"The combination of request headers (header-based routing and domain-based routing) follows the routing matching rules of SandboxGateway. For more information, see Configure routing rules.
Authentication failure response
If the token carried by the request fails the validation, sandbox-gateway returns HTTP 401. The response body is:
unauthorized: invalid or missing access token
Compatibility
-
Ingress traffic authentication is disabled by default. Upgrading the add-on does not affect existing traffic. You must enable this feature manually.
-
If no
envdAccessTokenis generated when the Sandbox is created,sandbox-gatewayskips token validation and forwards the request directly.
JWT-based dynamic token authentication
Applicable scope
After you enable this feature, sandbox-gateway validates the E2B-Traffic-Access-Token request header of each inbound request. The token value is the trafficAccessToken field in the response body returned when the Sandbox is created.
-
The
ack-sandbox-manageradd-on is upgraded to v0.6.8 or later. -
The
ack-agent-identityadd-on (v0.4.1-rc.1 or later) is installed, and Enable token delegation is selected. This add-on issues and validates JWTs.
Procedure
After you enable ingress traffic authentication, existing traffic that does not carry the authentication request header is rejected with HTTP 403. Make the change during off-peak hours.
-
On the cluster Add-ons page, click Configuration for
ack-sandbox-manager, select the following configuration items based on the add-on version, and then click Save.-
Whether to enable JWT (TrafficAccessToken) verification
-
IdentityProvider plays a critical role in securing OpenKruise Agents through sandboxing
NoteThe mapping between add-on versions and checkboxes, and the resulting differences, are as follows:
-
v0.6.12 and later: Select only Whether to enable JWT (TrafficAccessToken) verification to enable JWT authentication. If you also select Whether to enable ingress traffic authentication, for Sandboxes that do not enable
enable-jwt-auththroughmetadata, the gateway falls back toX-Access-Token(static access token) authentication. -
Versions earlier than v0.6.12: Select both Whether to enable ingress traffic authentication and Whether to enable JWT (TrafficAccessToken) verification. For Sandboxes that do not enable
enable-jwt-auththroughmetadata, no gateway authentication is enabled.
sandbox-gatewaymust restart to apply the change. During the restart, established persistent connections are disconnected. Service resumes after clients reconnect. Evaluate the impact of the change in advance. -
-
Enable JWT authentication explicitly by passing the
metadataparameter when you create a Sandbox, and then obtaintrafficAccessTokenfrom the response. The following example uses the E2B Python SDK:ImportantE2B-Traffic-Access-Tokenis a critical security credential for accessing the Agent Sandbox data plane. If the token is leaked, your business data and sandbox environment may be exposed to security risks. Store the token in encrypted form and never write it in plaintext to code or logs.from e2b_code_interpreter import Sandbox sbx = Sandbox.create( template="code-interpreter", metadata={ "security.agents.kruise.io/enable-jwt-auth": "true", }, ) # Obtain the JWT from the Sandbox object (the Python SDK does not mask the token). traffic_access_token = sbx.traffic_access_tokenIn the current version, the JWT validity period is controlled by the
sandbox-managercomponent, which passes issuance parameters toack-agent-identity. The default validity period is 100 years. If the JWT expires, create a new Sandbox or call the issuance API to obtain a new token.To configure a short validity period for the token and enable automatic token rotation by the SDK, see Configure Agent Sandbox TrafficAccessToken auto-rotation.
-
When you access a service inside the Sandbox, include the
E2B-Traffic-Access-Tokenrequest header. The following example uses curl to access a service on a non-envd port:curl https://<YOUR-DOMAIN>/ \ -H "E2B-Traffic-Access-Token: <TRAFFIC-ACCESS-TOKEN>" \ -H "e2b-sandbox-id: <NAMESPACE>--<NAME>" \ -H "e2b-sandbox-port: <PORT>"NoteThe native E2B SDK does not automatically inject
trafficAccessTokeninto Sandbox data plane requests. To make native methods such assbx.commands.runandsbx.files.*include this request header, use a Monkey Patch to write the token into the SDK's extra request header configuration. This solution depends on the internal implementation and private fields of the E2B SDK. Before you use the patch, make sure that your SDK version is compatible. Apply the patch before you create any Sandbox instance.The combination of request headers (header-based routing and domain-based routing) follows the routing matching rules of SandboxGateway. For more information, see Configure routing rules.
JWT-based dynamic token authentication: authentication failure response
If JWT validation fails, for example because the token is invalid, expired, or does not match the target Sandbox, sandbox-gateway returns HTTP 403.
If a Sandbox is created with JWT-based dynamic token authentication enabled through metadata, but dynamic token authentication is not enabled on the gateway, sandbox-gateway returns HTTP 503.
FAQ
How to roll back on service failure
-
In the console, under Add-ons, click Configuration for ack-sandbox-manager, and switch dataplaneService back to
sandbox-manager. -
On the page, check whether the forwarding path of the gateway (such as ALB) has switched back to
sandbox-manager. -
In the left navigation bar of the target cluster, select , and switch to the Application Logs tab.
-
On the Application Logs page, set Logstore to
ack-sandbox-manager-envoy, view the access log to confirm that the traffic has been switched back, and observe whether service traffic has returned to normal.
How to adjust the SandboxGateway specification
SandboxGateway instances are subject to the limitations of underlying network resources. Because SandboxGateway acts as a proxy, it handles both inbound and outbound traffic, which requires twice the underlying network bandwidth (see Limitations).
After evaluating your capacity requirements, adjust the specification in the following ways:
Both adjustments increase fees: sandboxGateway.replicaCount sets the number of SandboxGateway pods, and sandboxGateway.resources sets the vCPU and memory of each pod. Confirm the billing impact before you make changes. For more information, see Billing overview.
-
Increase the replica count of SandboxGateway instances. On the Configuration page of the
ack-sandbox-manageradd-on, adjustsandboxGateway.replicaCount. -
Adjust the instance specification to increase underlying bandwidth.
On the Configuration page of the
ack-sandbox-manageradd-on, adjust the CPU and memory insandboxGateway.resources.envoy.concurrencydepends onresources.cpu: adjustenvoy.concurrencywhenever you changeresources.cputo keep SandboxGateway performance optimal.