Manage egress traffic access policies for Agent Sandbox by using the E2B SDK
Based on enhanced-traffic-policy and the agents.kruise.io TrafficPolicy, outbound TCP traffic of Agent Sandbox is controlled at the granularity of a single Sandbox, and you can configure an egress traffic allowlist.
Overview
When creating a Sandbox, the E2B SDK accepts a network config parameter to configure the egress traffic access policy of the Sandbox. For more information, see the E2B official documentation.
Based on the enhanced-traffic-policy and agents.kruise.io TrafficPolicy implementation, this topic describes the best practices for using the network config parameter of the E2B SDK to manage Agent Sandbox egress traffic access policies.
The network config parameter supports the following fields:
-
allow_out: the list of allowed destinations. Each entry can be a fully qualified domain name (FQDN), an IP address, or a Classless Inter-Domain Routing (CIDR) block. -
deny_out: the list of denied destinations. Each entry can be only an IP address or a CIDR block.
Based on the network config, the backend services separately create a TrafficPolicy with a priority of 100 for the Sandbox. The policy takes effect only on that Sandbox.
Currently, only the following behaviors are recommended:
-
Do not pass the network config. TCP remains open by default.
-
Explicitly specify
deny_out=["0.0.0.0/0"]to deny all IPv4 TCP traffic. -
Explicitly specify
allow_outtogether withdeny_out=["0.0.0.0/0"]to build a TCP allowlist. -
Explicitly specify
deny_outtogether withagents.kruise.io/allow-internet-access: "true"to build a TCP blocklist.
Prerequisites
Add-on requirements
-
ack-agent-sandbox-controller >= 0.5.22. -
ack-sandbox-manager >= 0.6.11.-
The
enhancedTrafficManagementconfiguration item is enabled.
-
-
The Poseidon add-on is installed.
-
E2B SDK integration is complete, including installing the
e2bpackage and configuring theE2B_DOMAINandE2B_API_KEYenvironment variables. For more information, see Use the E2B SDK to connect to Agent Sandbox.
Default TrafficPolicy / GlobalTrafficPolicy configuration
When you create a Sandbox, you can use allow-internet-access to bind a default Network Policy. The administrator must configure the default Network Policy in the cluster in advance, and its priority must be lower than the priority of per-Sandbox rules. The default priority is 100, and a larger value indicates a lower priority. When allow-internet-access=true is specified, or when the parameter is not explicitly specified, the Sandbox is assigned the agents.kruise.io/allow-internet-access: "true" label. When allow-internet-access=false is specified, the Sandbox is assigned the agents.kruise.io/allow-internet-access: "false" label. The administrator can create default Network Policies based on these labels.
The enhanced-traffic-policy mode supports TrafficPolicy resources in both theagents.kruise.ioandnetwork.alibabacloud.comAPI groups. Both versions are fully compatible in capabilities. The only difference is that the Action field of theagents.kruise.ioversion supports onlyallowandreject. For more information, see Manage enhanced egress traffic for Agent Sandbox.
Sample policy configurations:
allow-internet-access: "true":
kubectl apply -f - <<'EOF'
apiVersion: agents.kruise.io/v1alpha1
kind: GlobalTrafficPolicy
metadata:
name: e2b-allow-internet
labels:
app.kubernetes.io/name: e2b-allow-internet
app.kubernetes.io/managed-by: kubectl
spec:
priority: 900
selector:
matchLabels:
agents.kruise.io/allow-internet-access: "true"
egress:
rules:
- action: allow
to:
- cidr: 0.0.0.0/0
EOF
allow-internet-access: "false":
kubectl apply -f - <<'EOF'
apiVersion: agents.kruise.io/v1alpha1
kind: GlobalTrafficPolicy
metadata:
name: e2b-deny-internet
labels:
app.kubernetes.io/name: e2b-deny-internet
app.kubernetes.io/managed-by: kubectl
spec:
priority: 900
selector:
matchLabels:
agents.kruise.io/allow-internet-access: "false"
egress:
rules:
- action: reject
to:
- cidr: 0.0.0.0/0
EOF
Template configuration
Before you create any pre-warmed Sandbox, configure the annotation on the object that declares it.
SandboxSet example:
apiVersion: agents.kruise.io/v1alpha1
kind: SandboxSet
metadata:
name: code-interpreter
namespace: default
spec:
runtimes:
- name: csi
- name: agent-runtime
replicas: 4
template:
metadata:
annotations:
network.alibabacloud.com/network-policy-mode: enhanced-traffic-policy
labels:
alibabacloud.com/acs: "true"
alibabacloud.com/compute-class: agent-sandbox
alibabacloud.com/compute-qos: default
spec:
containers:
- name: sandbox
image: <sandbox-image>
A standalone SandboxTemplate places the annotation in the same location:
apiVersion: agents.kruise.io/v1alpha1
kind: SandboxTemplate
spec:
template:
metadata:
annotations:
network.alibabacloud.com/network-policy-mode: enhanced-traffic-policy
Procedure
The following examples all use the E2B Python SDK to create a Sandbox and configure the egress traffic access policy through the network parameter. Each example then sends requests from inside the Sandbox to verify the policy effect. Replace template="code-interpreter" in the examples with the actual SandboxSet or SandboxTemplate name. In the output, http_code=000 indicates that the TCP connection is denied.
The network parameter is written as follows in each scenario:
-
Open by default: Do not pass the
networkparameter. -
Deny all IPv4 TCP traffic:
deny_out=["0.0.0.0/0"]. -
TCP allowlist:
allow_outtogether withdeny_out=["0.0.0.0/0"]. -
TCP blocklist:
allow_internet_access=Truetogether withdeny_out=[a specific IP address or CIDR].
A new TrafficPolicy takes several seconds to propagate from creation to the data plane. In the examples, time.sleep(10) waits until the policy takes effect before verification.
Open by default
Requirement: Do not restrict outbound TCP traffic from the Sandbox.
Do not pass the network parameter when you create the Sandbox. Full example:
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
)
try:
print(f"sandbox_id={sandbox.sandbox_id}")
result = sandbox.commands.run(
"curl --max-time 10 -sS -o /dev/null -w '%{http_code}' https://example.com 2>/dev/null || true"
)
print(f"http_code={result.stdout}")
finally:
sandbox.kill()
Expected output:
sandbox_id=default--code-interpreter-xhmbv
http_code=200
If allow_internet_access is not passed, the backend services process it as true by default and write the agents.kruise.io/allow-internet-access=true label on both the Sandbox and the Pod. Therefore, omitting the parameter is equivalent to explicitly passing allow_internet_access=True. This default value determines which platform fallback is selected, but it does not create a TrafficPolicy by itself.
An empty network config can also be passed explicitly, but this is not necessary:
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
network={},
)
Expected:
-
No per-Sandbox TrafficPolicy is created.
-
TCP remains open by default.
-
UDP and ICMP traffic follows the default behavior of the underlying network.
Deny all IPv4 TCP
Requirement: The Sandbox cannot establish external TCP application connections.
(Recommended) Explicitly specify deny_out=["0.0.0.0/0"]. Full example:
import time
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
network={
"deny_out": ["0.0.0.0/0"],
},
)
try:
print(f"sandbox_id={sandbox.sandbox_id}")
# Wait until the egress traffic policy is propagated to the data plane
time.sleep(10)
result = sandbox.commands.run(
"curl --max-time 10 -sS -o /dev/null -w '%{http_code}' https://example.com 2>/dev/null || true"
)
print(f"http_code={result.stdout}")
result = sandbox.commands.run("getent hosts example.com | head -1")
print(f"dns={result.stdout}")
finally:
sandbox.kill()
Expected output:
sandbox_id=default--code-interpreter-fp247
http_code=000
dns=104.20.23.154 example.com
The TCP connection is denied, but UDP DNS resolution is not affected. The backend services create the following TrafficPolicy for this Sandbox (the name is randomly generated):
apiVersion: agents.kruise.io/v1alpha1
kind: TrafficPolicy
metadata:
name: tp-9lv7k
namespace: default
spec:
priority: 100
selector:
matchLabels:
agents.kruise.io/sandbox-name: <SANDBOX-POD-NAME>
egress:
rules:
- action: reject
to:
- cidr: 0.0.0.0/0
The current data plane controls only TCP. This configuration cannot block:
-
UDP DNS.
-
Other UDP applications.
-
ICMP.
-
Other protocols that do not pass through the enhanced TCP data plane.
TCP allowlist
Requirement: Allow access only to explicitly approved TCP domain names, IP addresses, or CIDR blocks.
Full example:
import time
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
network={
"allow_out": [
"example.com",
"1.1.1.1",
"10.0.0.0/8",
],
"deny_out": ["0.0.0.0/0"],
},
)
try:
print(f"sandbox_id={sandbox.sandbox_id}")
# Wait until the egress traffic policy is propagated to the data plane
time.sleep(10)
result = sandbox.commands.run(
"for url in https://example.com http://1.1.1.1 https://www.aliyun.com; do "
" code=$(curl --max-time 10 -sS -o /dev/null -w '%{http_code}' \"$url\" 2>/dev/null || true); "
" echo \"$url http_code=$code\"; "
"done"
)
print(result.stdout, end="")
finally:
sandbox.kill()
Expected output:
sandbox_id=default--code-interpreter-67fdk
https://example.com http_code=200
http://1.1.1.1 http_code=301
https://www.aliyun.com http_code=000
The backend services create the following TrafficPolicy for this Sandbox. Rules with the allow action always come before rules with the reject action:
apiVersion: agents.kruise.io/v1alpha1
kind: TrafficPolicy
metadata:
name: tp-ncn5j
namespace: default
spec:
priority: 100
selector:
matchLabels:
agents.kruise.io/sandbox-name: <SANDBOX-POD-NAME>
egress:
rules:
- action: allow
to:
- cidr: 1.1.1.1/32
- cidr: 10.0.0.0/8
- fqdn: example.com
- action: reject
to:
- cidr: 0.0.0.0/0
Behavior:
-
A destination matched by allow_out is allowed.
-
Other IPv4 TCP traffic is denied.
-
If allow and deny ranges overlap, allow takes precedence.
-
A bare IP address is automatically normalized to
/32. -
FQDNs can appear only in
allow_out.deny_outaccepts only IP addresses or CIDR blocks. If a domain name is passed, the create request returns 400. -
Wildcard domain names are not supported. If
*.example.comis passed, the create request returns 400.
TCP blocklist
Requirement: Deny only specific IP addresses or CIDR blocks, and keep other outbound traffic allowed.
This approach relies on an allow-type default policy as a fallback. Both of the following conditions must be met:
-
The cluster is configured with the default TrafficPolicy / GlobalTrafficPolicy described in the Prerequisites section, and an allow-type default policy for
allow-internet-access: "true"is created. -
When the Sandbox is created,
allow_internet_accessis set toTrue, so that the Sandbox matches the allow-type default policy.
Full example:
import time
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
allow_internet_access=True,
network={
"deny_out": ["1.0.0.0/8"],
},
)
try:
print(f"sandbox_id={sandbox.sandbox_id}")
# Wait until the egress traffic policy is propagated to the data plane
time.sleep(10)
result = sandbox.commands.run(
"for url in http://1.0.0.1 https://example.com; do "
" code=$(curl --max-time 10 -sS -o /dev/null -w '%{http_code}' \"$url\" 2>/dev/null || true); "
" echo \"$url http_code=$code\"; "
"done"
)
print(result.stdout, end="")
finally:
sandbox.kill()
Expected output:
sandbox_id=default--code-interpreter-cx77n
http://1.0.0.1 http_code=000
https://example.com http_code=200
The TrafficPolicy that the backend services create for this Sandbox contains only reject rules. Traffic that does not match these rules continues to match default policies with a larger priority:
apiVersion: agents.kruise.io/v1alpha1
kind: TrafficPolicy
metadata:
name: tp-99wgd
namespace: default
spec:
priority: 100
selector:
matchLabels:
agents.kruise.io/sandbox-name: <SANDBOX-POD-NAME>
egress:
rules:
- action: reject
to:
- cidr: 1.0.0.0/8
Overlapping allow and deny CIDR blocks
You can put specific exceptions in allow_out and the larger range in deny_out. Full example:
import time
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
network={
"allow_out": ["1.1.1.1"],
"deny_out": ["1.0.0.0/8"],
},
)
try:
print(f"sandbox_id={sandbox.sandbox_id}")
# Wait until the egress traffic policy is propagated to the data plane
time.sleep(10)
result = sandbox.commands.run(
"for url in http://1.1.1.1 http://1.0.0.1 https://example.com; do "
" code=$(curl --max-time 10 -sS -o /dev/null -w '%{http_code}' \"$url\" 2>/dev/null || true); "
" echo \"$url http_code=$code\"; "
"done"
)
print(result.stdout, end="")
finally:
sandbox.kill()
Expected output:
sandbox_id=default--code-interpreter-v5v9m
http://1.1.1.1 http_code=301
http://1.0.0.1 http_code=000
https://example.com http_code=200
Observed behavior:
-
A destination matched by allow_out is allowed, even if it also falls within a deny_out CIDR block.
-
A destination matched by deny_out and not covered by allow_out is denied.
-
For traffic that matches neither list, no default deny rule is created. The traffic continues to match default policies with a larger
priority. If an allow-type default policy exists in the cluster, this traffic is allowed.
Multi-target allowlist
Requirement: Allow multiple domain names, IP addresses, and CIDR blocks at a time. Each allow_out or deny_out list can contain at most 20 entries. Full example:
import time
from e2b import Sandbox
sandbox = Sandbox.create(
template="code-interpreter",
timeout=900,
network={
"allow_out": [
"example.com",
"1.1.1.1",
"10.0.0.0/8",
"198.51.100.0/24",
],
"deny_out": ["0.0.0.0/0"],
},
)
try:
print(f"sandbox_id={sandbox.sandbox_id}")
# Wait until the egress traffic policy is propagated to the data plane
time.sleep(10)
result = sandbox.commands.run(
"for url in https://example.com http://1.1.1.1 https://mirrors.aliyun.com; do "
" code=$(curl --max-time 10 -sS -o /dev/null -w '%{http_code}' \"$url\" 2>/dev/null || true); "
" echo \"$url http_code=$code\"; "
"done"
)
print(result.stdout, end="")
finally:
sandbox.kill()
Expected output:
sandbox_id=default--code-interpreter-lqljt
https://example.com http_code=200
http://1.1.1.1 http_code=301
https://mirrors.aliyun.com http_code=000
If a list contains more than 20 entries, the create request fails and the Sandbox is automatically cleaned up. The error message is as follows:
SandboxException: 500: failed to create network policy: failed to create TrafficPolicy:
TrafficPolicy.agents.kruise.io "tp-xqqqg" is invalid:
[spec.egress.rules[0].to: Too many: 21: must have at most 20 items]
Limitations
-
UDP and ICMP cannot be isolated.
-
Wildcard domain names are not supported.
-
A single allow_out or deny_out list cannot contain more than 20 entries.
-
Dynamic switching of
allow_internet_accessis not supported. After the Sandbox is created, callingupdate_networkwith this field does not return an error, but neither the label nor the policy changes. -
It takes several seconds for a TrafficPolicy to propagate to the data plane. A request sent immediately after the Sandbox is created may still be processed based on the previous policy.
Related documents
-
To manage network access for agents in the cluster directly through TrafficPolicy and GlobalTrafficPolicy, see Manage agent network access with TrafficPolicy.
-
To implement Layer 7 egress traffic blocking based on SecurityProfile, see Manage enhanced egress traffic for Agent Sandbox.