strategy_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. Copyright 2016 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 seccomp
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. api "k8s.io/kubernetes/pkg/apis/core"
  20. )
  21. var (
  22. withoutSeccomp = map[string]string{"foo": "bar"}
  23. allowAnyNoDefault = map[string]string{
  24. AllowedProfilesAnnotationKey: "*",
  25. }
  26. allowAnyDefault = map[string]string{
  27. AllowedProfilesAnnotationKey: "*",
  28. DefaultProfileAnnotationKey: "foo",
  29. }
  30. allowAnyAndSpecificDefault = map[string]string{
  31. AllowedProfilesAnnotationKey: "*,bar",
  32. DefaultProfileAnnotationKey: "foo",
  33. }
  34. allowSpecific = map[string]string{
  35. AllowedProfilesAnnotationKey: "foo",
  36. }
  37. )
  38. func TestNewStrategy(t *testing.T) {
  39. tests := map[string]struct {
  40. annotations map[string]string
  41. expectedAllowedProfilesString string
  42. expectedAllowAny bool
  43. expectedAllowedProfiles map[string]bool
  44. expectedDefaultProfile string
  45. }{
  46. "no seccomp": {
  47. annotations: withoutSeccomp,
  48. expectedAllowAny: false,
  49. expectedAllowedProfilesString: "",
  50. expectedAllowedProfiles: nil,
  51. expectedDefaultProfile: "",
  52. },
  53. "allow any, no default": {
  54. annotations: allowAnyNoDefault,
  55. expectedAllowAny: true,
  56. expectedAllowedProfilesString: "*",
  57. expectedAllowedProfiles: map[string]bool{},
  58. expectedDefaultProfile: "",
  59. },
  60. "allow any, default": {
  61. annotations: allowAnyDefault,
  62. expectedAllowAny: true,
  63. expectedAllowedProfilesString: "*",
  64. expectedAllowedProfiles: map[string]bool{},
  65. expectedDefaultProfile: "foo",
  66. },
  67. "allow any and specific, default": {
  68. annotations: allowAnyAndSpecificDefault,
  69. expectedAllowAny: true,
  70. expectedAllowedProfilesString: "*,bar",
  71. expectedAllowedProfiles: map[string]bool{
  72. "bar": true,
  73. },
  74. expectedDefaultProfile: "foo",
  75. },
  76. }
  77. for k, v := range tests {
  78. s := NewStrategy(v.annotations)
  79. internalStrat, _ := s.(*strategy)
  80. if internalStrat.allowAnyProfile != v.expectedAllowAny {
  81. t.Errorf("%s expected allowAnyProfile to be %t but found %t", k, v.expectedAllowAny, internalStrat.allowAnyProfile)
  82. }
  83. if internalStrat.allowedProfilesString != v.expectedAllowedProfilesString {
  84. t.Errorf("%s expected allowedProfilesString to be %s but found %s", k, v.expectedAllowedProfilesString, internalStrat.allowedProfilesString)
  85. }
  86. if internalStrat.defaultProfile != v.expectedDefaultProfile {
  87. t.Errorf("%s expected defaultProfile to be %s but found %s", k, v.expectedDefaultProfile, internalStrat.defaultProfile)
  88. }
  89. if !reflect.DeepEqual(v.expectedAllowedProfiles, internalStrat.allowedProfiles) {
  90. t.Errorf("%s expected expectedAllowedProfiles to be %#v but found %#v", k, v.expectedAllowedProfiles, internalStrat.allowedProfiles)
  91. }
  92. }
  93. }
  94. func TestGenerate(t *testing.T) {
  95. tests := map[string]struct {
  96. pspAnnotations map[string]string
  97. podAnnotations map[string]string
  98. expectedProfile string
  99. }{
  100. "no seccomp, no pod annotations": {
  101. pspAnnotations: withoutSeccomp,
  102. podAnnotations: nil,
  103. expectedProfile: "",
  104. },
  105. "no seccomp, pod annotations": {
  106. pspAnnotations: withoutSeccomp,
  107. podAnnotations: map[string]string{
  108. api.SeccompPodAnnotationKey: "foo",
  109. },
  110. expectedProfile: "foo",
  111. },
  112. "seccomp with no default, no pod annotations": {
  113. pspAnnotations: allowAnyNoDefault,
  114. podAnnotations: nil,
  115. expectedProfile: "",
  116. },
  117. "seccomp with no default, pod annotations": {
  118. pspAnnotations: allowAnyNoDefault,
  119. podAnnotations: map[string]string{
  120. api.SeccompPodAnnotationKey: "foo",
  121. },
  122. expectedProfile: "foo",
  123. },
  124. "seccomp with default, no pod annotations": {
  125. pspAnnotations: allowAnyDefault,
  126. podAnnotations: nil,
  127. expectedProfile: "foo",
  128. },
  129. "seccomp with default, pod annotations": {
  130. pspAnnotations: allowAnyDefault,
  131. podAnnotations: map[string]string{
  132. api.SeccompPodAnnotationKey: "bar",
  133. },
  134. expectedProfile: "bar",
  135. },
  136. }
  137. for k, v := range tests {
  138. s := NewStrategy(v.pspAnnotations)
  139. actual, err := s.Generate(v.podAnnotations, nil)
  140. if err != nil {
  141. t.Errorf("%s received error during generation %#v", k, err)
  142. continue
  143. }
  144. if actual != v.expectedProfile {
  145. t.Errorf("%s expected profile %s but received %s", k, v.expectedProfile, actual)
  146. }
  147. }
  148. }
  149. func TestValidatePod(t *testing.T) {
  150. tests := map[string]struct {
  151. pspAnnotations map[string]string
  152. podAnnotations map[string]string
  153. expectedError string
  154. }{
  155. "no pod annotations, required profiles": {
  156. pspAnnotations: allowSpecific,
  157. podAnnotations: nil,
  158. expectedError: "Forbidden: is not an allowed seccomp profile. Valid values are foo",
  159. },
  160. "no pod annotations, no required profiles": {
  161. pspAnnotations: withoutSeccomp,
  162. podAnnotations: nil,
  163. expectedError: "",
  164. },
  165. "valid pod annotations, required profiles": {
  166. pspAnnotations: allowSpecific,
  167. podAnnotations: map[string]string{
  168. api.SeccompPodAnnotationKey: "foo",
  169. },
  170. expectedError: "",
  171. },
  172. "invalid pod annotations, required profiles": {
  173. pspAnnotations: allowSpecific,
  174. podAnnotations: map[string]string{
  175. api.SeccompPodAnnotationKey: "bar",
  176. },
  177. expectedError: "Forbidden: bar is not an allowed seccomp profile. Valid values are foo",
  178. },
  179. "pod annotations, no required profiles": {
  180. pspAnnotations: withoutSeccomp,
  181. podAnnotations: map[string]string{
  182. api.SeccompPodAnnotationKey: "foo",
  183. },
  184. expectedError: "Forbidden: seccomp may not be set",
  185. },
  186. "pod annotations, allow any": {
  187. pspAnnotations: allowAnyNoDefault,
  188. podAnnotations: map[string]string{
  189. api.SeccompPodAnnotationKey: "foo",
  190. },
  191. expectedError: "",
  192. },
  193. "no pod annotations, allow any": {
  194. pspAnnotations: allowAnyNoDefault,
  195. podAnnotations: nil,
  196. expectedError: "",
  197. },
  198. }
  199. for k, v := range tests {
  200. pod := &api.Pod{
  201. ObjectMeta: metav1.ObjectMeta{
  202. Annotations: v.podAnnotations,
  203. },
  204. }
  205. s := NewStrategy(v.pspAnnotations)
  206. errs := s.ValidatePod(pod)
  207. if v.expectedError == "" && len(errs) != 0 {
  208. t.Errorf("%s expected no errors but received %#v", k, errs.ToAggregate().Error())
  209. }
  210. if v.expectedError != "" && len(errs) == 0 {
  211. t.Errorf("%s expected error %s but received none", k, v.expectedError)
  212. }
  213. if v.expectedError != "" && len(errs) > 1 {
  214. t.Errorf("%s received multiple errors: %s", k, errs.ToAggregate().Error())
  215. }
  216. if v.expectedError != "" && len(errs) == 1 && !strings.Contains(errs.ToAggregate().Error(), v.expectedError) {
  217. t.Errorf("%s expected error %s but received %s", k, v.expectedError, errs.ToAggregate().Error())
  218. }
  219. }
  220. }
  221. func TestValidateContainer(t *testing.T) {
  222. tests := map[string]struct {
  223. pspAnnotations map[string]string
  224. podAnnotations map[string]string
  225. expectedError string
  226. }{
  227. "no pod annotations, required profiles": {
  228. pspAnnotations: allowSpecific,
  229. podAnnotations: nil,
  230. expectedError: "Forbidden: is not an allowed seccomp profile. Valid values are foo",
  231. },
  232. "no pod annotations, no required profiles": {
  233. pspAnnotations: withoutSeccomp,
  234. podAnnotations: nil,
  235. expectedError: "",
  236. },
  237. "valid pod annotations, required profiles": {
  238. pspAnnotations: allowSpecific,
  239. podAnnotations: map[string]string{
  240. api.SeccompContainerAnnotationKeyPrefix + "container": "foo",
  241. },
  242. expectedError: "",
  243. },
  244. "invalid pod annotations, required profiles": {
  245. pspAnnotations: allowSpecific,
  246. podAnnotations: map[string]string{
  247. api.SeccompContainerAnnotationKeyPrefix + "container": "bar",
  248. },
  249. expectedError: "Forbidden: bar is not an allowed seccomp profile. Valid values are foo",
  250. },
  251. "pod annotations, no required profiles": {
  252. pspAnnotations: withoutSeccomp,
  253. podAnnotations: map[string]string{
  254. api.SeccompContainerAnnotationKeyPrefix + "container": "foo",
  255. },
  256. expectedError: "Forbidden: seccomp may not be set",
  257. },
  258. "pod annotations, allow any": {
  259. pspAnnotations: allowAnyNoDefault,
  260. podAnnotations: map[string]string{
  261. api.SeccompContainerAnnotationKeyPrefix + "container": "foo",
  262. },
  263. expectedError: "",
  264. },
  265. "no pod annotations, allow any": {
  266. pspAnnotations: allowAnyNoDefault,
  267. podAnnotations: nil,
  268. expectedError: "",
  269. },
  270. "container inherits valid pod annotation": {
  271. pspAnnotations: allowSpecific,
  272. podAnnotations: map[string]string{
  273. api.SeccompPodAnnotationKey: "foo",
  274. },
  275. expectedError: "",
  276. },
  277. "container inherits invalid pod annotation": {
  278. pspAnnotations: allowSpecific,
  279. podAnnotations: map[string]string{
  280. api.SeccompPodAnnotationKey: "bar",
  281. },
  282. expectedError: "Forbidden: bar is not an allowed seccomp profile. Valid values are foo",
  283. },
  284. }
  285. for k, v := range tests {
  286. pod := &api.Pod{
  287. ObjectMeta: metav1.ObjectMeta{
  288. Annotations: v.podAnnotations,
  289. },
  290. }
  291. container := &api.Container{
  292. Name: "container",
  293. }
  294. s := NewStrategy(v.pspAnnotations)
  295. errs := s.ValidateContainer(pod, container)
  296. if v.expectedError == "" && len(errs) != 0 {
  297. t.Errorf("%s expected no errors but received %#v", k, errs.ToAggregate().Error())
  298. }
  299. if v.expectedError != "" && len(errs) == 0 {
  300. t.Errorf("%s expected error %s but received none", k, v.expectedError)
  301. }
  302. if v.expectedError != "" && len(errs) > 1 {
  303. t.Errorf("%s received multiple errors: %s", k, errs.ToAggregate().Error())
  304. }
  305. if v.expectedError != "" && len(errs) == 1 && !strings.Contains(errs.ToAggregate().Error(), v.expectedError) {
  306. t.Errorf("%s expected error %s but received %s", k, v.expectedError, errs.ToAggregate().Error())
  307. }
  308. }
  309. }