Skip to content

Commit 152a936

Browse files
committed
feat(konnect): add support for basic auth credentials for consumers
1 parent 522702b commit 152a936

25 files changed

+1246
-23
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 `KognConsumer` 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,49 @@
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: test1
15+
namespace: default
16+
spec:
17+
name: test1
18+
labels:
19+
app: test1
20+
key1: test1
21+
konnect:
22+
authRef:
23+
name: konnect-api-auth-dev-1
24+
---
25+
kind: KongService
26+
apiVersion: configuration.konghq.com/v1alpha1
27+
metadata:
28+
name: service-1
29+
namespace: default
30+
spec:
31+
name: service-1
32+
host: example.com
33+
controlPlaneRef:
34+
type: konnectNamespacedRef
35+
konnectNamespacedRef:
36+
name: test1
37+
---
38+
kind: KongConsumer
39+
apiVersion: configuration.konghq.com/v1
40+
metadata:
41+
name: consumer-1
42+
namespace: default
43+
username: consumer-1
44+
custom_id: 08433C12-2B81-4738-B61D-3AA2136F0212
45+
spec:
46+
controlPlaneRef:
47+
type: konnectNamespacedRef
48+
konnectNamespacedRef:
49+
name: test1

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

+11
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ type ReferencedKongServiceIsBeingDeleted struct {
3535
func (e ReferencedKongServiceIsBeingDeleted) Error() string {
3636
return fmt.Sprintf("referenced Kong Service %s is being deleted", e.Reference)
3737
}
38+
39+
// ReferencedKongConsumerIsBeingDeleted is an error type that is returned when
40+
// a Konnect entity references a Kong Consumer which is being deleted.
41+
type ReferencedKongConsumerIsBeingDeleted struct {
42+
Reference types.NamespacedName
43+
}
44+
45+
// Error implements the error interface.
46+
func (e ReferencedKongConsumerIsBeingDeleted) Error() string {
47+
return fmt.Sprintf("referenced Kong Consumer %s is being deleted", e.Reference)
48+
}

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)