Skip to content

Commit

Permalink
[packetbeat] Fix interface device parsing for packetbeat protocols (#…
Browse files Browse the repository at this point in the history
…37946)

* Fix interface device parsing for packetbeat protocols

* Add unit test

* Move device check out of validation function

* Add err check on condition for clarity
  • Loading branch information
marc-gr committed Feb 15, 2024
1 parent 4163848 commit aba91a4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fields added to events containing the Beats version. {pull}37553[37553]

*Packetbeat*

- Fix interface device parsing for packetbeat protocols. {pull}37946[37946]

*Winlogbeat*

Expand Down
27 changes: 19 additions & 8 deletions packetbeat/protos/protos.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,9 @@ func (s ProtocolsStruct) configureProtocol(test bool, device string, pub reporte

if device != "" {
// This could happen earlier, but let any errors be found first.
var protocol struct {
Device string `config:"interface"`
}
err := config.Unpack(&protocol)
if err != nil {
if isValid, err := validateProtocolDevice(device, config); !isValid || err != nil {
return err
}
if protocol.Device != "" && protocol.Device != device {
return nil
}
}

var client beat.Client
Expand All @@ -205,6 +198,24 @@ func (s ProtocolsStruct) configureProtocol(test bool, device string, pub reporte
return nil
}

func validateProtocolDevice(device string, config *conf.C) (bool, error) {
var protocol struct {
Interface struct {
Device string `config:"device"`
} `config:"interface"`
}

if err := config.Unpack(&protocol); err != nil {
return false, err
}

if protocol.Interface.Device != "" && protocol.Interface.Device != device {
return false, nil
}

return true, nil
}

func (s ProtocolsStruct) register(proto Protocol, client beat.Client, plugin Plugin) {
if _, exists := s.all[proto]; exists {
logp.Warn("Protocol (%s) plugin will overwritten by another plugin", proto.String())
Expand Down
57 changes: 57 additions & 0 deletions packetbeat/protos/protos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"time"

"github.com/elastic/beats/v7/libbeat/common"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/go-ucfg"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -200,3 +202,58 @@ func TestGetUDP(t *testing.T) {
assert.NotNil(t, udp)
assert.Contains(t, udp.GetPorts(), 53)
}

func TestValidateProtocolDevice(t *testing.T) {
tcs := []struct {
testCase, device string
config map[string]interface{}
expectedValid bool
expectedErr string
}{
{
"DeviceIsIncorrect",
"eth0",
map[string]interface{}{
"interface": map[string]interface{}{
"device": "eth1",
},
},
false,
"",
},
{
"DeviceIsCorrect",
"eth1",
map[string]interface{}{
"interface": map[string]interface{}{
"device": "eth1",
},
},
true,
"",
},
{
"ConfigIsInvalid",
"eth0",
map[string]interface{}{
"interface": "eth1",
},
false,
"required 'object', but found 'string' in field 'interface'",
},
}

for _, tc := range tcs {
tc := tc
t.Run(tc.testCase, func(t *testing.T) {
cfg := (*conf.C)(ucfg.MustNewFrom(tc.config))
isValid, err := validateProtocolDevice(tc.device, cfg)
assert.Equal(t, tc.expectedValid, isValid)
if tc.expectedErr == "" {
assert.Nil(t, err)
} else {
assert.EqualError(t, err, tc.expectedErr)
}
})
}
}

0 comments on commit aba91a4

Please sign in to comment.