selinux_util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2017 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 dockershim
  14. import (
  15. "fmt"
  16. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  17. )
  18. // selinuxLabelUser returns the fragment of a Docker security opt that
  19. // describes the SELinux user. Note that strictly speaking this is not
  20. // actually the name of the security opt, but a fragment of the whole key-
  21. // value pair necessary to set the opt.
  22. func selinuxLabelUser(separator rune) string {
  23. return fmt.Sprintf("label%cuser", separator)
  24. }
  25. // selinuxLabelRole returns the fragment of a Docker security opt that
  26. // describes the SELinux role. Note that strictly speaking this is not
  27. // actually the name of the security opt, but a fragment of the whole key-
  28. // value pair necessary to set the opt.
  29. func selinuxLabelRole(separator rune) string {
  30. return fmt.Sprintf("label%crole", separator)
  31. }
  32. // selinuxLabelType returns the fragment of a Docker security opt that
  33. // describes the SELinux type. Note that strictly speaking this is not
  34. // actually the name of the security opt, but a fragment of the whole key-
  35. // value pair necessary to set the opt.
  36. func selinuxLabelType(separator rune) string {
  37. return fmt.Sprintf("label%ctype", separator)
  38. }
  39. // selinuxLabelLevel returns the fragment of a Docker security opt that
  40. // describes the SELinux level. Note that strictly speaking this is not
  41. // actually the name of the security opt, but a fragment of the whole key-
  42. // value pair necessary to set the opt.
  43. func selinuxLabelLevel(separator rune) string {
  44. return fmt.Sprintf("label%clevel", separator)
  45. }
  46. // addSELinuxOptions adds SELinux options to config using the given
  47. // separator.
  48. func addSELinuxOptions(config []string, selinuxOpts *runtimeapi.SELinuxOption, separator rune) []string {
  49. // Note, strictly speaking, we are actually mutating the values of these
  50. // keys, rather than formatting name and value into a string. Docker re-
  51. // uses the same option name multiple times (it's just 'label') with
  52. // different values which are themselves key-value pairs. For example,
  53. // the SELinux type is represented by the security opt:
  54. //
  55. // label<separator>type:<selinux_type>
  56. //
  57. // In Docker API versions before 1.23, the separator was the `:` rune; in
  58. // API version 1.23 it changed to the `=` rune.
  59. config = modifySecurityOption(config, selinuxLabelUser(separator), selinuxOpts.User)
  60. config = modifySecurityOption(config, selinuxLabelRole(separator), selinuxOpts.Role)
  61. config = modifySecurityOption(config, selinuxLabelType(separator), selinuxOpts.Type)
  62. config = modifySecurityOption(config, selinuxLabelLevel(separator), selinuxOpts.Level)
  63. return config
  64. }
  65. // modifySecurityOption adds the security option of name to the config array
  66. // with value in the form of name:value.
  67. func modifySecurityOption(config []string, name, value string) []string {
  68. if len(value) > 0 {
  69. config = append(config, fmt.Sprintf("%s:%s", name, value))
  70. }
  71. return config
  72. }