-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathops_credentialacl.go
More file actions
220 lines (191 loc) · 6.61 KB
/
ops_credentialacl.go
File metadata and controls
220 lines (191 loc) · 6.61 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
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 createKongCredentialACL(
ctx context.Context,
sdk sdkkonnectgo.ACLsSDK,
cred *configurationv1alpha1.KongCredentialACL,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: CreateOp}
}
resp, err := sdk.CreateACLWithConsumer(ctx,
sdkkonnectops.CreateACLWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
ACLWithoutParents: kongCredentialACLToACLWithoutParents(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.ACL == nil || resp.ACL.ID == nil {
return fmt.Errorf("failed creating %s: %w", cred.GetTypeName(), ErrNilResponse)
}
cred.SetKonnectID(*resp.ACL.ID)
return nil
}
// updateKongCredentialACL updates the Konnect ACL entity.
// It is assumed that the provided ACL has Konnect ID set in status.
// It returns an error if the ACL does not have a ControlPlaneRef or
// if the operation fails.
func updateKongCredentialACL(
ctx context.Context,
sdk sdkkonnectgo.ACLsSDK,
cred *configurationv1alpha1.KongCredentialACL,
) error {
cpID := cred.GetControlPlaneID()
if cpID == "" {
return CantPerformOperationWithoutControlPlaneIDError{Entity: cred, Op: UpdateOp}
}
_, err := sdk.UpsertACLWithConsumer(ctx,
sdkkonnectops.UpsertACLWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
ACLID: cred.GetKonnectStatus().GetKonnectID(),
ACLWithoutParents: kongCredentialACLToACLWithoutParents(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, UpdateOp, cred); errWrap != nil {
return errWrap
}
return nil
}
// deleteKongCredentialACL deletes an ACL credential in Konnect.
// It is assumed that the provided ACL has Konnect ID set in status.
// It returns an error if the operation fails.
func deleteKongCredentialACL(
ctx context.Context,
sdk sdkkonnectgo.ACLsSDK,
cred *configurationv1alpha1.KongCredentialACL,
) error {
cpID := cred.GetControlPlaneID()
id := cred.GetKonnectStatus().GetKonnectID()
_, err := sdk.DeleteACLWithConsumer(ctx,
sdkkonnectops.DeleteACLWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
ACLID: id,
})
if errWrap := wrapErrIfKonnectOpFailed(err, DeleteOp, cred); errWrap != nil {
return handleDeleteError(ctx, err, cred)
}
return nil
}
func adoptKongCredentialACL(
ctx context.Context,
sdk sdkkonnectgo.ACLsSDK,
cred *configurationv1alpha1.KongCredentialACL,
) 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.GetACLWithConsumer(ctx, sdkkonnectops.GetACLWithConsumerRequest{
ControlPlaneID: cpID,
ConsumerIDForNestedEntities: cred.Status.Konnect.GetConsumerID(),
ACLID: konnectID,
})
if err != nil {
return KonnectEntityAdoptionFetchError{
KonnectID: konnectID,
Err: err,
}
}
if resp == nil || resp.ACL == nil {
return fmt.Errorf("failed to adopt %s: %w", cred.GetTypeName(), ErrNilResponse)
}
uidTag, hasUIDTag := findUIDTag(resp.ACL.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 = updateKongCredentialACL(ctx, sdk, credCopy); err != nil {
return err
}
case commonv1alpha1.AdoptModeMatch:
if !credentialACLMatch(resp.ACL, cred) {
return KonnectEntityAdoptionNotMatchError{
KonnectID: konnectID,
}
}
default:
return fmt.Errorf("failed to adopt: adopt mode %q not supported", adoptMode)
}
cred.SetKonnectID(konnectID)
return nil
}
func kongCredentialACLToACLWithoutParents(
cred *configurationv1alpha1.KongCredentialACL,
) sdkkonnectcomp.ACLWithoutParents {
return sdkkonnectcomp.ACLWithoutParents{
Group: cred.Spec.Group,
Tags: GenerateTagsForObject(cred, cred.Spec.Tags...),
}
}
// getKongCredentialACLForUID lists API key credentials in Konnect with given k8s uid as its tag.
func getKongCredentialACLForUID(
ctx context.Context,
sdk sdkkonnectgo.ACLsSDK,
cred *configurationv1alpha1.KongCredentialACL,
) (string, error) {
cpID := cred.GetControlPlaneID()
req := sdkkonnectops.ListACLRequest{
// 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.ListACL(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)
}
x := sliceToEntityWithIDPtrSlice(resp.Object.Data)
_, id, err := getMatchingEntryFromListResponseData(x, cred)
return id, err
}
func credentialACLMatch(
konnectACL *sdkkonnectcomp.ACL,
cred *configurationv1alpha1.KongCredentialACL,
) bool {
if konnectACL == nil {
return false
}
return konnectACL.Group == cred.Spec.Group
}