Skip to content

Commit

Permalink
Fix DHPLogMessage path
Browse files Browse the repository at this point in the history
The DHPLogMessage path did not enforce base64 encoding
of the message potentially creating invalid LogEvent resources

Refs #279
  • Loading branch information
loafoe committed Nov 25, 2022
1 parent d5615fd commit eef1775
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion queue/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (n *nilStorer) StoreResources(_ []logging.Resource, count int) (*logging.St
}

func TestChannelQueue(t *testing.T) {
q, err := queue.NewChannelQueue()
q, err := queue.NewChannelQueue(queue.WithMetrics(&nilMetrics{}))
assert.Nil(t, err)
assert.NotNil(t, q)

Expand Down
8 changes: 7 additions & 1 deletion queue/deliverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,19 @@ func ProcessMessage(rfcLogMessage syslog.Message, m Metrics) (*logging.Resource,
}

msg = wrapResource("logproxy-wrapped", rfcLogMessage)

err = json.Unmarshal([]byte(*logMessage), &dhp)
if err == nil {
if dhp.TransactionID != "" {
msg.TransactionID = dhp.TransactionID
}
if dhp.LogData.Message != "" {
msg.LogData.Message = dhp.LogData.Message
if !Base64Pattern.MatchString(dhp.LogData.Message) { // Encode
msg.LogData.Message = base64.StdEncoding.EncodeToString([]byte(dhp.LogData.Message))
m.IncEnhancedEncodedMessage()
} else { // Already base64 encoded
msg.LogData.Message = dhp.LogData.Message
}
}
if dhp.ApplicationInstance != "" {
msg.ApplicationInstance = dhp.ApplicationInstance
Expand Down
10 changes: 6 additions & 4 deletions queue/deliverer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package queue_test

import (
"bytes"
"encoding/base64"
"io"
"os"
"regexp"
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestCustomJSONInProcessMessage(t *testing.T) {
assert.Nilf(t, err, "Expected Parse() to succeed")
resource, err := queue.ProcessMessage(msg, &nilMetrics{})
assert.Nilf(t, err, "Expected ProcessMessage() to succeed")
assert.Equal(t, `Log message`, resource.LogData.Message)
assert.Equal(t, base64.StdEncoding.EncodeToString([]byte(`Log message`)), resource.LogData.Message)

}

Expand Down Expand Up @@ -116,7 +117,8 @@ func TestProcessMessage(t *testing.T) {
assert.Equal(t, "com.philips.MyLoggingClass%28%29", resource.ServiceName)
assert.Equal(t, "%40396f1a94-86f3-470b-784c-17cc%3D%3D", resource.ServerName)
assert.Equal(t, "FATAL%7C", resource.Severity)
assert.Equal(t, payload, resource.LogData.Message)
encoded := base64.StdEncoding.EncodeToString([]byte(payload))
assert.Equal(t, encoded, resource.LogData.Message)
assert.Equal(t, "logproxy%24,", resource.OriginatingUser)

msg, err = parser.Parse([]byte(nonDHPMessage))
Expand Down Expand Up @@ -174,7 +176,7 @@ func TestResourceWorker(t *testing.T) {
assert.Nilf(t, err, "Expected NewDeliverer() to succeed")
deliverer.Debug = true

q, _ := queue.NewChannelQueue()
q, _ := queue.NewChannelQueue(queue.WithMetrics(&nilMetrics{}))
done, _ := q.Start()

go deliverer.ResourceWorker(q, done, nil)
Expand Down Expand Up @@ -230,7 +232,7 @@ func TestDroppedMessages(t *testing.T) {
assert.Nilf(t, err, "Expected NewDeliverer() to succeed")
deliverer.Debug = true

q, _ := queue.NewChannelQueue()
q, _ := queue.NewChannelQueue(queue.WithMetrics(&nilMetrics{}))
done, _ := q.Start()

go deliverer.ResourceWorker(q, done, nil)
Expand Down

0 comments on commit eef1775

Please sign in to comment.