This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcert.go
286 lines (251 loc) · 9.92 KB
/
cert.go
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
/*
Copyright 2023.
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.
*/
package deployment
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"time"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
certmgrv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
dataplanev1 "github.com/openstack-k8s-operators/dataplane-operator/api/v1beta1"
infranetworkv1 "github.com/openstack-k8s-operators/infra-operator/apis/network/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/certmanager"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
)
// Generates an organized data structure that is leveraged to create the secrets.
func createSecretsDataStructure(secretMaxSize int,
certsData map[string][]byte,
) []map[string][]byte {
ci := []map[string][]byte{}
keys := []string{}
for k := range certsData {
keys = append(keys, k)
}
sort.Strings(keys)
totalSize := secretMaxSize
var cur *map[string][]byte
// Going 3 by 3 to include CA, crt and key, in the same secret.
for k := 0; k < len(keys)-1; k += 3 {
szCa := len(certsData[keys[k]]) + len(keys[k])
szCrt := len(certsData[keys[k+1]]) + len(keys[k+1])
szKey := len(certsData[keys[k+2]]) + len(keys[k+2])
sz := szCa + szCrt + szKey
if (totalSize + sz) > secretMaxSize {
i := len(ci)
ci = append(ci, make(map[string][]byte))
cur = &ci[i]
totalSize = 0
}
totalSize += sz
(*cur)[keys[k]] = certsData[keys[k]]
(*cur)[keys[k+1]] = certsData[keys[k+1]]
(*cur)[keys[k+2]] = certsData[keys[k+2]]
}
return ci
}
// EnsureTLSCerts generates secrets containing all the certificates for the relevant service
// These secrets will be mounted by the ansibleEE pod as an extra mount when the service is deployed.
func EnsureTLSCerts(ctx context.Context, helper *helper.Helper,
instance *dataplanev1.OpenStackDataPlaneNodeSet,
allHostnames map[string]map[infranetworkv1.NetNameStr]string,
allIPs map[string]map[infranetworkv1.NetNameStr]string,
service dataplanev1.OpenStackDataPlaneService,
certKey string,
) (*ctrl.Result, error) {
certsData := map[string][]byte{}
secretMaxSize := instance.Spec.SecretMaxSize
// for each node in the nodeset, issue all the TLS certs needed based on the
// ips or DNS Names
for nodeName, node := range instance.Spec.Nodes {
var dnsNames map[infranetworkv1.NetNameStr]string
var ipsMap map[infranetworkv1.NetNameStr]string
var hosts []string
var ips []string
var issuer *certmgrv1.Issuer
var issuerLabelSelector map[string]string
var certName string
var certSecret *corev1.Secret
var err error
var result ctrl.Result
// TODO(alee) decide if we want to use other labels
// For now we just add the hostname so we can select all the certs on one node
hostName := node.HostName
labels := map[string]string{
HostnameLabel: hostName,
ServiceLabel: service.Name,
ServiceKeyLabel: certKey,
NodeSetLabel: instance.Name,
}
certName = service.Name + "-" + certKey + "-" + hostName
dnsNames = allHostnames[hostName]
ipsMap = allIPs[hostName]
dnsNamesInCert := slices.Contains(service.Spec.TLSCerts[certKey].Contents, DNSNamesStr)
ipValuesInCert := slices.Contains(service.Spec.TLSCerts[certKey].Contents, IPValuesStr)
// Create the hosts and ips lists
if dnsNamesInCert {
if len(service.Spec.TLSCerts[certKey].Networks) == 0 {
hosts = make([]string, 0, len(dnsNames))
for _, host := range dnsNames {
hosts = append(hosts, host)
}
} else {
hosts = make([]string, 0, len(service.Spec.TLSCerts[certKey].Networks))
for _, network := range service.Spec.TLSCerts[certKey].Networks {
certNetwork := strings.ToLower(string(network))
hosts = append(hosts, dnsNames[infranetworkv1.NetNameStr(certNetwork)])
}
}
}
if ipValuesInCert {
if len(service.Spec.TLSCerts[certKey].Networks) == 0 {
ips = make([]string, 0, len(ipsMap))
for _, ip := range ipsMap {
ips = append(ips, ip)
}
} else {
ips = make([]string, 0, len(service.Spec.TLSCerts[certKey].Networks))
for _, network := range service.Spec.TLSCerts[certKey].Networks {
certNetwork := strings.ToLower(string(network))
ips = append(ips, ipsMap[infranetworkv1.NetNameStr(certNetwork)])
}
}
}
if service.Spec.TLSCerts[certKey].Issuer == "" {
// by default, use the internal root CA
issuerLabelSelector = map[string]string{certmanager.RootCAIssuerInternalLabel: ""}
} else {
issuerLabelSelector = map[string]string{service.Spec.TLSCerts[certKey].Issuer: ""}
}
issuer, err = certmanager.GetIssuerByLabels(ctx, helper, instance.Namespace, issuerLabelSelector)
if err != nil {
helper.GetLogger().Info("Error retrieving issuer by label", "issuerLabelSelector", issuerLabelSelector)
return &result, err
}
// NOTE: we are assuming that there will always be a ctlplane network
// that means if you are not using network isolation with multiple networks
// you should still need to have a ctlplane network at a minimum to use tls-e
baseName, ok := dnsNames[CtlPlaneNetwork]
if !ok {
return &result, fmt.Errorf(
"control plane network not found for node %s , tls-e requires a control plane network to be present",
nodeName)
}
certSecret, result, err = GetTLSNodeCert(ctx, helper, instance, certName,
issuer, labels, baseName, hosts, ips, service.Spec.TLSCerts[certKey].KeyUsages)
// handle cert request errors
if (err != nil) || (result != ctrl.Result{}) {
return &result, err
}
// TODO(alee) Add an owner reference to the secret so it can be monitored
// We'll do this once stuggi adds a function to do this in libcommon
// To use this cert, add it to the relevant service data
certsData[baseName+"-tls.key"] = certSecret.Data["tls.key"]
certsData[baseName+"-tls.crt"] = certSecret.Data["tls.crt"]
certsData[baseName+"-ca.crt"] = certSecret.Data["ca.crt"]
}
// Calculate number of secrets to create
ci := createSecretsDataStructure(secretMaxSize, certsData)
labels := map[string]string{
"numberOfSecrets": strconv.Itoa(len(ci)),
}
// create secrets to hold the certs for the services
for i := range ci {
labels["secretNumber"] = strconv.Itoa(i)
serviceCertsSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: GetServiceCertsSecretName(instance, service.Name, certKey, i),
Namespace: instance.Namespace,
Labels: labels,
},
Data: ci[i],
}
_, result, err := secret.CreateOrPatchSecret(ctx, helper, instance, serviceCertsSecret)
if err != nil {
err = fmt.Errorf("error creating certs secret for %s - %w", service.Name, err)
return &ctrl.Result{}, err
} else if result != controllerutil.OperationResultNone {
return &ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
}
return &ctrl.Result{}, nil
}
// GetTLSNodeCert creates or retrieves the cert for a node for a given service
func GetTLSNodeCert(ctx context.Context, helper *helper.Helper,
instance *dataplanev1.OpenStackDataPlaneNodeSet,
certName string, issuer *certmgrv1.Issuer,
labels map[string]string,
commonName string,
hostnames []string, ips []string, usages []certmgrv1.KeyUsage,
) (*corev1.Secret, ctrl.Result, error) {
// use cert duration and renewBefore from annotations set on issuer
// - if no duration annotation is set, use the default from certmanager lib-common module,
// - if no renewBefore annotation is set, the cert-manager default is used.
durationString := certmanager.CertDefaultDuration
if d, ok := issuer.Annotations[certmanager.CertDurationAnnotation]; ok && d != "" {
durationString = d
}
duration, err := time.ParseDuration(durationString)
if err != nil {
err = fmt.Errorf("error parsing duration annotation %s - %w", certmanager.CertDurationAnnotation, err)
return nil, ctrl.Result{}, err
}
var renewBefore *time.Duration
if r, ok := issuer.Annotations[certmanager.CertRenewBeforeAnnotation]; ok && r != "" {
rb, err := time.ParseDuration(r)
if err != nil {
err = fmt.Errorf("error parsing renewBefore annotation %s - %w", certmanager.CertRenewBeforeAnnotation, err)
return nil, ctrl.Result{}, err
}
renewBefore = &rb
}
request := certmanager.CertificateRequest{
CommonName: &commonName,
IssuerName: issuer.Name,
CertName: certName,
Duration: &duration,
RenewBefore: renewBefore,
Hostnames: hostnames,
Ips: ips,
Annotations: nil,
Labels: labels,
Usages: usages,
Subject: &certmgrv1.X509Subject{
// NOTE(owalsh): For libvirt/QEMU this should match issuer CN
Organizations: []string{issuer.Name},
},
}
certSecret, result, err := certmanager.EnsureCert(ctx, helper, request, instance)
if err != nil {
return nil, ctrl.Result{}, err
} else if (result != ctrl.Result{}) {
return nil, result, nil
}
return certSecret, ctrl.Result{}, nil
}
// GetServiceCertsSecretName - return name of secret to be mounted in ansibleEE which contains
// all the TLS certs that fit in a secret for the relevant service. The index variable is used
// to make the secret name unique.
// The convention we use here is "<nodeset.name>-<service>-<certkey>-certs-<index>", for example,
// openstack-epdm-nova-default-certs-0.
func GetServiceCertsSecretName(instance *dataplanev1.OpenStackDataPlaneNodeSet, serviceName string,
certKey string, index int) string {
return fmt.Sprintf("%s-%s-%s-certs-%s", instance.Name, serviceName, certKey, strconv.Itoa(index))
}