psp_util.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 framework
  14. import (
  15. "fmt"
  16. "sync"
  17. corev1 "k8s.io/api/core/v1"
  18. policy "k8s.io/api/policy/v1beta1"
  19. rbacv1beta1 "k8s.io/api/rbac/v1beta1"
  20. apierrs "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. "k8s.io/apiserver/pkg/authentication/serviceaccount"
  24. "k8s.io/kubernetes/pkg/security/podsecuritypolicy/seccomp"
  25. "k8s.io/kubernetes/test/e2e/framework/auth"
  26. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  27. "github.com/onsi/ginkgo"
  28. )
  29. const (
  30. podSecurityPolicyPrivileged = "e2e-test-privileged-psp"
  31. )
  32. var (
  33. isPSPEnabledOnce sync.Once
  34. isPSPEnabled bool
  35. )
  36. // privilegedPSP creates a PodSecurityPolicy that allows everything.
  37. func privilegedPSP(name string) *policy.PodSecurityPolicy {
  38. allowPrivilegeEscalation := true
  39. return &policy.PodSecurityPolicy{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: name,
  42. Annotations: map[string]string{seccomp.AllowedProfilesAnnotationKey: seccomp.AllowAny},
  43. },
  44. Spec: policy.PodSecurityPolicySpec{
  45. Privileged: true,
  46. AllowPrivilegeEscalation: &allowPrivilegeEscalation,
  47. AllowedCapabilities: []corev1.Capability{"*"},
  48. Volumes: []policy.FSType{policy.All},
  49. HostNetwork: true,
  50. HostPorts: []policy.HostPortRange{{Min: 0, Max: 65535}},
  51. HostIPC: true,
  52. HostPID: true,
  53. RunAsUser: policy.RunAsUserStrategyOptions{
  54. Rule: policy.RunAsUserStrategyRunAsAny,
  55. },
  56. SELinux: policy.SELinuxStrategyOptions{
  57. Rule: policy.SELinuxStrategyRunAsAny,
  58. },
  59. SupplementalGroups: policy.SupplementalGroupsStrategyOptions{
  60. Rule: policy.SupplementalGroupsStrategyRunAsAny,
  61. },
  62. FSGroup: policy.FSGroupStrategyOptions{
  63. Rule: policy.FSGroupStrategyRunAsAny,
  64. },
  65. ReadOnlyRootFilesystem: false,
  66. AllowedUnsafeSysctls: []string{"*"},
  67. },
  68. }
  69. }
  70. // IsPodSecurityPolicyEnabled returns true if PodSecurityPolicy is enabled. Otherwise false.
  71. func IsPodSecurityPolicyEnabled(f *Framework) bool {
  72. isPSPEnabledOnce.Do(func() {
  73. psps, err := f.ClientSet.PolicyV1beta1().PodSecurityPolicies().List(metav1.ListOptions{})
  74. if err != nil {
  75. e2elog.Logf("Error listing PodSecurityPolicies; assuming PodSecurityPolicy is disabled: %v", err)
  76. isPSPEnabled = false
  77. } else if psps == nil || len(psps.Items) == 0 {
  78. e2elog.Logf("No PodSecurityPolicies found; assuming PodSecurityPolicy is disabled.")
  79. isPSPEnabled = false
  80. } else {
  81. e2elog.Logf("Found PodSecurityPolicies; assuming PodSecurityPolicy is enabled.")
  82. isPSPEnabled = true
  83. }
  84. })
  85. return isPSPEnabled
  86. }
  87. var (
  88. privilegedPSPOnce sync.Once
  89. )
  90. func createPrivilegedPSPBinding(f *Framework, namespace string) {
  91. if !IsPodSecurityPolicyEnabled(f) {
  92. return
  93. }
  94. // Create the privileged PSP & role
  95. privilegedPSPOnce.Do(func() {
  96. _, err := f.ClientSet.PolicyV1beta1().PodSecurityPolicies().Get(
  97. podSecurityPolicyPrivileged, metav1.GetOptions{})
  98. if !apierrs.IsNotFound(err) {
  99. // Privileged PSP was already created.
  100. ExpectNoError(err, "Failed to get PodSecurityPolicy %s", podSecurityPolicyPrivileged)
  101. return
  102. }
  103. psp := privilegedPSP(podSecurityPolicyPrivileged)
  104. psp, err = f.ClientSet.PolicyV1beta1().PodSecurityPolicies().Create(psp)
  105. if !apierrs.IsAlreadyExists(err) {
  106. ExpectNoError(err, "Failed to create PSP %s", podSecurityPolicyPrivileged)
  107. }
  108. if auth.IsRBACEnabled(f.ClientSet.RbacV1beta1()) {
  109. // Create the Role to bind it to the namespace.
  110. _, err = f.ClientSet.RbacV1beta1().ClusterRoles().Create(&rbacv1beta1.ClusterRole{
  111. ObjectMeta: metav1.ObjectMeta{Name: podSecurityPolicyPrivileged},
  112. Rules: []rbacv1beta1.PolicyRule{{
  113. APIGroups: []string{"extensions"},
  114. Resources: []string{"podsecuritypolicies"},
  115. ResourceNames: []string{podSecurityPolicyPrivileged},
  116. Verbs: []string{"use"},
  117. }},
  118. })
  119. if !apierrs.IsAlreadyExists(err) {
  120. ExpectNoError(err, "Failed to create PSP role")
  121. }
  122. }
  123. })
  124. if auth.IsRBACEnabled(f.ClientSet.RbacV1beta1()) {
  125. ginkgo.By(fmt.Sprintf("Binding the %s PodSecurityPolicy to the default service account in %s",
  126. podSecurityPolicyPrivileged, namespace))
  127. err := auth.BindClusterRoleInNamespace(f.ClientSet.RbacV1beta1(),
  128. podSecurityPolicyPrivileged,
  129. namespace,
  130. rbacv1beta1.Subject{
  131. Kind: rbacv1beta1.ServiceAccountKind,
  132. Namespace: namespace,
  133. Name: "default",
  134. })
  135. ExpectNoError(err)
  136. ExpectNoError(auth.WaitForNamedAuthorizationUpdate(f.ClientSet.AuthorizationV1beta1(),
  137. serviceaccount.MakeUsername(namespace, "default"), namespace, "use", podSecurityPolicyPrivileged,
  138. schema.GroupResource{Group: "extensions", Resource: "podsecuritypolicies"}, true))
  139. }
  140. }