-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_credentialapikey.go
More file actions
217 lines (188 loc) · 6.75 KB
/
ops_credentialapikey.go
File metadata and controls
217 lines (188 loc) · 6.75 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
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"
"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"
)
func createKongCredentialAPIKey(
ctx context.Context,
sdk sdkkonnectgo.APIKeysSDK,
cred *configurationv1alpha1.KongCredentialAPIKey,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}
resp, err := sdk.CreateKeyAuthWithConsumer(ctx,
sdkkonnectops.CreateKeyAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
KeyAuthWithoutParents: kongCredentialAPIKeyToKeyAuthWithoutParents(cred),
},
)
// 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, cred); errWrap != nil {
return errWrap
}
if resp == nil || resp.KeyAuth == nil || resp.KeyAuth.ID == nil {
return fmt.Errorf("failed creating %s: %w", cred.GetTypeName(), ErrNilResponse)
}
cred.SetKonnectID(*resp.KeyAuth.ID)
return nil
}
// updateKongCredentialAPIKey updates the Konnect BasicAuth entity.
// It is assumed that the provided BasicAuth has Konnect ID set in status.
// It returns an error if the BasicAuth does not have a ControlPlaneRef or
// if the operation fails.
func updateKongCredentialAPIKey(
ctx context.Context,
sdk sdkkonnectgo.APIKeysSDK,
cred *configurationv1alpha1.KongCredentialAPIKey,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}
_, err := sdk.UpsertKeyAuthWithConsumer(ctx,
sdkkonnectops.UpsertKeyAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
KeyAuthID: cred.GetKonnectStatus().GetKonnectID(),
KeyAuthWithoutParents: kongCredentialAPIKeyToKeyAuthWithoutParents(cred),
})
if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, cred); errWrap != nil {
return errWrap
}
return nil
}
// deleteKongCredentialAPIKey deletes a BasicAuth credential in Konnect.
// It is assumed that the provided BasicAuth has Konnect ID set in status.
// It returns an error if the operation fails.
func deleteKongCredentialAPIKey(
ctx context.Context,
sdk sdkkonnectgo.APIKeysSDK,
cred *configurationv1alpha1.KongCredentialAPIKey,
) error {
cpID := cred.GetControlPlaneID()
id := cred.GetKonnectStatus().GetKonnectID()
_, err := sdk.DeleteKeyAuthWithConsumer(ctx,
sdkkonnectops.DeleteKeyAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
KeyAuthID: id,
})
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cred); errWrap != nil {
return handleDeleteError(ctx, err, cred)
}
return nil
}
func adoptKongCredentialAPIKey(
ctx context.Context,
sdk sdkkonnectgo.APIKeysSDK,
cred *configurationv1alpha1.KongCredentialAPIKey,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return KonnectEntityAdoptionMissingControlPlaneIDError{}
}
if cred.Status.Konnect == nil || cred.Status.Konnect.GetConsumerID() == "" {
return fmt.Errorf("can't adopt %T %s without a Konnect Consumer ID", cred, client.ObjectKeyFromObject(cred))
}
if cred.Spec.Adopt == nil || cred.Spec.Adopt.Konnect == nil {
return fmt.Errorf("missing Konnect adoption options for %T %s", cred, client.ObjectKeyFromObject(cred))
}
adoptOptions := cred.Spec.Adopt
konnectID := adoptOptions.Konnect.ID
resp, err := sdk.GetKeyAuthWithConsumer(ctx, sdkkonnectops.GetKeyAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
KeyAuthID: konnectID,
})
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
if resp == nil || resp.KeyAuth == nil {
return fmt.Errorf("failed to adopt %s: %w", cred.GetTypeName(), ErrNilResponse)
}
uidTag, hasUIDTag := findUIDTag(resp.KeyAuth.Tags)
if hasUIDTag && extractUIDFromTag(uidTag) != string(cred.UID) {
return KonnectEntityAdoptionUIDTagConflictError{
KonnectID: konnectID,
ActualUIDTag: extractUIDFromTag(uidTag),
}
}
adoptMode := adoptOptions.Mode
if adoptMode == "" {
adoptMode = commonv1alpha1.AdoptModeOverride
}
switch adoptMode {
case commonv1alpha1.AdoptModeOverride:
credCopy := cred.DeepCopy()
credCopy.SetKonnectID(konnectID)
if err = updateKongCredentialAPIKey(ctx, sdk, credCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
if !credentialAPIKeyMatch(resp.KeyAuth, cred) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
cred.SetKonnectID(konnectID)
return nil
}
func kongCredentialAPIKeyToKeyAuthWithoutParents(
cred *configurationv1alpha1.KongCredentialAPIKey,
) *sdkkonnectcomp.KeyAuthWithoutParents {
return &sdkkonnectcomp.KeyAuthWithoutParents{
// Key is required in CRD so we don't need to check if it has been provided.
Key: new(cred.Spec.Key),
Tags: GenerateTagsForObject(cred, cred.Spec.Tags...),
}
}
// getKongCredentialAPIKeyForUID lists API key credentials in Konnect with given k8s uid as its tag.
func getKongCredentialAPIKeyForUID(
ctx context.Context,
sdk sdkkonnectgo.APIKeysSDK,
cred *configurationv1alpha1.KongCredentialAPIKey,
) (string, error) {
cpID := cred.GetControlPlaneID()
req := sdkkonnectops.ListKeyAuthRequest{
// NOTE: only filter on object's UID.
// Other fields like name might have changed in the meantime but that's OK.
// Those will be enforced via subsequent updates.
ControlPlaneID: cpID,
Tags: new(UIDLabelForObject(cred)),
}
resp, err := sdk.ListKeyAuth(ctx, req)
if err != nil {
return "", fmt.Errorf("failed listing %s: %w", cred.GetTypeName(), err)
}
if resp == nil || resp.Object == nil {
return "", fmt.Errorf("failed listing %s: %w", cred.GetTypeName(), ErrNilResponse)
}
_, id, err := getMatchingEntryFromListResponseData(sliceToEntityWithIDPtrSlice(resp.Object.Data), cred)
return id, err
}
func credentialAPIKeyMatch(
konnectKeyAuth *sdkkonnectcomp.KeyAuth,
cred *configurationv1alpha1.KongCredentialAPIKey,
) bool {
if konnectKeyAuth == nil || konnectKeyAuth.Key == nil {
return false
}
return *konnectKeyAuth.Key == cred.Spec.Key
}