Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/oci/caps"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -117,6 +118,11 @@ func Service(
}
}

capabilities, err := convertCapabilities(apiVersion, service.CapAdd, service.CapDrop, service.Privileged)
if err != nil {
return swarm.ServiceSpec{}, err
}

serviceSpec := swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: name,
Expand Down Expand Up @@ -147,6 +153,7 @@ func Service(
Isolation: container.Isolation(service.Isolation),
Init: service.Init,
Sysctls: service.Sysctls,
Capabilities: capabilities,
},
LogDriver: logDriver,
Resources: resources,
Expand Down Expand Up @@ -675,3 +682,18 @@ func convertCredentialSpec(namespace Namespace, spec composetypes.CredentialSpec
}
return &swarmCredSpec, nil
}

func convertCapabilities(apiVersion string, capAdd, capDrop []string, privileged bool) ([]string, error) {
capabilities := []string{}
if privileged || len(capAdd) > 0 || len(capDrop) > 0 {
if versions.LessThan(apiVersion, "1.41") {
return nil, errors.Errorf("Engine version does not support exact list of capabilities")
}
capabilities, err := caps.TweakCapabilities(caps.DefaultCapabilities(), capAdd, capDrop, nil, privileged)
if err != nil {
return nil, err
}
return capabilities, nil
}
return capabilities, nil
}
55 changes: 55 additions & 0 deletions cli/compose/convert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/docker/docker/oci/caps"
"github.com/pkg/errors"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
Expand Down Expand Up @@ -623,3 +624,57 @@ func TestConvertUpdateConfigParallelism(t *testing.T) {
})
assert.Check(t, is.Equal(parallel, updateConfig.Parallelism))
}

func TestConvertServiceCapAddAndCapDrop(t *testing.T) {
result, err := Service("1.41", Namespace{name: "foo"}, composetypes.ServiceConfig{}, nil, nil, nil, nil)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(result.TaskTemplate.ContainerSpec.Capabilities, []string{}))

result, err = Service("1.40", Namespace{name: "foo"},
composetypes.ServiceConfig{CapAdd: []string{"SYS_NICE"}}, nil, nil, nil, nil)
assert.Error(t, err, "Engine version does not support exact list of capabilities")

service := composetypes.ServiceConfig{
CapAdd: []string{
"SYS_NICE",
"CAP_NET_ADMIN",
},
CapDrop: []string{
"CHOWN",
"DAC_OVERRIDE",
"CAP_FSETID",
"CAP_FOWNER",
},
}
expected := []string{
"CAP_MKNOD",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_NET_BIND_SERVICE",
"CAP_SYS_CHROOT",
"CAP_KILL",
"CAP_AUDIT_WRITE",
"CAP_SYS_NICE",
"CAP_NET_ADMIN",
}
result, err = Service("1.41", Namespace{name: "foo"}, service, nil, nil, nil, nil)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(result.TaskTemplate.ContainerSpec.Capabilities, expected))
}

func TestConvertServicePrivileged(t *testing.T) {
service := composetypes.ServiceConfig{
Privileged: true,
}

result, err := Service("1.40", Namespace{name: "foo"}, service, nil, nil, nil, nil)
assert.Error(t, err, "Engine version does not support exact list of capabilities")

expected := caps.GetAllCapabilities()
result, err = Service("1.41", Namespace{name: "foo"}, service, nil, nil, nil, nil)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(result.TaskTemplate.ContainerSpec.Capabilities, expected))
}
3 changes: 0 additions & 3 deletions cli/compose/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
// UnsupportedProperties not yet supported by this implementation of the compose file
var UnsupportedProperties = []string{
"build",
"cap_add",
"cap_drop",
"cgroupns_mode",
"cgroup_parent",
"devices",
Expand All @@ -21,7 +19,6 @@ var UnsupportedProperties = []string{
"mac_address",
"network_mode",
"pid",
"privileged",
"restart",
"security_opt",
"shm_size",
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/docker/docker/oci/caps/defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

169 changes: 169 additions & 0 deletions vendor/github.com/docker/docker/oci/caps/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.