Skip to content

Commit 3a06aeb

Browse files
committed
podsecurityreadinesscontroller: create filter without side-effects
1 parent d6adece commit 3a06aeb

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

pkg/operator/podsecurityreadinesscontroller/classification.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"k8s.io/apimachinery/pkg/util/sets"
1111
"k8s.io/klog/v2"
1212
psapi "k8s.io/pod-security-admission/api"
13+
"k8s.io/pod-security-admission/policy"
1314
)
1415

1516
var (
@@ -40,15 +41,12 @@ func (c *PodSecurityReadinessController) classifyViolatingNamespace(
4041
return nil
4142
}
4243

43-
isUserViolation, err := c.isUserViolation(ctx, ns, enforceLevel)
44+
allPods, err := c.kubeClient.CoreV1().Pods(ns.Name).List(ctx, metav1.ListOptions{})
4445
if err != nil {
45-
klog.V(2).ErrorS(err, "Error checking user violations", "namespace", ns.Name)
46-
// Transient API server error or temporary resource unavailability (most likely).
47-
// Theoretically, psapi parsing errors could occur that retry without hope for recovery.
46+
klog.V(2).ErrorS(err, "Failed to list pods in namespace", "namespace", ns.Name)
4847
return err
4948
}
50-
51-
if isUserViolation {
49+
if hasUserSCCViolatingPod(c.psaEvaluator, enforceLevel, allPods.Items) {
5250
conditions.addViolatingUserSCC(ns)
5351
return nil
5452
}
@@ -60,52 +58,45 @@ func (c *PodSecurityReadinessController) classifyViolatingNamespace(
6058
return nil
6159
}
6260

63-
func (c *PodSecurityReadinessController) isUserViolation(
64-
ctx context.Context,
65-
ns *corev1.Namespace,
61+
func hasUserSCCViolatingPod(
62+
psaEvaluator policy.Evaluator,
6663
enforcementLevel psapi.Level,
67-
) (bool, error) {
68-
allPods, err := c.kubeClient.CoreV1().Pods(ns.Name).List(ctx, metav1.ListOptions{})
69-
if err != nil {
70-
klog.V(2).ErrorS(err, "Failed to list pods in namespace", "namespace", ns.Name)
71-
return false, err
72-
}
73-
64+
pods []corev1.Pod,
65+
) bool {
7466
var userPods []corev1.Pod
75-
for _, pod := range allPods.Items {
67+
for _, pod := range pods {
7668
if strings.HasPrefix(pod.Annotations[securityv1.ValidatedSCCAnnotation], "restricted-v") {
77-
// restricted-v2 is allowed for all system:authenticated, also for ServiceAccounts.
78-
// But ServiceAccounts are not part of the group. So restricted-v2 will always
79-
// result in user-based SCC. So we skip them as the user-based SCCs cause harm
80-
// if they need a higher privileged than restricted.
81-
// We watch for any restricted version above the first one. We might introduce
82-
// restricted-v3 for user namespaces.
69+
// If the SCC evaluation is restricted-v*, it shouldn't be possible
70+
// to violate as a user-based SCC.
8371
continue
8472
}
8573

8674
if pod.Annotations[securityv1.ValidatedSCCSubjectTypeAnnotation] == "user" {
8775
userPods = append(userPods, pod)
8876
}
8977
}
90-
9178
if len(userPods) == 0 {
92-
return false, nil // No user pods = violation is from service accounts
79+
return false // No user pods = violation is based upon service accounts
9380
}
9481

95-
enforcementVersion := psapi.LatestVersion()
82+
enforcement := psapi.LevelVersion{
83+
Level: enforcementLevel,
84+
Version: psapi.LatestVersion(),
85+
}
9686
for _, pod := range userPods {
97-
results := c.psaEvaluator.EvaluatePod(
98-
psapi.LevelVersion{Level: enforcementLevel, Version: enforcementVersion},
87+
results := psaEvaluator.EvaluatePod(
88+
enforcement,
9989
&pod.ObjectMeta,
10090
&pod.Spec,
10191
)
10292

93+
// results contains between 1 and 2 elements
10394
for _, result := range results {
10495
if !result.Allowed {
105-
return true, nil
96+
return true
10697
}
10798
}
10899
}
109100

110-
return false, nil
101+
return false
111102
}

0 commit comments

Comments
 (0)