util.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2014 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 securitycontext
  14. import (
  15. "fmt"
  16. "strings"
  17. "k8s.io/kubernetes/pkg/api"
  18. )
  19. // HasPrivilegedRequest returns the value of SecurityContext.Privileged, taking into account
  20. // the possibility of nils
  21. func HasPrivilegedRequest(container *api.Container) bool {
  22. if container.SecurityContext == nil {
  23. return false
  24. }
  25. if container.SecurityContext.Privileged == nil {
  26. return false
  27. }
  28. return *container.SecurityContext.Privileged
  29. }
  30. // HasCapabilitiesRequest returns true if Adds or Drops are defined in the security context
  31. // capabilities, taking into account nils
  32. func HasCapabilitiesRequest(container *api.Container) bool {
  33. if container.SecurityContext == nil {
  34. return false
  35. }
  36. if container.SecurityContext.Capabilities == nil {
  37. return false
  38. }
  39. return len(container.SecurityContext.Capabilities.Add) > 0 || len(container.SecurityContext.Capabilities.Drop) > 0
  40. }
  41. const expectedSELinuxFields = 4
  42. // ParseSELinuxOptions parses a string containing a full SELinux context
  43. // (user, role, type, and level) into an SELinuxOptions object. If the
  44. // context is malformed, an error is returned.
  45. func ParseSELinuxOptions(context string) (*api.SELinuxOptions, error) {
  46. fields := strings.SplitN(context, ":", expectedSELinuxFields)
  47. if len(fields) != expectedSELinuxFields {
  48. return nil, fmt.Errorf("expected %v fields in selinux; got %v (context: %v)", expectedSELinuxFields, len(fields), context)
  49. }
  50. return &api.SELinuxOptions{
  51. User: fields[0],
  52. Role: fields[1],
  53. Type: fields[2],
  54. Level: fields[3],
  55. }, nil
  56. }
  57. // HasNonRootUID returns true if the runAsUser is set and is greater than 0.
  58. func HasRootUID(container *api.Container) bool {
  59. if container.SecurityContext == nil {
  60. return false
  61. }
  62. if container.SecurityContext.RunAsUser == nil {
  63. return false
  64. }
  65. return *container.SecurityContext.RunAsUser == 0
  66. }
  67. // HasRunAsUser determines if the sc's runAsUser field is set.
  68. func HasRunAsUser(container *api.Container) bool {
  69. return container.SecurityContext != nil && container.SecurityContext.RunAsUser != nil
  70. }
  71. // HasRootRunAsUser returns true if the run as user is set and it is set to 0.
  72. func HasRootRunAsUser(container *api.Container) bool {
  73. return HasRunAsUser(container) && HasRootUID(container)
  74. }