func NewMessageWriter(host string, port int, sharedSecret string, sentCounter *metricsreporter.Counter) *messageWriter { output, err := net.Dial("udp4", fmt.Sprintf("%s:%d", host, port)) if err != nil { fmt.Printf("DIAL Error: %s\n", err.Error()) } var writer writers.ByteArrayWriter writer = networkWriter{ sentCounter: sentCounter, conn: output, } if len(sharedSecret) > 0 { secret := []byte(sharedSecret) nested := writer signedWriter := func(message []byte) { signedMessage := signature.SignMessage(message, secret) nested.Write(signedMessage) } writer = MessageWriterFunc(signedWriter) } return &messageWriter{ writer: writer, } }
func (d *DopplerForwarder) networkWrite(message *events.Envelope) { client, err := d.clientPool.RandomClient() if err != nil { d.logger.Errord(map[string]interface{}{ "error": err.Error(), }, "DopplerForwarder: can't forward message") return } messageBytes, err := proto.Marshal(message) if err != nil { d.logger.Errorf("DopplerForwarder: marshal error %v", err) metrics.BatchIncrementCounter("dropsondeMarshaller.marshalErrors") return } switch client.Scheme() { case "udp": signedMessage := signature.SignMessage(messageBytes, d.sharedSecret) bytesWritten, err := client.Write(signedMessage) if err != nil { metrics.BatchIncrementCounter("udp.sendErrorCount") d.logger.Debugd(map[string]interface{}{ "scheme": client.Scheme(), "address": client.Address(), }, "Error writing legacy message") return } metrics.BatchIncrementCounter("udp.sentMessageCount") metrics.BatchAddCounter("udp.sentByteCount", uint64(bytesWritten)) case "tls": var bytesWritten int err = binary.Write(client, binary.LittleEndian, uint32(len(messageBytes))) if err == nil { bytesWritten, err = client.Write(messageBytes) } if err != nil { metrics.BatchIncrementCounter("tls.retryCount") d.inputChan <- message client.Close() d.logger.Errord(map[string]interface{}{ "scheme": client.Scheme(), "address": client.Address(), "error": err.Error(), }, "DopplerForwarder: streaming error") return } metrics.BatchIncrementCounter("tls.sentMessageCount") metrics.BatchAddCounter("tls.sentByteCount", uint64(bytesWritten+4)) default: d.logger.Errorf("DopplerForwarder: unknown protocol, %s for %s", client.Scheme(), client.Address()) return } d.incrementMessageCount(message.GetEventType()) metrics.BatchIncrementCounter("DopplerForwarder.sentMessages") }
func main() { flag.Parse() la, err := net.ResolveUDPAddr("udp", *destination) if err != nil { log.Fatalf("Error resolving loggregator address %s, %s", *destination, err) } // connection, err := net.DialUDP("udp", nil, la) connection, err := net.ListenPacket("udp4", "") if err != nil { log.Fatalf("Error opening udp stuff") } msg := make([]byte, *size, *size) envelope, err := emitter.Wrap(&events.LogMessage{ Message: msg, MessageType: events.LogMessage_OUT.Enum(), Timestamp: proto.Int64(time.Now().UnixNano()), }, "origin") if err != nil { log.Fatal(err.Error()) } buf, err := proto.Marshal(envelope) if err != nil { log.Fatal(err.Error()) } finalBytes := signature.SignMessage(buf, []byte(*secret)) // println("marshal time", t2.Sub(t1).String()) var i int d := time.Duration(*duration) time.AfterFunc(d*time.Second, func() { fmt.Printf("%d, %d, %d, ", *duration, len(finalBytes), i) connection.Close() os.Exit(0) }) t := time.NewTicker(time.Second / time.Duration(*rate)) for { <-t.C connection.WriteTo(finalBytes, la) i++ } }
func (u *UDPWrapper) Write(client Client, message []byte) error { signedMessage := signature.SignMessage(message, u.sharedSecret) sentLength, err := client.Write(signedMessage) if err != nil { u.logger.Errorf("Error writing to UDP client %v\n", err) metrics.BatchIncrementCounter("udp.sendErrorCount") return err } metrics.BatchIncrementCounter("udp.sentMessageCount") metrics.BatchAddCounter("udp.sentByteCount", uint64(sentLength)) // The TLS side writes this metric in the batch.Writer. For UDP, // it needs to be done here. metrics.BatchIncrementCounter("DopplerForwarder.sentMessages") return nil }
func MarshalEvent(event events.Event, secret string) []byte { envelope, _ := emitter.Wrap(event, "origin") envelopeBytes := MarshalProtoBuf(envelope) return signature.SignMessage(envelopeBytes, []byte(secret)) }
It("increments transmitErrorCount if client write fails", func() { err := errors.New("Client Write Failed") client.WriteReturns(0, err) forwarder.Write(envelope) Eventually(func() uint64 { return sender.GetCounter("udp.sendErrorCount") }).Should(BeEquivalentTo(1)) }) It("marshals, signs, writes and emits metrics", func() { bytes, err := proto.Marshal(envelope) Expect(err).NotTo(HaveOccurred()) bytes = signature.SignMessage(bytes, sharedSecret) forwarder.Write(envelope) Eventually(client.WriteCallCount).Should(Equal(1)) Eventually(func() []byte { return client.WriteArgsForCall(0) }).Should(Equal(bytes)) Eventually(func() uint64 { return sender.GetCounter("DopplerForwarder.sentMessages") }).Should(BeEquivalentTo(1)) Eventually(func() uint64 { return sender.GetCounter("dropsondeMarshaller.logMessageMarshalled") }).Should(BeEquivalentTo(1)) }) Context("when writes fail", func() { BeforeEach(func() { client.WriteReturns(0, errors.New("boom")) }) It("does not increment message count or sentMessages", func() {
package signer_test import ( "metron/writers/signer" "github.com/cloudfoundry/dropsonde/signature" "metron/writers/mocks" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Signer", func() { It("sends signed messages to output writer", func() { writer := &mocks.MockByteArrayWriter{} s := signer.New("shared-secret", writer) message := []byte("Some message") s.Write(message) Expect(writer.Data()).To(HaveLen(1)) signedMessage := signature.SignMessage(message, []byte("shared-secret")) Expect(writer.Data()[0]).To(Equal(signedMessage)) }) })
return sender.GetCounter("udp.sendErrorCount") }).Should(BeEquivalentTo(0)) err = errors.New("Client Write Failed") client.WriteOutput.sentLength <- 0 client.WriteOutput.err <- err err = udpWrapper.Write(client, message) Expect(err).To(HaveOccurred()) Eventually(func() uint64 { return sender.GetCounter("udp.sendErrorCount") }).Should(BeEquivalentTo(1)) }) It("signs and writes a message", func() { signedMessage := signature.SignMessage(message, sharedSecret) client.WriteOutput.sentLength <- len(signedMessage) client.WriteOutput.err <- nil udpWrapper.Write(client, message) Eventually(client.WriteCalled).Should(HaveLen(1)) Eventually(client.WriteInput.message).Should(Receive(Equal(signedMessage))) Consistently(client.CloseCalled).ShouldNot(Receive()) }) Context("when client write fail", func() { BeforeEach(func() { client.WriteOutput.sentLength <- 0 client.WriteOutput.err <- errors.New("failed")
func (s *Signer) Write(message []byte) { signedMessage := signature.SignMessage(message, []byte(s.sharedSecret)) s.outputWriter.Write(signedMessage) }