Skip to content

Commit 0e4d7b0

Browse files
committed
Automate OCP-32383
1 parent 00af731 commit 0e4d7b0

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package apiserver
2+
3+
import (
4+
"context"
5+
6+
g "github.com/onsi/ginkgo/v2"
7+
o "github.com/onsi/gomega"
8+
9+
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
e2e "k8s.io/kubernetes/test/e2e/framework"
12+
admissionapi "k8s.io/pod-security-admission/api"
13+
14+
exutil "github.com/openshift/origin/test/extended/util"
15+
)
16+
17+
var _ = g.Describe("[sig-auth][Feature:ControlPlaneSecurity]", func() {
18+
defer g.GinkgoRecover()
19+
ctx := context.Background()
20+
oc := exutil.NewCLIWithPodSecurityLevel("control-plane-security", admissionapi.LevelPrivileged)
21+
22+
// Verifies that control plane containers have proper securityContext.privileged settings
23+
// This ensures the control plane components can perform necessary privileged operations
24+
// Related issues:
25+
// OCP-32383: Control plane security context verification
26+
//bug 1793694: Init container security context
27+
g.It("should have privileged securityContext for control plane init and main containers", func() {
28+
// Skip on MicroShift clusters
29+
isMicroShift, err := exutil.IsMicroShiftCluster(oc.AdminKubeClient())
30+
o.Expect(err).NotTo(o.HaveOccurred())
31+
if isMicroShift {
32+
g.Skip("MicroShift has different security context requirements and architecture")
33+
}
34+
35+
// Skip on Hypershift clusters (control plane pods run in management cluster)
36+
isHyperShift, err := exutil.IsHypershift(ctx, oc.AdminConfigClient())
37+
o.Expect(err).NotTo(o.HaveOccurred())
38+
if isHyperShift {
39+
g.Skip("Hypershift control plane pods are not accessible from hosted cluster")
40+
}
41+
42+
checkItems := []struct {
43+
namespace string
44+
containerName string
45+
}{
46+
{
47+
namespace: "openshift-kube-apiserver",
48+
containerName: "kube-apiserver",
49+
},
50+
{
51+
namespace: "openshift-apiserver",
52+
containerName: "openshift-apiserver",
53+
},
54+
}
55+
56+
for _, checkItem := range checkItems {
57+
g.By("Getting pods in " + checkItem.namespace)
58+
e2e.Logf("Checking namespace: %s", checkItem.namespace)
59+
60+
// Use client-go to list pods with label selector
61+
podList, err := oc.AdminKubeClient().CoreV1().Pods(checkItem.namespace).List(ctx, metav1.ListOptions{
62+
LabelSelector: "apiserver",
63+
})
64+
o.Expect(err).NotTo(o.HaveOccurred())
65+
o.Expect(podList.Items).NotTo(o.BeEmpty(), "Expected to find at least one pod in %s", checkItem.namespace)
66+
67+
pod := podList.Items[0]
68+
e2e.Logf("Found pod: %s in namespace %s", pod.Name, checkItem.namespace)
69+
70+
g.By("Verifying container securityContext.privileged for " + checkItem.containerName)
71+
72+
// Find the specified container
73+
var targetContainer *corev1.Container
74+
for i := range pod.Spec.Containers {
75+
if pod.Spec.Containers[i].Name == checkItem.containerName {
76+
targetContainer = &pod.Spec.Containers[i]
77+
break
78+
}
79+
}
80+
o.Expect(targetContainer).NotTo(o.BeNil(), "Container %s not found in pod %s", checkItem.containerName, pod.Name)
81+
82+
// Verify the container has securityContext
83+
o.Expect(targetContainer.SecurityContext).NotTo(o.BeNil(),
84+
"Container %s in pod %s does not have securityContext", checkItem.containerName, pod.Name)
85+
86+
// Verify privileged is set to true
87+
o.Expect(targetContainer.SecurityContext.Privileged).NotTo(o.BeNil(),
88+
"Container %s in pod %s does not have securityContext.privileged set", checkItem.containerName, pod.Name)
89+
o.Expect(*targetContainer.SecurityContext.Privileged).To(o.BeTrue(),
90+
"Container %s in pod %s should have securityContext.privileged=true", checkItem.containerName, pod.Name)
91+
92+
e2e.Logf("Container %s has securityContext.privileged=true", checkItem.containerName)
93+
94+
g.By("Verifying init container securityContext.privileged")
95+
96+
// Verify all init containers have privileged=true
97+
o.Expect(pod.Spec.InitContainers).NotTo(o.BeEmpty(),
98+
"Expected to find at least one init container in pod %s", pod.Name)
99+
100+
for _, initContainer := range pod.Spec.InitContainers {
101+
o.Expect(initContainer.SecurityContext).NotTo(o.BeNil(),
102+
"Init container %s in pod %s does not have securityContext", initContainer.Name, pod.Name)
103+
o.Expect(initContainer.SecurityContext.Privileged).NotTo(o.BeNil(),
104+
"Init container %s in pod %s does not have securityContext.privileged set", initContainer.Name, pod.Name)
105+
o.Expect(*initContainer.SecurityContext.Privileged).To(o.BeTrue(),
106+
"Init container %s in pod %s should have securityContext.privileged=true", initContainer.Name, pod.Name)
107+
108+
e2e.Logf("Init container %s has securityContext.privileged=true", initContainer.Name)
109+
}
110+
}
111+
})
112+
})

0 commit comments

Comments
 (0)