mustrunas_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 selinux
  14. import (
  15. corev1 "k8s.io/api/core/v1"
  16. policy "k8s.io/api/policy/v1beta1"
  17. api "k8s.io/kubernetes/pkg/apis/core"
  18. "k8s.io/kubernetes/pkg/apis/core/v1"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. )
  23. func TestMustRunAsOptions(t *testing.T) {
  24. tests := map[string]struct {
  25. opts *policy.SELinuxStrategyOptions
  26. pass bool
  27. }{
  28. "nil opts": {
  29. opts: nil,
  30. pass: false,
  31. },
  32. "invalid opts": {
  33. opts: &policy.SELinuxStrategyOptions{},
  34. pass: false,
  35. },
  36. "valid opts": {
  37. opts: &policy.SELinuxStrategyOptions{SELinuxOptions: &corev1.SELinuxOptions{}},
  38. pass: true,
  39. },
  40. }
  41. for name, tc := range tests {
  42. _, err := NewMustRunAs(tc.opts)
  43. if err != nil && tc.pass {
  44. t.Errorf("%s expected to pass but received error %#v", name, err)
  45. }
  46. if err == nil && !tc.pass {
  47. t.Errorf("%s expected to fail but did not receive an error", name)
  48. }
  49. }
  50. }
  51. func TestMustRunAsGenerate(t *testing.T) {
  52. opts := &policy.SELinuxStrategyOptions{
  53. SELinuxOptions: &corev1.SELinuxOptions{
  54. User: "user",
  55. Role: "role",
  56. Type: "type",
  57. Level: "level",
  58. },
  59. }
  60. mustRunAs, err := NewMustRunAs(opts)
  61. if err != nil {
  62. t.Fatalf("unexpected error initializing NewMustRunAs %v", err)
  63. }
  64. generated, err := mustRunAs.Generate(nil, nil)
  65. if err != nil {
  66. t.Fatalf("unexpected error generating selinux %v", err)
  67. }
  68. internalSELinuxOptions := &api.SELinuxOptions{}
  69. v1.Convert_v1_SELinuxOptions_To_core_SELinuxOptions(opts.SELinuxOptions, internalSELinuxOptions, nil)
  70. if !reflect.DeepEqual(generated, internalSELinuxOptions) {
  71. t.Errorf("generated selinux does not equal configured selinux")
  72. }
  73. }
  74. func TestMustRunAsValidate(t *testing.T) {
  75. newValidOpts := func() *corev1.SELinuxOptions {
  76. return &corev1.SELinuxOptions{
  77. User: "user",
  78. Role: "role",
  79. Level: "s0:c0,c6",
  80. Type: "type",
  81. }
  82. }
  83. newValidOptsWithLevel := func(level string) *corev1.SELinuxOptions {
  84. opts := newValidOpts()
  85. opts.Level = level
  86. return opts
  87. }
  88. role := newValidOpts()
  89. role.Role = "invalid"
  90. user := newValidOpts()
  91. user.User = "invalid"
  92. seType := newValidOpts()
  93. seType.Type = "invalid"
  94. validOpts := newValidOpts()
  95. tests := map[string]struct {
  96. podSeLinux *corev1.SELinuxOptions
  97. pspSeLinux *corev1.SELinuxOptions
  98. expectedMsg string
  99. }{
  100. "invalid role": {
  101. podSeLinux: role,
  102. pspSeLinux: validOpts,
  103. expectedMsg: "role: Invalid value",
  104. },
  105. "invalid user": {
  106. podSeLinux: user,
  107. pspSeLinux: validOpts,
  108. expectedMsg: "user: Invalid value",
  109. },
  110. "levels are not equal": {
  111. podSeLinux: newValidOptsWithLevel("s0"),
  112. pspSeLinux: newValidOptsWithLevel("s0:c1,c2"),
  113. expectedMsg: "level: Invalid value",
  114. },
  115. "levels differ by sensitivity": {
  116. podSeLinux: newValidOptsWithLevel("s0:c6"),
  117. pspSeLinux: newValidOptsWithLevel("s1:c6"),
  118. expectedMsg: "level: Invalid value",
  119. },
  120. "levels differ by categories": {
  121. podSeLinux: newValidOptsWithLevel("s0:c0,c8"),
  122. pspSeLinux: newValidOptsWithLevel("s0:c1,c7"),
  123. expectedMsg: "level: Invalid value",
  124. },
  125. "valid": {
  126. podSeLinux: validOpts,
  127. pspSeLinux: validOpts,
  128. expectedMsg: "",
  129. },
  130. "valid with different order of categories": {
  131. podSeLinux: newValidOptsWithLevel("s0:c6,c0"),
  132. pspSeLinux: validOpts,
  133. expectedMsg: "",
  134. },
  135. }
  136. for name, tc := range tests {
  137. opts := &policy.SELinuxStrategyOptions{
  138. SELinuxOptions: tc.pspSeLinux,
  139. }
  140. mustRunAs, err := NewMustRunAs(opts)
  141. if err != nil {
  142. t.Errorf("unexpected error initializing NewMustRunAs for testcase %s: %#v", name, err)
  143. continue
  144. }
  145. internalSELinuxOptions := api.SELinuxOptions{}
  146. v1.Convert_v1_SELinuxOptions_To_core_SELinuxOptions(tc.podSeLinux, &internalSELinuxOptions, nil)
  147. errs := mustRunAs.Validate(nil, nil, nil, &internalSELinuxOptions)
  148. //should've passed but didn't
  149. if len(tc.expectedMsg) == 0 && len(errs) > 0 {
  150. t.Errorf("%s expected no errors but received %v", name, errs)
  151. }
  152. //should've failed but didn't
  153. if len(tc.expectedMsg) != 0 && len(errs) == 0 {
  154. t.Errorf("%s expected error %s but received no errors", name, tc.expectedMsg)
  155. }
  156. //failed with additional messages
  157. if len(tc.expectedMsg) != 0 && len(errs) > 1 {
  158. t.Errorf("%s expected error %s but received multiple errors: %v", name, tc.expectedMsg, errs)
  159. }
  160. //check that we got the right message
  161. if len(tc.expectedMsg) != 0 && len(errs) == 1 {
  162. if !strings.Contains(errs[0].Error(), tc.expectedMsg) {
  163. t.Errorf("%s expected error to contain %s but it did not: %v", name, tc.expectedMsg, errs)
  164. }
  165. }
  166. }
  167. }