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

Goflow fix failed to parse ip #336

Merged
merged 4 commits into from
Jun 24, 2021
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
## 1.1.2 - Unreleased

### Fixed
- Resolved an issue where empty ip address fields result in failed parsing [PR 336](https://github.com/observIQ/stanza/pull/336)

## 1.1.1 - 2021-06-21

Expand Down
6 changes: 5 additions & 1 deletion operator/builtin/input/goflow/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ func Parse(message flowmessage.FlowMessage) (map[string]interface{}, time.Time,
delete(m, key)
switch x := val.(type) {
case []byte:
// If the field is not set, skip it
if len(x) == 0 {
continue
}
ip, err := bytesToIP(x)
if err != nil {
return nil, timestamp, errors.Wrap(err, "error converting DstAddr to string")
return nil, timestamp, errors.Wrap(err, fmt.Sprintf("error converting %s to string", key))
}
m[key] = ip.String()
default:
Expand Down
129 changes: 129 additions & 0 deletions operator/builtin/input/goflow/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ func TestParse(t *testing.T) {
input flowmessage.FlowMessage
expect map[string]interface{}
expectTime time.Time
expectErr bool
}{
{
"minimal",
flowmessage.FlowMessage{},
map[string]interface{}{},
time.Time{},
false,
},
{
"addresses",
Expand All @@ -51,6 +53,128 @@ func TestParse(t *testing.T) {
"dstaddrencap": DstAddrEncap.String(),
},
time.Time{},
false,
},
{
"empty-srcaddr",
flowmessage.FlowMessage{
SamplerAddress: SamplerAddress,
SrcAddr: []byte{},
DstAddr: DstAddr,
NextHop: NextHop,
SrcAddrEncap: SrcAddrEncap,
DstAddrEncap: DstAddrEncap,
},
map[string]interface{}{
"sampleraddress": SamplerAddress.String(),
"dstaddr": DstAddr.String(),
"nexthop": NextHop.String(),
"srcaddrencap": SrcAddrEncap.String(),
"dstaddrencap": DstAddrEncap.String(),
},
time.Time{},
false,
},
{
"empty-dstaddr",
flowmessage.FlowMessage{
SamplerAddress: SamplerAddress,
SrcAddr: SrcAddr,
DstAddr: []byte{},
NextHop: NextHop,
SrcAddrEncap: SrcAddrEncap,
DstAddrEncap: DstAddrEncap,
},
map[string]interface{}{
"sampleraddress": SamplerAddress.String(),
"srcaddr": SrcAddr.String(),
"nexthop": NextHop.String(),
"srcaddrencap": SrcAddrEncap.String(),
"dstaddrencap": DstAddrEncap.String(),
},
time.Time{},
false,
},
{
"empty-nexthop",
flowmessage.FlowMessage{
SamplerAddress: SamplerAddress,
SrcAddr: SrcAddr,
DstAddr: DstAddr,
NextHop: []byte{},
SrcAddrEncap: SrcAddrEncap,
DstAddrEncap: DstAddrEncap,
},
map[string]interface{}{
"sampleraddress": SamplerAddress.String(),
"srcaddr": SrcAddr.String(),
"dstaddr": DstAddr.String(),
"srcaddrencap": SrcAddrEncap.String(),
"dstaddrencap": DstAddrEncap.String(),
},
time.Time{},
false,
},
{
"empty-srcaddrencap",
flowmessage.FlowMessage{
SamplerAddress: SamplerAddress,
SrcAddr: SrcAddr,
DstAddr: DstAddr,
NextHop: NextHop,
SrcAddrEncap: []byte{},
DstAddrEncap: DstAddrEncap,
},
map[string]interface{}{
"sampleraddress": SamplerAddress.String(),
"srcaddr": SrcAddr.String(),
"dstaddr": DstAddr.String(),
"nexthop": NextHop.String(),
"dstaddrencap": DstAddrEncap.String(),
},
time.Time{},
false,
},
{
"empty-dstaddrencap",
flowmessage.FlowMessage{
SamplerAddress: SamplerAddress,
SrcAddr: SrcAddr,
DstAddr: DstAddr,
NextHop: NextHop,
SrcAddrEncap: SrcAddrEncap,
DstAddrEncap: []byte{},
},
map[string]interface{}{
"sampleraddress": SamplerAddress.String(),
"srcaddr": SrcAddr.String(),
"dstaddr": DstAddr.String(),
"nexthop": NextHop.String(),
"srcaddrencap": SrcAddrEncap.String(),
},
time.Time{},
false,
},
{
"malformed-addresses",
flowmessage.FlowMessage{
SamplerAddress: SamplerAddress,
SrcAddr: []byte("ip:10.1.1.1"),
DstAddr: DstAddr,
NextHop: NextHop,
SrcAddrEncap: SrcAddrEncap,
DstAddrEncap: DstAddrEncap,
},
map[string]interface{}{
"sampleraddress": SamplerAddress.String(),
"srcaddr": "ip:10.1.1.1",
"dstaddr": DstAddr.String(),
"nexthop": NextHop.String(),
"srcaddrencap": SrcAddrEncap.String(),
"dstaddrencap": DstAddrEncap.String(),
},
time.Time{},
true,
},
{
"promote-time",
Expand All @@ -72,12 +196,17 @@ func TestParse(t *testing.T) {
"dstaddrencap": DstAddrEncap.String(),
},
time.Unix(int64(1623774351), 0),
false,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
output, outputTime, err := Parse(tc.input)
if tc.expectErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tc.expect, output)

Expand Down