forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
privileged.go
181 lines (159 loc) · 5.7 KB
/
privileged.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"encoding/json"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/latest"
"k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned"
"net/url"
)
const (
privilegedPodName = "privileged-pod"
privilegedContainerName = "privileged-container"
privilegedHttpPort = 8080
privilegedUdpPort = 8081
notPrivilegedHttpPort = 9090
notPrivilegedUdpPort = 9091
notPrivilegedContainerName = "not-privileged-container"
privilegedContainerImage = "gcr.io/google_containers/netexec:1.1"
privilegedCommand = "ip link add dummy1 type dummy"
)
type PrivilegedPodTestConfig struct {
privilegedPod *api.Pod
f *Framework
nodes []string
}
var _ = Describe("PrivilegedPod", func() {
f := NewFramework("e2e-privilegedpod")
config := &PrivilegedPodTestConfig{
f: f,
}
It("should test privileged pod", func() {
SkipUnlessProviderIs(providersWithSSH...)
By("Getting ssh-able hosts")
hosts, err := NodeSSHHosts(config.f.Client)
Expect(err).NotTo(HaveOccurred())
if len(hosts) == 0 {
Failf("No ssh-able nodes")
}
config.nodes = hosts
By("Creating a privileged pod")
config.createPrivilegedPod()
By("Executing privileged command on privileged container")
config.runPrivilegedCommandOnPrivilegedContainer()
By("Executing privileged command on non-privileged container")
config.runPrivilegedCommandOnNonPrivilegedContainer()
})
})
func (config *PrivilegedPodTestConfig) runPrivilegedCommandOnPrivilegedContainer() {
outputMap := config.dialFromContainer(config.privilegedPod.Status.PodIP, privilegedHttpPort)
if len(outputMap["error"]) > 0 {
Failf("Privileged command failed unexpectedly on privileged container, output:%v", outputMap)
}
}
func (config *PrivilegedPodTestConfig) runPrivilegedCommandOnNonPrivilegedContainer() {
outputMap := config.dialFromContainer(config.privilegedPod.Status.PodIP, notPrivilegedHttpPort)
if len(outputMap["error"]) == 0 {
Failf("Privileged command should have failed on non-privileged container, output:%v", outputMap)
}
}
func (config *PrivilegedPodTestConfig) dialFromContainer(containerIP string, containerHttpPort int) map[string]string {
v := url.Values{}
v.Set("shellCommand", "ip link add dummy1 type dummy")
cmd := fmt.Sprintf("curl -q 'http://%s:%d/shell?%s'",
containerIP,
containerHttpPort,
v.Encode())
By(fmt.Sprintf("Exec-ing into container over http. Running command:%s", cmd))
stdout := config.ssh(cmd)
Logf("Output is %q", stdout)
var output map[string]string
err := json.Unmarshal([]byte(stdout), &output)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Could not unmarshal curl response: %s", stdout))
Logf("Deserialized output is %v", stdout)
return output
}
func (config *PrivilegedPodTestConfig) createPrivilegedPodSpec() *api.Pod {
isPrivileged := true
notPrivileged := false
pod := &api.Pod{
TypeMeta: unversioned.TypeMeta{
Kind: "Pod",
APIVersion: latest.GroupOrDie("").Version,
},
ObjectMeta: api.ObjectMeta{
Name: privilegedPodName,
Namespace: config.f.Namespace.Name,
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: privilegedContainerName,
Image: privilegedContainerImage,
ImagePullPolicy: api.PullIfNotPresent,
SecurityContext: &api.SecurityContext{Privileged: &isPrivileged},
Command: []string{
"/netexec",
fmt.Sprintf("--http-port=%d", privilegedHttpPort),
fmt.Sprintf("--udp-port=%d", privilegedUdpPort),
},
},
{
Name: notPrivilegedContainerName,
Image: privilegedContainerImage,
ImagePullPolicy: api.PullIfNotPresent,
SecurityContext: &api.SecurityContext{Privileged: ¬Privileged},
Command: []string{
"/netexec",
fmt.Sprintf("--http-port=%d", notPrivilegedHttpPort),
fmt.Sprintf("--udp-port=%d", notPrivilegedUdpPort),
},
},
},
},
}
return pod
}
func (config *PrivilegedPodTestConfig) createPrivilegedPod() {
podSpec := config.createPrivilegedPodSpec()
config.privilegedPod = config.createPod(podSpec)
}
func (config *PrivilegedPodTestConfig) createPod(pod *api.Pod) *api.Pod {
createdPod, err := config.getPodClient().Create(pod)
if err != nil {
Failf("Failed to create %q pod: %v", pod.Name, err)
}
expectNoError(config.f.WaitForPodRunning(pod.Name))
createdPod, err = config.getPodClient().Get(pod.Name)
if err != nil {
Failf("Failed to retrieve %q pod: %v", pod.Name, err)
}
return createdPod
}
func (config *PrivilegedPodTestConfig) getPodClient() client.PodInterface {
return config.f.Client.Pods(config.f.Namespace.Name)
}
func (config *PrivilegedPodTestConfig) getNamespaceClient() client.NamespaceInterface {
return config.f.Client.Namespaces()
}
func (config *PrivilegedPodTestConfig) ssh(cmd string) string {
stdout, _, code, err := SSH(cmd, config.nodes[0], testContext.Provider)
Expect(err).NotTo(HaveOccurred(), "error while SSH-ing to node: %v (code %v)", err, code)
Expect(code).Should(BeZero(), "command exited with non-zero code %v. cmd:%s", code, cmd)
return stdout
}