-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_kongcertificate.go
More file actions
315 lines (275 loc) · 9.35 KB
/
ops_kongcertificate.go
File metadata and controls
315 lines (275 loc) · 9.35 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package ops
import (
"context"
"fmt"
"strings"
sdkkonnectgo "github.com/Kong/sdk-konnect-go"
sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
commonv1alpha1 "github.com/kong/kong-operator/v2/api/common/v1alpha1"
configurationv1alpha1 "github.com/kong/kong-operator/v2/api/configuration/v1alpha1"
)
// createCertificate creates a KongCertificate in Konnect.
// It sets the KonnectID in the KongCertificate status.
func createCertificate(
ctx context.Context,
cl client.Client,
sdk sdkkonnectgo.CertificatesSDK,
cert *configurationv1alpha1.KongCertificate,
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: CreateOp}
}
// NOTE: This is a workaround for the fact that the Konnect SDK does not
// return a conflict error when creating a Certificate as there are no criteria
// that would prevent the creation of a Certificate with the same spec fields.
// This can be expanded to other entities by changing the order of get -> list
// to list -> get in ops.go's Create() function.
// Check if the Certificate already exists in Konnect: it is possible that
// the Certificate was already created and cache the controller is using
// is outdated, so we can't rely on the status.konnect.id.
// If it does, set the KonnectID in the KongCertificate status and return.
respList, err := sdk.ListCertificate(ctx, sdkkonnectops.ListCertificateRequest{
ControlPlaneID: cpID,
Tags: new(strings.Join(GenerateTagsForObject(cert, cert.Spec.Tags...), ",")),
})
if err != nil {
return fmt.Errorf("failed to list: %w", err)
}
if respList.Object != nil && len(respList.Object.Data) > 0 {
certList := respList.Object.Data[0]
if certList.ID == nil {
return fmt.Errorf("failed listing: found a cert without ID")
}
cert.SetKonnectID(*certList.ID)
return nil
}
// Generate the input for the creation.
// This may fetch the Secret if the Certificate source type is SecretRef.
input, err := kongCertificateToCertificateInput(ctx, cl, cert)
if err != nil {
return err
}
resp, err := sdk.CreateCertificate(ctx,
cpID,
input,
)
// TODO: handle already exists
// Can't adopt it as it will cause conflicts between the controller
// that created that entity and already manages it, hm
if errWrap := wrapErrIfKonnectOpFailed(err, CreateOp, cert); errWrap != nil {
return errWrap
}
if resp == nil || resp.Certificate == nil || resp.Certificate.ID == nil || *resp.Certificate.ID == "" {
return fmt.Errorf("failed creating %s: %w", cert.GetTypeName(), ErrNilResponse)
}
// At this point, the Certificate has been created successfully.
cert.SetKonnectID(*resp.Certificate.ID)
return nil
}
// updateCertificate updates a KongCertificate in Konnect.
// The KongCertificate must have a KonnectID set in its status.
// It returns an error if the KongCertificate does not have a KonnectID.
func updateCertificate(
ctx context.Context,
cl client.Client,
sdk sdkkonnectgo.CertificatesSDK,
cert *configurationv1alpha1.KongCertificate,
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: UpdateOp}
}
// Generate the input for the update.
// This may fetch the Secret if the Certificate source type is SecretRef.
input, err := kongCertificateToCertificateInput(ctx, cl, cert)
if err != nil {
return err
}
_, err = sdk.UpsertCertificate(ctx,
sdkkonnectops.UpsertCertificateRequest{
ControlPlaneID: cpID,
CertificateID: cert.GetKonnectStatus().GetKonnectID(),
Certificate: input,
},
)
if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, cert); errWrap != nil {
return errWrap
}
return nil
}
// deleteCertificate deletes a KongCertificate in Konnect.
// The KongCertificate must have a KonnectID set in its status.
// It returns an error if the operation fails.
func deleteCertificate(
ctx context.Context,
sdk sdkkonnectgo.CertificatesSDK,
cert *configurationv1alpha1.KongCertificate,
) error {
id := cert.Status.Konnect.GetKonnectID()
_, err := sdk.DeleteCertificate(ctx, cert.GetControlPlaneID(), id)
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cert); errWrap != nil {
return handleDeleteError(ctx, err, cert)
}
return nil
}
func adoptCertificate(
ctx context.Context,
cl client.Client,
sdk sdkkonnectgo.CertificatesSDK,
cert *configurationv1alpha1.KongCertificate,
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return KonnectEntityAdoptionMissingControlPlaneIDError{}
}
adoptOptions := cert.Spec.Adopt
konnectID := adoptOptions.Konnect.ID
resp, err := sdk.GetCertificate(ctx, konnectID, cpID)
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
if resp == nil || resp.Certificate == nil {
return fmt.Errorf("failed to adopt %s: %w", cert.GetTypeName(), ErrNilResponse)
}
uidTag, hasUIDTag := findUIDTag(resp.Certificate.Tags)
if hasUIDTag && extractUIDFromTag(uidTag) != string(cert.UID) {
return KonnectEntityAdoptionUIDTagConflictError{
KonnectID: konnectID,
ActualUIDTag: extractUIDFromTag(uidTag),
}
}
adoptMode := adoptOptions.Mode
if adoptMode == "" {
adoptMode = commonv1alpha1.AdoptModeOverride
}
switch adoptMode {
case commonv1alpha1.AdoptModeOverride:
certCopy := cert.DeepCopy()
certCopy.SetKonnectID(konnectID)
if err = updateCertificate(ctx, cl, sdk, certCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
if !certificateMatch(resp.Certificate, cert) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
cert.SetKonnectID(konnectID)
return nil
}
func fetchTLSDataFromSecret(ctx context.Context, cl client.Client, parentNamespace string, secretRef *commonv1alpha1.NamespacedRef) (certData, keyData string, err error) {
if secretRef == nil {
return "", "", fmt.Errorf("secretRef is nil")
}
ns := parentNamespace
if secretRef.Namespace != nil && *secretRef.Namespace != "" {
ns = *secretRef.Namespace
}
secret := &corev1.Secret{}
if err := cl.Get(ctx, client.ObjectKey{
Namespace: ns,
Name: secretRef.Name,
}, secret); err != nil {
return "", "", fmt.Errorf("failed to fetch Secret %s/%s: %w", ns, secretRef.Name, err)
}
certBytes, ok := secret.Data["tls.crt"]
if !ok {
return "", "", fmt.Errorf("secret %s/%s is missing key 'tls.crt'", ns, secretRef.Name)
}
keyBytes, ok := secret.Data["tls.key"]
if !ok {
return "", "", fmt.Errorf("secret %s/%s is missing key 'tls.key'", ns, secretRef.Name)
}
return string(certBytes), string(keyBytes), nil
}
func kongCertificateToCertificateInput(ctx context.Context, cl client.Client, cert *configurationv1alpha1.KongCertificate) (sdkkonnectcomp.Certificate, error) {
var certData, keyData, certAltData, keyAltData string
// Check the certificate data type.
if cert.Spec.Type != nil && *cert.Spec.Type == configurationv1alpha1.KongCertificateSourceTypeSecretRef {
// Fetch the Secret.
secretRef := cert.Spec.SecretRef
if secretRef == nil {
// This should not happen due to validation, but just in case.
return sdkkonnectcomp.Certificate{}, fmt.Errorf("secretRef is nil")
}
var err error
parentNamespace := cert.GetNamespace()
certData, keyData, err = fetchTLSDataFromSecret(ctx, cl, parentNamespace, secretRef)
if err != nil {
return sdkkonnectcomp.Certificate{}, err
}
// Optional alternative cert/key.
if cert.Spec.SecretRefAlt != nil {
certAltData, keyAltData, err = fetchTLSDataFromSecret(ctx, cl, parentNamespace, cert.Spec.SecretRefAlt)
if err != nil {
return sdkkonnectcomp.Certificate{}, err
}
}
} else {
// Use inline certificate data.
certData = cert.Spec.Cert
keyData = cert.Spec.Key
certAltData = cert.Spec.CertAlt
keyAltData = cert.Spec.KeyAlt
}
input := sdkkonnectcomp.Certificate{
Cert: certData,
Key: keyData,
Tags: GenerateTagsForObject(cert, cert.Spec.Tags...),
}
if certAltData != "" {
input.CertAlt = new(certAltData)
}
if keyAltData != "" {
input.KeyAlt = new(keyAltData)
}
return input, nil
}
func getKongCertificateForUID(
ctx context.Context,
sdk sdkkonnectgo.CertificatesSDK,
cert *configurationv1alpha1.KongCertificate,
) (string, error) {
resp, err := sdk.ListCertificate(ctx, sdkkonnectops.ListCertificateRequest{
ControlPlaneID: cert.GetControlPlaneID(),
Tags: new(UIDLabelForObject(cert)),
})
if err != nil {
return "", fmt.Errorf("failed to list %s: %w", cert.GetTypeName(), err)
}
if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", cert.GetTypeName(), ErrNilResponse)
}
_, id, err := getMatchingEntryFromListResponseData(sliceToEntityWithIDPtrSlice(resp.Object.Data), cert)
return id, err
}
func certificateMatch(
konnectCert *sdkkonnectcomp.Certificate,
cert *configurationv1alpha1.KongCertificate,
) bool {
if konnectCert == nil {
return false
}
spec := cert.Spec
return konnectCert.Cert == spec.Cert &&
konnectCert.Key == spec.Key &&
equalWithDefault(konnectCert.CertAlt, stringPtrOrNil(spec.CertAlt), "") &&
equalWithDefault(konnectCert.KeyAlt, stringPtrOrNil(spec.KeyAlt), "")
}
func stringPtrOrNil(val string) *string {
if val == "" {
return nil
}
return new(val)
}