Make libcontainer's CapabilitiesMask into a []string (Capabilities).

Docker-DCO-1.1-Signed-off-by: Victor Marmol <vmarmol@google.com> (github: vmarmol)
This commit is contained in:
Victor Marmol 2014-05-17 00:44:10 +00:00
parent 724c84c6fc
commit 73f678f6f8
5 changed files with 41 additions and 45 deletions

View file

@ -19,7 +19,7 @@ type Container struct {
Env []string `json:"environment,omitempty"` // environment to set Env []string `json:"environment,omitempty"` // environment to set
Tty bool `json:"tty,omitempty"` // setup a proper tty or not Tty bool `json:"tty,omitempty"` // setup a proper tty or not
Namespaces map[string]bool `json:"namespaces,omitempty"` // namespaces to apply Namespaces map[string]bool `json:"namespaces,omitempty"` // namespaces to apply
CapabilitiesMask map[string]bool `json:"capabilities_mask,omitempty"` // capabilities to drop Capabilities []string `json:"capabilities,omitempty"` // capabilities given to the container
Networks []*Network `json:"networks,omitempty"` // nil for host's network stack Networks []*Network `json:"networks,omitempty"` // nil for host's network stack
Cgroups *cgroups.Cgroup `json:"cgroups,omitempty"` // cgroups Cgroups *cgroups.Cgroup `json:"cgroups,omitempty"` // cgroups
Context Context `json:"context,omitempty"` // generic context for specific options (apparmor, selinux) Context Context `json:"context,omitempty"` // generic context for specific options (apparmor, selinux)

View file

@ -24,24 +24,9 @@
"mtu": 1500 "mtu": 1500
} }
], ],
"capabilities_mask": { "capabilities": [
"SYSLOG": false, "MKNOD"
"MKNOD": true, ],
"NET_ADMIN": false,
"MAC_ADMIN": false,
"MAC_OVERRIDE": false,
"AUDIT_CONTROL": false,
"AUDIT_WRITE": false,
"SYS_TTY_CONFIG": false,
"SETPCAP": false,
"SYS_MODULE": false,
"SYS_RAWIO": false,
"SYS_PACCT": false,
"SYS_ADMIN": false,
"SYS_NICE": false,
"SYS_RESOURCE": false,
"SYS_TIME": false
},
"cgroups": { "cgroups": {
"name": "docker-koye", "name": "docker-koye",
"parent": "docker" "parent": "docker"

View file

@ -6,6 +6,16 @@ import (
"testing" "testing"
) )
// Checks whether the expected capability is specified in the capabilities.
func hasCapability(expected string, capabilities []string) bool {
for _, capability := range capabilities {
if capability == expected {
return true
}
}
return false
}
func TestContainerJsonFormat(t *testing.T) { func TestContainerJsonFormat(t *testing.T) {
f, err := os.Open("container.json") f, err := os.Open("container.json")
if err != nil { if err != nil {
@ -37,22 +47,17 @@ func TestContainerJsonFormat(t *testing.T) {
t.Fail() t.Fail()
} }
if _, exists := container.CapabilitiesMask["SYS_ADMIN"]; !exists { if hasCapability("SYS_ADMIN", container.Capabilities) {
t.Log("capabilities mask should contain SYS_ADMIN")
t.Fail()
}
if container.CapabilitiesMask["SYS_ADMIN"] {
t.Log("SYS_ADMIN should not be enabled in capabilities mask") t.Log("SYS_ADMIN should not be enabled in capabilities mask")
t.Fail() t.Fail()
} }
if !container.CapabilitiesMask["MKNOD"] { if !hasCapability("MKNOD", container.Capabilities) {
t.Log("MKNOD should be enabled in capabilities mask") t.Log("MKNOD should be enabled in capabilities mask")
t.Fail() t.Fail()
} }
if container.CapabilitiesMask["SYS_CHROOT"] { if hasCapability("SYS_CHROOT", container.Capabilities) {
t.Log("capabilities mask should not contain SYS_CHROOT") t.Log("capabilities mask should not contain SYS_CHROOT")
t.Fail() t.Fail()
} }

View file

@ -26,15 +26,13 @@ func DropCapabilities(container *libcontainer.Container) error {
return nil return nil
} }
// getCapabilitiesMask returns the capabilities that should not be dropped by the container. // getEnabledCapabilities returns the capabilities that should not be dropped by the container.
func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap { func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap {
keep := []capability.Cap{} keep := []capability.Cap{}
for key, enabled := range container.CapabilitiesMask { for _, capability := range container.Capabilities {
if enabled { if c := libcontainer.GetCapability(capability); c != nil {
if c := libcontainer.GetCapability(key); c != nil {
keep = append(keep, c.Value) keep = append(keep, c.Value)
} }
} }
}
return keep return keep
} }

View file

@ -123,6 +123,14 @@ func GetCapability(key string) *Capability {
return nil return nil
} }
func GetAllCapabilities() []string {
output := make([]string, len(capabilityList))
for i, capability := range capabilityList {
output[i] = capability.String()
}
return output
}
// Contains returns true if the specified Capability is // Contains returns true if the specified Capability is
// in the slice // in the slice
func (c Capabilities) Contains(capp string) bool { func (c Capabilities) Contains(capp string) bool {