node_authz.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Copyright 2017 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. "context"
  16. "fmt"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. clientset "k8s.io/client-go/kubernetes"
  23. restclient "k8s.io/client-go/rest"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. imageutils "k8s.io/kubernetes/test/utils/image"
  26. "github.com/onsi/ginkgo"
  27. )
  28. const (
  29. nodesGroup = "system:nodes"
  30. nodeNamePrefix = "system:node:"
  31. )
  32. var _ = SIGDescribe("[Feature:NodeAuthorizer]", func() {
  33. f := framework.NewDefaultFramework("node-authz")
  34. // client that will impersonate a node
  35. var c clientset.Interface
  36. var ns string
  37. var asUser string
  38. var defaultSaSecret string
  39. var nodeName string
  40. ginkgo.BeforeEach(func() {
  41. ns = f.Namespace.Name
  42. nodeList, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
  43. framework.ExpectNoError(err, "failed to list nodes in namespace: %s", ns)
  44. framework.ExpectNotEqual(len(nodeList.Items), 0)
  45. nodeName = nodeList.Items[0].Name
  46. asUser = nodeNamePrefix + nodeName
  47. saName := "default"
  48. sa, err := f.ClientSet.CoreV1().ServiceAccounts(ns).Get(context.TODO(), saName, metav1.GetOptions{})
  49. framework.ExpectNotEqual(len(sa.Secrets), 0)
  50. framework.ExpectNoError(err, "failed to retrieve service account (%s:%s)", ns, saName)
  51. defaultSaSecret = sa.Secrets[0].Name
  52. ginkgo.By("Creating a kubernetes client that impersonates a node")
  53. config, err := framework.LoadConfig()
  54. framework.ExpectNoError(err, "failed to load kubernetes client config")
  55. config.Impersonate = restclient.ImpersonationConfig{
  56. UserName: asUser,
  57. Groups: []string{nodesGroup},
  58. }
  59. c, err = clientset.NewForConfig(config)
  60. framework.ExpectNoError(err, "failed to create Clientset for the given config: %+v", *config)
  61. })
  62. ginkgo.It("Getting a non-existent secret should exit with the Forbidden error, not a NotFound error", func() {
  63. _, err := c.CoreV1().Secrets(ns).Get(context.TODO(), "foo", metav1.GetOptions{})
  64. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  65. })
  66. ginkgo.It("Getting an existing secret should exit with the Forbidden error", func() {
  67. _, err := c.CoreV1().Secrets(ns).Get(context.TODO(), defaultSaSecret, metav1.GetOptions{})
  68. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  69. })
  70. ginkgo.It("Getting a non-existent configmap should exit with the Forbidden error, not a NotFound error", func() {
  71. _, err := c.CoreV1().ConfigMaps(ns).Get(context.TODO(), "foo", metav1.GetOptions{})
  72. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  73. })
  74. ginkgo.It("Getting an existing configmap should exit with the Forbidden error", func() {
  75. ginkgo.By("Create a configmap for testing")
  76. configmap := &v1.ConfigMap{
  77. ObjectMeta: metav1.ObjectMeta{
  78. Namespace: ns,
  79. Name: "node-auth-configmap",
  80. },
  81. Data: map[string]string{
  82. "data": "content",
  83. },
  84. }
  85. _, err := f.ClientSet.CoreV1().ConfigMaps(ns).Create(context.TODO(), configmap, metav1.CreateOptions{})
  86. framework.ExpectNoError(err, "failed to create configmap (%s:%s) %+v", ns, configmap.Name, *configmap)
  87. _, err = c.CoreV1().ConfigMaps(ns).Get(context.TODO(), configmap.Name, metav1.GetOptions{})
  88. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  89. })
  90. ginkgo.It("Getting a secret for a workload the node has access to should succeed", func() {
  91. ginkgo.By("Create a secret for testing")
  92. secret := &v1.Secret{
  93. ObjectMeta: metav1.ObjectMeta{
  94. Namespace: ns,
  95. Name: "node-auth-secret",
  96. },
  97. Data: map[string][]byte{
  98. "data": []byte("keep it secret"),
  99. },
  100. }
  101. _, err := f.ClientSet.CoreV1().Secrets(ns).Create(context.TODO(), secret, metav1.CreateOptions{})
  102. framework.ExpectNoError(err, "failed to create secret (%s:%s)", ns, secret.Name)
  103. ginkgo.By("Node should not get the secret")
  104. _, err = c.CoreV1().Secrets(ns).Get(context.TODO(), secret.Name, metav1.GetOptions{})
  105. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  106. ginkgo.By("Create a pod that use the secret")
  107. pod := &v1.Pod{
  108. ObjectMeta: metav1.ObjectMeta{
  109. Name: "pause",
  110. },
  111. Spec: v1.PodSpec{
  112. Containers: []v1.Container{
  113. {
  114. Name: "pause",
  115. Image: imageutils.GetPauseImageName(),
  116. },
  117. },
  118. NodeName: nodeName,
  119. Volumes: []v1.Volume{
  120. {
  121. Name: "node-auth-secret",
  122. VolumeSource: v1.VolumeSource{
  123. Secret: &v1.SecretVolumeSource{
  124. SecretName: secret.Name,
  125. },
  126. },
  127. },
  128. },
  129. },
  130. }
  131. _, err = f.ClientSet.CoreV1().Pods(ns).Create(context.TODO(), pod, metav1.CreateOptions{})
  132. framework.ExpectNoError(err, "failed to create pod (%s:%s)", ns, pod.Name)
  133. ginkgo.By("The node should able to access the secret")
  134. itv := framework.Poll
  135. dur := 1 * time.Minute
  136. err = wait.Poll(itv, dur, func() (bool, error) {
  137. _, err = c.CoreV1().Secrets(ns).Get(context.TODO(), secret.Name, metav1.GetOptions{})
  138. if err != nil {
  139. framework.Logf("Failed to get secret %v, err: %v", secret.Name, err)
  140. return false, nil
  141. }
  142. return true, nil
  143. })
  144. framework.ExpectNoError(err, "failed to get secret after trying every %v for %v (%s:%s)", itv, dur, ns, secret.Name)
  145. })
  146. ginkgo.It("A node shouldn't be able to create another node", func() {
  147. node := &v1.Node{
  148. ObjectMeta: metav1.ObjectMeta{Name: "foo"},
  149. TypeMeta: metav1.TypeMeta{
  150. Kind: "Node",
  151. APIVersion: "v1",
  152. },
  153. }
  154. ginkgo.By(fmt.Sprintf("Create node foo by user: %v", asUser))
  155. _, err := c.CoreV1().Nodes().Create(context.TODO(), node, metav1.CreateOptions{})
  156. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  157. })
  158. ginkgo.It("A node shouldn't be able to delete another node", func() {
  159. ginkgo.By(fmt.Sprintf("Create node foo by user: %v", asUser))
  160. err := c.CoreV1().Nodes().Delete(context.TODO(), "foo", &metav1.DeleteOptions{})
  161. framework.ExpectEqual(apierrors.IsForbidden(err), true)
  162. })
  163. })