nonroot.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 user
  14. import (
  15. policy "k8s.io/api/policy/v1beta1"
  16. "k8s.io/apimachinery/pkg/util/validation/field"
  17. api "k8s.io/kubernetes/pkg/apis/core"
  18. )
  19. type nonRoot struct{}
  20. var _ RunAsUserStrategy = &nonRoot{}
  21. func NewRunAsNonRoot(options *policy.RunAsUserStrategyOptions) (RunAsUserStrategy, error) {
  22. return &nonRoot{}, nil
  23. }
  24. // Generate creates the uid based on policy rules. This strategy does return a UID. It assumes
  25. // that the user will specify a UID or the container image specifies a UID.
  26. func (s *nonRoot) Generate(pod *api.Pod, container *api.Container) (*int64, error) {
  27. return nil, nil
  28. }
  29. // Validate ensures that the specified values fall within the range of the strategy. Validation
  30. // of this will pass if either the UID is not set, assuming that the image will provided the UID
  31. // or if the UID is set it is not root. Validation will fail if RunAsNonRoot is set to false.
  32. // In order to work properly this assumes that the kubelet performs a final check on runAsUser
  33. // or the image UID when runAsUser is nil.
  34. func (s *nonRoot) Validate(scPath *field.Path, _ *api.Pod, _ *api.Container, runAsNonRoot *bool, runAsUser *int64) field.ErrorList {
  35. allErrs := field.ErrorList{}
  36. if runAsNonRoot == nil && runAsUser == nil {
  37. allErrs = append(allErrs, field.Required(scPath.Child("runAsNonRoot"), "must be true"))
  38. return allErrs
  39. }
  40. if runAsNonRoot != nil && *runAsNonRoot == false {
  41. allErrs = append(allErrs, field.Invalid(scPath.Child("runAsNonRoot"), *runAsNonRoot, "must be true"))
  42. return allErrs
  43. }
  44. if runAsUser != nil && *runAsUser == 0 {
  45. allErrs = append(allErrs, field.Invalid(scPath.Child("runAsUser"), *runAsUser, "running with the root UID is forbidden"))
  46. return allErrs
  47. }
  48. return allErrs
  49. }