Skip to content

Commit

Permalink
processors/util: ensure hardware addresses are not duplicated (elasti…
Browse files Browse the repository at this point in the history
…c#26877)

When a linux host is using a virtual bridge, the mac address of that
interface will be duplicated in the results from net.Interfaces() since a
dummy device will have been created to support the virtual bridge device.
This change ensures that hardware addresses are unique in the returned list.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and kush-elastic committed May 2, 2022
1 parent 16c656e commit 9998ccc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
23 changes: 22 additions & 1 deletion libbeat/processors/util/netinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package util

import (
"net"
"sort"

"github.com/joeshaw/multierror"
)
Expand Down Expand Up @@ -63,5 +64,25 @@ func GetNetInfo() (ipList []string, hwList []string, err error) {
}
}

return ipList, hwList, errs.Err()
return ipList, unique(hwList), errs.Err()
}

// unique returns addrs lexically sorted and with repeated elements
// omitted.
func unique(addrs []string) []string {
if len(addrs) < 2 {
return addrs
}
sort.Strings(addrs)
curr := 0
for i, addr := range addrs {
if addr == addrs[curr] {
continue
}
curr++
if curr < i {
addrs[curr], addrs[i] = addrs[i], ""
}
}
return addrs[:curr+1]
}
56 changes: 56 additions & 0 deletions libbeat/processors/util/netinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package util

import (
"reflect"
"sort"
"testing"
)

func TestUnique(t *testing.T) {
var tests = [][]string{
{},
{"a"},
{"a", "a"},
{"a", "b"},
{"b", "a"},
{"a", "b", "c"},
{"c", "b", "a"},
{"c", "a", "a", "b", "c", "a"},
}

for i, test := range tests {
// Allocating naive implementation of unique.
seen := make(map[string]bool)
for _, e := range test {
seen[e] = true
}
want := make([]string, 0, len(seen))
for e := range seen {
want = append(want, e)
}
sort.Strings(want)

got := unique(test)

if !reflect.DeepEqual(got, want) {
t.Errorf("unexpected result for test %d: got:%q want:%q", i, got, want)
}
}
}

0 comments on commit 9998ccc

Please sign in to comment.