Skip to content

Commit

Permalink
feat(konnect): add support for basic auth credentials for consumers (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
pmalek and czeslavo authored Sep 20, 2024
1 parent fdceaed commit 9702944
Show file tree
Hide file tree
Showing 25 changed files with 1,353 additions and 30 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ packages:
PluginSDK:
UpstreamsSDK:
MeSDK:
CredentialBasicAuthSDK:
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
the creation of a managed `KongPluginBinding` resource, which is taken by the
`KongPluginBinding` reconciler to create the corresponding plugin object in Konnect.
[#550](https://github.com/Kong/gateway-operator/pull/550)
- Add support for `KongConsumer` credentials:
- basic-auth [#625](https://github.com/Kong/gateway-operator/pull/625)

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions config/rbac/role/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ rules:
- apiGroups:
- configuration.konghq.com
resources:
- credentialbasicauths
- ingressclassparameterses
- kongclusterplugins
- kongconsumergroups
Expand All @@ -143,6 +144,7 @@ rules:
- apiGroups:
- configuration.konghq.com
resources:
- credentialbasicauths/status
- kongclusterplugins/status
- kongconsumergroups/status
- kongconsumers/status
Expand Down
46 changes: 46 additions & 0 deletions config/samples/konnect_kongconsumer_basicauth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
kind: KonnectAPIAuthConfiguration
apiVersion: konnect.konghq.com/v1alpha1
metadata:
name: konnect-api-auth-dev-1
namespace: default
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: us.api.konghq.com
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
metadata:
name: test-cp-basic-auth
namespace: default
spec:
name: test-cp-basic-auth
labels:
app: test-cp-basic-auth
key1: test-cp-basic-auth
konnect:
authRef:
name: konnect-api-auth-dev-1
---
kind: KongConsumer
apiVersion: configuration.konghq.com/v1
metadata:
name: consumer1
namespace: default
username: consumer1
spec:
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
name: test-cp-basic-auth
---
apiVersion: configuration.konghq.com/v1alpha1
kind: CredentialBasicAuth
metadata:
name: basic-auth-1
namespace: default
spec:
consumerRef:
name: consumer1
password: pass
username: username
14 changes: 14 additions & 0 deletions controller/konnect/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ const (
// condition type indicating that the KongService reference is invalid.
KongServiceRefReasonInvalid = "Invalid"
)

const (
// KongConsumerRefValidConditionType is the type of the condition that indicates
// whether the KongConsumer reference is valid and points to an existing
// KongConsumer.
KongConsumerRefValidConditionType = "KongConsumerRefValid"

// KongConsumerRefReasonValid is the reason used with the KongConsumerRefValid
// condition type indicating that the KongConsumer reference is valid.
KongConsumerRefReasonValid = "Valid"
// KongConsumerRefReasonInvalid is the reason used with the KongConsumerRefValid
// condition type indicating that the KongConsumer reference is invalid.
KongConsumerRefReasonInvalid = "Invalid"
)
1 change: 1 addition & 0 deletions controller/konnect/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SupportedKonnectEntityType interface {
configurationv1.KongConsumer |
configurationv1beta1.KongConsumerGroup |
configurationv1alpha1.KongPluginBinding |
configurationv1alpha1.CredentialBasicAuth |
configurationv1alpha1.KongUpstream
// TODO: add other types

Expand Down
26 changes: 26 additions & 0 deletions controller/konnect/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package konnect

import (
"fmt"
"time"

"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -35,3 +36,28 @@ type ReferencedKongServiceIsBeingDeleted struct {
func (e ReferencedKongServiceIsBeingDeleted) Error() string {
return fmt.Sprintf("referenced Kong Service %s is being deleted", e.Reference)
}

// ReferencedKongConsumerIsBeingDeleted is an error type that is returned when
// a Konnect entity references a Kong Consumer which is being deleted.
type ReferencedKongConsumerIsBeingDeleted struct {
Reference types.NamespacedName
DeletionTimestamp time.Time
}

// Error implements the error interface.
func (e ReferencedKongConsumerIsBeingDeleted) Error() string {
return fmt.Sprintf("referenced Kong Consumer %s is being deleted (deletion timestamp: %s)",
e.Reference, e.DeletionTimestamp,
)
}

// ReferencedKongConsumerDoesNotExist is an error type that is returned when the referenced KongConsumer does not exist.
type ReferencedKongConsumerDoesNotExist struct {
Reference types.NamespacedName
Err error
}

// Error implements the error interface.
func (e ReferencedKongConsumerDoesNotExist) Error() string {
return fmt.Sprintf("referenced Kong Consumer %s does not exist: %v", e.Reference, e.Err)
}
4 changes: 3 additions & 1 deletion controller/konnect/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ func ReconciliationIndexOptionsForEntity[
T constraints.SupportedKonnectEntityType,
]() []ReconciliationIndexOption {
var e TEnt
switch any(e).(type) { //nolint:gocritic // TODO: add index options required for other entities
switch any(e).(type) {
case *configurationv1alpha1.KongPluginBinding:
return IndexOptionsForKongPluginBinding()
case *configurationv1alpha1.CredentialBasicAuth:
return IndexOptionsForCredentialsBasicAuth()
}
return nil
}
32 changes: 32 additions & 0 deletions controller/konnect/index_credentials_basicauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package konnect

import (
"sigs.k8s.io/controller-runtime/pkg/client"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

const (
// IndexFieldCredentialBasicAuthReferencesKongConsumer is the index name for CredentialBasicAuth -> Consumer.
IndexFieldCredentialBasicAuthReferencesKongConsumer = "kongCredentialsBasicAuthConsumerRef"
)

// IndexOptionsForCredentialsBasicAuth returns required Index options for CredentialBasicAuth.
func IndexOptionsForCredentialsBasicAuth() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1alpha1.CredentialBasicAuth{},
IndexField: IndexFieldCredentialBasicAuthReferencesKongConsumer,
ExtractValue: kongCredentialBasicAuthReferencesConsumer,
},
}
}

// kongCredentialBasicAuthReferencesConsumer returns the name of referenced Consumer.
func kongCredentialBasicAuthReferencesConsumer(obj client.Object) []string {
cred, ok := obj.(*configurationv1alpha1.CredentialBasicAuth)
if !ok {
return nil
}
return []string{cred.Spec.ConsumerRef.Name}
}
14 changes: 14 additions & 0 deletions controller/konnect/ops/credentialbasicauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ops

import (
"context"

sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
)

// CredentialBasicAuthSDK is the interface for the Konnect CredentialBasicAuthSDK.
type CredentialBasicAuthSDK interface {
CreateBasicAuthWithConsumer(ctx context.Context, req sdkkonnectops.CreateBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreateBasicAuthWithConsumerResponse, error)
DeleteBasicAuthWithConsumer(ctx context.Context, request sdkkonnectops.DeleteBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeleteBasicAuthWithConsumerResponse, error)
UpsertBasicAuthWithConsumer(ctx context.Context, request sdkkonnectops.UpsertBasicAuthWithConsumerRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertBasicAuthWithConsumerResponse, error)
}
Loading

0 comments on commit 9702944

Please sign in to comment.