k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/cloud/gcp/kubelet_security.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package gcp
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"time"
    25  
    26  	v1 "k8s.io/api/core/v1"
    27  	"k8s.io/kubernetes/pkg/cluster/ports"
    28  	"k8s.io/kubernetes/test/e2e/feature"
    29  	"k8s.io/kubernetes/test/e2e/framework"
    30  	e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet"
    31  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    32  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    33  	admissionapi "k8s.io/pod-security-admission/api"
    34  
    35  	"github.com/onsi/ginkgo/v2"
    36  	"github.com/onsi/gomega"
    37  )
    38  
    39  var _ = SIGDescribe("Ports Security Check", feature.KubeletSecurity, func() {
    40  	f := framework.NewDefaultFramework("kubelet-security")
    41  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    42  
    43  	var node *v1.Node
    44  	var nodeName string
    45  
    46  	ginkgo.BeforeEach(func(ctx context.Context) {
    47  		e2eskipper.SkipUnlessProviderIs("gce", "gke")
    48  		var err error
    49  		node, err = e2enode.GetRandomReadySchedulableNode(ctx, f.ClientSet)
    50  		framework.ExpectNoError(err)
    51  		nodeName = node.Name
    52  	})
    53  
    54  	// make sure kubelet readonly (10255) and cadvisor (4194) ports are disabled via API server proxy
    55  	ginkgo.It(fmt.Sprintf("should not be able to proxy to the readonly kubelet port %v using proxy subresource", ports.KubeletReadOnlyPort), func(ctx context.Context) {
    56  		result, err := e2ekubelet.ProxyRequest(ctx, f.ClientSet, nodeName, "pods/", ports.KubeletReadOnlyPort)
    57  		framework.ExpectNoError(err)
    58  
    59  		var statusCode int
    60  		result.StatusCode(&statusCode)
    61  		gomega.Expect(statusCode).ToNot(gomega.Equal(http.StatusOK))
    62  	})
    63  	ginkgo.It("should not be able to proxy to cadvisor port 4194 using proxy subresource", func(ctx context.Context) {
    64  		result, err := e2ekubelet.ProxyRequest(ctx, f.ClientSet, nodeName, "containers/", 4194)
    65  		framework.ExpectNoError(err)
    66  
    67  		var statusCode int
    68  		result.StatusCode(&statusCode)
    69  		gomega.Expect(statusCode).ToNot(gomega.Equal(http.StatusOK))
    70  	})
    71  
    72  	// make sure kubelet readonly (10255) and cadvisor (4194) ports are closed on the public IP address
    73  	disabledPorts := []int{ports.KubeletReadOnlyPort, 4194}
    74  	for _, port := range disabledPorts {
    75  		port := port
    76  		ginkgo.It(fmt.Sprintf("should not have port %d open on its all public IP addresses", port), func(ctx context.Context) {
    77  			portClosedTest(f, node, port)
    78  		})
    79  	}
    80  })
    81  
    82  // checks whether the target port is closed
    83  func portClosedTest(f *framework.Framework, pickNode *v1.Node, port int) {
    84  	nodeAddrs := e2enode.GetAddresses(pickNode, v1.NodeExternalIP)
    85  	gomega.Expect(nodeAddrs).ToNot(gomega.BeEmpty())
    86  
    87  	for _, addr := range nodeAddrs {
    88  		conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", addr, port), 1*time.Minute)
    89  		if err == nil {
    90  			conn.Close()
    91  			framework.Failf("port %d is not disabled", port)
    92  		}
    93  	}
    94  }