_securityContext.tpl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {{/* Returns Container Security Context */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.container.securityContext" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The object data to be used to render the container.
  6. */}}
  7. {{- define "ix.v1.common.lib.container.securityContext" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{/* Initialize from the "global" options */}}
  11. {{- $secContext := fromJson (include "ix.v1.common.lib.container.securityContext.calculate" (dict "rootCtx" $rootCtx "objectData" $objectData)) }}
  12. runAsNonRoot: {{ $secContext.runAsNonRoot }}
  13. runAsUser: {{ $secContext.runAsUser }}
  14. runAsGroup: {{ $secContext.runAsGroup }}
  15. readOnlyRootFilesystem: {{ $secContext.readOnlyRootFilesystem }}
  16. allowPrivilegeEscalation: {{ $secContext.allowPrivilegeEscalation }}
  17. privileged: {{ $secContext.privileged }}
  18. seccompProfile:
  19. type: {{ $secContext.seccompProfile.type }}
  20. {{- if eq $secContext.seccompProfile.type "Localhost" }}
  21. localhostProfile: {{ $secContext.seccompProfile.profile }}
  22. {{- end }}
  23. capabilities:
  24. {{- if $secContext.capabilities.add }}
  25. add:
  26. {{- range $secContext.capabilities.add }}
  27. - {{ . }}
  28. {{- end -}}
  29. {{- else }}
  30. add: []
  31. {{- end -}}
  32. {{- if $secContext.capabilities.drop }}
  33. drop:
  34. {{- range $secContext.capabilities.drop }}
  35. - {{ . }}
  36. {{- end -}}
  37. {{- else }}
  38. drop: []
  39. {{- end -}}
  40. {{- end -}}
  41. {{/* Calculates Container Security Context */}}
  42. {{/* Call this template:
  43. {{ include "ix.v1.common.lib.container.securityContext.calculate" (dict "rootCtx" $ "objectData" $objectData) }}
  44. rootCtx: The root context of the chart.
  45. objectData: The object data to be used to render the container.
  46. */}}
  47. {{- define "ix.v1.common.lib.container.securityContext.calculate" -}}
  48. {{- $rootCtx := .rootCtx -}}
  49. {{- $objectData := .objectData -}}
  50. {{- if not $rootCtx.Values.securityContext.container -}}
  51. {{- fail "Container - Expected non-empty <.Values.securityContext.container>" -}}
  52. {{- end -}}
  53. {{/* Initialize from the "global" options */}}
  54. {{- $secContext := mustDeepCopy $rootCtx.Values.securityContext.container -}}
  55. {{/* Override with container's options */}}
  56. {{- with $objectData.securityContext -}}
  57. {{- $secContext = mustMergeOverwrite $secContext . -}}
  58. {{- end -}}
  59. {{/* Validations, as we might endup with null values after merge */}}
  60. {{- range $key := (list "privileged" "allowPrivilegeEscalation" "runAsNonRoot" "readOnlyRootFilesystem") -}}
  61. {{- $value := (get $secContext $key) -}}
  62. {{- if not (kindIs "bool" $value) -}}
  63. {{- fail (printf "Container - Expected <securityContext.%s> to be [bool], but got [%s] of type [%s]" $key $value (kindOf $value)) -}}
  64. {{- end -}}
  65. {{- end -}}
  66. {{- range $key := (list "runAsUser" "runAsGroup") -}}
  67. {{- $value := (get $secContext $key) -}}
  68. {{- if not (mustHas (kindOf $value) (list "float64" "int")) -}}
  69. {{- fail (printf "Container - Expected <securityContext.%s> to be [int], but got [%s] of type [%s]" $key $value (kindOf $value)) -}}
  70. {{- end -}}
  71. {{- end -}}
  72. {{- if not $secContext.seccompProfile -}}
  73. {{- fail "Container - Expected <securityContext.seccompProfile> to be defined" -}}
  74. {{- end -}}
  75. {{- $profiles := (list "RuntimeDefault" "Localhost" "Unconfined") -}}
  76. {{- if not (mustHas $secContext.seccompProfile.type $profiles) -}}
  77. {{- fail (printf "Container - Expected <securityContext.seccompProfile> to be one of [%s], but got [%s]" (join ", " $profiles) $secContext.seccompProfile.type) -}}
  78. {{- end -}}
  79. {{- if eq $secContext.seccompProfile.type "Localhost" -}}
  80. {{- if not $secContext.seccompProfile.profile -}}
  81. {{- fail "Container - Expected <securityContext.seccompProfile.profile> to be defined on type [Localhost]" -}}
  82. {{- end -}}
  83. {{- end -}}
  84. {{- if not $secContext.capabilities -}}
  85. {{- fail "Container - Expected <securityContext.capabilities> to be defined" -}}
  86. {{- end -}}
  87. {{- range $key := (list "add" "drop") -}}
  88. {{- $item := (get $secContext.capabilities $key) -}}
  89. {{- if not (kindIs "slice" $item) -}}
  90. {{- fail (printf "Container - Expected <securityContext.capabilities.%s> to be [list], but got [%s]" $key (kindOf $item)) -}}
  91. {{- end -}}
  92. {{- range $item -}}
  93. {{- if not (kindIs "string" .) -}}
  94. {{- fail (printf "Container - Expected items of <securityContext.capabilities.%s> to be [string], but got [%s]" $key (kindOf .)) -}}
  95. {{- end -}}
  96. {{- end -}}
  97. {{- end -}}
  98. {{- if or (eq (int $secContext.runAsUser) 0) (eq (int $secContext.runAsGroup) 0) -}}
  99. {{- if $secContext.runAsNonRoot -}}
  100. {{- fail "Container - Expected <securityContext.runAsNonRoot> to be [false] with either [runAsUser, runAsGroup] set to [0]" -}}
  101. {{- end -}}
  102. {{- end -}}
  103. {{- $secContext | toJson -}}
  104. {{- end -}}