First draft of the SMO package based on the ONAP charts
[it/dep.git] / smo-install / oran_oom / aux-common / templates / _secret.yaml
1 {{/*
2 # Copyright © 2019 AT&T, Samsung Electronics
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 */}}
16
17 {{/*
18   For internal use only!
19
20   Generates a secret header with given name and desired labels.
21
22   The template takes two arguments:
23     - .global: environment (.)
24     - .name: name of the secret
25
26   Example call:
27     {{ include "common.secret._header" (dict "global" . "name" "myFancyName") }}
28 */}}
29 {{- define "common.secret._header" -}}
30 {{- $global := .global }}
31 {{- $name := .name }}
32 apiVersion: v1
33 kind: Secret
34 metadata:
35   name: {{ $name }}
36   namespace: {{ include "common.namespace" $global }}
37   labels:
38     app: {{ include "common.name" $global }}
39     chart: {{ $global.Chart.Name }}-{{ $global.Chart.Version | replace "+" "_" }}
40     release: {{ $global.Release.Name }}
41     heritage: {{ $global.Release.Service }}
42 type: Opaque
43 {{- end -}}
44
45 {{/*
46   For internal use only!
47
48   Pick a value based on "user input" and generation policy.
49
50   The template takes below arguments:
51     - .global: environment (.)
52     - .secretName: name of the secret where the value will be placed
53     - .secretEnv: map of values which configures this secret. This can contain below keys:
54         - value: Value of secret key provided by user (can be a template inside a string)
55         - policy: What to do if value is missing or empty. Possible options are:
56             - generate: Generate a new password deriving it from master password
57             - required: Fail the deployment if value has not been provided
58           Defaults to generate.
59         - name: Name of the key to which this value should be assigned
60 */}}
61 {{- define "common.secret._value" -}}
62   {{- $global := .global }}
63   {{- $name := .secretName }}
64   {{- $secretEnv := .secretEnv }}
65   {{- $value := tpl $secretEnv.value $global }}
66   {{- $policy := default "generate" $secretEnv.policy }}
67
68   {{- if $value }}
69     {{- $value | quote }}
70   {{- else if eq $policy "generate" }}
71     {{- include "common.createPassword" (dict "dot" $global "uid" $name) | quote }}
72   {{- else }}
73     {{- fail (printf "Value for %s secret %s key not provided" $name $secretEnv.name) }}
74   {{- end }}
75 {{- end -}}
76
77
78 {{/*
79   For internal use only!
80
81   Generate a secret name based on provided name or UID.
82   If UID is provided then the name is generated by appending this UID right after
83   the chart name. If name is provided, it overrides the name generation algorith
84   and is used right away. Both name and uid strings may contain a template to be
85   resolved.
86
87   The template takes below arguments:
88     - .global: environment (.)
89     - .uid: string that uniquely identifies this secret within a helm chart
90     - .name: string that can be used to override default name generation algorithm
91         and provide a custom name for the secret
92 */}}
93 {{- define "common.secret._genName" -}}
94   {{- $global := .global }}
95   {{- $uid := tpl (default "" .uid) $global }}
96   {{- $name := tpl (default "" .name) $global }}
97   {{- default (printf "%s-%s" (include "common.fullname" $global) $uid) $name }}
98 {{- end -}}
99
100 {{/*
101   Get the real secret name by UID or name, based on the configuration provided by user.
102   User may decide to not create a new secret but reuse existing one for this deployment
103   (aka externalSecret). In this case the real name of secret to be used is different
104   than the one declared in secret definition. This easily retrieve current secret real
105   name based on declared name or UID even if it has been overrided by the user using
106   externalSecret option. You should use this template always when you need to reference
107   a secret created using common.secret template by name.
108
109   The template takes below arguments:
110     - .global: environment (.)
111     - .uid: string that uniquely identifies this secret within a helm chart
112         (can be omitted if name has been provided)
113     - .name: name which was used to declare a secret
114         (can be omitted if uid has been provided)
115 */}}
116 {{- define "common.secret.getSecretName" -}}
117   {{- $global := .global }}
118   {{- $targetName := include "common.secret._genName" (dict "global" $global "uid" .uid "name" .name) }}
119   {{- range $secret := $global.Values.secrets }}
120     {{- $currName := include "common.secret._genName" (dict "global" $global "uid" $secret.uid "name" $secret.name) }}
121     {{- if eq $currName $targetName }}
122       {{- $externalSecret := tpl (default "" $secret.externalSecret) $global }}
123       {{- default $currName $externalSecret }}
124     {{- end }}
125   {{- end }}
126 {{- end -}}
127
128 {{/*
129   Convenience template which can be used to easily set the value of environment variable
130   to the value of a key in a secret.
131
132   It takes care of all name mangling, usage of external secrets etc.
133
134   The template takes below arguments:
135     - .global: environment (.)
136     - .uid: string that uniquely identifies this secret within a helm chart
137         (can be omitted if name has been provided)
138     - .name: name which was used to declare a secret
139         (can be omitted if uid has been provided)
140     - .key: Key within this secret which value should be assigned to this variable
141
142   Example usage:
143   env:
144     - name: SECRET_PASSWORD
145       {{- include "common.secret.envFromSecret" (dict "global" . "uid" "secret" "key" "password") | indent 8}}
146 */}}
147 {{- define "common.secret.envFromSecret" -}}
148   {{- $key := .key }}
149 valueFrom:
150   secretKeyRef:
151     name: {{ include "common.secret.getSecretName" . }}
152     key: {{ $key }}
153 {{- end -}}
154
155 {{/*
156   Define secrets to be used by chart.
157   Every secret has a type which is one of:
158     - generic:
159         Generic secret template that allows to input some raw data (from files).
160         File Input can be passed as list of files (filePaths) or as a single string
161         (filePath)
162     - genericKV:
163         Type of secret which allows you to define a list of key value pairs.
164         The list is assiged to envs value. Every item may define below items:
165           - name:
166               Identifier of this value within secret
167           - value:
168               String that defines a value associated with given key.
169               This can be a simple string or a template.
170           - policy:
171               Defines what to do if value is not provided by the user.
172               Available options are:
173                 - generate:
174                     Generate a value by derriving it from master password
175                 - required:
176                     Fail the deployment
177     - password:
178         Type of secret that holds only the password.
179         Only two items can be defined for this type:
180           - password:
181               Equivalent of value field from genericKV
182           - policy:
183               The same meaning as for genericKV policy field
184     - basicAuth:
185         Type of secret that holds both username and password.
186         Below fields are available:
187           - login:
188               The value for login key.
189               This can be a simple string or a template.
190               Providing a value for login is always required.
191           - password:
192               The value for password key.
193               This can be a simple string or a template.
194           - passwordPolicy:
195               The same meaning as the policy field in genericKV.
196               Only the policy for password can be set.
197
198   Every secret can be identified using:
199     - uid:
200         A string to be appended to the chart fullname to generate a secret name.
201     - name:
202         Overrides default secret name generation and allows to set immutable
203         and globaly unique name
204
205   To allow sharing a secret between the components and allow to pre-deploy secrets
206   before ONAP deployment it is possible to use already existing secret instead of
207   creating a new one. For this purpose externalSecret field can be used. If value of
208   this field is evaluated to true no new secret is created, only the name of the
209   secret is aliased to the external one.
210
211   Example usage:
212     secrets.yaml:
213       {{ include "common.secret" . }}
214
215     values.yaml:
216       mysqlLogin: "root"
217
218       mysqlExternalSecret: "some-other-secret-name"
219
220       secrets:
221         - uid: "mysql"
222           externalSecret: '{{ tpl .Values.passExternalSecret . }}'
223           type: basicAuth
224           login: '{{ .Values.mysqlLogin }}'
225           mysqlPassword: '{{ .Values.mysqlPassword }}'
226           passwordPolicy: generate
227
228     In the above example new secret is not going to be created.
229     Already existing one (some-other-secret-name) is going to be used.
230     To force creating a new one, just make sure that mysqlExternalSecret
231     is not set.
232
233 */}}
234 {{- define "common.secret" -}}
235   {{- $global := . }}
236   {{- range $secret := .Values.secrets }}
237     {{- $name := include "common.secret._genName" (dict "global" $global "uid" $secret.uid "name" $secret.name) }}
238     {{- $type := default "generic" $secret.type }}
239     {{- $externalSecret := tpl (default "" $secret.externalSecret) $global }}
240     {{- if not $externalSecret }}
241 ---
242       {{ include "common.secret._header" (dict "global" $global "name" $name) }}
243
244       {{- if eq $type "generic" }}
245 data:
246         {{- range $curFilePath := $secret.filePaths }}
247           {{ tpl ($global.Files.Glob $curFilePath).AsSecrets $global | indent 2 }}
248         {{- end }}
249         {{- if $secret.filePath }}
250           {{ tpl ($global.Files.Glob $secret.filePath).AsSecrets $global | indent 2 }}
251         {{- end }}
252       {{- else if eq $type "genericKV" }}
253 stringData:
254         {{- if $secret.envs }}
255           {{- range $secretEnv := $secret.envs }}
256             {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
257     {{ $secretEnv.name }}: {{ include "common.secret._value" $valueDesc }}
258           {{- end }}
259         {{- end }}
260       {{- else if eq $type "password" }}
261         {{- $secretEnv := (dict "policy" (default "generate" $secret.policy) "name" "password" "value" $secret.password) }}
262         {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
263 stringData:
264   password: {{ include "common.secret._value" $valueDesc }}
265       {{- else if eq $type "basicAuth" }}
266 stringData:
267         {{- $secretEnv := (dict "policy" "required" "name" "login" "value" $secret.login) }}
268         {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
269   login: {{ include "common.secret._value" $valueDesc }}
270         {{- $secretEnv := (dict "policy" (default "generate" $secret.passwordPolicy) "name" "password" "value" $secret.password) }}
271         {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
272   password: {{ include "common.secret._value" $valueDesc }}
273       {{- end }}
274     {{- end }}
275   {{- end }}
276 {{- end -}}