mustrunas.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "fmt"
  16. "sort"
  17. "strings"
  18. policy "k8s.io/api/policy/v1beta1"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/apis/core/v1"
  22. "k8s.io/kubernetes/pkg/security/podsecuritypolicy/util"
  23. )
  24. type mustRunAs struct {
  25. opts *api.SELinuxOptions
  26. }
  27. var _ SELinuxStrategy = &mustRunAs{}
  28. func NewMustRunAs(options *policy.SELinuxStrategyOptions) (SELinuxStrategy, error) {
  29. if options == nil {
  30. return nil, fmt.Errorf("MustRunAs requires SELinuxContextStrategyOptions")
  31. }
  32. if options.SELinuxOptions == nil {
  33. return nil, fmt.Errorf("MustRunAs requires SELinuxOptions")
  34. }
  35. internalSELinuxOptions := &api.SELinuxOptions{}
  36. if err := v1.Convert_v1_SELinuxOptions_To_core_SELinuxOptions(options.SELinuxOptions, internalSELinuxOptions, nil); err != nil {
  37. return nil, err
  38. }
  39. return &mustRunAs{
  40. opts: internalSELinuxOptions,
  41. }, nil
  42. }
  43. // Generate creates the SELinuxOptions based on constraint rules.
  44. func (s *mustRunAs) Generate(_ *api.Pod, _ *api.Container) (*api.SELinuxOptions, error) {
  45. return s.opts, nil
  46. }
  47. // Validate ensures that the specified values fall within the range of the strategy.
  48. func (s *mustRunAs) Validate(fldPath *field.Path, _ *api.Pod, _ *api.Container, seLinux *api.SELinuxOptions) field.ErrorList {
  49. allErrs := field.ErrorList{}
  50. if seLinux == nil {
  51. allErrs = append(allErrs, field.Required(fldPath, ""))
  52. return allErrs
  53. }
  54. if !equalLevels(s.opts.Level, seLinux.Level) {
  55. detail := fmt.Sprintf("must be %s", s.opts.Level)
  56. allErrs = append(allErrs, field.Invalid(fldPath.Child("level"), seLinux.Level, detail))
  57. }
  58. if seLinux.Role != s.opts.Role {
  59. detail := fmt.Sprintf("must be %s", s.opts.Role)
  60. allErrs = append(allErrs, field.Invalid(fldPath.Child("role"), seLinux.Role, detail))
  61. }
  62. if seLinux.Type != s.opts.Type {
  63. detail := fmt.Sprintf("must be %s", s.opts.Type)
  64. allErrs = append(allErrs, field.Invalid(fldPath.Child("type"), seLinux.Type, detail))
  65. }
  66. if seLinux.User != s.opts.User {
  67. detail := fmt.Sprintf("must be %s", s.opts.User)
  68. allErrs = append(allErrs, field.Invalid(fldPath.Child("user"), seLinux.User, detail))
  69. }
  70. return allErrs
  71. }
  72. // equalLevels compares SELinux levels for equality.
  73. func equalLevels(expected, actual string) bool {
  74. if expected == actual {
  75. return true
  76. }
  77. // "s0:c6,c0" => [ "s0", "c6,c0" ]
  78. expectedParts := strings.SplitN(expected, ":", 2)
  79. actualParts := strings.SplitN(actual, ":", 2)
  80. // both SELinux levels must be in a format "sX:cY"
  81. if len(expectedParts) != 2 || len(actualParts) != 2 {
  82. return false
  83. }
  84. if !equalSensitivity(expectedParts[0], actualParts[0]) {
  85. return false
  86. }
  87. if !equalCategories(expectedParts[1], actualParts[1]) {
  88. return false
  89. }
  90. return true
  91. }
  92. // equalSensitivity compares sensitivities of the SELinux levels for equality.
  93. func equalSensitivity(expected, actual string) bool {
  94. return expected == actual
  95. }
  96. // equalCategories compares categories of the SELinux levels for equality.
  97. func equalCategories(expected, actual string) bool {
  98. expectedCategories := strings.Split(expected, ",")
  99. actualCategories := strings.Split(actual, ",")
  100. sort.Strings(expectedCategories)
  101. sort.Strings(actualCategories)
  102. return util.EqualStringSlices(expectedCategories, actualCategories)
  103. }