-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_credentialhmac.go
More file actions
237 lines (205 loc) · 7.06 KB
/
ops_credentialhmac.go
File metadata and controls
237 lines (205 loc) · 7.06 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
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 createKongCredentialHMAC(
ctx context.Context,
sdk sdkkonnectgo.HMACAuthCredentialsSDK,
cred *configurationv1alpha1.KongCredentialHMAC,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}
resp, err := sdk.CreateHmacAuthWithConsumer(ctx,
sdkkonnectops.CreateHmacAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
HMACAuthWithoutParents: kongCredentialHMACToHMACWithoutParents(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.HMACAuth == nil || resp.HMACAuth.ID == nil {
return fmt.Errorf("failed creating %s: %w", cred.GetTypeName(), ErrNilResponse)
}
cred.SetKonnectID(*resp.HMACAuth.ID)
return nil
}
// updateKongCredentialHMAC updates the Konnect HMAC entity.
// It is assumed that the provided HMAC has Konnect ID set in status.
// It returns an error if the HMAC does not have a ControlPlaneRef or
// if the operation fails.
func updateKongCredentialHMAC(
ctx context.Context,
sdk sdkkonnectgo.HMACAuthCredentialsSDK,
cred *configurationv1alpha1.KongCredentialHMAC,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}
_, err := sdk.UpsertHmacAuthWithConsumer(ctx,
sdkkonnectops.UpsertHmacAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
HMACAuthID: cred.GetKonnectStatus().GetKonnectID(),
HMACAuthWithoutParents: kongCredentialHMACToHMACWithoutParents(cred),
})
if errWrap := wrapErrIfKonnectOpFailed(err, UpdateOp, cred); errWrap != nil {
return errWrap
}
return nil
}
// deleteKongCredentialHMAC deletes an HMAC credential in Konnect.
// It is assumed that the provided HMAC has Konnect ID set in status.
// It returns an error if the operation fails.
func deleteKongCredentialHMAC(
ctx context.Context,
sdk sdkkonnectgo.HMACAuthCredentialsSDK,
cred *configurationv1alpha1.KongCredentialHMAC,
) error {
cpID := cred.GetControlPlaneID()
id := cred.GetKonnectStatus().GetKonnectID()
_, err := sdk.DeleteHmacAuthWithConsumer(ctx,
sdkkonnectops.DeleteHmacAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
HMACAuthID: id,
})
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cred); errWrap != nil {
return handleDeleteError(ctx, err, cred)
}
return nil
}
func adoptKongCredentialHMAC(
ctx context.Context,
sdk sdkkonnectgo.HMACAuthCredentialsSDK,
cred *configurationv1alpha1.KongCredentialHMAC,
) 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.GetHmacAuthWithConsumer(ctx, sdkkonnectops.GetHmacAuthWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
HMACAuthID: konnectID,
})
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
if resp == nil || resp.HMACAuth == nil {
return fmt.Errorf("failed to adopt %s: %w", cred.GetTypeName(), ErrNilResponse)
}
uidTag, hasUIDTag := findUIDTag(resp.HMACAuth.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 = updateKongCredentialHMAC(ctx, sdk, credCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
if !credentialHMACMatch(resp.HMACAuth, cred) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
cred.SetKonnectID(konnectID)
return nil
}
func kongCredentialHMACToHMACWithoutParents(
cred *configurationv1alpha1.KongCredentialHMAC,
) sdkkonnectcomp.HMACAuthWithoutParents {
username := ""
if cred.Spec.Username != nil {
username = *cred.Spec.Username
}
return sdkkonnectcomp.HMACAuthWithoutParents{
Username: username,
Secret: cred.Spec.Secret,
Tags: GenerateTagsForObject(cred, cred.Spec.Tags...),
}
}
// getKongCredentialHMACForUID lists HMAC credentials in Konnect with given k8s uid as its tag.
func getKongCredentialHMACForUID(
ctx context.Context,
sdk sdkkonnectgo.HMACAuthCredentialsSDK,
cred *configurationv1alpha1.KongCredentialHMAC,
) (string, error) {
cpID := cred.GetControlPlaneID()
req := sdkkonnectops.ListHmacAuthRequest{
// 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.ListHmacAuth(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 credentialHMACMatch(
konnectHMAC *sdkkonnectcomp.HMACAuth,
cred *configurationv1alpha1.KongCredentialHMAC,
) bool {
if konnectHMAC == nil || cred.Spec.Username == nil {
return false
}
if konnectHMAC.Username != *cred.Spec.Username {
return false
}
if cred.Spec.ID != nil {
if konnectHMAC.ID == nil || *konnectHMAC.ID != *cred.Spec.ID {
return false
}
}
if cred.Spec.Secret != nil {
if konnectHMAC.Secret == nil || *konnectHMAC.Secret != *cred.Spec.Secret {
return false
}
}
return true
}