|
| 1 | +package podsecurityreadinesscontroller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + securityv1 "github.com/openshift/api/security/v1" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/util/sets" |
| 12 | + "k8s.io/klog/v2" |
| 13 | + psapi "k8s.io/pod-security-admission/api" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + runLevelZeroNamespaces = sets.New[string]( |
| 18 | + "default", |
| 19 | + "kube-system", |
| 20 | + "kube-public", |
| 21 | + "kube-node-lease", |
| 22 | + ) |
| 23 | +) |
| 24 | + |
| 25 | +func (c *PodSecurityReadinessController) classifyViolatingNamespace(ctx context.Context, conditions *podSecurityOperatorConditions, ns *corev1.Namespace, enforceLevel string) error { |
| 26 | + if runLevelZeroNamespaces.Has(ns.Name) { |
| 27 | + conditions.addViolatingRunLevelZero(ns) |
| 28 | + return nil |
| 29 | + } |
| 30 | + if strings.HasPrefix(ns.Name, "openshift") { |
| 31 | + conditions.addViolatingOpenShift(ns) |
| 32 | + return nil |
| 33 | + } |
| 34 | + if ns.Labels[labelSyncControlLabel] == "false" { |
| 35 | + conditions.addViolatingDisabledSyncer(ns) |
| 36 | + return nil |
| 37 | + } |
| 38 | + |
| 39 | + // TODO@ibihim: increase log level |
| 40 | + klog.InfoS("Checking for user violations", "namespace", ns.Name, "enforceLevel", enforceLevel) |
| 41 | + isUserViolation, err := c.isUserViolation(ctx, ns, enforceLevel) |
| 42 | + if err != nil { |
| 43 | + klog.V(2).ErrorS(err, "Error checking user violations", "namespace", ns.Name) |
| 44 | + // Transient API server error or temporary resource unavailability (most likely). |
| 45 | + // Theoretically, psapi parsing errors could occur that retry without hope for recovery. |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + // TODO@ibihim: increase log level |
| 50 | + klog.InfoS("User violation check result", "namespace", ns.Name, "isUserViolation", isUserViolation) |
| 51 | + if isUserViolation { |
| 52 | + // TODO@ibihim: increase log level |
| 53 | + klog.InfoS("Adding namespace to user SCC violations", "namespace", ns.Name) |
| 54 | + conditions.addViolatingUserSCC(ns) |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + // Historically, we assume that this is a customer issue, but |
| 59 | + // actually it means we don't know what the root cause is. |
| 60 | + conditions.addViolatingCustomer(ns) |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (c *PodSecurityReadinessController) isUserViolation(ctx context.Context, ns *corev1.Namespace, label string) (bool, error) { |
| 66 | + var enforcementLevel psapi.Level |
| 67 | + switch strings.ToLower(label) { |
| 68 | + case "restricted": |
| 69 | + enforcementLevel = psapi.LevelRestricted |
| 70 | + case "baseline": |
| 71 | + enforcementLevel = psapi.LevelBaseline |
| 72 | + case "privileged": |
| 73 | + // If privileged is violating, something is seriously wrong |
| 74 | + // but testing against privileged level is pointless (everything passes) |
| 75 | + klog.V(2).InfoS("Namespace violating privileged level - skipping user check", |
| 76 | + "namespace", ns.Name) |
| 77 | + return false, nil |
| 78 | + default: |
| 79 | + return false, fmt.Errorf("unknown level: %q", label) |
| 80 | + } |
| 81 | + |
| 82 | + allPods, err := c.kubeClient.CoreV1().Pods(ns.Name).List(ctx, metav1.ListOptions{}) |
| 83 | + if err != nil { |
| 84 | + klog.V(2).ErrorS(err, "Failed to list pods in namespace", "namespace", ns.Name) |
| 85 | + return false, err |
| 86 | + } |
| 87 | + |
| 88 | + var userPods []corev1.Pod |
| 89 | + for _, pod := range allPods.Items { |
| 90 | + // TODO@ibihim: we should exclude Pod that have restricted-v2. |
| 91 | + // restricted-v2 SCCs are allowed for all system:authenticated. ServiceAccounts |
| 92 | + // are able to use that, but they are not part of the group. So restricted-v2 |
| 93 | + // will always result in user. |
| 94 | + if pod.Annotations[securityv1.ValidatedSCCSubjectTypeAnnotation] == "user" { |
| 95 | + userPods = append(userPods, pod) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if len(userPods) == 0 { |
| 100 | + return false, nil // No user pods = violation is from service accounts |
| 101 | + } |
| 102 | + |
| 103 | + enforcementVersion := psapi.LatestVersion() |
| 104 | + for _, pod := range userPods { |
| 105 | + klog.InfoS("Evaluating user pod against PSA level", |
| 106 | + "namespace", ns.Name, "pod", pod.Name, "level", label, |
| 107 | + "podSecurityContext", pod.Spec.SecurityContext) |
| 108 | + |
| 109 | + results := c.psaEvaluator.EvaluatePod( |
| 110 | + psapi.LevelVersion{Level: enforcementLevel, Version: enforcementVersion}, |
| 111 | + &pod.ObjectMeta, |
| 112 | + &pod.Spec, |
| 113 | + ) |
| 114 | + |
| 115 | + klog.InfoS("PSA evaluation results", |
| 116 | + "namespace", ns.Name, "pod", pod.Name, "level", label, |
| 117 | + "resultCount", len(results)) |
| 118 | + |
| 119 | + for _, result := range results { |
| 120 | + klog.InfoS("PSA evaluation result", |
| 121 | + "namespace", ns.Name, "pod", pod.Name, "level", label, |
| 122 | + "allowed", result.Allowed, "reason", result.ForbiddenReason, |
| 123 | + "detail", result.ForbiddenDetail) |
| 124 | + if !result.Allowed { |
| 125 | + klog.InfoS("User pod violates PSA level", |
| 126 | + "namespace", ns.Name, "pod", pod.Name, "level", label) |
| 127 | + return true, nil |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return false, nil |
| 133 | +} |
0 commit comments