Skip to content

Commit abba035

Browse files
authored
Fix unique name generator for auto-created EventType (#7160)
* fix: Fix unique name generator for auto-created EventType * Fix nolint:gosec * Remove leftover variable
1 parent 8eaf932 commit abba035

File tree

2 files changed

+107
-9
lines changed

2 files changed

+107
-9
lines changed

pkg/eventtype/eventtypes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package eventtype
1818

1919
import (
2020
"context"
21-
"encoding/base64"
21+
"crypto/md5" //nolint:gosec
2222
"fmt"
2323

2424
"github.com/cloudevents/sdk-go/v2/event"
@@ -44,14 +44,14 @@ type EventTypeAutoHandler struct {
4444
Logger *zap.Logger
4545
}
4646

47-
// generateEventTypeName is a pseudo unique name for EvenType object based on on the input params
47+
// generateEventTypeName is a pseudo unique name for EvenType object based on the input params
4848
func generateEventTypeName(name, namespace, eventType, eventSource string) string {
4949
suffixParts := eventType + eventSource + namespace + name
50-
suffix := base64.StdEncoding.EncodeToString([]byte(suffixParts))[:10]
51-
return utils.ToDNS1123Subdomain(fmt.Sprintf("%s-%s-%s", "et", name, suffix))
50+
suffix := md5.Sum([]byte(suffixParts)) //nolint:gosec
51+
return utils.ToDNS1123Subdomain(fmt.Sprintf("%s-%s-%x", "et", name, suffix))
5252
}
5353

54-
// AutoCreateEventType creates EventType object based on processed events's types from addressable KReference objects
54+
// AutoCreateEventType creates EventType object based on processed event's types from addressable KReference objects
5555
func (h *EventTypeAutoHandler) AutoCreateEventType(ctx context.Context, event *event.Event, addressable *duckv1.KReference, ownerUID types.UID) error {
5656
// Feature flag gate
5757
if !h.FeatureStore.IsEnabled(feature.EvenTypeAutoCreate) {

pkg/eventtype/eventtypes_test.go

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,21 @@ func TestEventTypeAutoHandler_GenerateEventTypeName(t *testing.T) {
129129
namespace: "default",
130130
eventType: "events.type",
131131
eventSource: "events.source",
132-
expectedName: "et-example-zxzlbnrzln",
132+
expectedName: "et-example-eaed2996d719048dfae11b607d68b7ad",
133133
},
134134
{
135135
name: "EXAMPLE",
136136
namespace: "default",
137137
eventType: "events.type",
138138
eventSource: "events.source",
139-
expectedName: "et-example-zxzlbnrzln",
139+
expectedName: "et-example-f4d9f28cdbe397fe92eb5899e7294121",
140140
},
141141
{
142-
name: "emptyName",
142+
name: "",
143143
namespace: "default",
144144
eventType: "events.type",
145145
eventSource: "events.source",
146-
expectedName: "et-emptyname-zxzlbnrzln",
146+
expectedName: "et--72f2dad9d914dcd0445cae74ba99d15c",
147147
},
148148
}
149149

@@ -158,6 +158,104 @@ func TestEventTypeAutoHandler_GenerateEventTypeName(t *testing.T) {
158158
}
159159
}
160160

161+
func TestEventTypeAutoHandler_GenerateEventTypeNameUnique(t *testing.T) {
162+
type eventTypeMock struct {
163+
name string
164+
namespace string
165+
eventType string
166+
eventSource string
167+
}
168+
169+
testCases := []struct {
170+
name string
171+
eventTypeA eventTypeMock
172+
eventTypeB eventTypeMock
173+
expectedUnique bool
174+
}{
175+
{
176+
name: "Long equal name of addressable",
177+
eventTypeA: eventTypeMock{
178+
name: "long-addressable-name",
179+
namespace: "default",
180+
eventType: "com.example.organization.events.typeA",
181+
eventSource: "events.source.A",
182+
},
183+
eventTypeB: eventTypeMock{
184+
name: "long-addressable-name",
185+
namespace: "default",
186+
eventType: "com.example.organization.events.typeB",
187+
eventSource: "events.source.B",
188+
},
189+
expectedUnique: true,
190+
},
191+
{
192+
name: "Long equal name & same source",
193+
eventTypeA: eventTypeMock{
194+
name: "long-addressable-name",
195+
namespace: "default",
196+
eventType: "com.example.organization.events.typeA",
197+
eventSource: "events.source.A",
198+
},
199+
eventTypeB: eventTypeMock{
200+
name: "long-addressable-name",
201+
namespace: "default",
202+
eventType: "com.example.organization.events.typeB",
203+
eventSource: "events.source.A",
204+
},
205+
expectedUnique: true,
206+
},
207+
{
208+
name: "Long name with one char diff",
209+
eventTypeA: eventTypeMock{
210+
name: "long-addressable-nameA",
211+
namespace: "default",
212+
eventType: "com.example.organization.events.typeA",
213+
eventSource: "events.source.A",
214+
},
215+
eventTypeB: eventTypeMock{
216+
name: "long-addressable-nameB",
217+
namespace: "default",
218+
eventType: "com.example.organization.events.typeA",
219+
eventSource: "events.source.B",
220+
},
221+
expectedUnique: true,
222+
},
223+
{
224+
name: "All same input params are not unique",
225+
eventTypeA: eventTypeMock{
226+
name: "long-addressable-name",
227+
namespace: "default",
228+
eventType: "com.example.organization.events.typeA",
229+
eventSource: "events.source.A",
230+
},
231+
eventTypeB: eventTypeMock{
232+
name: "long-addressable-name",
233+
namespace: "default",
234+
eventType: "com.example.organization.events.typeA",
235+
eventSource: "events.source.A",
236+
},
237+
expectedUnique: false,
238+
},
239+
}
240+
241+
for _, tc := range testCases {
242+
t.Run(tc.name, func(t *testing.T) {
243+
resultA := generateEventTypeName(tc.eventTypeA.name, tc.eventTypeA.namespace, tc.eventTypeA.eventType, tc.eventTypeA.eventSource)
244+
resultB := generateEventTypeName(tc.eventTypeB.name, tc.eventTypeB.namespace, tc.eventTypeB.eventType, tc.eventTypeB.eventSource)
245+
246+
if tc.expectedUnique {
247+
if resultA == resultB {
248+
t.Errorf("test case '%s', generated name '%s' x '%s' must be unique", tc.name, resultA, resultB)
249+
}
250+
} else {
251+
if resultA != resultB {
252+
t.Errorf("test case '%s', generated name '%s' x '%s' must be equal", tc.name, resultA, resultB)
253+
}
254+
}
255+
})
256+
}
257+
}
258+
161259
func initFeatureStore(t *testing.T, enabled string) *feature.Store {
162260
featureStore := feature.NewStore(logtesting.TestLogger(t))
163261
cm := resources.ConfigMap(

0 commit comments

Comments
 (0)