Skip to content

Commit 5dbe3e3

Browse files
authored
Merge pull request kubernetes#91737 from liggitt/psp-detect
Detect PSP enablement more accurately
2 parents 1bc526a + 0e06298 commit 5dbe3e3

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.
127127
// compute the context. Mutation is allowed. ValidatedPSPAnnotation is not taken into account.
128128
allowedPod, pspName, validationErrs, err := p.computeSecurityContext(ctx, a, pod, true, "")
129129
if err != nil {
130-
return admission.NewForbidden(a, err)
130+
return admission.NewForbidden(a, fmt.Errorf("PodSecurityPolicy: %w", err))
131131
}
132132
if allowedPod != nil {
133133
*pod = *allowedPod
@@ -145,8 +145,8 @@ func (p *Plugin) Admit(ctx context.Context, a admission.Attributes, o admission.
145145
}
146146

147147
// we didn't validate against any provider, reject the pod and give the errors for each attempt
148-
klog.V(4).Infof("unable to validate pod %s (generate: %s) in namespace %s against any pod security policy: %v", pod.Name, pod.GenerateName, a.GetNamespace(), validationErrs)
149-
return admission.NewForbidden(a, fmt.Errorf("unable to validate against any pod security policy: %v", validationErrs))
148+
klog.V(4).Infof("unable to admit pod %s (generate: %s) in namespace %s against any pod security policy: %v", pod.Name, pod.GenerateName, a.GetNamespace(), validationErrs)
149+
return admission.NewForbidden(a, fmt.Errorf("PodSecurityPolicy: unable to admit pod: %v", validationErrs))
150150
}
151151

152152
// Validate verifies attributes against the PodSecurityPolicy
@@ -162,7 +162,7 @@ func (p *Plugin) Validate(ctx context.Context, a admission.Attributes, o admissi
162162
// compute the context. Mutation is not allowed. ValidatedPSPAnnotation is used as a hint to gain same speed-up.
163163
allowedPod, pspName, validationErrs, err := p.computeSecurityContext(ctx, a, pod, false, pod.ObjectMeta.Annotations[psputil.ValidatedPSPAnnotation])
164164
if err != nil {
165-
return admission.NewForbidden(a, err)
165+
return admission.NewForbidden(a, fmt.Errorf("PodSecurityPolicy: %w", err))
166166
}
167167
if apiequality.Semantic.DeepEqual(pod, allowedPod) {
168168
key := auditKeyPrefix + "/" + "validate-policy"
@@ -174,7 +174,7 @@ func (p *Plugin) Validate(ctx context.Context, a admission.Attributes, o admissi
174174

175175
// we didn't validate against any provider, reject the pod and give the errors for each attempt
176176
klog.V(4).Infof("unable to validate pod %s (generate: %s) in namespace %s against any pod security policy: %v", pod.Name, pod.GenerateName, a.GetNamespace(), validationErrs)
177-
return admission.NewForbidden(a, fmt.Errorf("unable to validate against any pod security policy: %v", validationErrs))
177+
return admission.NewForbidden(a, fmt.Errorf("PodSecurityPolicy: unable to validate pod: %v", validationErrs))
178178
}
179179

180180
func shouldIgnore(a admission.Attributes) (bool, error) {

test/e2e/framework/psp.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package framework
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223
"sync"
2324

2425
v1 "k8s.io/api/core/v1"
@@ -29,6 +30,7 @@ import (
2930
"k8s.io/apimachinery/pkg/runtime/schema"
3031
"k8s.io/apiserver/pkg/authentication/serviceaccount"
3132
clientset "k8s.io/client-go/kubernetes"
33+
imageutils "k8s.io/kubernetes/test/utils/image"
3234

3335
"github.com/onsi/ginkgo"
3436

@@ -92,14 +94,34 @@ func IsPodSecurityPolicyEnabled(kubeClient clientset.Interface) bool {
9294
psps, err := kubeClient.PolicyV1beta1().PodSecurityPolicies().List(context.TODO(), metav1.ListOptions{})
9395
if err != nil {
9496
Logf("Error listing PodSecurityPolicies; assuming PodSecurityPolicy is disabled: %v", err)
95-
isPSPEnabled = false
96-
} else if psps == nil || len(psps.Items) == 0 {
97+
return
98+
}
99+
if psps == nil || len(psps.Items) == 0 {
97100
Logf("No PodSecurityPolicies found; assuming PodSecurityPolicy is disabled.")
98-
isPSPEnabled = false
99-
} else {
100-
Logf("Found PodSecurityPolicies; assuming PodSecurityPolicy is enabled.")
101-
isPSPEnabled = true
101+
return
102+
}
103+
Logf("Found PodSecurityPolicies; testing pod creation to see if PodSecurityPolicy is enabled")
104+
testPod := &v1.Pod{
105+
ObjectMeta: metav1.ObjectMeta{GenerateName: "psp-test-pod-"},
106+
Spec: v1.PodSpec{Containers: []v1.Container{{Name: "test", Image: imageutils.GetPauseImageName()}}},
107+
}
108+
dryRunPod, err := kubeClient.CoreV1().Pods("kube-system").Create(context.TODO(), testPod, metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}})
109+
if err != nil {
110+
if strings.Contains(err.Error(), "PodSecurityPolicy") {
111+
Logf("PodSecurityPolicy error creating dryrun pod; assuming PodSecurityPolicy is enabled: %v", err)
112+
isPSPEnabled = true
113+
} else {
114+
Logf("Error creating dryrun pod; assuming PodSecurityPolicy is disabled: %v", err)
115+
}
116+
return
117+
}
118+
pspAnnotation, pspAnnotationExists := dryRunPod.Annotations["kubernetes.io/psp"]
119+
if !pspAnnotationExists {
120+
Logf("No PSP annotation exists on dry run pod; assuming PodSecurityPolicy is disabled")
121+
return
102122
}
123+
Logf("PSP annotation exists on dry run pod: %q; assuming PodSecurityPolicy is enabled", pspAnnotation)
124+
isPSPEnabled = true
103125
})
104126
return isPSPEnabled
105127
}

0 commit comments

Comments
 (0)