-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_kongcacertificate.go
More file actions
255 lines (219 loc) · 7.38 KB
/
ops_kongcacertificate.go
File metadata and controls
255 lines (219 loc) · 7.38 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
package ops
import (
"context"
"fmt"
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"
)
// createCACertificate creates a KongCACertificate in Konnect.
// It sets the KonnectID the KongCACertificate status.
func createCACertificate(
ctx context.Context,
cl client.Client,
sdk sdkkonnectgo.CACertificatesSDK,
cert *configurationv1alpha1.KongCACertificate,
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cert, Op: CreateOp}
}
// Generate the input for the creation.
// This may fetch the Secret if the CACertificate source type is SecretRef.
input, err := kongCACertificateToCACertificateInput(ctx, cl, cert)
if err != nil {
return err
}
resp, err := sdk.CreateCaCertificate(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.CACertificate == nil || resp.CACertificate.ID == nil || *resp.CACertificate.ID == "" {
return fmt.Errorf("failed creating %s: %w", cert.GetTypeName(), ErrNilResponse)
}
// At this point, the CACertificate has been created successfully.
cert.SetKonnectID(*resp.CACertificate.ID)
return nil
}
// updateCACertificate updates a KongCACertificate in Konnect.
// The KongCACertificate must have a KonnectID set in its status.
// It returns an error if the KongCACertificate does not have a KonnectID.
func updateCACertificate(
ctx context.Context,
cl client.Client,
sdk sdkkonnectgo.CACertificatesSDK,
cert *configurationv1alpha1.KongCACertificate,
) 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 CACertificate source type is SecretRef.
input, err := kongCACertificateToCACertificateInput(ctx, cl, cert)
if err != nil {
return err
}
_, err = sdk.UpsertCaCertificate(ctx,
sdkkonnectops.UpsertCaCertificateRequest{
ControlPlaneID: cpID,
CACertificateID: cert.GetKonnectStatus().GetKonnectID(),
CACertificate: input,
},
)
if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, cert); errWrap != nil {
return errWrap
}
return nil
}
// deleteCACertificate deletes a KongCACertificate in Konnect.
// The KongCACertificate must have a KonnectID set in its status.
// It returns an error if the operation fails.
func deleteCACertificate(
ctx context.Context,
sdk sdkkonnectgo.CACertificatesSDK,
cert *configurationv1alpha1.KongCACertificate,
) error {
id := cert.Status.Konnect.GetKonnectID()
_, err := sdk.DeleteCaCertificate(ctx, cert.GetControlPlaneID(), id)
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cert); errWrap != nil {
return handleDeleteError(ctx, err, cert)
}
return nil
}
func adoptCACertificate(
ctx context.Context,
cl client.Client,
sdk sdkkonnectgo.CACertificatesSDK,
cert *configurationv1alpha1.KongCACertificate,
) error {
cpID := cert.GetControlPlaneID()
if cpID == "" {
return KonnectEntityAdoptionMissingControlPlaneIDError{}
}
adoptOptions := cert.Spec.Adopt
konnectID := adoptOptions.Konnect.ID
resp, err := sdk.GetCaCertificate(ctx, konnectID, cpID)
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
if resp == nil || resp.CACertificate == nil {
return fmt.Errorf("failed to adopt %s: %w", cert.GetTypeName(), ErrNilResponse)
}
uidTag, hasUIDTag := findUIDTag(resp.CACertificate.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 = updateCACertificate(ctx, cl, sdk, certCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
if !caCertificateMatch(resp.CACertificate, cert) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
cert.SetKonnectID(konnectID)
return nil
}
func fetchCACertDataFromSecret(ctx context.Context, cl client.Client, parentNamespace string, secretRef *commonv1alpha1.NamespacedRef) (certData 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["ca.crt"]
if !ok {
return "", fmt.Errorf("secret %s/%s is missing key 'ca.crt'", ns, secretRef.Name)
}
return string(certBytes), nil
}
func kongCACertificateToCACertificateInput(ctx context.Context, cl client.Client, cert *configurationv1alpha1.KongCACertificate) (sdkkonnectcomp.CACertificate, error) {
var certData string
// Check the certificate data type.
if cert.Spec.Type != nil && *cert.Spec.Type == configurationv1alpha1.KongCACertificateSourceTypeSecretRef {
// Fetch the Secret.
secretRef := cert.Spec.SecretRef
if secretRef == nil {
// This should not happen due to validation, but just in case.
return sdkkonnectcomp.CACertificate{}, fmt.Errorf("secretRef is nil")
}
var err error
parentNamespace := cert.GetNamespace()
certData, err = fetchCACertDataFromSecret(ctx, cl, parentNamespace, secretRef)
if err != nil {
return sdkkonnectcomp.CACertificate{}, err
}
} else {
// Use inline certificate data.
certData = cert.Spec.Cert
}
return sdkkonnectcomp.CACertificate{
Cert: certData,
// Deduplicate tags to avoid rejection by Konnect.
Tags: GenerateTagsForObject(cert, cert.Spec.Tags...),
}, nil
}
func getKongCACertificateForUID(
ctx context.Context,
sdk sdkkonnectgo.CACertificatesSDK,
cert *configurationv1alpha1.KongCACertificate,
) (string, error) {
resp, err := sdk.ListCaCertificate(ctx, sdkkonnectops.ListCaCertificateRequest{
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 caCertificateMatch(
konnectCert *sdkkonnectcomp.CACertificate,
cert *configurationv1alpha1.KongCACertificate,
) bool {
if konnectCert == nil {
return false
}
return konnectCert.Cert == cert.Spec.Cert
}