summaryrefslogtreecommitdiffstats
path: root/kubernetes/common/certManagerCertificate/templates/_certificate.tpl
blob: 6fc667429ec93bdeed1bb58f3d5a650b16f3e666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{{/*#
# Copyright © 2020-2021, Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.*/}}

{{/*
# This is a template for requesting a certificate from the cert-manager (https://cert-manager.io).
#
# To request a certificate following steps are to be done:
#  - create an object 'certificates' in the values.yaml
#  - create a file templates/certificate.yaml and invoke the function "certManagerCertificate.certificate".
#
# Here is an example of the certificate request for a component:
#
# Directory structure:
#   component
#     templates
#       certifictes.yaml
#     values.yaml
#
# To be added in the file certificates.yamll
#
# To be added in the file values.yaml
#  1. Minimal version (certificates only in PEM format)
#  certificates:
#    - commonName: component.onap.org
#
#  2. Extended version (with defined own issuer and additional certificate format):
#  certificates:
#    - name:       onap-component-certificate
#      secretName: onap-component-certificate
#      commonName: component.onap.org
#      dnsNames:
#        - component.onap.org
#      issuer:
#        group: certmanager.onap.org
#        kind: CMPv2Issuer
#        name: cmpv2-issuer-for-the-component
#      keystore:
#        outputType:
#          - p12
#          - jks
#        passwordSecretRef:
#          name: secret-name
#          key:  secret-key
#          create: true
#
# Fields 'name', 'secretName' and 'commonName' are mandatory and required to be defined.
# Other mandatory fields for the certificate definition do not have to be defined directly,
# in that case they will be taken from default values.
#
# Default values are defined in file onap/values.yaml (see-> global.certificate.default)
# and can be overriden during onap installation process.
#
*/}}

{{- define "certManagerCertificate.certificate" -}}
{{- $dot := default . .dot -}}
{{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}

{{- $certificates := $dot.Values.certificates -}}
{{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global }}

{{ range $i, $certificate := $certificates }}
{{/*# General certifiacate attributes  #*/}}
{{- $name           := include "common.fullname" $dot                                                             -}}
{{- $certName       := default (printf "%s-cert-%d"   $name $i) $certificate.name                                 -}}
{{- $secretName     := default (printf "%s-secret-%d" $name $i) (tpl (default "" $certificate.secretName) $ )  -}}
{{- $commonName     := (required "'commonName' for Certificate is required." $certificate.commonName)          -}}
{{- $renewBefore    := default $subchartGlobal.certificate.default.renewBefore     $certificate.renewBefore    -}}
{{- $duration       := default $subchartGlobal.certificate.default.duration        $certificate.duration       -}}
{{- $namespace      := $dot.Release.Namespace      -}}
{{/*# SAN's #*/}}
{{- $dnsNames       := $certificate.dnsNames       -}}
{{- $ipAddresses    := $certificate.ipAddresses    -}}
{{- $uris           := $certificate.uris           -}}
{{- $emailAddresses := $certificate.emailAddresses -}}
{{/*# Subject #*/}}
{{- $subject        := $subchartGlobal.certificate.default.subject                                             -}}
{{- if $certificate.subject -}}
{{-   $subject       = $certificate.subject                                              -}}
{{- end -}}
{{/*# Issuer #*/}}
{{- $issuer         := $subchartGlobal.certificate.default.issuer                                              -}}
{{- if $certificate.issuer -}}
{{-   $issuer        = $certificate.issuer                                               -}}
{{- end -}}
{{/*# Secret #*/}}
{{ if $certificate.keystore -}}
  {{- $passwordSecretRef := $certificate.keystore.passwordSecretRef -}}
  {{- $password := include "common.createPassword" (dict "dot" $dot "uid" $certName) | quote -}}
  {{- if $passwordSecretRef.create }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $passwordSecretRef.name }}
  namespace: {{ $namespace }}
type: Opaque
stringData:
  {{ $passwordSecretRef.key }}: {{ $password }}
  {{- end }}
{{ end -}}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name:        {{ $certName }}
  namespace:   {{ $namespace }}
spec:
  secretName:  {{ $secretName }}
  commonName:  {{ $commonName }}
  renewBefore: {{ $renewBefore }}
  {{- if $duration }}
  duration:    {{ $duration }}
  {{- end }}
  {{- if $certificate.isCA }}
  isCA: {{ $certificate.isCA }}
  {{- end }}
  {{- if $certificate.usages }}
  usages:
    {{- range $usage := $certificate.usages }}
      - {{ $usage }}
    {{- end }}
  {{- end }}
  subject:
    organizations:
      - {{ $subject.organization }}
    countries:
      - {{ $subject.country }}
    localities:
      - {{ $subject.locality }}
    provinces:
      - {{ $subject.province }}
    organizationalUnits:
      - {{ $subject.organizationalUnit }}
  {{- if $dnsNames }}
  dnsNames:
    {{- range $dnsName := $dnsNames }}
      - {{ $dnsName }}
    {{- end }}
  {{- end }}
  {{- if $ipAddresses }}
  ipAddresses:
    {{- range $ipAddress := $ipAddresses }}
      - {{ $ipAddress }}
    {{- end }}
  {{- end }}
  {{- if $uris }}
  uris:
    {{- range $uri := $uris }}
      - {{ $uri }}
    {{- end }}
  {{- end }}
  {{- if $emailAddresses }}
  emailAddresses:
    {{- range $emailAddress := $emailAddresses }}
      - {{ $emailAddress }}
    {{- end }}
  {{- end }}
  issuerRef:
    {{- if not (eq $issuer.kind "Issuer" ) }}
    group: {{ $issuer.group }}
    {{- end }}
    kind:  {{ $issuer.kind }}
    name:  {{ $issuer.name }}
  {{- if $certificate.keystore }}
  keystores:
    {{- range $outputType := $certificate.keystore.outputType }}
      {{- if eq $outputType "p12" }}
        {{- $outputType = "pkcs12" }}
      {{- end }}
    {{ $outputType }}:
      create: true
      passwordSecretRef:
        name: {{ tpl (default "" $certificate.keystore.passwordSecretRef.name) $ }}
        key: {{ $certificate.keystore.passwordSecretRef.key }}
    {{- end }}
  {{- end }}
{{ end }}
{{- end -}}

{{/*Using templates below allows read and write access to volume mounted at $mountPath*/}}

{{- define "common.certManager.volumeMounts" -}}
{{- $dot := default . .dot -}}
{{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
{{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
  {{- range $i, $certificate := $dot.Values.certificates -}}
    {{- $mountPath := $certificate.mountPath -}}
- mountPath: {{ (printf "%s/secret-%d" $mountPath $i) }}
  name: certmanager-certs-volume-{{ $i }}
- mountPath: {{ $mountPath }}
  name: certmanager-certs-volume-{{ $i }}-dir
   {{- end -}}
{{- end -}}

{{- define "common.certManager.volumes" -}}
{{- $dot := default . .dot -}}
{{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
{{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
{{- $certificates := $dot.Values.certificates -}}
  {{- range $i, $certificate := $certificates -}}
    {{- $name := include "common.fullname" $dot -}}
    {{- $certificatesSecretName := default (printf "%s-secret-%d" $name $i) $certificate.secretName -}}
- name: certmanager-certs-volume-{{ $i }}-dir
  emptyDir: {}
- name: certmanager-certs-volume-{{ $i }}
  projected:
    sources:
    - secret:
        name: {{ $certificatesSecretName }}
    {{- if $certificate.keystore }}
        items:
        {{- range $outputType := $certificate.keystore.outputType }}
          - key: keystore.{{ $outputType }}
            path: keystore.{{ $outputType }}
          - key: truststore.{{ $outputType }}
            path: truststore.{{ $outputType }}
        {{- end }}
    - secret:
        name: {{ $certificate.keystore.passwordSecretRef.name }}
        items:
          - key: {{ $certificate.keystore.passwordSecretRef.key }}
            path: keystore.pass
          - key: {{ $certificate.keystore.passwordSecretRef.key }}
            path: truststore.pass
     {{- end }}
  {{- end -}}
{{- end -}}

{{- define "common.certManager.linkVolumeMounts" -}}
{{- $dot := default . .dot -}}
{{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
{{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
{{- $certificates := $dot.Values.certificates -}}
{{- $certsLinkCommand := "" -}}
  {{- range $i, $certificate := $certificates -}}
    {{- $destnationPath := (required "'mountPath' for Certificate is required." $certificate.mountPath) -}}
    {{- $sourcePath := (printf "%s/secret-%d/*" $destnationPath $i) -}}
    {{- $certsLinkCommand = (printf "ln -s %s %s; %s" $sourcePath $destnationPath $certsLinkCommand) -}}
  {{- end -}}
{{ $certsLinkCommand }}
{{- end -}}

{{/*Using templates below allows only read access to volume mounted at $mountPath*/}}

{{- define "common.certManager.volumeMountsReadOnly" -}}
{{- $dot := default . .dot -}}
{{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
{{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
  {{- range $i, $certificate := $dot.Values.certificates -}}
    {{- $mountPath := $certificate.mountPath -}}
- mountPath: {{ $mountPath }}
  name: certmanager-certs-volume-{{ $i }}
   {{- end -}}
{{- end -}}

{{- define "common.certManager.volumesReadOnly" -}}
{{- $dot := default . .dot -}}
{{- $initRoot := default $dot.Values.certManagerCertificate .initRoot -}}
{{- $subchartGlobal := mergeOverwrite (deepCopy $initRoot.global) $dot.Values.global -}}
{{- $certificates := $dot.Values.certificates -}}
  {{- range $i, $certificate := $certificates -}}
    {{- $name := include "common.fullname" $dot -}}
    {{- $certificatesSecretName := default (printf "%s-secret-%d" $name $i) $certificate.secretName -}}
- name: certmanager-certs-volume-{{ $i }}
  projected:
    sources:
    - secret:
        name: {{ $certificatesSecretName }}
    {{- if $certificate.keystore }}
        items:
        {{- range $outputType := $certificate.keystore.outputType }}
          - key: keystore.{{ $outputType }}
            path: keystore.{{ $outputType }}
          - key: truststore.{{ $outputType }}
            path: truststore.{{ $outputType }}
        {{- end }}
    - secret:
        name: {{ $certificate.keystore.passwordSecretRef.name }}
        items:
          - key: {{ $certificate.keystore.passwordSecretRef.key }}
            path: keystore.pass
          - key: {{ $certificate.keystore.passwordSecretRef.key }}
            path: truststore.pass
     {{- end }}
  {{- end -}}
{{- end -}}