Merge "[COMMON] Add custom certs into AAF truststore"
[oom.git] / kubernetes / common / certManagerCertificate / templates / _certificate.tpl
1 {{/*#
2 # Copyright © 2020-2021, Nokia
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 # This is a template for requesting a certificate from the cert-manager (https://cert-manager.io).
18 #
19 # To request a certificate following steps are to be done:
20 #  - create an object 'certificates' in the values.yaml
21 #  - create a file templates/certificate.yaml and invoke the function "certManagerCertificate.certificate".
22 #
23 # Here is an example of the certificate request for a component:
24 #
25 # Directory structure:
26 #   component
27 #     templates
28 #       certifictes.yaml
29 #     values.yaml
30 #
31 # To be added in the file certificates.yamll
32 #
33 # To be added in the file values.yaml
34 #  1. Minimal version (certificates only in PEM format)
35 #  certificates:
36 #    - commonName: component.onap.org
37 #
38 #  2. Extended version (with defined own issuer and additional certificate format):
39 #  certificates:
40 #    - name:       onap-component-certificate
41 #      secretName: onap-component-certificate
42 #      commonName: component.onap.org
43 #      dnsNames:
44 #        - component.onap.org
45 #      issuer:
46 #        group: certmanager.onap.org
47 #        kind: CMPv2Issuer
48 #        name: cmpv2-issuer-for-the-component
49 #      keystore:
50 #        outputType:
51 #          - p12
52 #          - jks
53 #        passwordSecretRef:
54 #          name: secret-name
55 #          key:  secret-key
56 #          create: true
57 #
58 # Fields 'name', 'secretName' and 'commonName' are mandatory and required to be defined.
59 # Other mandatory fields for the certificate definition do not have to be defined directly,
60 # in that case they will be taken from default values.
61 #
62 # Default values are defined in file onap/values.yaml (see-> global.certificate.default)
63 # and can be overriden during onap installation process.
64 #
65 */}}
66
67 {{- define "certManagerCertificate.certificate" -}}
68 {{- $dot := default . .dot -}}
69 {{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
70
71 {{- $certificates := $dot.Values.certificates -}}
72 {{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global }}
73
74 {{ range $i, $certificate := $certificates }}
75 {{/*# General certifiacate attributes  #*/}}
76 {{- $name           := include "common.fullname" $dot                                                             -}}
77 {{- $certName       := default (printf "%s-cert-%d"   $name $i) $certificate.name                                 -}}
78 {{- $secretName     := default (printf "%s-secret-%d" $name $i) (tpl (default "" $certificate.secretName) $ )  -}}
79 {{- $commonName     := (required "'commonName' for Certificate is required." $certificate.commonName)          -}}
80 {{- $renewBefore    := default $subchartGlobal.certificate.default.renewBefore     $certificate.renewBefore    -}}
81 {{- $duration       := default $subchartGlobal.certificate.default.duration        $certificate.duration       -}}
82 {{- $namespace      := $dot.Release.Namespace      -}}
83 {{/*# SAN's #*/}}
84 {{- $dnsNames       := $certificate.dnsNames       -}}
85 {{- $ipAddresses    := $certificate.ipAddresses    -}}
86 {{- $uris           := $certificate.uris           -}}
87 {{- $emailAddresses := $certificate.emailAddresses -}}
88 {{/*# Subject #*/}}
89 {{- $subject        := $subchartGlobal.certificate.default.subject                                             -}}
90 {{- if $certificate.subject -}}
91 {{-   $subject       = $certificate.subject                                              -}}
92 {{- end -}}
93 {{/*# Issuer #*/}}
94 {{- $issuer         := $subchartGlobal.certificate.default.issuer                                              -}}
95 {{- if $certificate.issuer -}}
96 {{-   $issuer        = $certificate.issuer                                               -}}
97 {{- end -}}
98 {{/*# Secret #*/}}
99 {{ if $certificate.keystore -}}
100   {{- $passwordSecretRef := $certificate.keystore.passwordSecretRef -}}
101   {{- $password := include "common.createPassword" (dict "dot" $dot "uid" $certName) | quote -}}
102   {{- if $passwordSecretRef.create }}
103 apiVersion: v1
104 kind: Secret
105 metadata:
106   name: {{ $passwordSecretRef.name }}
107   namespace: {{ $namespace }}
108 type: Opaque
109 stringData:
110   {{ $passwordSecretRef.key }}: {{ $password }}
111   {{- end }}
112 {{ end -}}
113 ---
114 apiVersion: cert-manager.io/v1
115 kind: Certificate
116 metadata:
117   name:        {{ $certName }}
118   namespace:   {{ $namespace }}
119 spec:
120   secretName:  {{ $secretName }}
121   commonName:  {{ $commonName }}
122   renewBefore: {{ $renewBefore }}
123   {{- if $duration }}
124   duration:    {{ $duration }}
125   {{- end }}
126   {{- if $certificate.isCA }}
127   isCA: {{ $certificate.isCA }}
128   {{- end }}
129   {{- if $certificate.usages }}
130   usages:
131     {{- range $usage := $certificate.usages }}
132       - {{ $usage }}
133     {{- end }}
134   {{- end }}
135   subject:
136     organizations:
137       - {{ $subject.organization }}
138     countries:
139       - {{ $subject.country }}
140     localities:
141       - {{ $subject.locality }}
142     provinces:
143       - {{ $subject.province }}
144     organizationalUnits:
145       - {{ $subject.organizationalUnit }}
146   {{- if $dnsNames }}
147   dnsNames:
148     {{- range $dnsName := $dnsNames }}
149       - {{ $dnsName }}
150     {{- end }}
151   {{- end }}
152   {{- if $ipAddresses }}
153   ipAddresses:
154     {{- range $ipAddress := $ipAddresses }}
155       - {{ $ipAddress }}
156     {{- end }}
157   {{- end }}
158   {{- if $uris }}
159   uris:
160     {{- range $uri := $uris }}
161       - {{ $uri }}
162     {{- end }}
163   {{- end }}
164   {{- if $emailAddresses }}
165   emailAddresses:
166     {{- range $emailAddress := $emailAddresses }}
167       - {{ $emailAddress }}
168     {{- end }}
169   {{- end }}
170   issuerRef:
171     {{- if not (eq $issuer.kind "Issuer" ) }}
172     group: {{ $issuer.group }}
173     {{- end }}
174     kind:  {{ $issuer.kind }}
175     name:  {{ $issuer.name }}
176   {{- if $certificate.keystore }}
177   keystores:
178     {{- range $outputType := $certificate.keystore.outputType }}
179       {{- if eq $outputType "p12" }}
180         {{- $outputType = "pkcs12" }}
181       {{- end }}
182     {{ $outputType }}:
183       create: true
184       passwordSecretRef:
185         name: {{ tpl (default "" $certificate.keystore.passwordSecretRef.name) $ }}
186         key: {{ $certificate.keystore.passwordSecretRef.key }}
187     {{- end }}
188   {{- end }}
189 {{ end }}
190 {{- end -}}
191
192 {{/*Using templates below allows read and write access to volume mounted at $mountPath*/}}
193
194 {{- define "common.certManager.volumeMounts" -}}
195 {{- $dot := default . .dot -}}
196 {{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
197 {{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
198   {{- range $i, $certificate := $dot.Values.certificates -}}
199     {{- $mountPath := $certificate.mountPath -}}
200 - mountPath: {{ (printf "%s/secret-%d" $mountPath $i) }}
201   name: certmanager-certs-volume-{{ $i }}
202 - mountPath: {{ $mountPath }}
203   name: certmanager-certs-volume-{{ $i }}-dir
204    {{- end -}}
205 {{- end -}}
206
207 {{- define "common.certManager.volumes" -}}
208 {{- $dot := default . .dot -}}
209 {{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
210 {{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
211 {{- $certificates := $dot.Values.certificates -}}
212   {{- range $i, $certificate := $certificates -}}
213     {{- $name := include "common.fullname" $dot -}}
214     {{- $certificatesSecretName := default (printf "%s-secret-%d" $name $i) $certificate.secretName -}}
215 - name: certmanager-certs-volume-{{ $i }}-dir
216   emptyDir: {}
217 - name: certmanager-certs-volume-{{ $i }}
218   projected:
219     sources:
220     - secret:
221         name: {{ $certificatesSecretName }}
222     {{- if $certificate.keystore }}
223         items:
224         {{- range $outputType := $certificate.keystore.outputType }}
225           - key: keystore.{{ $outputType }}
226             path: keystore.{{ $outputType }}
227           - key: truststore.{{ $outputType }}
228             path: truststore.{{ $outputType }}
229         {{- end }}
230     - secret:
231         name: {{ $certificate.keystore.passwordSecretRef.name }}
232         items:
233           - key: {{ $certificate.keystore.passwordSecretRef.key }}
234             path: keystore.pass
235           - key: {{ $certificate.keystore.passwordSecretRef.key }}
236             path: truststore.pass
237      {{- end }}
238   {{- end -}}
239 {{- end -}}
240
241 {{- define "common.certManager.linkVolumeMounts" -}}
242 {{- $dot := default . .dot -}}
243 {{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
244 {{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
245 {{- $certificates := $dot.Values.certificates -}}
246 {{- $certsLinkCommand := "" -}}
247   {{- range $i, $certificate := $certificates -}}
248     {{- $destnationPath := (required "'mountPath' for Certificate is required." $certificate.mountPath) -}}
249     {{- $sourcePath := (printf "%s/secret-%d/*" $destnationPath $i) -}}
250     {{- $certsLinkCommand = (printf "ln -s %s %s; %s" $sourcePath $destnationPath $certsLinkCommand) -}}
251   {{- end -}}
252 {{ $certsLinkCommand }}
253 {{- end -}}
254
255 {{/*Using templates below allows only read access to volume mounted at $mountPath*/}}
256
257 {{- define "common.certManager.volumeMountsReadOnly" -}}
258 {{- $dot := default . .dot -}}
259 {{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
260 {{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
261   {{- range $i, $certificate := $dot.Values.certificates -}}
262     {{- $mountPath := $certificate.mountPath -}}
263 - mountPath: {{ $mountPath }}
264   name: certmanager-certs-volume-{{ $i }}
265    {{- end -}}
266 {{- end -}}
267
268 {{- define "common.certManager.volumesReadOnly" -}}
269 {{- $dot := default . .dot -}}
270 {{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
271 {{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
272 {{- $certificates := $dot.Values.certificates -}}
273   {{- range $i, $certificate := $certificates -}}
274     {{- $name := include "common.fullname" $dot -}}
275     {{- $certificatesSecretName := default (printf "%s-secret-%d" $name $i) $certificate.secretName -}}
276 - name: certmanager-certs-volume-{{ $i }}
277   projected:
278     sources:
279     - secret:
280         name: {{ $certificatesSecretName }}
281     {{- if $certificate.keystore }}
282         items:
283         {{- range $outputType := $certificate.keystore.outputType }}
284           - key: keystore.{{ $outputType }}
285             path: keystore.{{ $outputType }}
286           - key: truststore.{{ $outputType }}
287             path: truststore.{{ $outputType }}
288         {{- end }}
289     - secret:
290         name: {{ $certificate.keystore.passwordSecretRef.name }}
291         items:
292           - key: {{ $certificate.keystore.passwordSecretRef.key }}
293             path: keystore.pass
294           - key: {{ $certificate.keystore.passwordSecretRef.key }}
295             path: truststore.pass
296      {{- end }}
297   {{- end -}}
298 {{- end -}}