Skip to content

Commit 71fc282

Browse files
committed
podsecurityreadinesscontroller: create filter without side-effects
1 parent d6adece commit 71fc282

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

pkg/operator/podsecurityreadinesscontroller/classification.go

Lines changed: 25 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,49 @@ 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+
// User-based SCCs are only causing violations if they are higher
70+
// privilege than whatever ServiceAccounts can provide. The
71+
// restricted-v* is the lowest privilege possible and can't elevate
72+
// privileges.
73+
// We watch for any restricted version above the first one (without a v).
74+
// We might introduce restricted-v3 for user namespaces.
8375
continue
8476
}
8577

8678
if pod.Annotations[securityv1.ValidatedSCCSubjectTypeAnnotation] == "user" {
8779
userPods = append(userPods, pod)
8880
}
8981
}
90-
9182
if len(userPods) == 0 {
92-
return false, nil // No user pods = violation is from service accounts
83+
return false // No user pods = violation is based upon service accounts
9384
}
9485

95-
enforcementVersion := psapi.LatestVersion()
86+
enforcement := psapi.LevelVersion{
87+
Level: enforcementLevel,
88+
Version: psapi.LatestVersion(),
89+
}
9690
for _, pod := range userPods {
97-
results := c.psaEvaluator.EvaluatePod(
98-
psapi.LevelVersion{Level: enforcementLevel, Version: enforcementVersion},
91+
results := psaEvaluator.EvaluatePod(
92+
enforcement,
9993
&pod.ObjectMeta,
10094
&pod.Spec,
10195
)
10296

97+
// results contains between 1 and 2 elements
10398
for _, result := range results {
10499
if !result.Allowed {
105-
return true, nil
100+
return true
106101
}
107102
}
108103
}
109104

110-
return false, nil
105+
return false
111106
}

0 commit comments

Comments
 (0)