Skip to content

Commit b109714

Browse files
committed
Add support for --cap-drop
- Support for --cap-add was added as part of #327 - This rounds out the feature set to also include support for --cap-drop - Updates tests to drop "chown" capability and verify doing so works - closes #389
1 parent 0db4700 commit b109714

7 files changed

+39
-16
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ containerRunOptions:
303303
- OTHER_SECRET_BAR
304304
capabilities: # Add list of Linux capabilities (--cap-add)
305305
- NET_BIND_SERVICE
306+
drop_capabilities: # Drop list of Linux capabilities (--cap-drop)
307+
- NET_BIND_SERVICE
306308
bindMounts: # Bind mount a volume (--volume, -v)
307309
- /etc/example/dir:/etc/dir
308310
```

pkg/drivers/docker_driver.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919
"bufio"
2020
"bytes"
2121
"fmt"
22-
"github.com/joho/godotenv"
2322
"io"
2423
"os"
2524
"path"
2625
"path/filepath"
2726
"strings"
2827

28+
"github.com/joho/godotenv"
29+
2930
"github.com/pkg/errors"
3031
"github.com/sirupsen/logrus"
3132

@@ -66,17 +67,19 @@ func NewDockerDriver(args DriverConfig) (Driver, error) {
6667
func (d *DockerDriver) hostConfig() *docker.HostConfig {
6768
if d.runOpts.IsSet() && d.runtime != "" {
6869
return &docker.HostConfig{
69-
Capabilities: d.runOpts.Capabilities,
70-
Binds: d.runOpts.BindMounts,
71-
Privileged: d.runOpts.Privileged,
72-
Runtime: d.runtime,
70+
CapAdd: d.runOpts.CapAdd,
71+
CapDrop: d.runOpts.CapDrop,
72+
Binds: d.runOpts.BindMounts,
73+
Privileged: d.runOpts.Privileged,
74+
Runtime: d.runtime,
7375
}
7476
}
7577
if d.runOpts.IsSet() {
7678
return &docker.HostConfig{
77-
Capabilities: d.runOpts.Capabilities,
78-
Binds: d.runOpts.BindMounts,
79-
Privileged: d.runOpts.Privileged,
79+
CapAdd: d.runOpts.CapAdd,
80+
CapDrop: d.runOpts.CapDrop,
81+
Binds: d.runOpts.BindMounts,
82+
Privileged: d.runOpts.Privileged,
8083
}
8184
}
8285
if d.runtime != "" {

pkg/types/unversioned/types.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ type Config struct {
4545
}
4646

4747
type ContainerRunOptions struct {
48-
User string
49-
Privileged bool
50-
TTY bool `yaml:"allocateTty"`
51-
EnvVars []string `yaml:"envVars"`
52-
EnvFile string `yaml:"envFile"`
53-
Capabilities []string
54-
BindMounts []string `yaml:"bindMounts"`
48+
User string
49+
Privileged bool
50+
TTY bool `yaml:"allocateTty"`
51+
EnvVars []string `yaml:"envVars"`
52+
EnvFile string `yaml:"envFile"`
53+
CapAdd []string `yaml:"capabilities"`
54+
CapDrop []string `yaml:"drop_capabilities"`
55+
BindMounts []string `yaml:"bindMounts"`
5556
}
5657

5758
func (opts *ContainerRunOptions) IsSet() bool {
@@ -60,7 +61,8 @@ func (opts *ContainerRunOptions) IsSet() bool {
6061
opts.TTY ||
6162
len(opts.EnvFile) > 0 ||
6263
(opts.EnvVars != nil && len(opts.EnvVars) > 0) ||
63-
(opts.Capabilities != nil && len(opts.Capabilities) > 0) ||
64+
(opts.CapAdd != nil && len(opts.CapAdd) > 0) ||
65+
(opts.CapDrop != nil && len(opts.CapDrop) > 0) ||
6466
(opts.BindMounts != nil && len(opts.BindMounts) > 0)
6567
}
6668

tests/amd64/ubuntu_20_04_containeropts_test.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ commandTests:
55
args: ["--print"]
66
expectedOutput:
77
- ".*cap_sys_admin.*"
8+
excludedOutput:
9+
- ".*chown.*"
810
- name: "Test bindMounts containerRunOptions"
911
command: "test"
1012
args:
@@ -15,5 +17,7 @@ containerRunOptions:
1517
privileged: true
1618
capabilities:
1719
- "sys_admin"
20+
drop_capabilities:
21+
- "chown"
1822
bindMounts:
1923
- "/tmp/test:/tmp/test"

tests/arm64/ubuntu_20_04_containeropts_test.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ commandTests:
55
args: ["--print"]
66
expectedOutput:
77
- ".*cap_sys_admin.*"
8+
excludedOutput:
9+
- ".*chown.*"
810
- name: "Test bindMounts containerRunOptions"
911
command: "test"
1012
args:
@@ -15,5 +17,7 @@ containerRunOptions:
1517
privileged: true
1618
capabilities:
1719
- "sys_admin"
20+
drop_capabilities:
21+
- "chown"
1822
bindMounts:
1923
- "/tmp/test:/tmp/test"

tests/ppc64le/ubuntu_20_04_containeropts_test.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ commandTests:
55
args: ["--print"]
66
expectedOutput:
77
- ".*cap_sys_admin.*"
8+
excludedOutput:
9+
- ".*chown.*"
810
- name: "Test bindMounts containerRunOptions"
911
command: "test"
1012
args:
@@ -15,5 +17,7 @@ containerRunOptions:
1517
privileged: true
1618
capabilities:
1719
- "sys_admin"
20+
drop_capabilities:
21+
- "chown"
1822
bindMounts:
1923
- "/tmp/test:/tmp/test"

tests/s390x/ubuntu_20_04_containeropts_test.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ commandTests:
55
args: ["--print"]
66
expectedOutput:
77
- ".*cap_sys_admin.*"
8+
excludedOutput:
9+
- ".*chown.*"
810
- name: "Test bindMounts containerRunOptions"
911
command: "test"
1012
args:
@@ -15,5 +17,7 @@ containerRunOptions:
1517
privileged: true
1618
capabilities:
1719
- "sys_admin"
20+
drop_capabilities:
21+
- "chown"
1822
bindMounts:
1923
- "/tmp/test:/tmp/test"

0 commit comments

Comments
 (0)