@@ -457,6 +457,30 @@ func Test_scs_0217_etcd_tls_communication(t *testing.T) {
457457 testenv .Test (t , f .Feature ())
458458}
459459
460+ // Test_scs_0217_etcd_isolation checks ETCD is isolated from k8s cluster by checking the etcd server endpoints.
461+ func Test_scs_0217_etcd_isolation (t * testing.T ) {
462+ f := features .New ("etcd security" ).Assess (
463+ "ETCD should be isolated from the Kubernetes cluster" ,
464+ func (ctx context.Context , t * testing.T , cfg * envconf.Config ) context.Context {
465+ restConf , err := rest .InClusterConfig ()
466+ if err != nil {
467+ t .Fatal ("failed to create rest config:" , err )
468+ }
469+
470+ kubeClient , err := kubernetes .NewForConfig (restConf )
471+ if err != nil {
472+ t .Fatal ("failed to create Kubernetes client:" , err )
473+ }
474+
475+ // Check if etcd is isolated from k8s cluster
476+ checkIsolationETCD (t , kubeClient )
477+
478+ return ctx
479+ })
480+
481+ testenv .Test (t , f .Feature ())
482+ }
483+
460484// checkPortOpen tries to establish a TCP connection to the given IP and port.
461485// It returns true if the port is open and false if the connection is refused or times out.
462486func checkPortOpen (ip , port string , timeout time.Duration ) bool {
@@ -774,3 +798,41 @@ func checkETCDPeerCommunicationTLS(t *testing.T, kubeClient *kubernetes.Clientse
774798 }
775799 }
776800}
801+
802+ // checkIsolationETCD checks whether the etcd is isolated from k8s cluster.
803+ func checkIsolationETCD (t * testing.T , kubeClient * kubernetes.Clientset ) {
804+ // List kube-apiserver pods
805+ podList , err := kubeClient .CoreV1 ().Pods ("kube-system" ).List (context .TODO (), v1.ListOptions {
806+ LabelSelector : "component=kube-apiserver" ,
807+ })
808+ if err != nil {
809+ t .Fatal ("failed to list kube-apiserver pods:" , err )
810+ }
811+
812+ // Check each kube-apiserver pod
813+ for _ , pod := range podList .Items {
814+ for _ , container := range pod .Spec .Containers {
815+ etcdServersFound := false
816+ for _ , cmd := range container .Command {
817+ if strings .Contains (cmd , "--etcd-servers=" ) {
818+ etcdServersFound = true
819+ etcdServers := strings .Split (cmd , "--etcd-servers=" )[1 ]
820+ etcdEndpoints := strings .Split (etcdServers , "," )
821+
822+ // Verify that etcd is not running on localhost
823+ for _ , endpoint := range etcdEndpoints {
824+ if strings .Contains (endpoint , "localhost" ) || strings .Contains (endpoint , "127.0.0.1" ) {
825+ t .Logf ("Warning: etcd should be isolated from k8s cluster, currently it is running on localhost: %s" , endpoint )
826+ } else {
827+ t .Logf ("ETCD is isolated at endpoint: %s" , endpoint )
828+ }
829+ }
830+ }
831+ }
832+
833+ if ! etcdServersFound {
834+ t .Errorf ("Error: --etcd-servers flag is missing in kube-apiserver pod: %s" , pod .Name )
835+ }
836+ }
837+ }
838+ }
0 commit comments