Skip to content

Commit abbea03

Browse files
committed
feat: modify agent install script to give CAP_NET_ADMIN if available
1 parent beac360 commit abbea03

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

dogfood/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ resource "docker_container" "workspace" {
332332
runtime = "sysbox-runc"
333333
env = [
334334
"CODER_AGENT_TOKEN=${coder_agent.dev.token}",
335+
"USE_CAP_NET_ADMIN=true",
335336
]
336337
host {
337338
host = "host.docker.internal"
@@ -342,6 +343,9 @@ resource "docker_container" "workspace" {
342343
volume_name = docker_volume.home_volume.name
343344
read_only = false
344345
}
346+
capabilities {
347+
add = ["CAP_NET_ADMIN", "CAP_SYS_NICE"]
348+
}
345349
# Add labels in Docker to keep track of orphan resources.
346350
labels {
347351
label = "coder.owner"

examples/templates/docker/build/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN apt-get update \
88
sudo \
99
vim \
1010
wget \
11+
libcap2-bin \
1112
&& rm -rf /var/lib/apt/lists/*
1213

1314
ARG USER=coder

examples/templates/docker/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ resource "docker_container" "workspace" {
187187
volume_name = docker_volume.home_volume.name
188188
read_only = false
189189
}
190+
# Uncomment this to allow higher network speeds. Be aware this has security implications.
191+
# See: https://man7.org/linux/man-pages/man7/capabilities.7.html
192+
# capabilities {
193+
# add = ["CAP_NET_ADMIN"]
194+
# }
195+
190196
# Add labels in Docker to keep track of orphan resources.
191197
labels {
192198
label = "coder.owner"

provisionersdk/scripts/bootstrap_linux.sh

100644100755
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,47 @@ if ! chmod +x $BINARY_NAME; then
4343
exit 1
4444
fi
4545

46+
haslibcap2() {
47+
command -v setcap /dev/null 2>&1
48+
command -v capsh /dev/null 2>&1
49+
}
50+
printnetadminmissing() {
51+
echo "The root user does not have CAP_NET_ADMIN permission. " + \
52+
"If running in Docker, add the capability to the container for " + \
53+
"improved network performance."
54+
echo "This has security implications. See https://man7.org/linux/man-pages/man7/capabilities.7.html"
55+
}
56+
57+
# Attempt to add CAP_NET_ADMIN to the agent binary. This allows us to increase
58+
# network buffers which improves network transfer speeds.
59+
if [ -n "${USE_CAP_NET_ADMIN:-}" ]; then
60+
# If running as root, we do not need to do anything.
61+
if [ "$(id -u)" -eq 0 ]; then
62+
echo "Running as root, skipping setcap"
63+
# Warn the user if root does not have CAP_NET_ADMIN.
64+
if ! capsh --has-p=CAP_NET_ADMIN; then
65+
printnetadminmissing
66+
fi
67+
68+
# If not running as root, make sure we have sudo perms and the "setcap" +
69+
# "capsh" binaries exist.
70+
elif sudo -nl && haslibcap2; then
71+
# Make sure the root user has CAP_NET_ADMIN.
72+
if sudo -n capsh --has-p=CAP_NET_ADMIN; then
73+
sudo -n setcap CAP_NET_ADMIN=+ep ./$BINARY_NAME || true
74+
else
75+
printnetadminmissing
76+
fi
77+
78+
# If we are not running as root, cant sudo, and "setcap" does not exist, we
79+
# cannot do anything.
80+
else
81+
echo "Unable to setcap agent binary. To enable improved network performance, " + \
82+
"give the agent passwordless sudo permissions and the \"setcap\" + \"capsh\" binaries."
83+
echo "This has security implications. See https://man7.org/linux/man-pages/man7/capabilities.7.html"
84+
fi
85+
fi
86+
4687
export CODER_AGENT_AUTH="${AUTH_TYPE}"
4788
export CODER_AGENT_URL="${ACCESS_URL}"
4889
exec ./$BINARY_NAME agent

0 commit comments

Comments
 (0)