Skip to content

Commit a22194c

Browse files
committed
added podSecurityLabels validation for enforce, warn and *-version presence
Signed-off-by: nmirasch <neus.miras@gmail.com>
1 parent 18bff86 commit a22194c

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

controllers/gitopsservice_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const (
7676
// PodSecurityLabelSyncLabel enables OpenShift to manage pod-security.kubernetes.io/* on the namespace.
7777
PodSecurityLabelSyncLabel = "security.openshift.io/scc.podSecurityLabelSync"
7878
PodSecurityLabelSyncLabelValue = "true"
79-
kamResourceName = "kam"
79+
kamResourceName = "kam"
8080
)
8181

8282
// SetupWithManager sets up the controller with the Manager.

test/openshift/e2e/ginkgo/sequential/1-110_validate_podsecurity_alerts_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sequential
22

33
import (
4+
"time"
5+
46
. "github.com/onsi/ginkgo/v2"
57
. "github.com/onsi/gomega"
68
"github.com/redhat-developer/gitops-operator/test/openshift/e2e/ginkgo/fixture"
@@ -9,6 +11,15 @@ import (
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012
)
1113

14+
const (
15+
psaAudit = "pod-security.kubernetes.io/audit"
16+
psaAuditVersion = "pod-security.kubernetes.io/audit-version"
17+
psaEnforce = "pod-security.kubernetes.io/enforce"
18+
psaEnforceVersion = "pod-security.kubernetes.io/enforce-version"
19+
psaWarn = "pod-security.kubernetes.io/warn"
20+
psaWarnVersion = "pod-security.kubernetes.io/warn-version"
21+
)
22+
1223
var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
1324

1425
Context("1-110_validate_podsecurity_alerts", func() {
@@ -29,9 +40,29 @@ var _ = Describe("GitOps Operator Sequential E2E Tests", func() {
2940
Eventually(gitopsNS, "5m", "5s").Should(
3041
k8sFixture.HaveLabelWithValue("security.openshift.io/scc.podSecurityLabelSync", "true"))
3142

32-
By("OpenShift sets pod-security.kubernetes.io/audit=restricted (pod-security *-version labels vary by cluster and are not asserted)")
33-
Eventually(gitopsNS, "5m", "5s").Should(
34-
k8sFixture.HaveLabelWithValue("pod-security.kubernetes.io/audit", "restricted"))
43+
By("OpenShift PSA label sync: audit and warn must be restricted; *-version present for each set mode. Enforce must be restricted when set (may be omitted by OpenShift)")
44+
Eventually(func() bool {
45+
if gitopsNS.Labels == nil {
46+
GinkgoWriter.Println("[1-110] openshift-gitops metadata.labels: <nil>")
47+
return false
48+
}
49+
l := gitopsNS.Labels
50+
51+
ok := l[psaAudit] == "restricted" && l[psaWarn] == "restricted" && l[psaAuditVersion] != "" && l[psaWarnVersion] != ""
52+
if enforceValue := l[psaEnforce]; enforceValue != "" { // enforce may be omitted by OpenShift. If the label is set, it must be restricted and pod-security.kubernetes.io/enforce-version must be non-empty.
53+
ok = ok && enforceValue == "restricted" && l[psaEnforceVersion] != ""
54+
}
55+
keys := make([]string, 0, len(gitopsNS.Labels))
56+
for k := range gitopsNS.Labels {
57+
keys = append(keys, k)
58+
}
59+
GinkgoWriter.Printf("[1-110] openshift-gitops metadata.labels (%d):\n", len(gitopsNS.Labels))
60+
for _, k := range keys {
61+
GinkgoWriter.Printf(" %s=%q\n", k, gitopsNS.Labels[k])
62+
}
63+
return ok
64+
}).WithTimeout(5*time.Minute).WithPolling(5*time.Second).Should(BeTrue(),
65+
"expected pod-security audit+warn=restricted with non-empty audit-version and warn-version; enforce=restricted+enforce-version when enforce label exists")
3566
})
3667

3768
})

0 commit comments

Comments
 (0)