validate_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 apparmor
  14. import (
  15. "errors"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func TestGetAppArmorFS(t *testing.T) {
  21. // This test only passes on systems running AppArmor with the default configuration.
  22. // The test should be manually run if modifying the getAppArmorFS function.
  23. t.Skip()
  24. const expectedPath = "/sys/kernel/security/apparmor"
  25. actualPath, err := getAppArmorFS()
  26. assert.NoError(t, err)
  27. assert.Equal(t, expectedPath, actualPath)
  28. }
  29. func TestValidateHost(t *testing.T) {
  30. // This test only passes on systems running AppArmor with the default configuration.
  31. // The test should be manually run if modifying the getAppArmorFS function.
  32. t.Skip()
  33. assert.NoError(t, validateHost("docker"))
  34. assert.Error(t, validateHost("rkt"))
  35. }
  36. func TestValidateProfile(t *testing.T) {
  37. loadedProfiles := map[string]bool{
  38. "docker-default": true,
  39. "foo-bar": true,
  40. "baz": true,
  41. "/usr/sbin/ntpd": true,
  42. "/usr/lib/connman/scripts/dhclient-script": true,
  43. "/usr/lib/NetworkManager/nm-dhcp-client.action": true,
  44. "/usr/bin/evince-previewer//sanitized_helper": true,
  45. }
  46. tests := []struct {
  47. profile string
  48. expectValid bool
  49. }{
  50. {"", true},
  51. {ProfileRuntimeDefault, true},
  52. {"baz", false}, // Missing local prefix.
  53. {ProfileNamePrefix + "/usr/sbin/ntpd", true},
  54. {ProfileNamePrefix + "foo-bar", true},
  55. {ProfileNamePrefix + "unloaded", false}, // Not loaded.
  56. {ProfileNamePrefix + "", false},
  57. }
  58. for _, test := range tests {
  59. err := validateProfile(test.profile, loadedProfiles)
  60. if test.expectValid {
  61. assert.NoError(t, err, "Profile %s should be valid", test.profile)
  62. } else {
  63. assert.Error(t, err, "Profile %s should not be valid", test.profile)
  64. }
  65. }
  66. }
  67. func TestValidateBadHost(t *testing.T) {
  68. hostErr := errors.New("expected host error")
  69. v := &validator{
  70. validateHostErr: hostErr,
  71. }
  72. tests := []struct {
  73. profile string
  74. expectValid bool
  75. }{
  76. {"", true},
  77. {ProfileRuntimeDefault, false},
  78. {ProfileNamePrefix + "docker-default", false},
  79. }
  80. for _, test := range tests {
  81. err := v.Validate(getPodWithProfile(test.profile))
  82. if test.expectValid {
  83. assert.NoError(t, err, "Pod with profile %q should be valid", test.profile)
  84. } else {
  85. assert.Equal(t, hostErr, err, "Pod with profile %q should trigger a host validation error", test.profile)
  86. }
  87. }
  88. }
  89. func TestValidateValidHost(t *testing.T) {
  90. v := &validator{
  91. appArmorFS: "./testdata/",
  92. }
  93. tests := []struct {
  94. profile string
  95. expectValid bool
  96. }{
  97. {"", true},
  98. {ProfileRuntimeDefault, true},
  99. {ProfileNamePrefix + "docker-default", true},
  100. {ProfileNamePrefix + "foo-container", true},
  101. {ProfileNamePrefix + "/usr/sbin/ntpd", true},
  102. {"docker-default", false},
  103. {ProfileNamePrefix + "foo", false},
  104. {ProfileNamePrefix + "", false},
  105. }
  106. for _, test := range tests {
  107. err := v.Validate(getPodWithProfile(test.profile))
  108. if test.expectValid {
  109. assert.NoError(t, err, "Pod with profile %q should be valid", test.profile)
  110. } else {
  111. assert.Error(t, err, "Pod with profile %q should trigger a validation error", test.profile)
  112. }
  113. }
  114. // Test multi-container pod.
  115. pod := &api.Pod{
  116. ObjectMeta: api.ObjectMeta{
  117. Annotations: map[string]string{
  118. ContainerAnnotationKeyPrefix + "init": ProfileNamePrefix + "foo-container",
  119. ContainerAnnotationKeyPrefix + "test1": ProfileRuntimeDefault,
  120. ContainerAnnotationKeyPrefix + "test2": ProfileNamePrefix + "docker-default",
  121. },
  122. },
  123. Spec: api.PodSpec{
  124. InitContainers: []api.Container{
  125. {Name: "init"},
  126. },
  127. Containers: []api.Container{
  128. {Name: "test1"},
  129. {Name: "test2"},
  130. {Name: "no-profile"},
  131. },
  132. },
  133. }
  134. assert.NoError(t, v.Validate(pod), "Multi-container pod should validate")
  135. for k, val := range pod.Annotations {
  136. pod.Annotations[k] = val + "-bad"
  137. assert.Error(t, v.Validate(pod), "Multi-container pod with invalid profile %s:%s", k, pod.Annotations[k])
  138. pod.Annotations[k] = val // Restore.
  139. }
  140. }
  141. func TestParseProfileName(t *testing.T) {
  142. tests := []struct{ line, expected string }{
  143. {"foo://bar/baz (kill)", "foo://bar/baz"},
  144. {"foo-bar (enforce)", "foo-bar"},
  145. {"/usr/foo/bar/baz (complain)", "/usr/foo/bar/baz"},
  146. }
  147. for _, test := range tests {
  148. name := parseProfileName(test.line)
  149. assert.Equal(t, test.expected, name, "Parsing %s", test.line)
  150. }
  151. }
  152. func getPodWithProfile(profile string) *api.Pod {
  153. annotations := map[string]string{
  154. ContainerAnnotationKeyPrefix + "test": profile,
  155. }
  156. if profile == "" {
  157. annotations = map[string]string{
  158. "foo": "bar",
  159. }
  160. }
  161. return &api.Pod{
  162. ObjectMeta: api.ObjectMeta{
  163. Annotations: annotations,
  164. },
  165. Spec: api.PodSpec{
  166. Containers: []api.Container{
  167. {
  168. Name: "test",
  169. },
  170. },
  171. },
  172. }
  173. }