admission_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright 2014 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 scdeny
  14. import (
  15. "testing"
  16. "k8s.io/apiserver/pkg/admission"
  17. api "k8s.io/kubernetes/pkg/apis/core"
  18. )
  19. // ensures the SecurityContext is denied if it defines anything more than Caps or Privileged
  20. func TestAdmission(t *testing.T) {
  21. handler := NewSecurityContextDeny()
  22. runAsUser := int64(1)
  23. priv := true
  24. cases := []struct {
  25. name string
  26. sc *api.SecurityContext
  27. podSc *api.PodSecurityContext
  28. expectError bool
  29. }{
  30. {
  31. name: "unset",
  32. },
  33. {
  34. name: "empty container.SecurityContext",
  35. sc: &api.SecurityContext{},
  36. },
  37. {
  38. name: "empty pod.Spec.SecurityContext",
  39. podSc: &api.PodSecurityContext{},
  40. },
  41. {
  42. name: "valid container.SecurityContext",
  43. sc: &api.SecurityContext{Privileged: &priv, Capabilities: &api.Capabilities{}},
  44. },
  45. {
  46. name: "valid pod.Spec.SecurityContext",
  47. podSc: &api.PodSecurityContext{},
  48. },
  49. {
  50. name: "container.SecurityContext.RunAsUser",
  51. sc: &api.SecurityContext{RunAsUser: &runAsUser},
  52. expectError: true,
  53. },
  54. {
  55. name: "container.SecurityContext.SELinuxOptions",
  56. sc: &api.SecurityContext{SELinuxOptions: &api.SELinuxOptions{}},
  57. expectError: true,
  58. },
  59. {
  60. name: "pod.Spec.SecurityContext.RunAsUser",
  61. podSc: &api.PodSecurityContext{RunAsUser: &runAsUser},
  62. expectError: true,
  63. },
  64. {
  65. name: "pod.Spec.SecurityContext.SELinuxOptions",
  66. podSc: &api.PodSecurityContext{SELinuxOptions: &api.SELinuxOptions{}},
  67. expectError: true,
  68. },
  69. }
  70. for _, tc := range cases {
  71. p := pod()
  72. p.Spec.SecurityContext = tc.podSc
  73. p.Spec.Containers[0].SecurityContext = tc.sc
  74. err := handler.Validate(admission.NewAttributesRecord(p, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
  75. if err != nil && !tc.expectError {
  76. t.Errorf("%v: unexpected error: %v", tc.name, err)
  77. } else if err == nil && tc.expectError {
  78. t.Errorf("%v: expected error", tc.name)
  79. }
  80. // verify init containers are also checked
  81. p = pod()
  82. p.Spec.SecurityContext = tc.podSc
  83. p.Spec.Containers[0].SecurityContext = tc.sc
  84. p.Spec.InitContainers = p.Spec.Containers
  85. p.Spec.Containers = nil
  86. err = handler.Validate(admission.NewAttributesRecord(p, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
  87. if err != nil && !tc.expectError {
  88. t.Errorf("%v: unexpected error: %v", tc.name, err)
  89. } else if err == nil && tc.expectError {
  90. t.Errorf("%v: expected error", tc.name)
  91. }
  92. }
  93. }
  94. func TestPodSecurityContextAdmission(t *testing.T) {
  95. handler := NewSecurityContextDeny()
  96. pod := api.Pod{
  97. Spec: api.PodSpec{
  98. Containers: []api.Container{
  99. {},
  100. },
  101. },
  102. }
  103. fsGroup := int64(1001)
  104. tests := []struct {
  105. securityContext api.PodSecurityContext
  106. errorExpected bool
  107. }{
  108. {
  109. securityContext: api.PodSecurityContext{},
  110. errorExpected: false,
  111. },
  112. {
  113. securityContext: api.PodSecurityContext{
  114. SupplementalGroups: []int64{int64(1234)},
  115. },
  116. errorExpected: true,
  117. },
  118. {
  119. securityContext: api.PodSecurityContext{
  120. FSGroup: &fsGroup,
  121. },
  122. errorExpected: true,
  123. },
  124. }
  125. for _, test := range tests {
  126. pod.Spec.SecurityContext = &test.securityContext
  127. err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
  128. if test.errorExpected && err == nil {
  129. t.Errorf("Expected error for security context %+v but did not get an error", test.securityContext)
  130. }
  131. if !test.errorExpected && err != nil {
  132. t.Errorf("Unexpected error %v for security context %+v", err, test.securityContext)
  133. }
  134. }
  135. }
  136. func TestHandles(t *testing.T) {
  137. handler := NewSecurityContextDeny()
  138. tests := map[admission.Operation]bool{
  139. admission.Update: true,
  140. admission.Create: true,
  141. admission.Delete: false,
  142. admission.Connect: false,
  143. }
  144. for op, expected := range tests {
  145. result := handler.Handles(op)
  146. if result != expected {
  147. t.Errorf("Unexpected result for operation %s: %v\n", op, result)
  148. }
  149. }
  150. }
  151. func pod() *api.Pod {
  152. return &api.Pod{
  153. Spec: api.PodSpec{
  154. Containers: []api.Container{
  155. {},
  156. },
  157. },
  158. }
  159. }