admission.go 5.5 KB

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