Skip to content

Commit 9702944

Browse files
pmalekczeslavo
andauthored
feat(konnect): add support for basic auth credentials for consumers (#625)
* feat(konnect): add support for basic auth credentials for consumers * Apply suggestions from code review Co-authored-by: Grzegorz Burzyński <[email protected]> * chore: refactor getServiceRef * fix: watch for consumer changes in CredentialBasicAuth reconciler * fix: fix handling Consumer deletion for credentials * refactor(konnect): refactor ReferencedKongConsumerIsBeingDeleted handling * Update controller/konnect/errors.go Co-authored-by: Grzegorz Burzyński <[email protected]> --------- Co-authored-by: Grzegorz Burzyński <[email protected]>
1 parent fdceaed commit 9702944

25 files changed

+1353
-30
lines changed

.mockery.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ packages:
1919
PluginSDK:
2020
UpstreamsSDK:
2121
MeSDK:
22+
CredentialBasicAuthSDK:

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
the creation of a managed `KongPluginBinding` resource, which is taken by the
7171
`KongPluginBinding` reconciler to create the corresponding plugin object in Konnect.
7272
[#550](https://github.com/Kong/gateway-operator/pull/550)
73+
- Add support for `KongConsumer` credentials:
74+
- basic-auth [#625](https://github.com/Kong/gateway-operator/pull/625)
7375

7476
### Fixed
7577

config/rbac/role/role.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ rules:
125125
- apiGroups:
126126
- configuration.konghq.com
127127
resources:
128+
- credentialbasicauths
128129
- ingressclassparameterses
129130
- kongclusterplugins
130131
- kongconsumergroups
@@ -143,6 +144,7 @@ rules:
143144
- apiGroups:
144145
- configuration.konghq.com
145146
resources:
147+
- credentialbasicauths/status
146148
- kongclusterplugins/status
147149
- kongconsumergroups/status
148150
- kongconsumers/status
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
kind: KonnectAPIAuthConfiguration
2+
apiVersion: konnect.konghq.com/v1alpha1
3+
metadata:
4+
name: konnect-api-auth-dev-1
5+
namespace: default
6+
spec:
7+
type: token
8+
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
9+
serverURL: us.api.konghq.com
10+
---
11+
kind: KonnectGatewayControlPlane
12+
apiVersion: konnect.konghq.com/v1alpha1
13+
metadata:
14+
name: test-cp-basic-auth
15+
namespace: default
16+
spec:
17+
name: test-cp-basic-auth
18+
labels:
19+
app: test-cp-basic-auth
20+
key1: test-cp-basic-auth
21+
konnect:
22+
authRef:
23+
name: konnect-api-auth-dev-1
24+
---
25+
kind: KongConsumer
26+
apiVersion: configuration.konghq.com/v1
27+
metadata:
28+
name: consumer1
29+
namespace: default
30+
username: consumer1
31+
spec:
32+
controlPlaneRef:
33+
type: konnectNamespacedRef
34+
konnectNamespacedRef:
35+
name: test-cp-basic-auth
36+
---
37+
apiVersion: configuration.konghq.com/v1alpha1
38+
kind: CredentialBasicAuth
39+
metadata:
40+
name: basic-auth-1
41+
namespace: default
42+
spec:
43+
consumerRef:
44+
name: consumer1
45+
password: pass
46+
username: username

controller/konnect/conditions/conditions.go

+14
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,17 @@ const (
8080
// condition type indicating that the KongService reference is invalid.
8181
KongServiceRefReasonInvalid = "Invalid"
8282
)
83+
84+
const (
85+
// KongConsumerRefValidConditionType is the type of the condition that indicates
86+
// whether the KongConsumer reference is valid and points to an existing
87+
// KongConsumer.
88+
KongConsumerRefValidConditionType = "KongConsumerRefValid"
89+
90+
// KongConsumerRefReasonValid is the reason used with the KongConsumerRefValid
91+
// condition type indicating that the KongConsumer reference is valid.
92+
KongConsumerRefReasonValid = "Valid"
93+
// KongConsumerRefReasonInvalid is the reason used with the KongConsumerRefValid
94+
// condition type indicating that the KongConsumer reference is invalid.
95+
KongConsumerRefReasonInvalid = "Invalid"
96+
)

controller/konnect/constraints/constraints.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type SupportedKonnectEntityType interface {
1919
configurationv1.KongConsumer |
2020
configurationv1beta1.KongConsumerGroup |
2121
configurationv1alpha1.KongPluginBinding |
22+
configurationv1alpha1.CredentialBasicAuth |
2223
configurationv1alpha1.KongUpstream
2324
// TODO: add other types
2425

controller/konnect/errors.go

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package konnect
22

33
import (
44
"fmt"
5+
"time"
56

67
"k8s.io/apimachinery/pkg/types"
78
)
@@ -35,3 +36,28 @@ type ReferencedKongServiceIsBeingDeleted struct {
3536
func (e ReferencedKongServiceIsBeingDeleted) Error() string {
3637
return fmt.Sprintf("referenced Kong Service %s is being deleted", e.Reference)
3738
}
39+
40+
// ReferencedKongConsumerIsBeingDeleted is an error type that is returned when
41+
// a Konnect entity references a Kong Consumer which is being deleted.
42+
type ReferencedKongConsumerIsBeingDeleted struct {
43+
Reference types.NamespacedName
44+
DeletionTimestamp time.Time
45+
}
46+
47+
// Error implements the error interface.
48+
func (e ReferencedKongConsumerIsBeingDeleted) Error() string {
49+
return fmt.Sprintf("referenced Kong Consumer %s is being deleted (deletion timestamp: %s)",
50+
e.Reference, e.DeletionTimestamp,
51+
)
52+
}
53+
54+
// ReferencedKongConsumerDoesNotExist is an error type that is returned when the referenced KongConsumer does not exist.
55+
type ReferencedKongConsumerDoesNotExist struct {
56+
Reference types.NamespacedName
57+
Err error
58+
}
59+
60+
// Error implements the error interface.
61+
func (e ReferencedKongConsumerDoesNotExist) Error() string {
62+
return fmt.Sprintf("referenced Kong Consumer %s does not exist: %v", e.Reference, e.Err)
63+
}

controller/konnect/index.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ func ReconciliationIndexOptionsForEntity[
2121
T constraints.SupportedKonnectEntityType,
2222
]() []ReconciliationIndexOption {
2323
var e TEnt
24-
switch any(e).(type) { //nolint:gocritic // TODO: add index options required for other entities
24+
switch any(e).(type) {
2525
case *configurationv1alpha1.KongPluginBinding:
2626
return IndexOptionsForKongPluginBinding()
27+
case *configurationv1alpha1.CredentialBasicAuth:
28+
return IndexOptionsForCredentialsBasicAuth()
2729
}
2830
return nil
2931
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package konnect
2+
3+
import (
4+
"sigs.k8s.io/controller-runtime/pkg/client"
5+
6+
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
7+
)
8+
9+
const (
10+
// IndexFieldCredentialBasicAuthReferencesKongConsumer is the index name for CredentialBasicAuth -> Consumer.
11+
IndexFieldCredentialBasicAuthReferencesKongConsumer = "kongCredentialsBasicAuthConsumerRef"
12+
)
13+
14+
// IndexOptionsForCredentialsBasicAuth returns required Index options for CredentialBasicAuth.
15+
func IndexOptionsForCredentialsBasicAuth() []ReconciliationIndexOption {
16+
return []ReconciliationIndexOption{
17+
{
18+
IndexObject: &configurationv1alpha1.CredentialBasicAuth{},
19+
IndexField: IndexFieldCredentialBasicAuthReferencesKongConsumer,
20+
ExtractValue: kongCredentialBasicAuthReferencesConsumer,
21+
},
22+
}
23+
}
24+
25+
// kongCredentialBasicAuthReferencesConsumer returns the name of referenced Consumer.
26+
func kongCredentialBasicAuthReferencesConsumer(obj client.Object) []string {
27+
cred, ok := obj.(*configurationv1alpha1.CredentialBasicAuth)
28+
if !ok {
29+
return nil
30+
}
31+
return []string{cred.Spec.ConsumerRef.Name}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ops
2+
3+
import (
4+
"context"
5+
6+
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
7+
)
8+
9+
// CredentialBasicAuthSDK is the interface for the Konnect CredentialBasicAuthSDK.
10+
type CredentialBasicAuthSDK interface {
11+
CreateBasicAuthWithConsumer(ctx context.Context, req sdkkonnectops.CreateBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateBasicAuthWithConsumerResponse, error)
12+
DeleteBasicAuthWithConsumer(ctx context.Context, request sdkkonnectops.DeleteBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteBasicAuthWithConsumerResponse, error)
13+
UpsertBasicAuthWithConsumer(ctx context.Context, request sdkkonnectops.UpsertBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertBasicAuthWithConsumerResponse, error)
14+
}

0 commit comments

Comments
 (0)