Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #14872: fix incorrectly handle with two-words redis command #14873

Merged
merged 5 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Packetbeat*

- Redis: fix incorrectly handle with two-words redis command. {issue}14872[14872] {pull}14873[14873]

*Winlogbeat*

Expand Down
20 changes: 16 additions & 4 deletions packetbeat/protos/redis/redis_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package redis

import (
"bytes"
"time"

"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -423,11 +424,22 @@ func (p *parser) parseArray(depth int, buf *streambuf.Buffer) (common.NetString,
}

// handle top-level request command
if depth == 0 && isRedisCommand(content[0]) {
var oneWordCommand, twoWordsCommand bool
oneWordCommand = isRedisCommand(content[0])
twoWordsCommand = count > 1 && isRedisCommand(bytes.Join(content[0:2], []byte(" ")))

if depth == 0 && (oneWordCommand || twoWordsCommand) {
p.message.isRequest = true
p.message.method = content[0]
if len(content) > 1 {
p.message.path = content[1]
if oneWordCommand {
p.message.method = content[0]
if len(content) > 1 {
p.message.path = content[1]
}
} else if twoWordsCommand {
p.message.method = bytes.Join(content[0:2], []byte(" "))
if len(content) > 2 {
p.message.path = content[2]
}
}

var value common.NetString
Expand Down
22 changes: 22 additions & 0 deletions packetbeat/protos/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,31 @@ func TestRedisParser_ArrayRequest(t *testing.T) {
assert.True(t, complete)
assert.True(t, msg.isRequest)
assert.Equal(t, "SET key1 Hello", string(msg.message))
assert.Equal(t, "SET", string(msg.method))
assert.Equal(t, "key1", string(msg.path))
assert.Equal(t, len(arrayRequest), msg.size)
}

var arrayRequest2 = []byte("*3\r\n" +
"$6\r\n" +
"CONFIG\r\n" +
"$3\r\n" +
"GET\r\n" +
"$1\r\n" +
"*\r\n")

func TestRedisParser_ArrayRequest2(t *testing.T) {
msg, ok, complete := parse(arrayRequest2)

assert.True(t, ok)
assert.True(t, complete)
assert.True(t, msg.isRequest)
assert.Equal(t, "CONFIG GET *", string(msg.message))
assert.Equal(t, "CONFIG GET", string(msg.method))
assert.Equal(t, "*", string(msg.path))
assert.Equal(t, len(arrayRequest2), msg.size)
}

var arrayResponse = []byte("*4\r\n" +
"$3\r\n" +
"foo\r\n" +
Expand Down