@@ -32,49 +32,63 @@ import (
32
32
)
33
33
34
34
var _ = describe ("PSP use" , func () {
35
- privilegedRole := "privileged-psp"
36
35
privilegedSA := "privileged-sa"
37
36
f := framework .NewDefaultFramework ("psp" )
37
+ f .SkipNamespaceCreation = true
38
38
f .NamespacePodSecurityEnforceLevel = admissionapi .LevelPrivileged
39
39
var cs kubernetes.Interface
40
40
41
41
BeforeEach (func () {
42
42
cs = f .ClientSet
43
- saObj := createServiceAccount (f .Namespace .Name , privilegedSA )
44
- _ , err := cs .CoreV1 ().ServiceAccounts (f .Namespace .Name ).Create (context .TODO (), saObj , metav1.CreateOptions {})
43
+ })
44
+
45
+ It ("Should not create a privileged POD if restricted SA [PSP] [Zalando]" , func () {
46
+ defaultSA := "default"
47
+ ns := "psp-restricted-zalando"
48
+ _ , err := cs .CoreV1 ().Namespaces ().Create (context .TODO (), & v1.Namespace {
49
+ ObjectMeta : metav1.ObjectMeta {
50
+ Name : ns ,
51
+ },
52
+ }, metav1.CreateOptions {})
45
53
Expect (err ).NotTo (HaveOccurred ())
46
54
47
- _ , err = cs .RbacV1 ().RoleBindings (f .Namespace .Name ).Create (context .TODO (), createRBACRoleBindingSA (privilegedRole , f .Namespace .Name , privilegedSA ), metav1.CreateOptions {})
55
+ // create SA
56
+ saObj := createServiceAccount (ns , privilegedSA )
57
+ _ , err = cs .CoreV1 ().ServiceAccounts (ns ).Create (context .TODO (), saObj , metav1.CreateOptions {})
48
58
Expect (err ).NotTo (HaveOccurred ())
49
- })
50
59
51
- // TODO: We have to have a solution to get an unprivileged
52
- // User to check this, if not it would always create a
53
- // privileged POD for an unprivileged serviceAccount.
54
- // --
55
- // It("Should not create a POD that use privileged PSP [PSP] [Zalando]", func() {
56
- // defaultSA := "default"
57
- // ns := f.Namespace.Name
58
- // label := map[string]string{
59
- // "app": "psp",
60
- // }
61
- // msg := fmt.Sprintf("Creating a privileged POD as %s", defaultSA)
62
- // By(msg)
63
- // route := fmt.Sprintf(`* -> inlineContent("%s") -> <shunt>`, "OK")
64
- // pod := createSkipperPodWithHostNetwork("", ns, defaultSA, route, label, 80)
65
- // defer func() {
66
- // By(msg)
67
- // defer GinkgoRecover()
68
- // err := cs.CoreV1().Pods(ns).Delete(pod.Name, metav1.NewDeleteOptions(0))
69
- // Expect(err).To(HaveOccurred())
70
- // }()
71
- // _, err := cs.CoreV1().Pods(ns).Create(pod)
72
- // Expect(err).To(HaveOccurred())
73
- // framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
74
- // })
60
+ label := map [string ]string {
61
+ "app" : "psp" ,
62
+ }
63
+ msg := fmt .Sprintf ("Creating a privileged POD as %s" , defaultSA )
64
+ By (msg )
65
+ route := fmt .Sprintf (`* -> inlineContent("%s") -> <shunt>` , "OK" )
66
+ pod := createSkipperPodWithHostNetwork ("" , ns , defaultSA , route , label , 80 )
67
+ defer func () {
68
+ By (msg )
69
+ defer GinkgoRecover ()
70
+
71
+ err = cs .CoreV1 ().Namespaces ().Delete (context .TODO (), ns , metav1.DeleteOptions {})
72
+ Expect (err ).NotTo (HaveOccurred ())
73
+ }()
74
+ _ , err = cs .CoreV1 ().Pods (ns ).Create (context .TODO (), pod , metav1.CreateOptions {})
75
+ Expect (err ).To (HaveOccurred ())
76
+ })
75
77
76
78
It ("Should create a POD that use privileged PSP [PSP] [Zalando]" , func () {
77
- ns := f .Namespace .Name
79
+ ns := "psp-privileged-zalando"
80
+ _ , err := cs .CoreV1 ().Namespaces ().Create (context .TODO (), & v1.Namespace {
81
+ ObjectMeta : metav1.ObjectMeta {
82
+ Name : ns ,
83
+ },
84
+ }, metav1.CreateOptions {})
85
+ Expect (err ).NotTo (HaveOccurred ())
86
+
87
+ // create SA
88
+ saObj := createServiceAccount (ns , privilegedSA )
89
+ _ , err = cs .CoreV1 ().ServiceAccounts (ns ).Create (context .TODO (), saObj , metav1.CreateOptions {})
90
+ Expect (err ).NotTo (HaveOccurred ())
91
+
78
92
label := map [string ]string {
79
93
"app" : "psp" ,
80
94
}
@@ -89,16 +103,31 @@ var _ = describe("PSP use", func() {
89
103
defer GinkgoRecover ()
90
104
err := cs .CoreV1 ().Pods (ns ).Delete (context .TODO (), pod .Name , metav1.DeleteOptions {})
91
105
Expect (err ).NotTo (HaveOccurred ())
106
+
107
+ err = cs .CoreV1 ().Namespaces ().Delete (context .TODO (), ns , metav1.DeleteOptions {})
108
+ Expect (err ).NotTo (HaveOccurred ())
92
109
}()
93
110
94
- _ , err : = cs .CoreV1 ().Pods (ns ).Create (context .TODO (), pod , metav1.CreateOptions {})
111
+ _ , err = cs .CoreV1 ().Pods (ns ).Create (context .TODO (), pod , metav1.CreateOptions {})
95
112
Expect (err ).NotTo (HaveOccurred ())
96
113
97
114
framework .ExpectNoError (e2epod .WaitForPodNameRunningInNamespace (f .ClientSet , pod .Name , pod .Namespace ))
98
115
})
99
116
100
117
It ("Should create a POD that use privileged PSP via deployment [PSP] [Zalando]" , func () {
101
- ns := f .Namespace .Name
118
+ ns := "psp-privileged-deployment-zalando"
119
+ _ , err := cs .CoreV1 ().Namespaces ().Create (context .TODO (), & v1.Namespace {
120
+ ObjectMeta : metav1.ObjectMeta {
121
+ Name : ns ,
122
+ },
123
+ }, metav1.CreateOptions {})
124
+ Expect (err ).NotTo (HaveOccurred ())
125
+
126
+ // create SA
127
+ saObj := createServiceAccount (ns , privilegedSA )
128
+ _ , err = cs .CoreV1 ().ServiceAccounts (ns ).Create (context .TODO (), saObj , metav1.CreateOptions {})
129
+ Expect (err ).NotTo (HaveOccurred ())
130
+
102
131
label := map [string ]string {
103
132
"app" : "psp" ,
104
133
}
@@ -117,6 +146,9 @@ var _ = describe("PSP use", func() {
117
146
defer GinkgoRecover ()
118
147
err := cs .AppsV1 ().Deployments (ns ).Delete (context .TODO (), d .Name , metav1.DeleteOptions {})
119
148
Expect (err ).NotTo (HaveOccurred ())
149
+
150
+ err = cs .CoreV1 ().Namespaces ().Delete (context .TODO (), ns , metav1.DeleteOptions {})
151
+ Expect (err ).NotTo (HaveOccurred ())
120
152
}()
121
153
122
154
deploy , err := cs .AppsV1 ().Deployments (ns ).Create (context .TODO (), d , metav1.CreateOptions {})
@@ -134,7 +166,7 @@ var _ = describe("PSP use", func() {
134
166
Expect (err ).NotTo (HaveOccurred ())
135
167
By (fmt .Sprintf ("Got rs: %s, from deployment: %s" , rs .Name , deploy .Name ))
136
168
137
- pods , err := e2epod .PodsCreatedByLabel (f .ClientSet , f . Namespace . Name , rs .Name , replicas , labelSelector )
169
+ pods , err := e2epod .PodsCreatedByLabel (f .ClientSet , ns , rs .Name , replicas , labelSelector )
138
170
Expect (err ).NotTo (HaveOccurred ())
139
171
By (fmt .Sprintf ("Ensuring each pod is running for rs: %s, pod: %s" , rs .Name , pods .Items [0 ].Name ))
140
172
// Wait for the pods to enter the running state. Waiting loops until the pods
0 commit comments