Skip to content

Commit b66e3b4

Browse files
committed
make sure that GC can delete privileged pods
1 parent 5ae844b commit b66e3b4

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

pkg/build/admission/strategyrestrictions/admission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (a *buildByStrategy) Admit(attr admission.Attributes) error {
5050
// if this is an update, see if we are only updating the ownerRef. Garbage collection does this
5151
// and we should allow it in general, since you had the power to update and the power to delete.
5252
// The worst that happens is that you delete something, but you aren't controlling the privileged object itself
53-
if attr.GetOldObject() != nil && oadmission.IsOnlyMutatingOwnerRefs(attr.GetObject(), attr.GetOldObject()) {
53+
if attr.GetOldObject() != nil && oadmission.IsOnlyMutatingGCFields(attr.GetObject(), attr.GetOldObject()) {
5454
return nil
5555
}
5656

pkg/cmd/server/admission/helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
kapi "k8s.io/kubernetes/pkg/api"
77
)
88

9-
func IsOnlyMutatingOwnerRefs(obj, old runtime.Object) bool {
9+
// IsOnlyMutatingGCFields checks finalizers and ownerrefs which GC manipulates
10+
// and indicates that only those fields are changing
11+
func IsOnlyMutatingGCFields(obj, old runtime.Object) bool {
1012
// make a copy of the newObj so that we can stomp for comparison
1113
copied, err := kapi.Scheme.Copy(obj)
1214
if err != nil {
@@ -22,6 +24,7 @@ func IsOnlyMutatingOwnerRefs(obj, old runtime.Object) bool {
2224
return false
2325
}
2426
copiedMeta.SetOwnerReferences(oldMeta.GetOwnerReferences())
27+
copiedMeta.SetFinalizers(oldMeta.GetFinalizers())
2528

2629
return kapi.Semantic.DeepEqual(copied, old)
2730
}

pkg/cmd/server/admission/helpers_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func newPod() *kapi.Pod {
1919

2020
}
2121

22-
func TestIsOnlyMutatingOwnerRefs(t *testing.T) {
22+
func TestIsOnlyMutatingGCFields(t *testing.T) {
2323
tests := []struct {
2424
name string
2525
obj func() runtime.Object
@@ -72,6 +72,19 @@ func TestIsOnlyMutatingOwnerRefs(t *testing.T) {
7272
},
7373
expected: true,
7474
},
75+
{
76+
name: "ownerRef and finalizer",
77+
obj: func() runtime.Object {
78+
obj := newPod()
79+
obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"})
80+
obj.Finalizers = []string{"final"}
81+
return obj
82+
},
83+
old: func() runtime.Object {
84+
return newPod()
85+
},
86+
expected: true,
87+
},
7588
{
7689
name: "and annotations",
7790
obj: func() runtime.Object {
@@ -101,7 +114,7 @@ func TestIsOnlyMutatingOwnerRefs(t *testing.T) {
101114
}
102115

103116
for _, tc := range tests {
104-
actual := IsOnlyMutatingOwnerRefs(tc.obj(), tc.old())
117+
actual := IsOnlyMutatingGCFields(tc.obj(), tc.old())
105118
if tc.expected != actual {
106119
t.Errorf("%s: expected %v, got %v", tc.name, tc.expected, actual)
107120
}

pkg/security/admission/admission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (c *constraint) Admit(a admission.Attributes) error {
7777
// if this is an update, see if we are only updating the ownerRef. Garbage collection does this
7878
// and we should allow it in general, since you had the power to update and the power to delete.
7979
// The worst that happens is that you delete something, but you aren't controlling the privileged object itself
80-
if a.GetOldObject() != nil && oadmission.IsOnlyMutatingOwnerRefs(a.GetObject(), a.GetOldObject()) {
80+
if a.GetOldObject() != nil && oadmission.IsOnlyMutatingGCFields(a.GetObject(), a.GetOldObject()) {
8181
return nil
8282
}
8383

test/cmd/policy.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ os::cmd::expect_success_and_not_text 'oc get rolebinding/cluster-admin --no-head
5050
os::cmd::expect_success 'oc policy remove-group system:unauthenticated'
5151
os::cmd::expect_success 'oc policy remove-user system:no-user'
5252

53+
# check to make sure that our SCC policies don't prevent GC from deleting pods
54+
os::cmd::expect_success 'oc create -f ${OS_ROOT}/test/testdata/privileged-pod.yaml'
55+
os::cmd::expect_success 'oc delete pod/test-build-pod-issue --cascade=false'
56+
os::cmd::try_until_failure 'oc get pods pod/test-build-pod-issue'
57+
5358

5459
os::cmd::expect_success_and_text 'oc policy add-role-to-user admin namespaced-user' 'role "admin" added: "namespaced-user"'
5560
# Ensure the user has create permissions on builds, but that build strategy permissions are granted through the authenticated users group

test/testdata/privileged-pod.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
annotations:
5+
openshift.io/scc: privileged
6+
name: test-build-pod-issue
7+
spec:
8+
containers:
9+
- image: openshift/hello-openshift
10+
imagePullPolicy: IfNotPresent
11+
name: hello
12+
securityContext:
13+
privileged: true
14+
restartPolicy: Never

0 commit comments

Comments
 (0)