Merge "[AAI] Add model-loader tracing config"
[oom.git] / kubernetes / common / mongodb / common / templates / _secrets.tpl
1 {{/*
2 Copyright VMware, Inc.
3 SPDX-License-Identifier: APACHE-2.0
4 */}}
5
6 {{/* vim: set filetype=mustache: */}}
7 {{/*
8 Generate secret name.
9
10 Usage:
11 {{ include "common.secrets.name" (dict "existingSecret" .Values.path.to.the.existingSecret "defaultNameSuffix" "mySuffix" "context" $) }}
12
13 Params:
14   - existingSecret - ExistingSecret/String - Optional. The path to the existing secrets in the values.yaml given by the user
15     to be used instead of the default one. Allows for it to be of type String (just the secret name) for backwards compatibility.
16     +info: https://github.com/bitnami/charts/tree/main/bitnami/common#existingsecret
17   - defaultNameSuffix - String - Optional. It is used only if we have several secrets in the same deployment.
18   - context - Dict - Required. The context for the template evaluation.
19 */}}
20 {{- define "common.secrets.name" -}}
21 {{- $name := (include "common.names.fullname" .context) -}}
22
23 {{- if .defaultNameSuffix -}}
24 {{- $name = printf "%s-%s" $name .defaultNameSuffix | trunc 63 | trimSuffix "-" -}}
25 {{- end -}}
26
27 {{- with .existingSecret -}}
28 {{- if not (typeIs "string" .) -}}
29 {{- with .name -}}
30 {{- $name = . -}}
31 {{- end -}}
32 {{- else -}}
33 {{- $name = . -}}
34 {{- end -}}
35 {{- end -}}
36
37 {{- printf "%s" $name -}}
38 {{- end -}}
39
40 {{/*
41 Generate secret key.
42
43 Usage:
44 {{ include "common.secrets.key" (dict "existingSecret" .Values.path.to.the.existingSecret "key" "keyName") }}
45
46 Params:
47   - existingSecret - ExistingSecret/String - Optional. The path to the existing secrets in the values.yaml given by the user
48     to be used instead of the default one. Allows for it to be of type String (just the secret name) for backwards compatibility.
49     +info: https://github.com/bitnami/charts/tree/main/bitnami/common#existingsecret
50   - key - String - Required. Name of the key in the secret.
51 */}}
52 {{- define "common.secrets.key" -}}
53 {{- $key := .key -}}
54
55 {{- if .existingSecret -}}
56   {{- if not (typeIs "string" .existingSecret) -}}
57     {{- if .existingSecret.keyMapping -}}
58       {{- $key = index .existingSecret.keyMapping $.key -}}
59     {{- end -}}
60   {{- end }}
61 {{- end -}}
62
63 {{- printf "%s" $key -}}
64 {{- end -}}
65
66 {{/*
67 Generate secret password or retrieve one if already created.
68
69 Usage:
70 {{ 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" $) }}
71
72 Params:
73   - secret - String - Required - Name of the 'Secret' resource where the password is stored.
74   - key - String - Required - Name of the key in the secret.
75   - 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.
76   - length - int - Optional - Length of the generated random password.
77   - strong - Boolean - Optional - Whether to add symbols to the generated random password.
78   - chartName - String - Optional - Name of the chart used when said chart is deployed as a subchart.
79   - context - Context - Required - Parent context.
80   - failOnNew - Boolean - Optional - Default to true. If set to false, skip errors adding new keys to existing secrets.
81   - skipB64enc - Boolean - Optional - Default to false. If set to true, no the secret will not be base64 encrypted.
82   - skipQuote - Boolean - Optional - Default to false. If set to true, no quotes will be added around the secret.
83 The order in which this function returns a secret password:
84   1. Already existing 'Secret' resource
85      (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)
86   2. Password provided via the values.yaml
87      (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)
88   3. Randomly generated secret password
89      (A new random secret password with the length specified in the 'length' parameter will be generated and returned)
90
91 */}}
92 {{- define "common.secrets.passwords.manage" -}}
93
94 {{- $password := "" }}
95 {{- $subchart := "" }}
96 {{- $chartName := default "" .chartName }}
97 {{- $passwordLength := default 10 .length }}
98 {{- $providedPasswordKey := include "common.utils.getKeyFromList" (dict "keys" .providedValues "context" $.context) }}
99 {{- $providedPasswordValue := include "common.utils.getValueFromKey" (dict "key" $providedPasswordKey "context" $.context) }}
100 {{- $secretData := (lookup "v1" "Secret" (include "common.names.namespace" .context) .secret).data }}
101 {{- if $secretData }}
102   {{- if hasKey $secretData .key }}
103     {{- $password = index $secretData .key | b64dec }}
104   {{- else if not (eq .failOnNew false) }}
105     {{- printf "\nPASSWORDS ERROR: The secret \"%s\" does not contain the key \"%s\"\n" .secret .key | fail -}}
106   {{- else if $providedPasswordValue }}
107     {{- $password = $providedPasswordValue | toString }}
108   {{- end -}}
109 {{- else if $providedPasswordValue }}
110   {{- $password = $providedPasswordValue | toString }}
111 {{- else }}
112
113   {{- if .context.Values.enabled }}
114     {{- $subchart = $chartName }}
115   {{- end -}}
116
117   {{- $requiredPassword := dict "valueKey" $providedPasswordKey "secret" .secret "field" .key "subchart" $subchart "context" $.context -}}
118   {{- $requiredPasswordError := include "common.validations.values.single.empty" $requiredPassword -}}
119   {{- $passwordValidationErrors := list $requiredPasswordError -}}
120   {{- include "common.errors.upgrade.passwords.empty" (dict "validationErrors" $passwordValidationErrors "context" $.context) -}}
121
122   {{- if .strong }}
123     {{- $subStr := list (lower (randAlpha 1)) (randNumeric 1) (upper (randAlpha 1)) | join "_" }}
124     {{- $password = randAscii $passwordLength }}
125     {{- $password = regexReplaceAllLiteral "\\W" $password "@" | substr 5 $passwordLength }}
126     {{- $password = printf "%s%s" $subStr $password | toString | shuffle }}
127   {{- else }}
128     {{- $password = randAlphaNum $passwordLength }}
129   {{- end }}
130 {{- end -}}
131 {{- if not .skipB64enc }}
132 {{- $password = $password | b64enc }}
133 {{- end -}}
134 {{- if .skipQuote -}}
135 {{- printf "%s" $password -}}
136 {{- else -}}
137 {{- printf "%s" $password | quote -}}
138 {{- end -}}
139 {{- end -}}
140
141 {{/*
142 Reuses the value from an existing secret, otherwise sets its value to a default value.
143
144 Usage:
145 {{ include "common.secrets.lookup" (dict "secret" "secret-name" "key" "keyName" "defaultValue" .Values.myValue "context" $) }}
146
147 Params:
148   - secret - String - Required - Name of the 'Secret' resource where the password is stored.
149   - key - String - Required - Name of the key in the secret.
150   - defaultValue - String - Required - The path to the validating value in the values.yaml, e.g: "mysql.password". Will pick first parameter with a defined value.
151   - context - Context - Required - Parent context.
152
153 */}}
154 {{- define "common.secrets.lookup" -}}
155 {{- $value := "" -}}
156 {{- $secretData := (lookup "v1" "Secret" (include "common.names.namespace" .context) .secret).data -}}
157 {{- if and $secretData (hasKey $secretData .key) -}}
158   {{- $value = index $secretData .key -}}
159 {{- else if .defaultValue -}}
160   {{- $value = .defaultValue | toString | b64enc -}}
161 {{- end -}}
162 {{- if $value -}}
163 {{- printf "%s" $value -}}
164 {{- end -}}
165 {{- end -}}
166
167 {{/*
168 Returns whether a previous generated secret already exists
169
170 Usage:
171 {{ include "common.secrets.exists" (dict "secret" "secret-name" "context" $) }}
172
173 Params:
174   - secret - String - Required - Name of the 'Secret' resource where the password is stored.
175   - context - Context - Required - Parent context.
176 */}}
177 {{- define "common.secrets.exists" -}}
178 {{- $secret := (lookup "v1" "Secret" (include "common.names.namespace" .context) .secret) }}
179 {{- if $secret }}
180   {{- true -}}
181 {{- end -}}
182 {{- end -}}