Migrate to HTTPS from HTTP - VNFSDK
[oom.git] / kubernetes / common / 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: {{ include "common.release" $global }}
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   Generate a secret name based on provided name or UID.
80   If UID is provided then the name is generated by appending this UID right after
81   the chart name. If name is provided, it overrides the name generation algorith
82   and is used right away. Both name and uid strings may contain a template to be
83   resolved.
84
85   The template takes below arguments:
86     - .global: environment (.)
87     - .uid: string that uniquely identifies this secret within a helm chart
88     - .name: string that can be used to override default name generation algorithm
89         and provide a custom name for the secret
90 */}}
91 {{- define "common.secret.genName" -}}
92   {{- $global := .global }}
93   {{- $uid := tpl (default "" .uid) $global }}
94   {{- $name := tpl (default "" .name) $global }}
95   {{- $fullname := ne (default "" .chartName) "" | ternary (include "common.fullnameExplicit" (dict "dot" $global "chartName" .chartName)) (include "common.fullname" $global) }}
96   {{- default (printf "%s-%s" $fullname $uid) $name }}
97 {{- end -}}
98
99 {{/*
100   Get the real secret name by UID or name, based on the configuration provided by user.
101   User may decide to not create a new secret but reuse existing one for this deployment
102   (aka externalSecret). In this case the real name of secret to be used is different
103   than the one declared in secret definition. This easily retrieve current secret real
104   name based on declared name or UID even if it has been overrided by the user using
105   externalSecret option. You should use this template always when you need to reference
106   a secret created using common.secret template by name.
107
108   The template takes below arguments:
109     - .global: environment (.)
110     - .uid: string that uniquely identifies this secret within a helm chart
111         (can be omitted if name has been provided)
112     - .name: name which was used to declare a secret
113         (can be omitted if uid has been provided)
114 */}}
115 {{- define "common.secret.getSecretName" -}}
116   {{- $global := .global }}
117   {{- $name := tpl (default "" .name) $global }}
118   {{- $uid := tpl (default "" .uid) $global }}
119   {{- $targetName := default (include "common.secret.genName" (dict "global" $global "uid" $uid "name" .name)) $name}}
120   {{- range $secret := $global.Values.secrets }}
121     {{- $givenName := tpl (default "" $secret.name) $global }}
122     {{- $currUID := tpl (default "" $secret.uid) $global }}
123     {{- $currName := default (include "common.secret.genName" (dict "global" $global "uid" $currUID "name" $secret.name)) $givenName }}
124     {{- if or (eq $uid $currUID) (eq $currName $targetName) }}
125       {{- $externalSecret := tpl (default "" $secret.externalSecret) $global }}
126       {{- default $currName $externalSecret }}
127     {{- end }}
128   {{- end }}
129 {{- end -}}
130
131 {{/*
132   Convenience template which can be used to easily set the value of environment variable
133   to the value of a key in a secret.
134
135   It takes care of all name mangling, usage of external secrets etc.
136
137   The template takes below arguments:
138     - .global: environment (.)
139     - .uid: string that uniquely identifies this secret within a helm chart
140         (can be omitted if name has been provided)
141     - .name: name which was used to declare a secret
142         (can be omitted if uid has been provided)
143     - .key: Key within this secret which value should be assigned to this variable
144
145   Example usage:
146   env:
147     - name: SECRET_PASSWORD
148       {{- include "common.secret.envFromSecret" (dict "global" . "uid" "secret" "key" "password") | indent 8}}
149 */}}
150 {{- define "common.secret.envFromSecret" -}}
151   {{- $key := .key }}
152 valueFrom:
153   secretKeyRef:
154     name: {{ include "common.secret.getSecretName" . }}
155     key: {{ $key }}
156 {{- end -}}
157
158 {{/*
159   Define secrets to be used by chart.
160   Every secret has a type which is one of:
161     - generic:
162         Generic secret template that allows to input some raw data (from files).
163         File Input can be passed as list of files (filePaths) or as a single string
164         (filePath)
165     - genericKV:
166         Type of secret which allows you to define a list of key value pairs.
167         The list is assiged to envs value. Every item may define below items:
168           - name:
169               Identifier of this value within secret
170           - value:
171               String that defines a value associated with given key.
172               This can be a simple string or a template.
173           - policy:
174               Defines what to do if value is not provided by the user.
175               Available options are:
176                 - generate:
177                     Generate a value by derriving it from master password
178                 - required:
179                     Fail the deployment
180     - password:
181         Type of secret that holds only the password.
182         Only two items can be defined for this type:
183           - password:
184               Equivalent of value field from genericKV
185           - policy:
186               The same meaning as for genericKV policy field
187     - basicAuth:
188         Type of secret that holds both username and password.
189         Below fields are available:
190           - login:
191               The value for login key.
192               This can be a simple string or a template.
193               Providing a value for login is always required.
194           - password:
195               The value for password key.
196               This can be a simple string or a template.
197           - passwordPolicy:
198               The same meaning as the policy field in genericKV.
199               Only the policy for password can be set.
200
201   Every secret can be identified using:
202     - uid:
203         A string to be appended to the chart fullname to generate a secret name.
204     - name:
205         Overrides default secret name generation and allows to set immutable
206         and globaly unique name
207
208   To allow sharing a secret between the components and allow to pre-deploy secrets
209   before ONAP deployment it is possible to use already existing secret instead of
210   creating a new one. For this purpose externalSecret field can be used. If value of
211   this field is evaluated to true no new secret is created, only the name of the
212   secret is aliased to the external one.
213
214   Example usage:
215     secrets.yaml:
216       {{ include "common.secret" . }}
217
218     values.yaml:
219       mysqlLogin: "root"
220
221       mysqlExternalSecret: "some-other-secret-name"
222
223       secrets:
224         - uid: "mysql"
225           externalSecret: '{{ tpl .Values.passExternalSecret . }}'
226           type: basicAuth
227           login: '{{ .Values.mysqlLogin }}'
228           mysqlPassword: '{{ .Values.mysqlPassword }}'
229           passwordPolicy: generate
230
231     In the above example new secret is not going to be created.
232     Already existing one (some-other-secret-name) is going to be used.
233     To force creating a new one, just make sure that mysqlExternalSecret
234     is not set.
235
236 */}}
237 {{- define "common.secret" -}}
238   {{- $global := . }}
239   {{- range $secret := .Values.secrets }}
240     {{- $uid := tpl (default "" $secret.uid) $global }}
241     {{- $name := include "common.secret.genName" (dict "global" $global "uid" $uid "name" $secret.name) }}
242     {{- $type := default "generic" $secret.type }}
243     {{- $externalSecret := tpl (default "" $secret.externalSecret) $global }}
244     {{- if not $externalSecret }}
245 ---
246       {{ include "common.secret._header" (dict "global" $global "name" $name) }}
247
248       {{- if eq $type "generic" }}
249 data:
250         {{- range $curFilePath := $secret.filePaths }}
251           {{ tpl ($global.Files.Glob $curFilePath).AsSecrets $global | indent 2 }}
252         {{- end }}
253         {{- if $secret.filePath }}
254           {{ tpl ($global.Files.Glob $secret.filePath).AsSecrets $global | indent 2 }}
255         {{- end }}
256       {{- else if eq $type "genericKV" }}
257 stringData:
258         {{- if $secret.envs }}
259           {{- range $secretEnv := $secret.envs }}
260             {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
261     {{ $secretEnv.name }}: {{ include "common.secret._value" $valueDesc }}
262           {{- end }}
263         {{- end }}
264       {{- else if eq $type "password" }}
265         {{- $secretEnv := (dict "policy" (default "generate" $secret.policy) "name" "password" "value" $secret.password) }}
266         {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
267 stringData:
268   password: {{ include "common.secret._value" $valueDesc }}
269       {{- else if eq $type "basicAuth" }}
270 stringData:
271         {{- $secretEnv := (dict "policy" "required" "name" "login" "value" $secret.login) }}
272         {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
273   login: {{ include "common.secret._value" $valueDesc }}
274         {{- $secretEnv := (dict "policy" (default "generate" $secret.passwordPolicy) "name" "password" "value" $secret.password) }}
275         {{- $valueDesc := (dict "global" $global "secretName" $name "secretEnv" $secretEnv) }}
276   password: {{ include "common.secret._value" $valueDesc }}
277       {{- end }}
278     {{- end }}
279   {{- end }}
280 {{- end -}}