Skip to content

Commit 234027d

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

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

dogfood/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ resource "docker_container" "workspace" {
342342
volume_name = docker_volume.home_volume.name
343343
read_only = false
344344
}
345+
capabilities {
346+
add = ["CAP_NET_ADMIN"]
347+
}
345348
# Add labels in Docker to keep track of orphan resources.
346349
labels {
347350
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ resource "docker_container" "workspace" {
187187
volume_name = docker_volume.home_volume.name
188188
read_only = false
189189
}
190+
capabilities {
191+
add = ["CAP_NET_ADMIN"]
192+
}
190193
# Add labels in Docker to keep track of orphan resources.
191194
labels {
192195
label = "coder.owner"

provisionersdk/scripts/bootstrap_linux.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,43 @@ 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+
51+
# Attempt to add CAP_NET_ADMIN to the agent binary. This allows us to increase
52+
# network buffers which improves network transfer speeds.
53+
if [ -n "${USE_CAP_NET_ADMIN}" ]; then
54+
# If running as root, we don't need to do anything.
55+
if [ "$(id -u)" -eq 0 ]; then
56+
echo "Running as root, skipping setcap"
57+
# Warn the user if root doesn't have CAP_NET_ADMIN.
58+
if ! capsh --has-p=CAP_NET_ADMIN; then
59+
echo "The root user doesn't have CAP_NET_ADMIN permission. " + \
60+
"If running in Docker, add the capability to the container for " + \
61+
"improved network performance."
62+
fi
63+
64+
# If not running as root, make sure we have sudo perms and the 'setcap' binary
65+
# exists.
66+
elif sudo -nl && haslibcap2; then
67+
# Make sure the root user has CAP_NET_ADMIN.
68+
if sudo -n capsh --has-p=CAP_NET_ADMIN; then
69+
sudo -n setcap CAP_NET_ADMIN=+ep ./$BINARY_NAME || true
70+
else
71+
echo "The root user doesn't have CAP_NET_ADMIN permission. " + \
72+
"If running in Docker, add the capability to the container for " + \
73+
"improved network performance."
74+
fi
75+
76+
# If we're not running as root, can't sudo, and 'setcap' doesn't exist, we can't
77+
# do anything.
78+
else
79+
echo "Unable to setcap agent binary. Missing passwordless sudo permissions or the 'setcap' binary."
80+
fi
81+
fi
82+
4683
export CODER_AGENT_AUTH="${AUTH_TYPE}"
4784
export CODER_AGENT_URL="${ACCESS_URL}"
4885
exec ./$BINARY_NAME agent

0 commit comments

Comments
 (0)