Skip to content

Commit 39846d6

Browse files
authored
feat: modify agent install script to give CAP_NET_ADMIN if available (#9908)
1 parent 2a19b46 commit 39846d6

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

dogfood/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ resource "docker_container" "workspace" {
272272
runtime = "sysbox-runc"
273273
env = [
274274
"CODER_AGENT_TOKEN=${coder_agent.dev.token}",
275+
"USE_CAP_NET_ADMIN=true",
275276
]
276277
host {
277278
host = "host.docker.internal"
@@ -282,6 +283,9 @@ resource "docker_container" "workspace" {
282283
volume_name = docker_volume.home_volume.name
283284
read_only = false
284285
}
286+
capabilities {
287+
add = ["CAP_NET_ADMIN", "CAP_SYS_NICE"]
288+
}
285289
# Add labels in Docker to keep track of orphan resources.
286290
labels {
287291
label = "coder.owner"

examples/templates/docker/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ resource "docker_container" "workspace" {
187187
volume_name = docker_volume.home_volume.name
188188
read_only = false
189189
}
190+
190191
# Add labels in Docker to keep track of orphan resources.
191192
labels {
192193
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)