Skip to content

Commit 188555f

Browse files
Adjust test for etcd tls communication
Signed-off-by: michal.gubricky <[email protected]>
1 parent 7fda22c commit 188555f

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

Tests/kaas/kaas-sonobuoy-tests/kind_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ kubeadmConfigPatches:
1111
name: config
1212
apiServer:
1313
extraArgs:
14-
enable-admission-plugins: "NodeRestriction,PodSecurity"
14+
enable-admission-plugins: "NodeRestriction,PodSecurity"
1515
nodes:
1616
- role: control-plane
1717
- role: worker

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

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,6 @@ func Test_scs_0217_etcd_tls_communication(t *testing.T) {
448448
// Check kube-apiserver communication with etcd
449449
checkKubeAPIServerETCDTLS(t, kubeClient)
450450

451-
// Check etcd peer communication for TLS
452-
checkETCDPeerCommunicationTLS(t, kubeClient)
453-
454451
return ctx
455452
})
456453

@@ -749,51 +746,33 @@ func checkKubeAPIServerETCDTLS(t *testing.T, kubeClient *kubernetes.Clientset) {
749746
t.Fatal("failed to list kube-apiserver pods:", err)
750747
}
751748

752-
// Check each kube-apiserver pod
753-
for _, pod := range podList.Items {
754-
for _, container := range pod.Spec.Containers {
755-
cmdFound := false
756-
for _, cmd := range container.Command {
757-
// Check for etcd certificates and key flags
758-
if strings.Contains(cmd, "--etcd-certfile") && strings.Contains(cmd, "--etcd-keyfile") && strings.Contains(cmd, "--etcd-cafile") {
759-
t.Logf("kube-apiserver communicates with etcd using TLS in container: %s of pod: %s", container.Name, pod.Name)
760-
cmdFound = true
761-
break
762-
}
763-
}
764-
765-
if !cmdFound {
766-
t.Errorf("Error: kube-apiserver does not use TLS for etcd communication in container: %s of pod: %s", container.Name, pod.Name)
767-
}
768-
}
769-
}
770-
}
771-
772-
// checkETCDPeerCommunicationTLS checks whether etcd peer communication is secured with TLS.
773-
func checkETCDPeerCommunicationTLS(t *testing.T, kubeClient *kubernetes.Clientset) {
774-
// List etcd pods
775-
podList, err := kubeClient.CoreV1().Pods("kube-system").List(context.TODO(), v1.ListOptions{
776-
LabelSelector: "component=etcd",
777-
})
778-
if err != nil {
779-
t.Fatal("failed to list etcd pods:", err)
749+
// Expected etcd TLS flags
750+
requiredFlags := []string{
751+
"--etcd-certfile=",
752+
"--etcd-keyfile=",
753+
"--etcd-cafile=",
780754
}
781755

782-
// Check each etcd pod
756+
// Check each kube-apiserver pod
783757
for _, pod := range podList.Items {
784758
for _, container := range pod.Spec.Containers {
785-
cmdFound := false
786-
for _, cmd := range container.Command {
787-
// Check for etcd peer certificate and key flags
788-
if strings.Contains(cmd, "--peer-cert-file") && strings.Contains(cmd, "--peer-key-file") && strings.Contains(cmd, "--peer-client-cert-auth") {
789-
t.Logf("Etcd peer communication is secured with TLS in container: %s of pod: %s", container.Name, pod.Name)
790-
cmdFound = true
791-
break
759+
// Gather all the commands into a single string for easier matching
760+
cmdLine := strings.Join(container.Command, " ")
761+
t.Logf("TEST: Checking container: %s of pod: %s", container.Name, pod.Name)
762+
763+
// Check if all required etcd TLS flags are present
764+
allFlagsPresent := true
765+
for _, flag := range requiredFlags {
766+
if !strings.Contains(cmdLine, flag) {
767+
t.Errorf("Missing flag %s in container: %s of pod: %s", flag, container.Name, pod.Name)
768+
allFlagsPresent = false
792769
}
793770
}
794771

795-
if !cmdFound {
796-
t.Errorf("Error: etcd peer communication is not secured with TLS in container: %s of pod: %s", container.Name, pod.Name)
772+
if allFlagsPresent {
773+
t.Logf("kube-apiserver communicates with etcd using TLS in container: %s of pod: %s", container.Name, pod.Name)
774+
} else {
775+
t.Errorf("Error: kube-apiserver does not use all required TLS flags for etcd communication in container: %s of pod: %s", container.Name, pod.Name)
797776
}
798777
}
799778
}

0 commit comments

Comments
 (0)