Browse code

Dont use custom marshaling for caps and namespaces

This also adds an enabled field to the types so that they
can be easily toggled.
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/03/21 08:09:01
Showing 2 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 package libcontainer
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"errors"
6 5
 	"github.com/syndtr/gocapability/capability"
7 6
 )
... ...
@@ -19,29 +18,30 @@ var (
19 19
 	namespaceList = Namespaces{}
20 20
 
21 21
 	capabilityList = Capabilities{
22
-		{Key: "SETPCAP", Value: capability.CAP_SETPCAP},
23
-		{Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE},
24
-		{Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO},
25
-		{Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT},
26
-		{Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN},
27
-		{Key: "SYS_NICE", Value: capability.CAP_SYS_NICE},
28
-		{Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE},
29
-		{Key: "SYS_TIME", Value: capability.CAP_SYS_TIME},
30
-		{Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG},
31
-		{Key: "MKNOD", Value: capability.CAP_MKNOD},
32
-		{Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE},
33
-		{Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL},
34
-		{Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE},
35
-		{Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN},
36
-		{Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN},
22
+		{Key: "SETPCAP", Value: capability.CAP_SETPCAP, Enabled: true},
23
+		{Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE, Enabled: true},
24
+		{Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO, Enabled: true},
25
+		{Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT, Enabled: true},
26
+		{Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN, Enabled: true},
27
+		{Key: "SYS_NICE", Value: capability.CAP_SYS_NICE, Enabled: true},
28
+		{Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE, Enabled: true},
29
+		{Key: "SYS_TIME", Value: capability.CAP_SYS_TIME, Enabled: true},
30
+		{Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG, Enabled: true},
31
+		{Key: "MKNOD", Value: capability.CAP_MKNOD, Enabled: true},
32
+		{Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE, Enabled: true},
33
+		{Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL, Enabled: true},
34
+		{Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE, Enabled: true},
35
+		{Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN, Enabled: true},
36
+		{Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN, Enabled: true},
37 37
 	}
38 38
 )
39 39
 
40 40
 type (
41 41
 	Namespace struct {
42
-		Key   string
43
-		Value int
44
-		File  string
42
+		Key     string `json:"key,omitempty"`
43
+		Enabled bool   `json:"enabled,omitempty"`
44
+		Value   int    `json:"value,omitempty"`
45
+		File    string `json:"file,omitempty"`
45 46
 	}
46 47
 	Namespaces []*Namespace
47 48
 )
... ...
@@ -50,23 +50,6 @@ func (ns *Namespace) String() string {
50 50
 	return ns.Key
51 51
 }
52 52
 
53
-func (ns *Namespace) MarshalJSON() ([]byte, error) {
54
-	return json.Marshal(ns.Key)
55
-}
56
-
57
-func (ns *Namespace) UnmarshalJSON(src []byte) error {
58
-	var nsName string
59
-	if err := json.Unmarshal(src, &nsName); err != nil {
60
-		return err
61
-	}
62
-	ret := GetNamespace(nsName)
63
-	if ret == nil {
64
-		return ErrUnkownNamespace
65
-	}
66
-	*ns = *ret
67
-	return nil
68
-}
69
-
70 53
 func GetNamespace(key string) *Namespace {
71 54
 	for _, ns := range namespaceList {
72 55
 		if ns.Key == key {
... ...
@@ -89,8 +72,9 @@ func (n Namespaces) Contains(ns string) bool {
89 89
 
90 90
 type (
91 91
 	Capability struct {
92
-		Key   string
93
-		Value capability.Cap
92
+		Key     string         `json:"key,omitempty"`
93
+		Enabled bool           `json:"enabled"`
94
+		Value   capability.Cap `json:"value,omitempty"`
94 95
 	}
95 96
 	Capabilities []*Capability
96 97
 )
... ...
@@ -99,23 +83,6 @@ func (c *Capability) String() string {
99 99
 	return c.Key
100 100
 }
101 101
 
102
-func (c *Capability) MarshalJSON() ([]byte, error) {
103
-	return json.Marshal(c.Key)
104
-}
105
-
106
-func (c *Capability) UnmarshalJSON(src []byte) error {
107
-	var capName string
108
-	if err := json.Unmarshal(src, &capName); err != nil {
109
-		return err
110
-	}
111
-	ret := GetCapability(capName)
112
-	if ret == nil {
113
-		return ErrUnkownCapability
114
-	}
115
-	*c = *ret
116
-	return nil
117
-}
118
-
119 102
 func GetCapability(key string) *Capability {
120 103
 	for _, capp := range capabilityList {
121 104
 		if capp.Key == key {
... ...
@@ -6,11 +6,11 @@ import (
6 6
 
7 7
 func init() {
8 8
 	namespaceList = Namespaces{
9
-		{Key: "NEWNS", Value: syscall.CLONE_NEWNS, File: "mnt"},
10
-		{Key: "NEWUTS", Value: syscall.CLONE_NEWUTS, File: "uts"},
11
-		{Key: "NEWIPC", Value: syscall.CLONE_NEWIPC, File: "ipc"},
12
-		{Key: "NEWUSER", Value: syscall.CLONE_NEWUSER, File: "user"},
13
-		{Key: "NEWPID", Value: syscall.CLONE_NEWPID, File: "pid"},
14
-		{Key: "NEWNET", Value: syscall.CLONE_NEWNET, File: "net"},
9
+		{Key: "NEWNS", Value: syscall.CLONE_NEWNS, File: "mnt", Enabled: true},
10
+		{Key: "NEWUTS", Value: syscall.CLONE_NEWUTS, File: "uts", Enabled: true},
11
+		{Key: "NEWIPC", Value: syscall.CLONE_NEWIPC, File: "ipc", Enabled: true},
12
+		{Key: "NEWUSER", Value: syscall.CLONE_NEWUSER, File: "user", Enabled: true},
13
+		{Key: "NEWPID", Value: syscall.CLONE_NEWPID, File: "pid", Enabled: true},
14
+		{Key: "NEWNET", Value: syscall.CLONE_NEWNET, File: "net", Enabled: true},
15 15
 	}
16 16
 }