Skip to content

Commit

Permalink
feat(konnect): add KongKey - KongKeySet binding
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Oct 1, 2024
1 parent c2e9bc2 commit 9545cf0
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 87 deletions.
61 changes: 61 additions & 0 deletions config/samples/konnect_kongkeyset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,64 @@ spec:
name: key-set-1
tags:
- production
---
kind: KongKey
apiVersion: configuration.konghq.com/v1alpha1
metadata:
name: key-1
namespace: default
annotations:
konghq.com/tags: "infra"
spec:
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
name: test1
keySetRef:
type: namespacedRef
namespacedRef:
name: key-set-1
tags:
- production
kid: kid
name: key-1
pem:
private_key: |
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCdLY9b3Pb/Fsxo
cIJAKxZQKEtqTU8k72BOvMBtzcIN9ekGFrUYB8GQgIJsPIUw4+c4XK99YNh0tVBE
/9W8OdyXwopzqNn+nRfrhXpxDu+BVvjQ/AENAHKqg8pJKhNTd4W6dAzxelLO/t7y
rlXbjGX/Ry/3ednPq6PpDcxvqgc+v7Rcmh+5dEKdIoIrppjUc2X38+LXcy9xOuML
FtxNtx+NB+5bNq31eooT9OKk3r7mA0gX4Su2DnIL+SLsdTIb0dnCBIydUpbLdYfd
dew1UGy2XtlWsxux3zoXjGe+RBtndUzPBvyb/k6g2QFAaIEwndPbwQ4fi9y4FrB7
hqjQa+OLAgMBAAECggEAMkWruCydHarLl04BAwgk+19f+7Cdc0lTCuwJOjWY70Er
loR1yKlWamMIFBlpWmFSh67xfE8Y/H8vnNodITZ6jVmuUd78VpklWPHY30dxKHPK
YoFvzppJkqtTbIJWKxir/551s1i2GrnfUkybbnzh9Lvuph9loKwb4YNF06NU7OcA
tgCk78oA/JpVa01PCJYmVy8zI4UERt/2mBzuummk8kJhPl+A7K9gVk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G9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnS2PW9z2/xbMaHCCQCsW
UChLak1PJO9gTrzAbc3CDfXpBha1GAfBkICCbDyFMOPnOFyvfWDYdLVQRP/VvDnc
l8KKc6jZ/p0X64V6cQ7vgVb40PwBDQByqoPKSSoTU3eFunQM8XpSzv7e8q5V24xl
/0cv93nZz6uj6Q3Mb6oHPr+0XJofuXRCnSKCK6aY1HNl9/Pi13MvcTrjCxbcTbcf
jQfuWzat9XqKE/TipN6+5gNIF+Ertg5yC/ki7HUyG9HZwgSMnVKWy3WH3XXsNVBs
tl7ZVrMbsd86F4xnvkQbZ3VMzwb8m/5OoNkBQGiBMJ3T28EOH4vcuBawe4ao0Gvj
iwIDAQAB
-----END PUBLIC KEY-----
14 changes: 14 additions & 0 deletions controller/konnect/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ const (
KongUpstreamRefReasonInvalid = "Invalid"
)

const (
// KeySetRefValidConditionType is the type of the condition that indicates
// whether the KeySet reference is valid and points to an existing
// KeySet.
KeySetRefValidConditionType = "KeySetRefValid"

// KeySetRefReasonValid is the reason used with the KeySetRefValid
// condition type indicating that the KeySet reference is valid.
KeySetRefReasonValid = "Valid"
// KeySetRefReasonInvalid is the reason used with the KeySetRefValid
// condition type indicating that the KeySet reference is invalid.
KeySetRefReasonInvalid = "Invalid"
)

const (
// KongCertificateRefValidConditionType is the type of the condition that indicates
// whether the KongCertificate reference is valid and points to an existing KongCertificate
Expand Down
8 changes: 8 additions & 0 deletions controller/konnect/ops/ops_kongkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func kongKeyToKeyInput(key *configurationv1alpha1.KongKey) sdkkonnectcomp.KeyInp
specTags = key.Spec.Tags
k8sMetaTags = GenerateKubernetesMetadataTags(key)
)

k := sdkkonnectcomp.KeyInput{
Jwk: key.Spec.JWK,
Kid: key.Spec.KID,
Expand All @@ -151,5 +152,12 @@ func kongKeyToKeyInput(key *configurationv1alpha1.KongKey) sdkkonnectcomp.KeyInp
PublicKey: lo.ToPtr(key.Spec.PEM.PublicKey),
}
}
if konnectStatus := key.Status.Konnect; konnectStatus != nil {
if keySetID := konnectStatus.GetKeySetID(); keySetID != "" {
k.Set = &sdkkonnectcomp.Set{
ID: lo.ToPtr(konnectStatus.GetKeySetID()),
}
}
}
return k
}
166 changes: 125 additions & 41 deletions controller/konnect/ops/ops_kongkey_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ops

import (
"sort"
"testing"

"github.com/google/uuid"
sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -12,55 +13,138 @@ import (
konnectconsts "github.com/kong/gateway-operator/controller/konnect/consts"

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

func TestKongKeyToKeyInput(t *testing.T) {
key := &configurationv1alpha1.KongKey{
TypeMeta: metav1.TypeMeta{
Kind: "KongKey",
APIVersion: "configuration.konghq.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "key-1",
Namespace: "default",
Generation: 2,
UID: k8stypes.UID(uuid.NewString()),
Annotations: map[string]string{
konnectconsts.AnnotationTags: "tag1,tag2,duplicate",
testCases := []struct {
name string
key *configurationv1alpha1.KongKey
expectedOutput sdkkonnectcomp.KeyInput
}{
{
name: "kong key with all fields set without key set",
key: &configurationv1alpha1.KongKey{
TypeMeta: metav1.TypeMeta{
Kind: "KongKey",
APIVersion: "configuration.konghq.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "key-1",
Namespace: "default",
Generation: 2,
UID: k8stypes.UID("key-uid"),
Annotations: map[string]string{
konnectconsts.AnnotationTags: "tag1,tag2,duplicate",
},
},
Spec: configurationv1alpha1.KongKeySpec{
KongKeyAPISpec: configurationv1alpha1.KongKeyAPISpec{
KID: "kid",
Name: lo.ToPtr("name"),
JWK: lo.ToPtr("jwk"),
PEM: &configurationv1alpha1.PEMKeyPair{
PublicKey: "public",
PrivateKey: "private",
},
Tags: []string{"tag3", "tag4", "duplicate"},
},
},
},
expectedOutput: sdkkonnectcomp.KeyInput{
Kid: "kid",
Name: lo.ToPtr("name"),
Jwk: lo.ToPtr("jwk"),
Pem: &sdkkonnectcomp.Pem{
PublicKey: lo.ToPtr("public"),
PrivateKey: lo.ToPtr("private"),
},
Tags: []string{
"duplicate",
"k8s-generation:2",
"k8s-group:configuration.konghq.com",
"k8s-kind:KongKey",
"k8s-name:key-1",
"k8s-namespace:default",
"k8s-uid:key-uid",
"k8s-version:v1alpha1",
"tag1",
"tag2",
"tag3",
"tag4",
},
},
},
Spec: configurationv1alpha1.KongKeySpec{
KongKeyAPISpec: configurationv1alpha1.KongKeyAPISpec{
KID: "kid",
{
name: "kong key with all fields set with key set",
key: &configurationv1alpha1.KongKey{
TypeMeta: metav1.TypeMeta{
Kind: "KongKey",
APIVersion: "configuration.konghq.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "key-1",
Namespace: "default",
Generation: 2,
UID: k8stypes.UID("key-uid"),
Annotations: map[string]string{
konnectconsts.AnnotationTags: "tag1,tag2,duplicate",
},
},
Spec: configurationv1alpha1.KongKeySpec{
KongKeyAPISpec: configurationv1alpha1.KongKeyAPISpec{
KID: "kid",
Name: lo.ToPtr("name"),
JWK: lo.ToPtr("jwk"),
PEM: &configurationv1alpha1.PEMKeyPair{
PublicKey: "public",
PrivateKey: "private",
},
Tags: []string{"tag3", "tag4", "duplicate"},
},
},
Status: configurationv1alpha1.KongKeyStatus{
Konnect: &konnectv1alpha1.KonnectEntityStatusWithControlPlaneAndKeySetRef{
KeySetID: "key-set-id",
},
},
},
expectedOutput: sdkkonnectcomp.KeyInput{
Kid: "kid",
Name: lo.ToPtr("name"),
JWK: lo.ToPtr("jwk"),
PEM: &configurationv1alpha1.PEMKeyPair{
PublicKey: "public",
PrivateKey: "private",
Jwk: lo.ToPtr("jwk"),
Pem: &sdkkonnectcomp.Pem{
PublicKey: lo.ToPtr("public"),
PrivateKey: lo.ToPtr("private"),
},
Set: &sdkkonnectcomp.Set{
ID: lo.ToPtr("key-set-id"),
},
Tags: []string{
"duplicate",
"k8s-generation:2",
"k8s-group:configuration.konghq.com",
"k8s-kind:KongKey",
"k8s-name:key-1",
"k8s-namespace:default",
"k8s-uid:key-uid",
"k8s-version:v1alpha1",
"tag1",
"tag2",
"tag3",
"tag4",
},
Tags: []string{"tag3", "tag4", "duplicate"},
},
},
}
output := kongKeyToKeyInput(key)
expectedTags := []string{
"k8s-generation:2",
"k8s-kind:KongKey",
"k8s-name:key-1",
"k8s-uid:" + string(key.GetUID()),
"k8s-version:v1alpha1",
"k8s-group:configuration.konghq.com",
"k8s-namespace:default",
"tag1",
"tag2",
"tag3",
"tag4",
"duplicate",

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output := kongKeyToKeyInput(tc.key)

// Tags order is not guaranteed, so we need to sort them before comparing.
sort.Strings(output.Tags)
require.Equal(t, tc.expectedOutput, output)
})
}
require.ElementsMatch(t, expectedTags, output.Tags)
require.Equal(t, "kid", output.Kid)
require.Equal(t, "name", *output.Name)
require.Equal(t, "jwk", *output.Jwk)
require.Equal(t, "public", *output.Pem.PublicKey)
require.Equal(t, "private", *output.Pem.PrivateKey)
}
Loading

0 comments on commit 9545cf0

Please sign in to comment.