Skip to content

Commit

Permalink
Add option to disable topic validation (#271)
Browse files Browse the repository at this point in the history
* Add option to disable topic validation

Signed-off-by: yaron2 <[email protected]>

* linter

Signed-off-by: yaron2 <[email protected]>
  • Loading branch information
yaron2 authored Apr 1, 2022
1 parent f1f5c7a commit f0e0931
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions service/common/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ type Subscription struct {
Match string `json:"match"`
// Priority is the priority in which to evaluate the match (lower to higher).
Priority int `json:"priority"`
// DisableTopicValidation allows to receive events from publisher topics that differ from the subscribed topic.
DisableTopicValidation bool `json:"disableTopicValidation"`
}

const (
Expand Down
12 changes: 11 additions & 1 deletion service/grpc/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@ func (s *Server) OnTopicEvent(ctx context.Context, in *pb.TopicEventRequest) (*p
return &pb.TopicEventResponse{Status: pb.TopicEventResponse_DROP}, errors.New("pub/sub and topic names required")
}
key := in.PubsubName + "-" + in.Topic
if sub, ok := s.topicRegistrar[key]; ok {
noValidationKey := in.PubsubName

var sub *internal.TopicRegistration
var ok bool

sub, ok = s.topicRegistrar[key]
if !ok {
sub, ok = s.topicRegistrar[noValidationKey]
}

if ok {
data := interface{}(in.Data)
if len(in.Data) > 0 {
mediaType, _, err := mime.ParseMediaType(in.DataContentType)
Expand Down
30 changes: 30 additions & 0 deletions service/grpc/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,36 @@ func TestTopic(t *testing.T) {
stopTestServer(t, server)
}

func TestTopicWithValidationDisabled(t *testing.T) {
ctx := context.Background()

sub := &common.Subscription{
PubsubName: "messages",
Topic: "*",
DisableTopicValidation: true,
}
server := getTestServer()

err := server.AddTopicEventHandler(sub, eventHandler)
assert.Nil(t, err)

startTestServer(server)

in := &runtime.TopicEventRequest{
Id: "a123",
Source: "test",
Type: "test",
SpecVersion: "v1.0",
DataContentType: "text/plain",
Data: []byte("test"),
Topic: "test",
PubsubName: sub.PubsubName,
}

_, err = server.OnTopicEvent(ctx, in)
assert.NoError(t, err)
}

func TestTopicWithErrors(t *testing.T) {
ctx := context.Background()

Expand Down
9 changes: 8 additions & 1 deletion service/internal/topicregistrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ func (m TopicRegistrar) AddSubscription(sub *common.Subscription, fn common.Topi
if fn == nil {
return fmt.Errorf("topic handler required")
}
key := sub.PubsubName + "-" + sub.Topic

var key string
if !sub.DisableTopicValidation {
key = sub.PubsubName + "-" + sub.Topic
} else {
key = sub.PubsubName
}

ts, ok := m[key]
if !ok {
ts = &TopicRegistration{
Expand Down

0 comments on commit f0e0931

Please sign in to comment.