admission.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright 2015 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 exec
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. corev1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apiserver/pkg/admission"
  21. genericadmissioninitializer "k8s.io/apiserver/pkg/admission/initializer"
  22. "k8s.io/client-go/kubernetes"
  23. "k8s.io/klog"
  24. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  25. )
  26. const (
  27. // DenyEscalatingExec indicates name of admission plugin.
  28. // Deprecated, will be removed in v1.18.
  29. // Use of PodSecurityPolicy or a custom admission plugin to limit creation of pods is recommended instead.
  30. DenyEscalatingExec = "DenyEscalatingExec"
  31. // DenyExecOnPrivileged indicates name of admission plugin.
  32. // Deprecated, will be removed in v1.18.
  33. // Use of PodSecurityPolicy or a custom admission plugin to limit creation of pods is recommended instead.
  34. DenyExecOnPrivileged = "DenyExecOnPrivileged"
  35. )
  36. // Register registers a plugin
  37. func Register(plugins *admission.Plugins) {
  38. plugins.Register(DenyEscalatingExec, func(config io.Reader) (admission.Interface, error) {
  39. klog.Warningf("the %s admission plugin is deprecated and will be removed in v1.18", DenyEscalatingExec)
  40. klog.Warningf("use of PodSecurityPolicy or a custom admission plugin to limit creation of pods is recommended instead")
  41. return NewDenyEscalatingExec(), nil
  42. })
  43. // This is for legacy support of the DenyExecOnPrivileged admission controller. Most
  44. // of the time DenyEscalatingExec should be preferred.
  45. plugins.Register(DenyExecOnPrivileged, func(config io.Reader) (admission.Interface, error) {
  46. klog.Warningf("the %s admission plugin is deprecated and will be removed in v1.18", DenyExecOnPrivileged)
  47. klog.Warningf("use of PodSecurityPolicy or a custom admission plugin to limit creation of pods is recommended instead")
  48. return NewDenyExecOnPrivileged(), nil
  49. })
  50. }
  51. // DenyExec is an implementation of admission.Interface which says no to a pod/exec on
  52. // a pod using host based configurations.
  53. type DenyExec struct {
  54. *admission.Handler
  55. client kubernetes.Interface
  56. // these flags control which items will be checked to deny exec/attach
  57. hostNetwork bool
  58. hostIPC bool
  59. hostPID bool
  60. privileged bool
  61. }
  62. var _ admission.ValidationInterface = &DenyExec{}
  63. var _ = genericadmissioninitializer.WantsExternalKubeClientSet(&DenyExec{})
  64. // NewDenyExecOnPrivileged creates a new admission controller that is only checking the privileged
  65. // option. This is for legacy support of the DenyExecOnPrivileged admission controller.
  66. // Most of the time NewDenyEscalatingExec should be preferred.
  67. func NewDenyExecOnPrivileged() *DenyExec {
  68. return &DenyExec{
  69. Handler: admission.NewHandler(admission.Connect),
  70. hostNetwork: false,
  71. hostIPC: false,
  72. hostPID: false,
  73. privileged: true,
  74. }
  75. }
  76. // NewDenyEscalatingExec creates a new admission controller that denies an exec operation on a pod
  77. // using host based configurations.
  78. func NewDenyEscalatingExec() *DenyExec {
  79. return &DenyExec{
  80. Handler: admission.NewHandler(admission.Connect),
  81. hostNetwork: true,
  82. hostIPC: true,
  83. hostPID: true,
  84. privileged: true,
  85. }
  86. }
  87. // SetExternalKubeClientSet implements the WantsInternalKubeClientSet interface.
  88. func (d *DenyExec) SetExternalKubeClientSet(client kubernetes.Interface) {
  89. d.client = client
  90. }
  91. // ValidateInitialization implements the InitializationValidator interface.
  92. func (d *DenyExec) ValidateInitialization() error {
  93. if d.client == nil {
  94. return fmt.Errorf("missing client")
  95. }
  96. return nil
  97. }
  98. // Validate makes an admission decision based on the request attributes
  99. func (d *DenyExec) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error) {
  100. path := a.GetResource().Resource
  101. if subresource := a.GetSubresource(); subresource != "" {
  102. path = path + "/" + subresource
  103. }
  104. // Only handle exec or attach requests on pods
  105. if path != "pods/exec" && path != "pods/attach" {
  106. return nil
  107. }
  108. pod, err := d.client.CoreV1().Pods(a.GetNamespace()).Get(context.TODO(), a.GetName(), metav1.GetOptions{})
  109. if err != nil {
  110. return admission.NewForbidden(a, err)
  111. }
  112. if d.hostNetwork && pod.Spec.HostNetwork {
  113. return admission.NewForbidden(a, fmt.Errorf("cannot exec into or attach to a container using host network"))
  114. }
  115. if d.hostPID && pod.Spec.HostPID {
  116. return admission.NewForbidden(a, fmt.Errorf("cannot exec into or attach to a container using host pid"))
  117. }
  118. if d.hostIPC && pod.Spec.HostIPC {
  119. return admission.NewForbidden(a, fmt.Errorf("cannot exec into or attach to a container using host ipc"))
  120. }
  121. if d.privileged && isPrivileged(pod) {
  122. return admission.NewForbidden(a, fmt.Errorf("cannot exec into or attach to a privileged container"))
  123. }
  124. return nil
  125. }
  126. // isPrivileged will return true a pod has any privileged containers
  127. func isPrivileged(pod *corev1.Pod) bool {
  128. var privileged bool
  129. podutil.VisitContainers(&pod.Spec, func(c *corev1.Container) bool {
  130. if c.SecurityContext == nil || c.SecurityContext.Privileged == nil {
  131. return true
  132. }
  133. if *c.SecurityContext.Privileged {
  134. privileged = true
  135. return false
  136. }
  137. return true
  138. })
  139. return privileged
  140. }