helpers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 apparmor
  14. import (
  15. "strings"
  16. "k8s.io/api/core/v1"
  17. )
  18. // TODO: Move these values into the API package.
  19. const (
  20. // The prefix to an annotation key specifying a container profile.
  21. ContainerAnnotationKeyPrefix = "container.apparmor.security.beta.kubernetes.io/"
  22. // The annotation key specifying the default AppArmor profile.
  23. DefaultProfileAnnotationKey = "apparmor.security.beta.kubernetes.io/defaultProfileName"
  24. // The annotation key specifying the allowed AppArmor profiles.
  25. AllowedProfilesAnnotationKey = "apparmor.security.beta.kubernetes.io/allowedProfileNames"
  26. // The profile specifying the runtime default.
  27. ProfileRuntimeDefault = "runtime/default"
  28. // The prefix for specifying profiles loaded on the node.
  29. ProfileNamePrefix = "localhost/"
  30. // Unconfined profile
  31. ProfileNameUnconfined = "unconfined"
  32. )
  33. // Checks whether app armor is required for pod to be run.
  34. func isRequired(pod *v1.Pod) bool {
  35. for key, value := range pod.Annotations {
  36. if strings.HasPrefix(key, ContainerAnnotationKeyPrefix) {
  37. return value != ProfileNameUnconfined
  38. }
  39. }
  40. return false
  41. }
  42. // Returns the name of the profile to use with the container.
  43. func GetProfileName(pod *v1.Pod, containerName string) string {
  44. return GetProfileNameFromPodAnnotations(pod.Annotations, containerName)
  45. }
  46. // GetProfileNameFromPodAnnotations gets the name of the profile to use with container from
  47. // pod annotations
  48. func GetProfileNameFromPodAnnotations(annotations map[string]string, containerName string) string {
  49. return annotations[ContainerAnnotationKeyPrefix+containerName]
  50. }
  51. // Sets the name of the profile to use with the container.
  52. func SetProfileName(pod *v1.Pod, containerName, profileName string) error {
  53. if pod.Annotations == nil {
  54. pod.Annotations = map[string]string{}
  55. }
  56. pod.Annotations[ContainerAnnotationKeyPrefix+containerName] = profileName
  57. return nil
  58. }
  59. // Sets the name of the profile to use with the container.
  60. func SetProfileNameFromPodAnnotations(annotations map[string]string, containerName, profileName string) error {
  61. if annotations == nil {
  62. return nil
  63. }
  64. annotations[ContainerAnnotationKeyPrefix+containerName] = profileName
  65. return nil
  66. }