util.go 6.5 KB

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