Skip to content

Commit 7513ee0

Browse files
Merge pull request #77 from Roblox/allow_privileged
Plugin configuration level privileged mode
2 parents a308178 + a8ac33d commit 7513ee0

File tree

8 files changed

+103
-19
lines changed

8 files changed

+103
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
8585
| :---: | :---: | :---: | :---: | :--- |
8686
| **enabled** | bool | no | true | Enable/Disable task driver. |
8787
| **containerd_runtime** | string | yes | N/A | Runtime for containerd e.g. `io.containerd.runc.v1` or `io.containerd.runc.v2`. |
88-
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats` |
88+
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats`. |
89+
| **allow_privileged** | bool | no | true | If set to `false`, driver will deny running privileged jobs. |
8990

9091
**Task Config**
9192

containerd/containerd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func (d *Driver) createContainer(containerConfig *ContainerConfig, config *TaskC
9595

9696
opts = append(opts, oci.WithImageConfigArgs(containerConfig.Image, args))
9797

98+
if !d.config.AllowPrivileged && config.Privileged {
99+
return nil, fmt.Errorf("Running privileged jobs are not allowed. Set allow_privileged to true in plugin config to allow running privileged jobs.")
100+
}
101+
98102
// Enable privileged mode.
99103
if config.Privileged {
100104
opts = append(opts, oci.WithPrivileged)

containerd/driver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ var (
7979
),
8080
"containerd_runtime": hclspec.NewAttr("containerd_runtime", "string", true),
8181
"stats_interval": hclspec.NewAttr("stats_interval", "string", false),
82+
"allow_privileged": hclspec.NewDefault(
83+
hclspec.NewAttr("allow_privileged", "bool", false),
84+
hclspec.NewLiteral("true"),
85+
),
8286
})
8387

8488
// taskConfigSpec is the specification of the plugin's configuration for
@@ -130,6 +134,7 @@ type Config struct {
130134
Enabled bool `codec:"enabled"`
131135
ContainerdRuntime string `codec:"containerd_runtime"`
132136
StatsInterval string `codec:"stats_interval"`
137+
AllowPrivileged bool `codec:"allow_privileged"`
133138
}
134139

135140
// Volume, bind, and tmpfs type mounts are supported.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
job "privileged-not-allowed" {
2+
datacenters = ["dc1"]
3+
4+
group "privileged-not-allowed-group" {
5+
task "privileged-not-allowed-task" {
6+
driver = "containerd-driver"
7+
8+
config {
9+
image = "ubuntu:16.04"
10+
command = "sleep"
11+
args = ["600s"]
12+
privileged = true
13+
}
14+
15+
resources {
16+
cpu = 500
17+
memory = 256
18+
}
19+
}
20+
}
21+
}
File renamed without changes.

tests/008-test-allow-privileged.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
source $SRCDIR/utils.sh
4+
job_name=privileged-not-allowed
5+
6+
# allow_privileged=false set in the plugin config, should deny all privileged jobs.
7+
test_allow_privileged() {
8+
pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example
9+
10+
cp agent.hcl agent.hcl.bkp
11+
12+
sed -i '8 i \ allow_privileged = false' agent.hcl
13+
sudo systemctl restart nomad
14+
is_systemd_service_active "nomad.service" true
15+
16+
echo "INFO: Starting nomad ${job_name} job using nomad-driver-containerd."
17+
nomad job run privileged_not_allowed.nomad
18+
# Sleep for 5 seconds, to allow ${alloc_id} to get populated.
19+
sleep 5s
20+
21+
echo "INFO: Checking status of ${job_name} job."
22+
alloc_id=$(nomad job status ${job_name}|grep failed|awk 'NR==1'|cut -d ' ' -f 1)
23+
output=$(nomad alloc status $alloc_id)
24+
echo -e "$output" |grep "Running privileged jobs are not allowed" &>/dev/null
25+
if [ $? -ne 0 ];then
26+
echo "ERROR: ${job_name} should have failed to run."
27+
return 1
28+
fi
29+
30+
echo "INFO: purge nomad ${job_name} job."
31+
nomad job stop -purge ${job_name}
32+
33+
mv agent.hcl.bkp agent.hcl
34+
popd
35+
}
36+
37+
cleanup() {
38+
if [ -f agent.hcl.bkp ]; then
39+
mv agent.hcl.bkp agent.hcl
40+
fi
41+
sudo systemctl restart nomad
42+
is_systemd_service_active "nomad.service" false
43+
}
44+
45+
trap cleanup EXIT
46+
47+
test_allow_privileged

tests/run_tests.sh

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ EOF
132132
sudo systemctl unmask containerd
133133
echo "INFO: Starting containerd daemon."
134134
sudo systemctl start containerd
135-
is_systemd_service_active "containerd.service"
135+
is_systemd_service_active "containerd.service" false
136136

137137
# Remove default golang (1.7.3) and install a custom version (1.14.3) of golang.
138138
# This is required for supporting go mod, and to be able to compile nomad-driver-containerd.
@@ -184,7 +184,7 @@ EOF
184184

185185
echo "INFO: Starting nomad server and nomad-driver-containerd."
186186
sudo systemctl start nomad
187-
is_systemd_service_active "nomad.service"
187+
is_systemd_service_active "nomad.service" false
188188
popd
189189
}
190190

@@ -216,20 +216,4 @@ is_containerd_driver_active() {
216216
fi
217217
}
218218

219-
is_systemd_service_active() {
220-
local service_name=$1
221-
i="0"
222-
while test $i -lt 5 && !(systemctl -q is-active "$service_name"); do
223-
printf "INFO: %s is down, sleep for 4 seconds.\n" $service_name
224-
sleep 4s
225-
i=$[$i+1]
226-
done
227-
228-
if [ $i -ge 5 ]; then
229-
printf "ERROR: %s didn't come up. exit 1.\n" $service_name
230-
exit 1
231-
fi
232-
printf "INFO: %s is up and running\n" $service_name
233-
}
234-
235219
main "$@"

tests/utils.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,25 @@ is_container_active() {
2525
exit 1
2626
fi
2727
}
28+
29+
is_systemd_service_active() {
30+
local service_name=$1
31+
local is_sleep=$2
32+
33+
i="0"
34+
while test $i -lt 5 && !(systemctl -q is-active "$service_name"); do
35+
printf "INFO: %s is down, sleep for 4 seconds.\n" $service_name
36+
sleep 4s
37+
i=$[$i+1]
38+
done
39+
40+
if [ $i -ge 5 ]; then
41+
printf "ERROR: %s didn't come up. exit 1.\n" $service_name
42+
exit 1
43+
fi
44+
45+
if [ "$is_sleep" = true ]; then
46+
sleep 7s
47+
fi
48+
printf "INFO: %s is up and running\n" $service_name
49+
}

0 commit comments

Comments
 (0)