kubelet_security.go 2.7 KB

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