_secrets.tpl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. {{/* vim: set filetype=mustache: */}}
  2. {{/*
  3. Generate secret name.
  4. Usage:
  5. {{ include "common.secrets.name" (dict "existingSecret" .Values.path.to.the.existingSecret "defaultNameSuffix" "mySuffix" "context" $) }}
  6. Params:
  7. - existingSecret - ExistingSecret/String - Optional. The path to the existing secrets in the values.yaml given by the user
  8. to be used instead of the default one. Allows for it to be of type String (just the secret name) for backwards compatibility.
  9. +info: https://github.com/bitnami/charts/tree/master/bitnami/common#existingsecret
  10. - defaultNameSuffix - String - Optional. It is used only if we have several secrets in the same deployment.
  11. - context - Dict - Required. The context for the template evaluation.
  12. */}}
  13. {{- define "common.secrets.name" -}}
  14. {{- $name := (include "common.names.fullname" .context) -}}
  15. {{- if .defaultNameSuffix -}}
  16. {{- $name = printf "%s-%s" $name .defaultNameSuffix | trunc 63 | trimSuffix "-" -}}
  17. {{- end -}}
  18. {{- with .existingSecret -}}
  19. {{- if not (typeIs "string" .) -}}
  20. {{- with .name -}}
  21. {{- $name = . -}}
  22. {{- end -}}
  23. {{- else -}}
  24. {{- $name = . -}}
  25. {{- end -}}
  26. {{- end -}}
  27. {{- printf "%s" $name -}}
  28. {{- end -}}
  29. {{/*
  30. Generate secret key.
  31. Usage:
  32. {{ include "common.secrets.key" (dict "existingSecret" .Values.path.to.the.existingSecret "key" "keyName") }}
  33. Params:
  34. - existingSecret - ExistingSecret/String - Optional. The path to the existing secrets in the values.yaml given by the user
  35. to be used instead of the default one. Allows for it to be of type String (just the secret name) for backwards compatibility.
  36. +info: https://github.com/bitnami/charts/tree/master/bitnami/common#existingsecret
  37. - key - String - Required. Name of the key in the secret.
  38. */}}
  39. {{- define "common.secrets.key" -}}
  40. {{- $key := .key -}}
  41. {{- if .existingSecret -}}
  42. {{- if not (typeIs "string" .existingSecret) -}}
  43. {{- if .existingSecret.keyMapping -}}
  44. {{- $key = index .existingSecret.keyMapping $.key -}}
  45. {{- end -}}
  46. {{- end }}
  47. {{- end -}}
  48. {{- printf "%s" $key -}}
  49. {{- end -}}
  50. {{/*
  51. Generate secret password or retrieve one if already created.
  52. Usage:
  53. {{ include "common.secrets.passwords.manage" (dict "secret" "secret-name" "key" "keyName" "providedValues" (list "path.to.password1" "path.to.password2") "length" 10 "strong" false "chartName" "chartName" "context" $) }}
  54. Params:
  55. - secret - String - Required - Name of the 'Secret' resource where the password is stored.
  56. - key - String - Required - Name of the key in the secret.
  57. - providedValues - List<String> - Required - The path to the validating value in the values.yaml, e.g: "mysql.password". Will pick first parameter with a defined value.
  58. - length - int - Optional - Length of the generated random password.
  59. - strong - Boolean - Optional - Whether to add symbols to the generated random password.
  60. - chartName - String - Optional - Name of the chart used when said chart is deployed as a subchart.
  61. - context - Context - Required - Parent context.
  62. The order in which this function returns a secret password:
  63. 1. Already existing 'Secret' resource
  64. (If a 'Secret' resource is found under the name provided to the 'secret' parameter to this function and that 'Secret' resource contains a key with the name passed as the 'key' parameter to this function then the value of this existing secret password will be returned)
  65. 2. Password provided via the values.yaml
  66. (If one of the keys passed to the 'providedValues' parameter to this function is a valid path to a key in the values.yaml and has a value, the value of the first key with a value will be returned)
  67. 3. Randomly generated secret password
  68. (A new random secret password with the length specified in the 'length' parameter will be generated and returned)
  69. */}}
  70. {{- define "common.secrets.passwords.manage" -}}
  71. {{- $password := "" }}
  72. {{- $subchart := "" }}
  73. {{- $chartName := default "" .chartName }}
  74. {{- $passwordLength := default 10 .length }}
  75. {{- $providedPasswordKey := include "common.utils.getKeyFromList" (dict "keys" .providedValues "context" $.context) }}
  76. {{- $providedPasswordValue := include "common.utils.getValueFromKey" (dict "key" $providedPasswordKey "context" $.context) }}
  77. {{- $secretData := (lookup "v1" "Secret" $.context.Release.Namespace .secret).data }}
  78. {{- if $secretData }}
  79. {{- if hasKey $secretData .key }}
  80. {{- $password = index $secretData .key }}
  81. {{- else }}
  82. {{- printf "\nPASSWORDS ERROR: The secret \"%s\" does not contain the key \"%s\"\n" .secret .key | fail -}}
  83. {{- end -}}
  84. {{- else if $providedPasswordValue }}
  85. {{- $password = $providedPasswordValue | toString | b64enc | quote }}
  86. {{- else }}
  87. {{- if .context.Values.enabled }}
  88. {{- $subchart = $chartName }}
  89. {{- end -}}
  90. {{- $requiredPassword := dict "valueKey" $providedPasswordKey "secret" .secret "field" .key "subchart" $subchart "context" $.context -}}
  91. {{- $requiredPasswordError := include "common.validations.values.single.empty" $requiredPassword -}}
  92. {{- $passwordValidationErrors := list $requiredPasswordError -}}
  93. {{- include "common.errors.upgrade.passwords.empty" (dict "validationErrors" $passwordValidationErrors "context" $.context) -}}
  94. {{- if .strong }}
  95. {{- $subStr := list (lower (randAlpha 1)) (randNumeric 1) (upper (randAlpha 1)) | join "_" }}
  96. {{- $password = randAscii $passwordLength }}
  97. {{- $password = regexReplaceAllLiteral "\\W" $password "@" | substr 5 $passwordLength }}
  98. {{- $password = printf "%s%s" $subStr $password | toString | shuffle | b64enc | quote }}
  99. {{- else }}
  100. {{- $password = randAlphaNum $passwordLength | b64enc | quote }}
  101. {{- end }}
  102. {{- end -}}
  103. {{- printf "%s" $password -}}
  104. {{- end -}}
  105. {{/*
  106. Returns whether a previous generated secret already exists
  107. Usage:
  108. {{ include "common.secrets.exists" (dict "secret" "secret-name" "context" $) }}
  109. Params:
  110. - secret - String - Required - Name of the 'Secret' resource where the password is stored.
  111. - context - Context - Required - Parent context.
  112. */}}
  113. {{- define "common.secrets.exists" -}}
  114. {{- $secret := (lookup "v1" "Secret" $.context.Release.Namespace .secret) }}
  115. {{- if $secret }}
  116. {{- true -}}
  117. {{- end -}}
  118. {{- end -}}