util.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 util
  14. import (
  15. "fmt"
  16. "strings"
  17. policy "k8s.io/api/policy/v1beta1"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. api "k8s.io/kubernetes/pkg/apis/core"
  20. )
  21. const (
  22. ValidatedPSPAnnotation = "kubernetes.io/psp"
  23. )
  24. func GetAllFSTypesExcept(exceptions ...string) sets.String {
  25. fstypes := GetAllFSTypesAsSet()
  26. for _, e := range exceptions {
  27. fstypes.Delete(e)
  28. }
  29. return fstypes
  30. }
  31. func GetAllFSTypesAsSet() sets.String {
  32. fstypes := sets.NewString()
  33. fstypes.Insert(
  34. string(policy.HostPath),
  35. string(policy.AzureFile),
  36. string(policy.Flocker),
  37. string(policy.FlexVolume),
  38. string(policy.EmptyDir),
  39. string(policy.GCEPersistentDisk),
  40. string(policy.AWSElasticBlockStore),
  41. string(policy.GitRepo),
  42. string(policy.Secret),
  43. string(policy.NFS),
  44. string(policy.ISCSI),
  45. string(policy.Glusterfs),
  46. string(policy.PersistentVolumeClaim),
  47. string(policy.RBD),
  48. string(policy.Cinder),
  49. string(policy.CephFS),
  50. string(policy.DownwardAPI),
  51. string(policy.FC),
  52. string(policy.ConfigMap),
  53. string(policy.VsphereVolume),
  54. string(policy.Quobyte),
  55. string(policy.AzureDisk),
  56. string(policy.PhotonPersistentDisk),
  57. string(policy.StorageOS),
  58. string(policy.Projected),
  59. string(policy.PortworxVolume),
  60. string(policy.ScaleIO),
  61. string(policy.CSI),
  62. )
  63. return fstypes
  64. }
  65. // getVolumeFSType gets the FSType for a volume.
  66. func GetVolumeFSType(v api.Volume) (policy.FSType, error) {
  67. switch {
  68. case v.HostPath != nil:
  69. return policy.HostPath, nil
  70. case v.EmptyDir != nil:
  71. return policy.EmptyDir, nil
  72. case v.GCEPersistentDisk != nil:
  73. return policy.GCEPersistentDisk, nil
  74. case v.AWSElasticBlockStore != nil:
  75. return policy.AWSElasticBlockStore, nil
  76. case v.GitRepo != nil:
  77. return policy.GitRepo, nil
  78. case v.Secret != nil:
  79. return policy.Secret, nil
  80. case v.NFS != nil:
  81. return policy.NFS, nil
  82. case v.ISCSI != nil:
  83. return policy.ISCSI, nil
  84. case v.Glusterfs != nil:
  85. return policy.Glusterfs, nil
  86. case v.PersistentVolumeClaim != nil:
  87. return policy.PersistentVolumeClaim, nil
  88. case v.RBD != nil:
  89. return policy.RBD, nil
  90. case v.FlexVolume != nil:
  91. return policy.FlexVolume, nil
  92. case v.Cinder != nil:
  93. return policy.Cinder, nil
  94. case v.CephFS != nil:
  95. return policy.CephFS, nil
  96. case v.Flocker != nil:
  97. return policy.Flocker, nil
  98. case v.DownwardAPI != nil:
  99. return policy.DownwardAPI, nil
  100. case v.FC != nil:
  101. return policy.FC, nil
  102. case v.AzureFile != nil:
  103. return policy.AzureFile, nil
  104. case v.ConfigMap != nil:
  105. return policy.ConfigMap, nil
  106. case v.VsphereVolume != nil:
  107. return policy.VsphereVolume, nil
  108. case v.Quobyte != nil:
  109. return policy.Quobyte, nil
  110. case v.AzureDisk != nil:
  111. return policy.AzureDisk, nil
  112. case v.PhotonPersistentDisk != nil:
  113. return policy.PhotonPersistentDisk, nil
  114. case v.StorageOS != nil:
  115. return policy.StorageOS, nil
  116. case v.Projected != nil:
  117. return policy.Projected, nil
  118. case v.PortworxVolume != nil:
  119. return policy.PortworxVolume, nil
  120. case v.ScaleIO != nil:
  121. return policy.ScaleIO, nil
  122. case v.CSI != nil:
  123. return policy.CSI, nil
  124. }
  125. return "", fmt.Errorf("unknown volume type for volume: %#v", v)
  126. }
  127. // FSTypeToStringSet converts an FSType slice to a string set.
  128. func FSTypeToStringSet(fsTypes []policy.FSType) sets.String {
  129. set := sets.NewString()
  130. for _, v := range fsTypes {
  131. set.Insert(string(v))
  132. }
  133. return set
  134. }
  135. // PSPAllowsAllVolumes checks for FSTypeAll in the psp's allowed volumes.
  136. func PSPAllowsAllVolumes(psp *policy.PodSecurityPolicy) bool {
  137. return PSPAllowsFSType(psp, policy.All)
  138. }
  139. // PSPAllowsFSType is a utility for checking if a PSP allows a particular FSType.
  140. // If all volumes are allowed then this will return true for any FSType passed.
  141. func PSPAllowsFSType(psp *policy.PodSecurityPolicy, fsType policy.FSType) bool {
  142. if psp == nil {
  143. return false
  144. }
  145. for _, v := range psp.Spec.Volumes {
  146. if v == fsType || v == policy.All {
  147. return true
  148. }
  149. }
  150. return false
  151. }
  152. // UserFallsInRange is a utility to determine it the id falls in the valid range.
  153. func UserFallsInRange(id int64, rng policy.IDRange) bool {
  154. return id >= rng.Min && id <= rng.Max
  155. }
  156. // GroupFallsInRange is a utility to determine it the id falls in the valid range.
  157. func GroupFallsInRange(id int64, rng policy.IDRange) bool {
  158. return id >= rng.Min && id <= rng.Max
  159. }
  160. // AllowsHostVolumePath is a utility for checking if a PSP allows the host volume path.
  161. // This only checks the path. You should still check to make sure the host volume fs type is allowed.
  162. func AllowsHostVolumePath(psp *policy.PodSecurityPolicy, hostPath string) (pathIsAllowed, mustBeReadOnly bool) {
  163. if psp == nil {
  164. return false, false
  165. }
  166. // If no allowed paths are specified then allow any path
  167. if len(psp.Spec.AllowedHostPaths) == 0 {
  168. return true, false
  169. }
  170. for _, allowedPath := range psp.Spec.AllowedHostPaths {
  171. if hasPathPrefix(hostPath, allowedPath.PathPrefix) {
  172. if !allowedPath.ReadOnly {
  173. return true, allowedPath.ReadOnly
  174. }
  175. pathIsAllowed = true
  176. mustBeReadOnly = true
  177. }
  178. }
  179. return pathIsAllowed, mustBeReadOnly
  180. }
  181. // hasPathPrefix returns true if the string matches pathPrefix exactly, or if is prefixed with pathPrefix at a path segment boundary
  182. // the string and pathPrefix are both normalized to remove trailing slashes prior to checking.
  183. func hasPathPrefix(s, pathPrefix string) bool {
  184. s = strings.TrimSuffix(s, "/")
  185. pathPrefix = strings.TrimSuffix(pathPrefix, "/")
  186. // Short circuit if s doesn't contain the prefix at all
  187. if !strings.HasPrefix(s, pathPrefix) {
  188. return false
  189. }
  190. pathPrefixLength := len(pathPrefix)
  191. if len(s) == pathPrefixLength {
  192. // Exact match
  193. return true
  194. }
  195. if s[pathPrefixLength:pathPrefixLength+1] == "/" {
  196. // The next character in s is a path segment boundary
  197. // Check this instead of normalizing pathPrefix to avoid allocating on every call
  198. // Example where this check applies: s=/foo/bar and pathPrefix=/foo
  199. return true
  200. }
  201. return false
  202. }
  203. // EqualStringSlices compares string slices for equality. Slices are equal when
  204. // their sizes and elements on similar positions are equal.
  205. func EqualStringSlices(a, b []string) bool {
  206. if len(a) != len(b) {
  207. return false
  208. }
  209. for i := 0; i < len(a); i++ {
  210. if a[i] != b[i] {
  211. return false
  212. }
  213. }
  214. return true
  215. }