@@ -17,129 +17,98 @@ limitations under the License.
17
17
package common
18
18
19
19
import (
20
- "encoding/json"
21
20
"fmt"
22
- "net/url"
23
21
24
22
. "github.com/onsi/ginkgo"
25
23
. "github.com/onsi/gomega"
26
24
"k8s.io/kubernetes/pkg/api/v1"
27
25
"k8s.io/kubernetes/test/e2e/framework"
28
26
)
29
27
30
- const (
31
- privilegedPodName = "privileged-pod"
32
- privilegedContainerName = "privileged-container"
33
- privilegedHttpPort = 8080
34
- privilegedUdpPort = 8081
35
- notPrivilegedHttpPort = 9090
36
- notPrivilegedUdpPort = 9091
37
- notPrivilegedContainerName = "not-privileged-container"
38
- privilegedContainerImage = "gcr.io/google_containers/netexec:1.7"
39
- privilegedCommand = "ip link add dummy1 type dummy"
40
- )
41
-
42
28
type PrivilegedPodTestConfig struct {
43
- privilegedPod * v1.Pod
44
- f * framework.Framework
45
- hostExecPod * v1.Pod
29
+ f * framework.Framework
30
+
31
+ privilegedPod string
32
+ privilegedContainer string
33
+ notPrivilegedContainer string
34
+
35
+ pod * v1.Pod
46
36
}
47
37
48
38
var _ = framework .KubeDescribe ("PrivilegedPod" , func () {
49
- f := framework .NewDefaultFramework ("e2e-privilegedpod" )
50
39
config := & PrivilegedPodTestConfig {
51
- f : f ,
40
+ f : framework .NewDefaultFramework ("e2e-privileged-pod" ),
41
+ privilegedPod : "privileged-pod" ,
42
+ privilegedContainer : "privileged-container" ,
43
+ notPrivilegedContainer : "not-privileged-container" ,
52
44
}
53
- It ("should test privileged pod" , func () {
54
- By ("Creating a hostexec pod" )
55
- config .createHostExecPod ()
56
45
57
- By ("Creating a privileged pod" )
58
- config .createPrivilegedPod ()
46
+ It ("should enable privileged commands" , func () {
47
+ By ("Creating a pod with a privileged container" )
48
+ config .createPods ()
59
49
60
- By ("Executing privileged command on privileged container" )
61
- config .runPrivilegedCommandOnPrivilegedContainer ( )
50
+ By ("Executing in the privileged container" )
51
+ config .run ( config . privilegedContainer , true )
62
52
63
- By ("Executing privileged command on non-privileged container" )
64
- config .runPrivilegedCommandOnNonPrivilegedContainer ( )
53
+ By ("Executing in the non-privileged container" )
54
+ config .run ( config . notPrivilegedContainer , false )
65
55
})
66
56
})
67
57
68
- func (config * PrivilegedPodTestConfig ) runPrivilegedCommandOnPrivilegedContainer () {
69
- outputMap := config .dialFromContainer (config .privilegedPod .Status .PodIP , privilegedHttpPort )
70
- if len (outputMap ["error" ]) > 0 {
71
- framework .Failf ("Privileged command failed unexpectedly on privileged container, output:%v" , outputMap )
72
- }
73
- }
74
-
75
- func (config * PrivilegedPodTestConfig ) runPrivilegedCommandOnNonPrivilegedContainer () {
76
- outputMap := config .dialFromContainer (config .privilegedPod .Status .PodIP , notPrivilegedHttpPort )
77
- if len (outputMap ["error" ]) == 0 {
78
- framework .Failf ("Privileged command should have failed on non-privileged container, output:%v" , outputMap )
58
+ func (c * PrivilegedPodTestConfig ) run (containerName string , expectSuccess bool ) {
59
+ cmd := []string {"ip" , "link" , "add" , "dummy1" , "type" , "dummy" }
60
+ reverseCmd := []string {"ip" , "link" , "del" , "dummy1" }
61
+
62
+ stdout , stderr , err := c .f .ExecCommandInContainerWithFullOutput (
63
+ c .privilegedPod , containerName , cmd ... )
64
+ msg := fmt .Sprintf ("cmd %v, stdout %q, stderr %q" , cmd , stdout , stderr )
65
+
66
+ if expectSuccess {
67
+ Expect (err ).NotTo (HaveOccurred (), msg )
68
+ // We need to clean up the dummy link that was created, as it
69
+ // leaks out into the node level -- yuck.
70
+ _ , _ , err := c .f .ExecCommandInContainerWithFullOutput (
71
+ c .privilegedPod , containerName , reverseCmd ... )
72
+ Expect (err ).NotTo (HaveOccurred (),
73
+ fmt .Sprintf ("could not remove dummy1 link: %v" , err ))
74
+ } else {
75
+ Expect (err ).To (HaveOccurred (), msg )
79
76
}
80
77
}
81
78
82
- func (config * PrivilegedPodTestConfig ) dialFromContainer (containerIP string , containerHttpPort int ) map [string ]string {
83
- v := url.Values {}
84
- v .Set ("shellCommand" , "ip link add dummy1 type dummy" )
85
- cmd := fmt .Sprintf ("curl -q 'http://%s:%d/shell?%s'" ,
86
- containerIP ,
87
- containerHttpPort ,
88
- v .Encode ())
89
-
90
- By (fmt .Sprintf ("Exec-ing into container over http. Running command:%s" , cmd ))
91
- stdout := config .f .ExecShellInPod (config .hostExecPod .Name , cmd )
92
- var output map [string ]string
93
- err := json .Unmarshal ([]byte (stdout ), & output )
94
- Expect (err ).NotTo (HaveOccurred (), fmt .Sprintf ("Could not unmarshal curl response: %s" , stdout ))
95
- framework .Logf ("Deserialized output is %v" , stdout )
96
- return output
97
- }
98
-
99
- func (config * PrivilegedPodTestConfig ) createPrivilegedPodSpec () * v1.Pod {
79
+ func (c * PrivilegedPodTestConfig ) createPodsSpec () * v1.Pod {
100
80
isPrivileged := true
101
81
notPrivileged := false
102
- pod := & v1.Pod {
82
+
83
+ const image = "gcr.io/google_containers/busybox:1.24"
84
+
85
+ return & v1.Pod {
103
86
ObjectMeta : v1.ObjectMeta {
104
- Name : privilegedPodName ,
105
- Namespace : config .f .Namespace .Name ,
87
+ Name : c . privilegedPod ,
88
+ Namespace : c .f .Namespace .Name ,
106
89
},
107
90
Spec : v1.PodSpec {
108
91
Containers : []v1.Container {
109
92
{
110
- Name : privilegedContainerName ,
111
- Image : privilegedContainerImage ,
93
+ Name : c . privilegedContainer ,
94
+ Image : image ,
112
95
ImagePullPolicy : v1 .PullIfNotPresent ,
113
96
SecurityContext : & v1.SecurityContext {Privileged : & isPrivileged },
114
- Command : []string {
115
- "/netexec" ,
116
- fmt .Sprintf ("--http-port=%d" , privilegedHttpPort ),
117
- fmt .Sprintf ("--udp-port=%d" , privilegedUdpPort ),
118
- },
97
+ Command : []string {"/bin/sleep" , "10000" },
119
98
},
120
99
{
121
- Name : notPrivilegedContainerName ,
122
- Image : privilegedContainerImage ,
100
+ Name : c . notPrivilegedContainer ,
101
+ Image : image ,
123
102
ImagePullPolicy : v1 .PullIfNotPresent ,
124
103
SecurityContext : & v1.SecurityContext {Privileged : & notPrivileged },
125
- Command : []string {
126
- "/netexec" ,
127
- fmt .Sprintf ("--http-port=%d" , notPrivilegedHttpPort ),
128
- fmt .Sprintf ("--udp-port=%d" , notPrivilegedUdpPort ),
129
- },
104
+ Command : []string {"/bin/sleep" , "10000" },
130
105
},
131
106
},
132
107
},
133
108
}
134
- return pod
135
- }
136
-
137
- func (config * PrivilegedPodTestConfig ) createHostExecPod () {
138
- podSpec := framework .NewHostExecPodSpec (config .f .Namespace .Name , "hostexec" )
139
- config .hostExecPod = config .f .PodClient ().CreateSync (podSpec )
140
109
}
141
110
142
- func (config * PrivilegedPodTestConfig ) createPrivilegedPod () {
143
- podSpec := config . createPrivilegedPodSpec ()
144
- config . privilegedPod = config .f .PodClient ().CreateSync (podSpec )
111
+ func (c * PrivilegedPodTestConfig ) createPods () {
112
+ podSpec := c . createPodsSpec ()
113
+ c . pod = c .f .PodClient ().CreateSync (podSpec )
145
114
}
0 commit comments