Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
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
46 changes: 43 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,51 @@ type MountConfig mount.MountConfig

type Network network.Network

type NamespaceType string

const (
NEWNET NamespaceType = "NEWNET"
NEWPID NamespaceType = "NEWPID"
NEWNS NamespaceType = "NEWNS"
NEWUTS NamespaceType = "NEWUTS"
NEWIPC NamespaceType = "NEWIPC"
NEWUSER NamespaceType = "NEWUSER"
)

// Namespace defines configuration for each namespace. It specifies an
// alternate path that is able to be joined via setns.
type Namespace struct {
Name string `json:"name"`
Path string `json:"path,omitempty"`
Type NamespaceType `json:"type"`
Path string `json:"path,omitempty"`
}

type Namespaces []Namespace

func (n Namespaces) Remove(t NamespaceType) bool {
i := n.index(t)
if i == -1 {
return false
}
n = append(n[:i], n[i+1:]...)
return true
}

func (n Namespaces) Add(t NamespaceType, path string) {
i := n.index(t)
if i == -1 {
n = append(n, Namespace{Type: t, Path: path})
return
}
n[i].Path = path
}

func (n Namespaces) index(t NamespaceType) int {
for i, ns := range n {
if ns.Type == t {
return i
}
}
return -1
}

// Config defines configuration options for executing a process inside a contained environment.
Expand Down Expand Up @@ -45,7 +85,7 @@ type Config struct {

// Namespaces specifies the container's namespaces that it should setup when cloning the init process
// If a namespace is not provided that namespace is shared from the container's parent process
Namespaces []Namespace `json:"namespaces,omitempty"`
Namespaces Namespaces `json:"namespaces,omitempty"`

// Capabilities specify the capabilities to keep when executing the process inside the container
// All capbilities not specified will be dropped from the processes capability mask
Expand Down
13 changes: 2 additions & 11 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func TestConfigJsonFormat(t *testing.T) {
t.Fail()
}

if getNamespaceIndex(container, "NEWNET") == -1 {
if container.Namespaces.index(NEWNET) == -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Contains" as a wrapper to do this check?

t.Log("namespaces should contain NEWNET")
t.Fail()
}

if getNamespaceIndex(container, "NEWUSER") != -1 {
if container.Namespaces.index(NEWUSER) != -1 {
t.Log("namespaces should not contain NEWUSER")
t.Fail()
}
Expand Down Expand Up @@ -158,12 +158,3 @@ func TestSelinuxLabels(t *testing.T) {
t.Fatalf("expected mount label %q but received %q", label, container.MountConfig.MountLabel)
}
}

func getNamespaceIndex(config *Config, name string) int {
for i, v := range config.Namespaces {
if v.Name == name {
return i
}
}
return -1
}
18 changes: 3 additions & 15 deletions integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func TestIPCHost(t *testing.T) {
}

config := newTemplateConfig(rootfs)
i := getNamespaceIndex(config, "NEWIPC")
config.Namespaces = append(config.Namespaces[:i], config.Namespaces[i+1:]...)
config.Namespaces.Remove(libcontainer.NEWIPC)
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -121,8 +120,7 @@ func TestIPCJoinPath(t *testing.T) {
}

config := newTemplateConfig(rootfs)
i := getNamespaceIndex(config, "NEWIPC")
config.Namespaces[i].Path = "/proc/1/ns/ipc"
config.Namespaces.Add(libcontainer.NEWIPC, "/proc/1/ns/ipc")

buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
if err != nil {
Expand Down Expand Up @@ -150,8 +148,7 @@ func TestIPCBadPath(t *testing.T) {
defer remove(rootfs)

config := newTemplateConfig(rootfs)
i := getNamespaceIndex(config, "NEWIPC")
config.Namespaces[i].Path = "/proc/1/ns/ipcc"
config.Namespaces.Add(libcontainer.NEWIPC, "/proc/1/ns/ipcc")

_, _, err = runContainer(config, "", "true")
if err == nil {
Expand Down Expand Up @@ -179,12 +176,3 @@ func TestRlimit(t *testing.T) {
t.Fatalf("expected rlimit to be 1024, got %s", limit)
}
}

func getNamespaceIndex(config *libcontainer.Config, name string) int {
for i, v := range config.Namespaces {
if v.Name == name {
return i
}
}
return -1
}
12 changes: 6 additions & 6 deletions integration/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func newTemplateConfig(rootfs string) *libcontainer.Config {
"KILL",
"AUDIT_WRITE",
},
Namespaces: []libcontainer.Namespace{
{Name: "NEWNS"},
{Name: "NEWUTS"},
{Name: "NEWIPC"},
{Name: "NEWPID"},
{Name: "NEWNET"},
Namespaces: libcontainer.Namespaces{
{Type: libcontainer.NEWNS},
{Type: libcontainer.NEWUTS},
{Type: libcontainer.NEWIPC},
{Type: libcontainer.NEWPID},
{Type: libcontainer.NEWNET},
},
Cgroups: &cgroups.Cgroup{
Parent: "integration",
Expand Down
2 changes: 1 addition & 1 deletion namespaces/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func joinExistingNamespaces(namespaces []libcontainer.Namespace) error {
if err != nil {
return err
}
err = system.Setns(f.Fd(), uintptr(namespaceInfo[ns.Name]))
err = system.Setns(f.Fd(), uintptr(namespaceInfo[ns.Type]))
f.Close()
if err != nil {
return err
Expand Down
18 changes: 9 additions & 9 deletions namespaces/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func (i initError) Error() string {
return i.Message
}

var namespaceInfo = map[string]int{
"NEWNET": syscall.CLONE_NEWNET,
"NEWNS": syscall.CLONE_NEWNS,
"NEWUSER": syscall.CLONE_NEWUSER,
"NEWIPC": syscall.CLONE_NEWIPC,
"NEWUTS": syscall.CLONE_NEWUTS,
"NEWPID": syscall.CLONE_NEWPID,
var namespaceInfo = map[libcontainer.NamespaceType]int{
libcontainer.NEWNET: syscall.CLONE_NEWNET,
libcontainer.NEWNS: syscall.CLONE_NEWNS,
libcontainer.NEWUSER: syscall.CLONE_NEWUSER,
libcontainer.NEWIPC: syscall.CLONE_NEWIPC,
libcontainer.NEWUTS: syscall.CLONE_NEWUTS,
libcontainer.NEWPID: syscall.CLONE_NEWPID,
}

// New returns a newly initialized Pipe for communication between processes
Expand All @@ -37,9 +37,9 @@ func newInitPipe() (parent *os.File, child *os.File, err error) {

// GetNamespaceFlags parses the container's Namespaces options to set the correct
// flags on clone, unshare, and setns
func GetNamespaceFlags(namespaces []libcontainer.Namespace) (flag int) {
func GetNamespaceFlags(namespaces libcontainer.Namespaces) (flag int) {
for _, v := range namespaces {
flag |= namespaceInfo[v.Name]
flag |= namespaceInfo[v.Type]
}
return flag
}
10 changes: 5 additions & 5 deletions sample_configs/apparmor.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@
],
"hostname": "koye",
"namespaces": [
{"name":"NEWIPC"},
{"name": "NEWNET"},
{"name": "NEWNS"},
{"name": "NEWPID"},
{"name": "NEWUTS"}
{"type":"NEWIPC"},
{"type": "NEWNET"},
{"type": "NEWNS"},
{"type": "NEWPID"},
{"type": "NEWUTS"}
],
"networks": [
{
Expand Down
10 changes: 5 additions & 5 deletions sample_configs/attach_to_bridge.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@
],
"hostname": "koye",
"namespaces": [
{"name": "NEWIPC"},
{"name": "NEWNET"},
{"name": "NEWNS"},
{"name": "NEWPID"},
{"name": "NEWUTS"}
{"type": "NEWIPC"},
{"type": "NEWNET"},
{"type": "NEWNS"},
{"type": "NEWPID"},
{"type": "NEWUTS"}
],
"networks": [
{
Expand Down
10 changes: 5 additions & 5 deletions sample_configs/minimal.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@
],
"hostname": "koye",
"namespaces": [
{"name": "NEWIPC"},
{"name": "NEWNET"},
{"name": "NEWNS"},
{"name": "NEWPID"},
{"name": "NEWUTS"}
{"type": "NEWIPC"},
{"type": "NEWNET"},
{"type": "NEWNS"},
{"type": "NEWPID"},
{"type": "NEWUTS"}
],
"networks": [
{
Expand Down
10 changes: 5 additions & 5 deletions sample_configs/route_source_address_selection.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@
],
"hostname": "koye",
"namespaces": [
{"name": "NEWIPC"},
{"name": "NEWNET"},
{"name": "NEWNS"},
{"name": "NEWPID"},
{"name": "NEWUTS"}
{"type": "NEWIPC"},
{"type": "NEWNET"},
{"type": "NEWNS"},
{"type": "NEWPID"},
{"type": "NEWUTS"}
],
"networks": [
{
Expand Down
10 changes: 5 additions & 5 deletions sample_configs/selinux.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@
],
"hostname": "koye",
"namespaces": [
{"name": "NEWIPC"},
{"name": "NEWNET"},
{"name": "NEWNS"},
{"name": "NEWPID"},
{"name": "NEWUTS"}
{"type": "NEWIPC"},
{"type": "NEWNET"},
{"type": "NEWNS"},
{"type": "NEWPID"},
{"type": "NEWUTS"}
],
"networks": [
{
Expand Down