Skip to content

Commit fd8bf54

Browse files
Adjust test which checks if kubelet readonly port is disabled
Signed-off-by: michal.gubricky <[email protected]>
1 parent 188555f commit fd8bf54

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

Tests/kaas/kaas-sonobuoy-tests/scs_k8s_conformance_tests/scs_0217_cluster_hardening_test.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ const (
3131
etcdPortRangeStart = 2379
3232
etcdPortRangeEnd = 2380
3333
kubeletApiPort = "10250"
34-
kubeletReadOnlyPort = "10255"
34+
kubeletReadOnlyPort = 10255
3535
connectionTimeout = 5 * time.Second
3636
sonobuoyResultsDir = "/tmp/sonobuoy/results"
3737
)
3838

3939
type KubeletConfig struct {
4040
KubeletConfig struct {
41+
ReadOnlyPort int `json:"readOnlyPort"`
4142
Authentication struct {
4243
Anonymous struct {
4344
Enabled bool `json:"enabled"`
@@ -56,20 +57,44 @@ func Test_scs_0217_sonobuoy_Kubelet_ReadOnly_Port_Disabled(t *testing.T) {
5657
f := features.New("kubelet security").Assess(
5758
"Kubelet read-only port (10255) should be disabled",
5859
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
59-
nodes := &corev1.NodeList{}
60-
err := cfg.Client().Resources().List(context.TODO(), nodes)
60+
restConf, err := rest.InClusterConfig()
6161
if err != nil {
62-
t.Fatal("failed to list nodes:", err)
62+
t.Fatal("failed to create rest config:", err)
6363
}
6464

65-
// Loop over each node and check if the read-only port is open
66-
for _, node := range nodes.Items {
67-
nodeIP := node.Status.Addresses[0].Address
68-
isPortOpen := checkPortOpen(nodeIP, kubeletReadOnlyPort, connectionTimeout)
69-
if isPortOpen {
70-
t.Logf("Warning: kubelet read-only port 10255 is open on node %s", nodeIP)
65+
kubeClient, err := kubernetes.NewForConfig(restConf)
66+
if err != nil {
67+
t.Fatal("failed to create Kubernetes client:", err)
68+
}
69+
70+
nodeList, err := kubeClient.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
71+
if err != nil {
72+
t.Fatal("failed to get node list:", err)
73+
}
74+
75+
nodeNames := make([]string, len(nodeList.Items))
76+
for i, node := range nodeList.Items {
77+
nodeNames[i] = node.Name
78+
}
79+
80+
if err := gatherNodeData(nodeNames, kubeClient.CoreV1().RESTClient(), sonobuoyResultsDir); err != nil {
81+
t.Fatal("failed to gather node data:", err)
82+
}
83+
84+
// Get kubelets configz file from each node
85+
for _, nodeName := range nodeNames {
86+
configzPath := path.Join(sonobuoyResultsDir, nodeName, "configz.json")
87+
kubeletConfig, err := readKubeletConfigFromFile(configzPath)
88+
if err != nil {
89+
t.Errorf("Failed to read Kubelet config from file %s: %v", configzPath, err)
90+
continue
91+
}
92+
93+
// Check if readonly port is enabled
94+
if kubeletConfig.KubeletConfig.ReadOnlyPort == kubeletReadOnlyPort {
95+
t.Logf("Warning: kubelet read-only port 10255 is open on node %s", nodeName)
7196
} else {
72-
t.Logf("Kubelet read-only port 10255 is correctly disabled on node %s", nodeIP)
97+
t.Logf("Kubelet read-only port 10255 is correctly disabled on node %s", nodeName)
7398
}
7499
}
75100
return ctx

0 commit comments

Comments
 (0)