Skip to content

Commit 976bcb4

Browse files
authored
Merge pull request #25 from onyx-dot-app/iptable-block
fix(k8s): Block network traffic via iptables
2 parents 0be4ab9 + 7ae4a08 commit 976bcb4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

code-interpreter/app/services/executor_kubernetes.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,32 @@ def _create_pod_manifest(
176176
],
177177
)
178178

179+
# Use iptables in an init container to drop all outbound traffic
180+
# before the main executor container starts. Since all containers
181+
# in a pod share a network namespace, rules set here apply to the
182+
# executor container as well. This eliminates the race condition
183+
# where the pod can send network requests before the Kubernetes
184+
# NetworkPolicy is enforced by the CNI.
185+
iptables_script = "set -e && iptables -A OUTPUT -j DROP && ip6tables -A OUTPUT -j DROP"
186+
network_lockdown_container = V1Container(
187+
name="network-lockdown",
188+
image=self.image,
189+
command=["sh", "-c", iptables_script],
190+
security_context={
191+
"runAsUser": 0,
192+
"runAsNonRoot": False,
193+
"allowPrivilegeEscalation": False,
194+
"readOnlyRootFilesystem": True,
195+
"capabilities": {"drop": ["ALL"], "add": ["NET_ADMIN"]},
196+
},
197+
resources={
198+
"limits": {"cpu": "100m", "memory": "32Mi"},
199+
"requests": {"cpu": "10m", "memory": "16Mi"},
200+
},
201+
)
202+
179203
spec = V1PodSpec(
204+
init_containers=[network_lockdown_container],
180205
containers=[container],
181206
restart_policy="Never",
182207
service_account_name=self.service_account if self.service_account else None,

executor/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN apt-get update \
66
build-essential \
77
curl \
88
gfortran \
9+
iptables \
910
libfreetype6-dev \
1011
liblapack-dev \
1112
libopenblas-dev \

0 commit comments

Comments
 (0)