Skip to content

Commit

Permalink
[release-1.14] fix: virtual secrets created from netspec have name an…
Browse files Browse the repository at this point in the history
…d namespace (#4006)

* fix: virtual secrets created from netspec have name and namespace

Signed-off-by: Calum Murray <[email protected]>

* cleanup: return a secret instead of data, name, namespace

Signed-off-by: Calum Murray <[email protected]>

---------

Signed-off-by: Calum Murray <[email protected]>
Co-authored-by: Calum Murray <[email protected]>
  • Loading branch information
knative-prow-robot and Cali0707 authored Jul 24, 2024
1 parent 12d1f32 commit 4844e64
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
31 changes: 26 additions & 5 deletions control-plane/pkg/security/secrets_provider_net_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package security
import (
"context"
"fmt"
"sort"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corelisters "k8s.io/client-go/listers/core/v1"
bindings "knative.dev/eventing-kafka-broker/control-plane/pkg/apis/bindings/v1beta1"

Expand Down Expand Up @@ -53,21 +56,21 @@ func ResolveAuthContextFromNetSpec(lister corelisters.SecretLister, namespace st
return nil, err
}
}
references, virtualSecretData := toContract(securityFields)
references, virtualSecret := toContract(securityFields)
multiSecretReference := &contract.MultiSecretReference{
Protocol: getProtocolContractFromNetSpec(netSpec),
References: references,
}
virtualSecretData[ProtocolKey] = []byte(getProtocolFromNetSpec(netSpec))
virtualSecret.Data[ProtocolKey] = []byte(getProtocolFromNetSpec(netSpec))

authContext := &NetSpecAuthContext{
VirtualSecret: &corev1.Secret{Data: virtualSecretData},
VirtualSecret: &virtualSecret,
MultiSecretReference: multiSecretReference,
}
return authContext, nil
}

func toContract(securityFields []*securityField) ([]*contract.SecretReference, map[string][]byte) {
func toContract(securityFields []*securityField) ([]*contract.SecretReference, corev1.Secret) {
virtualSecretData := make(map[string][]byte)
bySecretName := make(map[string][]securityField)
for _, f := range securityFields {
Expand All @@ -79,6 +82,8 @@ func toContract(securityFields []*securityField) ([]*contract.SecretReference, m
}

refs := make([]*contract.SecretReference, 0, 6 /* max number of secrets */)
names := make([]string, 0, 6)
namespaces := make([]string, 0, 6)
for secretName, securityFields := range bySecretName {
keyFieldReferences := make([]*contract.KeyFieldReference, 0, len(securityFields))
for _, f := range securityFields {
Expand All @@ -97,8 +102,16 @@ func toContract(securityFields []*securityField) ([]*contract.SecretReference, m
},
KeyFieldReferences: keyFieldReferences,
})
names = append(names, any.secret.Name)
namespaces = append(namespaces, any.secret.Namespace)
}
return refs, corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: stableConcat(names),
Namespace: stableConcat(namespaces),
},
Data: virtualSecretData,
}
return refs, virtualSecretData
}

type securityField struct {
Expand Down Expand Up @@ -160,3 +173,11 @@ func resolveSecret(lister corelisters.SecretLister, ns string, ref *corev1.Secre
}
return value, secret, nil
}

func stableConcat(elements []string) string {
sort.SliceStable(elements, func(i, j int) bool {
return elements[i] < elements[j]
})

return strings.Join(elements, "")
}
24 changes: 22 additions & 2 deletions control-plane/pkg/security/secrets_provider_net_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func TestResolveAuthContextFromNetSpec(t *testing.T) {
SaslMechanismKey: SaslScramSha512,
SaslUserKey: "key",
SaslPasswordKey: "key",
}},
}, ObjectMeta: metav1.ObjectMeta{
Name: "cacertcertkeypasswordtypeuser",
Namespace: "nsnsnsnsnsns",
},
},
MultiSecretReference: &contract.MultiSecretReference{
Protocol: contract.Protocol_SASL_SSL,
References: []*contract.SecretReference{
Expand Down Expand Up @@ -249,7 +253,11 @@ func TestResolveAuthContextFromNetSpec(t *testing.T) {
SaslMechanismKey: SaslScramSha256,
SaslUserKey: "key",
SaslPasswordKey: "key",
}},
}, ObjectMeta: metav1.ObjectMeta{
Name: "cacertcertkeypasswordtypeuser",
Namespace: "nsnsnsnsnsns",
},
},
MultiSecretReference: &contract.MultiSecretReference{
Protocol: contract.Protocol_SASL_SSL,
References: []*contract.SecretReference{
Expand Down Expand Up @@ -339,6 +347,9 @@ func TestResolveAuthContextFromNetSpec(t *testing.T) {
CaCertificateKey: "key",
UserCertificate: "key",
UserKey: "key",
}, ObjectMeta: metav1.ObjectMeta{
Name: "cacertcertkey",
Namespace: "nsnsns",
}},
MultiSecretReference: &contract.MultiSecretReference{
Protocol: contract.Protocol_SSL,
Expand Down Expand Up @@ -400,6 +411,9 @@ func TestResolveAuthContextFromNetSpec(t *testing.T) {
ProtocolKey: ProtocolSSL,
UserCertificate: "key",
UserKey: "key",
}, ObjectMeta: metav1.ObjectMeta{
Name: "certkey",
Namespace: "nsns",
}},
MultiSecretReference: &contract.MultiSecretReference{
Protocol: contract.Protocol_SSL,
Expand Down Expand Up @@ -466,6 +480,9 @@ func TestResolveAuthContextFromNetSpec(t *testing.T) {
SaslMechanismKey: SaslScramSha256,
SaslUserKey: "key",
SaslPasswordKey: "key",
}, ObjectMeta: metav1.ObjectMeta{
Name: "passwordtypeuser",
Namespace: "nsnsns",
}},
MultiSecretReference: &contract.MultiSecretReference{
Protocol: contract.Protocol_SASL_PLAINTEXT,
Expand Down Expand Up @@ -538,6 +555,9 @@ func TestResolveAuthContextFromNetSpec(t *testing.T) {
SaslMechanismKey: SaslScramSha512,
SaslUserKey: "key",
SaslPasswordKey: "key",
}, ObjectMeta: metav1.ObjectMeta{
Name: "passwordtypeuser",
Namespace: "nsnsns",
}},
MultiSecretReference: &contract.MultiSecretReference{
Protocol: contract.Protocol_SASL_PLAINTEXT,
Expand Down

0 comments on commit 4844e64

Please sign in to comment.