util.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 e2e_kubeadm
  14. import (
  15. appsv1 "k8s.io/api/apps/v1"
  16. authv1 "k8s.io/api/authorization/v1"
  17. corev1 "k8s.io/api/core/v1"
  18. rbacv1 "k8s.io/api/rbac/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. clientset "k8s.io/client-go/kubernetes"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. "github.com/onsi/gomega"
  23. "github.com/onsi/gomega/gstruct"
  24. )
  25. // ServiceAccounts utils
  26. // ExpectServiceAccount expects to be able to get the ServiceAccount with specific name from the namespace
  27. func ExpectServiceAccount(c clientset.Interface, namespace, name string) {
  28. _, err := c.CoreV1().
  29. ServiceAccounts(namespace).
  30. Get(name, metav1.GetOptions{})
  31. framework.ExpectNoError(err, "error getting ServiceAccount %q from namespace %q", name, namespace)
  32. }
  33. // Secret utils
  34. // GetSecret gets Secret with specific name from the namespace
  35. func GetSecret(c clientset.Interface, namespace, name string) *corev1.Secret {
  36. r, err := c.CoreV1().
  37. Secrets(namespace).
  38. Get(name, metav1.GetOptions{})
  39. framework.ExpectNoError(err, "error getting Secret %q from namespace %q", name, namespace)
  40. return r
  41. }
  42. // ConfigMaps utils
  43. // GetConfigMap gets ConfigMap with specific name from the namespace
  44. func GetConfigMap(c clientset.Interface, namespace, name string) *corev1.ConfigMap {
  45. r, err := c.CoreV1().
  46. ConfigMaps(namespace).
  47. Get(name, metav1.GetOptions{})
  48. framework.ExpectNoError(err, "error getting ConfigMap %q from namespace %q", name, namespace)
  49. return r
  50. }
  51. // Service utils
  52. // ExpectService expects to be able to get the Service with specific name from the namespace
  53. func ExpectService(c clientset.Interface, namespace, name string) {
  54. _, err := c.CoreV1().
  55. Services(namespace).
  56. Get(name, metav1.GetOptions{})
  57. framework.ExpectNoError(err, "error getting Service %q from namespace %q", name, namespace)
  58. }
  59. // Deployments utils
  60. // GetDeployment gets Deployment with specific name from the namespace
  61. func GetDeployment(c clientset.Interface, namespace, name string) *appsv1.Deployment {
  62. r, err := c.AppsV1().
  63. Deployments(namespace).
  64. Get(name, metav1.GetOptions{})
  65. framework.ExpectNoError(err, "error getting Deployment %q from namespace %q", name, namespace)
  66. return r
  67. }
  68. // DaemonSets utils
  69. // GetDaemonSet gets DaemonSet with specific name from the namespace
  70. func GetDaemonSet(c clientset.Interface, namespace, name string) *appsv1.DaemonSet {
  71. r, err := c.AppsV1().
  72. DaemonSets(namespace).
  73. Get(name, metav1.GetOptions{})
  74. framework.ExpectNoError(err, "error getting DaemonSet %q from namespace %q", name, namespace)
  75. return r
  76. }
  77. // RBAC utils
  78. // ExpectRole expects to be able to get the Role with specific name from the namespace
  79. func ExpectRole(c clientset.Interface, namespace, name string) {
  80. _, err := c.RbacV1().
  81. Roles(namespace).
  82. Get(name, metav1.GetOptions{})
  83. framework.ExpectNoError(err, "error getting Role %q from namespace %q", name, namespace)
  84. }
  85. // ExpectRoleBinding expects to be able to get the RoleBinding with specific name from the namespace
  86. func ExpectRoleBinding(c clientset.Interface, namespace, name string) {
  87. _, err := c.RbacV1().
  88. RoleBindings(namespace).
  89. Get(name, metav1.GetOptions{})
  90. framework.ExpectNoError(err, "error getting RoleBinding %q from namespace %q", name, namespace)
  91. }
  92. // ExpectClusterRole expects to be able to get the ClusterRole with specific name
  93. func ExpectClusterRole(c clientset.Interface, name string) {
  94. _, err := c.RbacV1().
  95. ClusterRoles().
  96. Get(name, metav1.GetOptions{})
  97. framework.ExpectNoError(err, "error getting ClusterRole %q", name)
  98. }
  99. // ExpectClusterRoleBinding expects to be able to get the ClusterRoleBinding with specific name
  100. func ExpectClusterRoleBinding(c clientset.Interface, name string) {
  101. _, err := c.RbacV1().
  102. ClusterRoleBindings().
  103. Get(name, metav1.GetOptions{})
  104. framework.ExpectNoError(err, "error getting ClusterRoleBindings %q", name)
  105. }
  106. // ExpectClusterRoleBindingWithSubjectAndRole expects to be able to get the ClusterRoleBinding with specific name, subject and role
  107. func ExpectClusterRoleBindingWithSubjectAndRole(c clientset.Interface, name, subjectKind, subject, role string) {
  108. binding, err := c.RbacV1().
  109. ClusterRoleBindings().
  110. Get(name, metav1.GetOptions{})
  111. framework.ExpectNoError(err, "error getting ClusterRoleBindings %q", name)
  112. gomega.Expect(binding.Subjects).To(
  113. gomega.ContainElement(subjectMatcher(
  114. subject,
  115. subjectKind,
  116. )),
  117. "ClusterRole %q does not have %s %q as subject", name, subjectKind, subject,
  118. )
  119. gomega.Expect(binding.RoleRef.Name).To(
  120. gomega.Equal(role),
  121. "ClusterRole %q does not have %q as role", name, role,
  122. )
  123. }
  124. // ExpectSubjectHasAccessToResource expects that the subject has access to the target resource
  125. func ExpectSubjectHasAccessToResource(c clientset.Interface, subjectKind, subject string, resource *authv1.ResourceAttributes) {
  126. var sar *authv1.SubjectAccessReview
  127. switch subjectKind {
  128. case rbacv1.GroupKind:
  129. sar = &authv1.SubjectAccessReview{
  130. Spec: authv1.SubjectAccessReviewSpec{
  131. Groups: []string{subject},
  132. ResourceAttributes: resource,
  133. },
  134. }
  135. case rbacv1.UserKind:
  136. fallthrough
  137. case rbacv1.ServiceAccountKind:
  138. sar = &authv1.SubjectAccessReview{
  139. Spec: authv1.SubjectAccessReviewSpec{
  140. User: subject,
  141. ResourceAttributes: resource,
  142. },
  143. }
  144. default:
  145. framework.Failf("invalid subjectKind %s", subjectKind)
  146. }
  147. s, err := c.AuthorizationV1().SubjectAccessReviews().Create(sar)
  148. framework.ExpectNoError(err, "error getting SubjectAccessReview for %s %s to resource %+v", subjectKind, subject, *sar.Spec.ResourceAttributes)
  149. gomega.Expect(s.Status.Allowed).Should(gomega.BeTrue(), "%s %s has no access to resource %+v", subjectKind, subject, *sar.Spec.ResourceAttributes)
  150. }
  151. // matchers
  152. func subjectMatcher(name, kind string) gomega.OmegaMatcher {
  153. return gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
  154. "Name": gomega.Equal(name),
  155. "Kind": gomega.Equal(kind),
  156. })
  157. }