Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Validate protos on both client and server
Browse files Browse the repository at this point in the history
Also includes automatic (optional) Unmarshal of Message
  • Loading branch information
htdvisser committed Feb 3, 2017
1 parent 2c3b1c8 commit 722d5c0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
14 changes: 14 additions & 0 deletions api/broker/client_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func NewMonitoredRouterStream(client BrokerClient, getContextFunc func() context
message, err := client.Recv()
if message != nil {
s.ctx.Debug("Receiving Downlink message")
if err := message.Validate(); err != nil {
s.ctx.WithError(err).Warn("Invalid Downlink")
continue
}
if err := message.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal Downlink payload")
}
select {
case s.down <- message:
default:
Expand Down Expand Up @@ -345,6 +352,13 @@ func NewMonitoredHandlerSubscribeStream(client BrokerClient, getContextFunc func
message, err = client.Recv()
if message != nil {
s.ctx.Debug("Receiving Uplink message")
if err := message.Validate(); err != nil {
s.ctx.WithError(err).Warn("Invalid Uplink")
continue
}
if err := message.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal Uplink payload")
}
select {
case s.ch <- message:
default:
Expand Down
13 changes: 10 additions & 3 deletions api/broker/server_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -70,7 +69,11 @@ func (s *BrokerStreamServer) Associate(stream Broker_AssociateServer) (err error
return err
}
if err := uplink.Validate(); err != nil {
return errors.Wrap(err, "Invalid Uplink")
s.ctx.WithError(err).Warn("Invalid Uplink")
continue
}
if err := uplink.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal uplink payload")
}
upChan <- uplink
}
Expand Down Expand Up @@ -139,7 +142,11 @@ func (s *BrokerStreamServer) Publish(stream Broker_PublishServer) error {
return err
}
if err := downlink.Validate(); err != nil {
return errors.Wrap(err, "Invalid Downlink")
s.ctx.WithError(err).Warn("Invalid Downlink")
continue
}
if err := downlink.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal downlink payload")
}
ch <- downlink
}
Expand Down
9 changes: 9 additions & 0 deletions api/message_marshaling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package api

// PayloadUnmarshaler unmarshals the Payload to a Message
type PayloadUnmarshaler interface {
UnmarshalPayload() error
}
7 changes: 7 additions & 0 deletions api/router/client_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ func NewMonitoredDownlinkStream(client RouterClientForGateway) DownlinkStream {
message, err = client.Recv()
if message != nil {
s.ctx.Debug("Receiving Downlink message")
if err := message.Validate(); err != nil {
s.ctx.WithError(err).Warn("Invalid Downlink")
continue
}
if err := message.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal Downlink payload")
}
select {
case s.ch <- message:
default:
Expand Down
6 changes: 5 additions & 1 deletion api/router/server_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ func (s *RouterStreamServer) Uplink(stream Router_UplinkServer) (err error) {
return err
}
if err := uplink.Validate(); err != nil {
return errors.Wrap(err, "Invalid Uplink")
s.ctx.WithError(err).Warn("Invalid Uplink")
continue
}
if err := uplink.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal Uplink payload")
}
ch <- uplink
}
Expand Down

0 comments on commit 722d5c0

Please sign in to comment.