node_authn.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 auth
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/kubernetes/pkg/master/ports"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. imageutils "k8s.io/kubernetes/test/utils/image"
  21. "github.com/onsi/ginkgo"
  22. "github.com/onsi/gomega"
  23. )
  24. var _ = SIGDescribe("[Feature:NodeAuthenticator]", func() {
  25. f := framework.NewDefaultFramework("node-authn")
  26. var ns string
  27. var nodeIPs []string
  28. ginkgo.BeforeEach(func() {
  29. ns = f.Namespace.Name
  30. nodeList, err := f.ClientSet.CoreV1().Nodes().List(metav1.ListOptions{})
  31. framework.ExpectNoError(err, "failed to list nodes in namespace: %s", ns)
  32. gomega.Expect(len(nodeList.Items)).NotTo(gomega.BeZero())
  33. pickedNode := nodeList.Items[0]
  34. nodeIPs = framework.GetNodeAddresses(&pickedNode, v1.NodeExternalIP)
  35. // The pods running in the cluster can see the internal addresses.
  36. nodeIPs = append(nodeIPs, framework.GetNodeAddresses(&pickedNode, v1.NodeInternalIP)...)
  37. // make sure ServiceAccount admission controller is enabled, so secret generation on SA creation works
  38. saName := "default"
  39. sa, err := f.ClientSet.CoreV1().ServiceAccounts(ns).Get(saName, metav1.GetOptions{})
  40. framework.ExpectNoError(err, "failed to retrieve service account (%s:%s)", ns, saName)
  41. gomega.Expect(len(sa.Secrets)).NotTo(gomega.BeZero())
  42. })
  43. ginkgo.It("The kubelet's main port 10250 should reject requests with no credentials", func() {
  44. pod := createNodeAuthTestPod(f)
  45. for _, nodeIP := range nodeIPs {
  46. // Anonymous authentication is disabled by default
  47. result := framework.RunHostCmdOrDie(ns, pod.Name, fmt.Sprintf("curl -sIk -o /dev/null -w '%s' https://%s:%v/metrics", "%{http_code}", nodeIP, ports.KubeletPort))
  48. gomega.Expect(result).To(gomega.Or(gomega.Equal("401"), gomega.Equal("403")), "the kubelet's main port 10250 should reject requests with no credentials")
  49. }
  50. })
  51. ginkgo.It("The kubelet can delegate ServiceAccount tokens to the API server", func() {
  52. ginkgo.By("create a new ServiceAccount for authentication")
  53. trueValue := true
  54. newSA := &v1.ServiceAccount{
  55. ObjectMeta: metav1.ObjectMeta{
  56. Namespace: ns,
  57. Name: "node-auth-newsa",
  58. },
  59. AutomountServiceAccountToken: &trueValue,
  60. }
  61. _, err := f.ClientSet.CoreV1().ServiceAccounts(ns).Create(newSA)
  62. framework.ExpectNoError(err, "failed to create service account (%s:%s)", ns, newSA.Name)
  63. pod := createNodeAuthTestPod(f)
  64. for _, nodeIP := range nodeIPs {
  65. result := framework.RunHostCmdOrDie(ns,
  66. pod.Name,
  67. fmt.Sprintf("curl -sIk -o /dev/null -w '%s' --header \"Authorization: Bearer `%s`\" https://%s:%v/metrics",
  68. "%{http_code}",
  69. "cat /var/run/secrets/kubernetes.io/serviceaccount/token",
  70. nodeIP, ports.KubeletPort))
  71. gomega.Expect(result).To(gomega.Or(gomega.Equal("401"), gomega.Equal("403")), "the kubelet can delegate ServiceAccount tokens to the API server")
  72. }
  73. })
  74. })
  75. func createNodeAuthTestPod(f *framework.Framework) *v1.Pod {
  76. pod := &v1.Pod{
  77. ObjectMeta: metav1.ObjectMeta{
  78. GenerateName: "test-node-authn-",
  79. },
  80. Spec: v1.PodSpec{
  81. Containers: []v1.Container{{
  82. Name: "test-node-authn",
  83. Image: imageutils.GetE2EImage(imageutils.Hostexec),
  84. Command: []string{"sleep", "3600"},
  85. }},
  86. RestartPolicy: v1.RestartPolicyNever,
  87. },
  88. }
  89. return f.PodClient().CreateSync(pod)
  90. }