Skip to content

Commit 3d5849f

Browse files
committed
admission: don't update psp annotation on update
1 parent 1a4a019 commit 3d5849f

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

plugin/pkg/admission/security/podsecuritypolicy/admission.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,16 @@ func (c *PodSecurityPolicyPlugin) Admit(a admission.Attributes) error {
119119
return nil
120120
}
121121

122+
// only mutate if this is a CREATE request. On updates we only validate.
123+
// TODO(liggitt): allow spec mutation during initializing updates?
124+
if a.GetOperation() != admission.Create {
125+
return nil
126+
}
127+
122128
pod := a.GetObject().(*api.Pod)
123129

124-
// compute the context. If the current security context is valid, this call won't change it.
125-
allowMutation := a.GetOperation() == admission.Create // TODO(liggitt): allow spec mutation during initializing updates?
126-
allowedPod, pspName, validationErrs, err := c.computeSecurityContext(a, pod, allowMutation)
130+
// compute the context
131+
allowedPod, pspName, validationErrs, err := c.computeSecurityContext(a, pod, true)
127132
if err != nil {
128133
return admission.NewForbidden(a, err)
129134
}
@@ -134,10 +139,6 @@ func (c *PodSecurityPolicyPlugin) Admit(a admission.Attributes) error {
134139
if pod.ObjectMeta.Annotations == nil {
135140
pod.ObjectMeta.Annotations = map[string]string{}
136141
}
137-
// set annotation to mark this as passed. Note, that the actual value is not important, the
138-
// validating PSP might even change later-on. Also not that pspName can be the empty string
139-
// if failOnNoPolicies is false.
140-
// TODO: if failOnNoPolicies is toggled from false to true, we will never update the annotation anymore. Is this desired?
141142
pod.ObjectMeta.Annotations[psputil.ValidatedPSPAnnotation] = pspName
142143
return nil
143144
}
@@ -156,7 +157,7 @@ func (c *PodSecurityPolicyPlugin) Validate(a admission.Attributes) error {
156157

157158
pod := a.GetObject().(*api.Pod)
158159

159-
// compute the context. If the current security context is valid, this call won't change it.
160+
// compute the context. Mutation is not allowed.
160161
allowedPod, _, validationErrs, err := c.computeSecurityContext(a, pod, false)
161162
if err != nil {
162163
return admission.NewForbidden(a, err)

plugin/pkg/admission/security/podsecuritypolicy/admission_test.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,19 @@ func TestAdmitPreferNonmutating(t *testing.T) {
329329
changedPod := unprivilegedRunAsAnyPod.DeepCopy()
330330
changedPod.Spec.Containers[0].Image = "myimage2"
331331

332+
podWithSC := unprivilegedRunAsAnyPod.DeepCopy()
333+
podWithSC.Annotations = map[string]string{psputil.ValidatedPSPAnnotation: privilegedPSP.Name}
334+
changedPodWithSC := changedPod.DeepCopy()
335+
changedPodWithSC.Annotations = map[string]string{psputil.ValidatedPSPAnnotation: privilegedPSP.Name}
336+
332337
gcChangedPod := unprivilegedRunAsAnyPod.DeepCopy()
333338
gcChangedPod.OwnerReferences = []metav1.OwnerReference{{Kind: "Foo", Name: "bar"}}
334339
gcChangedPod.Finalizers = []string{"foo"}
335340

336341
tests := map[string]struct {
337342
operation kadmission.Operation
338343
pod *kapi.Pod
339-
oldPod *kapi.Pod
344+
podBeforeUpdate *kapi.Pod
340345
psps []*extensions.PodSecurityPolicy
341346
shouldPassAdmit bool
342347
shouldPassValidate bool
@@ -380,8 +385,8 @@ func TestAdmitPreferNonmutating(t *testing.T) {
380385
},
381386
"pod should prefer non-mutating PSP on update": {
382387
operation: kadmission.Update,
383-
pod: unprivilegedRunAsAnyPod.DeepCopy(),
384-
oldPod: changedPod.DeepCopy(),
388+
pod: changedPodWithSC.DeepCopy(),
389+
podBeforeUpdate: podWithSC.DeepCopy(),
385390
psps: []*extensions.PodSecurityPolicy{mutating2, mutating1, privilegedPSP},
386391
shouldPassAdmit: true,
387392
shouldPassValidate: true,
@@ -390,12 +395,12 @@ func TestAdmitPreferNonmutating(t *testing.T) {
390395
expectedContainerUser: nil,
391396
expectedPSP: privilegedPSP.Name,
392397
},
393-
"pod should not allow mutation on update": {
398+
"pod should not mutate on update, but fail validation": {
394399
operation: kadmission.Update,
395-
pod: unprivilegedRunAsAnyPod.DeepCopy(),
396-
oldPod: changedPod.DeepCopy(),
400+
pod: changedPod.DeepCopy(),
401+
podBeforeUpdate: unprivilegedRunAsAnyPod.DeepCopy(),
397402
psps: []*extensions.PodSecurityPolicy{mutating2, mutating1},
398-
shouldPassAdmit: false,
403+
shouldPassAdmit: true,
399404
shouldPassValidate: false,
400405
expectMutation: false,
401406
expectedPodUser: nil,
@@ -405,7 +410,7 @@ func TestAdmitPreferNonmutating(t *testing.T) {
405410
"pod should be allowed if completely unchanged on update": {
406411
operation: kadmission.Update,
407412
pod: unprivilegedRunAsAnyPod.DeepCopy(),
408-
oldPod: unprivilegedRunAsAnyPod.DeepCopy(),
413+
podBeforeUpdate: unprivilegedRunAsAnyPod.DeepCopy(),
409414
psps: []*extensions.PodSecurityPolicy{mutating2, mutating1},
410415
shouldPassAdmit: true,
411416
shouldPassValidate: true,
@@ -416,8 +421,8 @@ func TestAdmitPreferNonmutating(t *testing.T) {
416421
},
417422
"pod should be allowed if unchanged on update except finalizers,ownerrefs": {
418423
operation: kadmission.Update,
419-
pod: unprivilegedRunAsAnyPod.DeepCopy(),
420-
oldPod: gcChangedPod.DeepCopy(),
424+
pod: gcChangedPod.DeepCopy(),
425+
podBeforeUpdate: unprivilegedRunAsAnyPod.DeepCopy(),
421426
psps: []*extensions.PodSecurityPolicy{mutating2, mutating1},
422427
shouldPassAdmit: true,
423428
shouldPassValidate: true,
@@ -429,7 +434,7 @@ func TestAdmitPreferNonmutating(t *testing.T) {
429434
}
430435

431436
for k, v := range tests {
432-
testPSPAdmitAdvanced(k, v.operation, v.psps, v.pod, v.oldPod, v.shouldPassAdmit, v.shouldPassValidate, v.expectMutation, v.expectedPSP, t)
437+
testPSPAdmitAdvanced(k, v.operation, v.psps, v.pod, v.podBeforeUpdate, v.shouldPassAdmit, v.shouldPassValidate, v.expectMutation, v.expectedPSP, t)
433438

434439
if v.shouldPassAdmit {
435440
actualPodUser := (*int64)(nil)

0 commit comments

Comments
 (0)