Skip to content

Commit 7fbef3f

Browse files
pmalekmlavacca
andauthored
feat(konnect): add KongCredentialSecretReconciler to reconcile consumer Secrets with basic-auth credentials and create KongCredentialBasicAuth in response (#1120)
* feat(konnect): add KongCredentialSecretReconciler to reconcile consumer Secrets and create Credential resources in response * tests: fix mock matcher * Apply suggestions from code review Co-authored-by: Mattia Lavacca <lavacca.mattia@gmail.com> * chore: fix typo in controller/konnect/ops/credenetialbasicauth.go * Update controller/konnect/reconciler_credential_secrets.go Co-authored-by: Mattia Lavacca <lavacca.mattia@gmail.com> * Apply suggestions from code review Co-authored-by: Mattia Lavacca <lavacca.mattia@gmail.com> * chore: hardcode Secret entity name * chore: add TODOs for other credential types * chore: add TODOs for other credential types * fix: add secret label selector for secrets to be reconciled in credential secrets reconciler * fix: fix deleting credentials when consumer credential secret link is removed * chore: add godoc for IndexFieldKongCredentialBasicAuthReferencesSecret * fix: fix filterKongCredentials --------- Co-authored-by: Mattia Lavacca <lavacca.mattia@gmail.com>
1 parent 9d01505 commit 7fbef3f

22 files changed

+1445
-26
lines changed

.golangci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ issues:
162162
- linters:
163163
- forbidigo
164164
text: 'use of `.*(Create|Delete)Dataplane.+` forbidden because "Please use camel case'
165+
- path: .*_test\.go
166+
linters:
167+
- gosec
168+
text: 'Use of weak random number generator'

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
It requires the experimental version of Gateway API (as of v1.2.x) installed in
5454
your cluster, and the flag `--enable-gateway-api-experimental` set.
5555
[#1010](https://github.com/Kong/gateway-operator/pull/1010)
56+
- Added support for `KongConsumer` `credentials` in Konnect entities support.
57+
Users can now specify credentials for `KongConsumer`s in `Secret`s and reference
58+
them in `KongConsumer`s' `credentials` field.
59+
- `basic-auth` [#1120](https://github.com/Kong/gateway-operator/pull/1120)
5660

5761
### Changed
5862

config/rbac/role/role.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ rules:
124124
- kongconsumers
125125
- kongcredentialacls
126126
- kongcredentialapikeys
127-
- kongcredentialbasicauths
128127
- kongcredentialhmacs
129128
- kongcredentialjwts
130129
- kongdataplaneclientcertificates
@@ -202,6 +201,7 @@ rules:
202201
- apiGroups:
203202
- configuration.konghq.com
204203
resources:
204+
- kongcredentialbasicauths
205205
- kongpluginbindings
206206
- kongplugins
207207
verbs:
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
credentials:
37+
- consumer1-basic-auth1
38+
---
39+
kind: Secret
40+
apiVersion: v1
41+
metadata:
42+
name: consumer1-basic-auth1
43+
namespace: default
44+
labels:
45+
konghq.com/credential: basic-auth
46+
stringData:
47+
username: username
48+
password: pass

controller/konnect/constraints/constraints.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ import (
1010
konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1"
1111
)
1212

13+
// SupportedCredentialType is a generic type constraint that all Kong credential
14+
// types must implement.
15+
type SupportedCredentialType interface {
16+
configurationv1alpha1.KongCredentialBasicAuth
17+
// TODO: add other credential types
18+
19+
GetTypeName() string
20+
}
21+
22+
// KongCredential is a generic type constraint that all Kong credential types
23+
// must implement.
24+
type KongCredential[T SupportedCredentialType] interface {
25+
*T
26+
client.Object
27+
GetConditions() []metav1.Condition
28+
SetConditions([]metav1.Condition)
29+
}
30+
1331
// SupportedKonnectEntityType is an interface that all Konnect entity types
1432
// must implement.
1533
type SupportedKonnectEntityType interface {

controller/konnect/index_credentials_basicauth.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
const (
1010
// IndexFieldKongCredentialBasicAuthReferencesKongConsumer is the index name for KongCredentialBasicAuth -> Consumer.
1111
IndexFieldKongCredentialBasicAuthReferencesKongConsumer = "kongCredentialsBasicAuthConsumerRef"
12+
// IndexFieldKongCredentialBasicAuthReferencesSecret is the index name for KongCredentialBasicAuth -> Secret.
13+
IndexFieldKongCredentialBasicAuthReferencesSecret = "kongCredentialsBasicAuthSecretRef"
1214
)
1315

1416
// IndexOptionsForCredentialsBasicAuth returns required Index options for KongCredentialBasicAuth.
@@ -19,6 +21,11 @@ func IndexOptionsForCredentialsBasicAuth() []ReconciliationIndexOption {
1921
IndexField: IndexFieldKongCredentialBasicAuthReferencesKongConsumer,
2022
ExtractValue: kongCredentialBasicAuthReferencesConsumer,
2123
},
24+
{
25+
IndexObject: &configurationv1alpha1.KongCredentialBasicAuth{},
26+
IndexField: IndexFieldKongCredentialBasicAuthReferencesSecret,
27+
ExtractValue: kongCredentialBasicAuthReferencesSecret,
28+
},
2229
}
2330
}
2431

@@ -30,3 +37,20 @@ func kongCredentialBasicAuthReferencesConsumer(obj client.Object) []string {
3037
}
3138
return []string{cred.Spec.ConsumerRef.Name}
3239
}
40+
41+
// kongCredentialBasicAuthReferencesSecret returns the name of Secret which was
42+
// used to populate this (managed) credential resource.
43+
func kongCredentialBasicAuthReferencesSecret(obj client.Object) []string {
44+
cred, ok := obj.(*configurationv1alpha1.KongCredentialBasicAuth)
45+
if !ok {
46+
return nil
47+
}
48+
49+
var ret []string
50+
for _, or := range cred.OwnerReferences {
51+
if or.Kind == "Secret" {
52+
ret = append(ret, or.Name)
53+
}
54+
}
55+
return ret
56+
}

controller/konnect/index_kongconsumer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const (
1414
IndexFieldKongConsumerOnPlugin = "consumerPluginRef"
1515
// IndexFieldKongConsumerOnKonnectGatewayControlPlane is the index field for KongConsumer -> KonnectGatewayControlPlane.
1616
IndexFieldKongConsumerOnKonnectGatewayControlPlane = "consumerKonnectGatewayControlPlaneRef"
17+
// IndexFieldKongConsumerReferencesSecrets is the index field for Consumer -> Secret.
18+
IndexFieldKongConsumerReferencesSecrets = "kongConsumerSecretRef"
1719
)
1820

1921
// IndexOptionsForKongConsumer returns required Index options for KongConsumer reconciler.
@@ -34,6 +36,11 @@ func IndexOptionsForKongConsumer(cl client.Client) []ReconciliationIndexOption {
3436
IndexField: IndexFieldKongConsumerOnKonnectGatewayControlPlane,
3537
ExtractValue: indexKonnectGatewayControlPlaneRef[configurationv1.KongConsumer](cl),
3638
},
39+
{
40+
IndexObject: &configurationv1.KongConsumer{},
41+
IndexField: IndexFieldKongConsumerReferencesSecrets,
42+
ExtractValue: kongConsumerReferencesSecrets,
43+
},
3744
}
3845
}
3946

@@ -52,3 +59,12 @@ func kongConsumerReferencesKongPluginsViaAnnotation(object client.Object) []stri
5259
}
5360
return metadata.ExtractPluginsWithNamespaces(consumer)
5461
}
62+
63+
// kongConsumerReferencesSecret returns name of referenced Secrets.
64+
func kongConsumerReferencesSecrets(obj client.Object) []string {
65+
consumer, ok := obj.(*configurationv1.KongConsumer)
66+
if !ok {
67+
return nil
68+
}
69+
return consumer.Credentials
70+
}

0 commit comments

Comments
 (0)