Skip to content

Commit

Permalink
feat(konnect): add consumer credentials secrets reconciler for API Ke…
Browse files Browse the repository at this point in the history
…y credentials (#1168)
  • Loading branch information
pmalek authored Feb 19, 2025
1 parent e4aef74 commit a8590e8
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
Users can now specify credentials for `KongConsumer`s in `Secret`s and reference
them in `KongConsumer`s' `credentials` field.
- `basic-auth` [#1120](https://github.com/Kong/gateway-operator/pull/1120)
- `key-auth` [#1168](https://github.com/Kong/gateway-operator/pull/1168)
- Added prometheus metrics for Konnect entity operations in the metrics server:
- `gateway_operator_konnect_entity_operation_count` for number of operations.
- `gateway_operator_konnect_entity_operation_duration_milliseconds` for duration of operations.
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/role/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ rules:
- kongconsumergroups
- kongconsumers
- kongcredentialacls
- kongcredentialapikeys
- kongcredentialhmacs
- kongcredentialjwts
- kongdataplaneclientcertificates
Expand Down Expand Up @@ -201,6 +200,7 @@ rules:
- apiGroups:
- configuration.konghq.com
resources:
- kongcredentialapikeys
- kongcredentialbasicauths
- kongpluginbindings
- kongplugins
Expand Down
47 changes: 47 additions & 0 deletions config/samples/konnect_kongconsumer_apikey_secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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-key-auth
namespace: default
spec:
name: test-cp-key-auth
labels:
app: test-cp-key-auth
key1: test-cp-key-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-key-auth
credentials:
- consumer1-key-auth1
---
kind: Secret
apiVersion: v1
metadata:
name: consumer1-key-auth1
namespace: default
labels:
konghq.com/credential: key-auth
stringData:
key: key
6 changes: 5 additions & 1 deletion controller/konnect/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import (
// SupportedCredentialType is a generic type constraint that all Kong credential
// types must implement.
type SupportedCredentialType interface {
configurationv1alpha1.KongCredentialBasicAuth
configurationv1alpha1.KongCredentialBasicAuth |
configurationv1alpha1.KongCredentialAPIKey
// TODO: add other credential types
// TODO: https://github.com/Kong/gateway-operator/issues/1124
// TODO: https://github.com/Kong/gateway-operator/issues/1125
// TODO: https://github.com/Kong/gateway-operator/issues/1126

GetTypeName() string
}
Expand Down
25 changes: 25 additions & 0 deletions controller/konnect/index_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package konnect

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

"github.com/kong/gateway-operator/controller/konnect/constraints"
)

func kongCredentialReferencesSecret[
T constraints.SupportedCredentialType,
TEnt constraints.KongCredential[T],
](obj client.Object) []string {
cred, ok := obj.(TEnt)
if !ok {
return nil
}

var ret []string
for _, or := range cred.GetOwnerReferences() {
if or.Kind == "Secret" {
ret = append(ret, or.Name)
}
}
return ret
}
7 changes: 7 additions & 0 deletions controller/konnect/index_credentials_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
const (
// IndexFieldKongCredentialAPIKeyReferencesKongConsumer is the index name for KongCredentialAPIKey -> Consumer.
IndexFieldKongCredentialAPIKeyReferencesKongConsumer = "kongCredentialsAPIKeyConsumerRef"
// IndexFieldKongCredentialAPIKeyReferencesSecret is the index name for KongCredentialAPIKey -> Secret.
IndexFieldKongCredentialAPIKeyReferencesSecret = "kongCredentialsAPIKeySecretRef"
)

// IndexOptionsForCredentialsAPIKey returns required Index options for KongCredentialAPIKey.
Expand All @@ -19,6 +21,11 @@ func IndexOptionsForCredentialsAPIKey() []ReconciliationIndexOption {
IndexField: IndexFieldKongCredentialAPIKeyReferencesKongConsumer,
ExtractValue: kongCredentialAPIKeyReferencesConsumer,
},
{
IndexObject: &configurationv1alpha1.KongCredentialAPIKey{},
IndexField: IndexFieldKongCredentialAPIKeyReferencesSecret,
ExtractValue: kongCredentialReferencesSecret[configurationv1alpha1.KongCredentialAPIKey],
},
}
}

Expand Down
19 changes: 1 addition & 18 deletions controller/konnect/index_credentials_basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func IndexOptionsForCredentialsBasicAuth() []ReconciliationIndexOption {
{
IndexObject: &configurationv1alpha1.KongCredentialBasicAuth{},
IndexField: IndexFieldKongCredentialBasicAuthReferencesSecret,
ExtractValue: kongCredentialBasicAuthReferencesSecret,
ExtractValue: kongCredentialReferencesSecret[configurationv1alpha1.KongCredentialBasicAuth],
},
}
}
Expand All @@ -37,20 +37,3 @@ func kongCredentialBasicAuthReferencesConsumer(obj client.Object) []string {
}
return []string{cred.Spec.ConsumerRef.Name}
}

// kongCredentialBasicAuthReferencesSecret returns the name of Secret which was
// used to populate this (managed) credential resource.
func kongCredentialBasicAuthReferencesSecret(obj client.Object) []string {
cred, ok := obj.(*configurationv1alpha1.KongCredentialBasicAuth)
if !ok {
return nil
}

var ret []string
for _, or := range cred.OwnerReferences {
if or.Kind == "Secret" {
ret = append(ret, or.Name)
}
}
return ret
}
Loading

0 comments on commit a8590e8

Please sign in to comment.