security_context_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 kuberuntime
  14. import (
  15. "k8s.io/api/core/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "github.com/stretchr/testify/assert"
  18. "testing"
  19. )
  20. func TestVerifyRunAsNonRoot(t *testing.T) {
  21. pod := &v1.Pod{
  22. ObjectMeta: metav1.ObjectMeta{
  23. UID: "12345678",
  24. Name: "bar",
  25. Namespace: "new",
  26. },
  27. Spec: v1.PodSpec{
  28. Containers: []v1.Container{
  29. {
  30. Name: "foo",
  31. Image: "busybox",
  32. ImagePullPolicy: v1.PullIfNotPresent,
  33. Command: []string{"testCommand"},
  34. WorkingDir: "testWorkingDir",
  35. },
  36. },
  37. },
  38. }
  39. rootUser := int64(0)
  40. anyUser := int64(1000)
  41. runAsNonRootTrue := true
  42. runAsNonRootFalse := false
  43. for _, test := range []struct {
  44. desc string
  45. sc *v1.SecurityContext
  46. uid *int64
  47. username string
  48. fail bool
  49. }{
  50. {
  51. desc: "Pass if SecurityContext is not set",
  52. sc: nil,
  53. uid: &rootUser,
  54. fail: false,
  55. },
  56. {
  57. desc: "Pass if RunAsNonRoot is not set",
  58. sc: &v1.SecurityContext{
  59. RunAsUser: &rootUser,
  60. },
  61. uid: &rootUser,
  62. fail: false,
  63. },
  64. {
  65. desc: "Pass if RunAsNonRoot is false (image user is root)",
  66. sc: &v1.SecurityContext{
  67. RunAsNonRoot: &runAsNonRootFalse,
  68. },
  69. uid: &rootUser,
  70. fail: false,
  71. },
  72. {
  73. desc: "Pass if RunAsNonRoot is false (RunAsUser is root)",
  74. sc: &v1.SecurityContext{
  75. RunAsNonRoot: &runAsNonRootFalse,
  76. RunAsUser: &rootUser,
  77. },
  78. uid: &rootUser,
  79. fail: false,
  80. },
  81. {
  82. desc: "Fail if container's RunAsUser is root and RunAsNonRoot is true",
  83. sc: &v1.SecurityContext{
  84. RunAsNonRoot: &runAsNonRootTrue,
  85. RunAsUser: &rootUser,
  86. },
  87. uid: &rootUser,
  88. fail: true,
  89. },
  90. {
  91. desc: "Fail if image's user is root and RunAsNonRoot is true",
  92. sc: &v1.SecurityContext{
  93. RunAsNonRoot: &runAsNonRootTrue,
  94. },
  95. uid: &rootUser,
  96. fail: true,
  97. },
  98. {
  99. desc: "Fail if image's username is set and RunAsNonRoot is true",
  100. sc: &v1.SecurityContext{
  101. RunAsNonRoot: &runAsNonRootTrue,
  102. },
  103. username: "test",
  104. fail: true,
  105. },
  106. {
  107. desc: "Pass if image's user is non-root and RunAsNonRoot is true",
  108. sc: &v1.SecurityContext{
  109. RunAsNonRoot: &runAsNonRootTrue,
  110. },
  111. uid: &anyUser,
  112. fail: false,
  113. },
  114. {
  115. desc: "Pass if container's user and image's user aren't set and RunAsNonRoot is true",
  116. sc: &v1.SecurityContext{
  117. RunAsNonRoot: &runAsNonRootTrue,
  118. },
  119. fail: false,
  120. },
  121. } {
  122. pod.Spec.Containers[0].SecurityContext = test.sc
  123. err := verifyRunAsNonRoot(pod, &pod.Spec.Containers[0], test.uid, test.username)
  124. if test.fail {
  125. assert.Error(t, err, test.desc)
  126. } else {
  127. assert.NoError(t, err, test.desc)
  128. }
  129. }
  130. }