Skip to content

Commit

Permalink
[meraki] fix panic due to uninitialized device wifi structs (#42746)
Browse files Browse the repository at this point in the history
* initialize structs if nil
use interface for client to facilitate testing

* add tests

* add changelog entry

* fix pr id

* make update

* fix linter

* revert getDeviceLicenses changes

* add more safety checks

* address reviews
  • Loading branch information
gpop63 authored Feb 20, 2025
1 parent ce6156b commit cd883f5
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ otherwise no tag is added. {issue}42208[42208] {pull}42403[42403]
- Remove `hostname` field from zookeeper's `mntr` data stream. {pull}41887[41887]
- Continue collecting metrics even if the Cisco Meraki `getDeviceLicenses` operation fails. {pull}42397[42397]
- Fixed errors in the `elasticsearch.index` metricset when index settings are missing. {issue}42424[42424] {pull}42426[42426]
- Fixed panic caused by uninitialized meraki device wifi0 and wifi1 struct pointers in the device WiFi data fetching. {issue}42745[42745] {pull}42746[42746]

*Osquerybeat*

Expand Down
62 changes: 31 additions & 31 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16435,6 +16435,37 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/go-resty/resty/v2
Version: v2.13.1
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/go-resty/resty/v2@v2.13.1/LICENSE:

The MIT License (MIT)

Copyright (c) 2015-2023 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/go-sql-driver/mysql
Version: v1.6.0
Expand Down Expand Up @@ -45330,37 +45361,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/go-resty/resty/v2
Version: v2.13.1
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/go-resty/resty/v2@v2.13.1/LICENSE:

The MIT License (MIT)

Copyright (c) 2015-2023 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/go-sourcemap/sourcemap
Version: v2.1.2+incompatible
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ require (
github.com/foxcpp/go-mockdns v0.0.0-20201212160233-ede2f9158d15
github.com/go-ldap/ldap/v3 v3.4.6
github.com/go-ole/go-ole v1.2.6
github.com/go-resty/resty/v2 v2.13.1
github.com/gofrs/uuid/v5 v5.2.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/cel-go v0.19.0
Expand Down Expand Up @@ -311,7 +312,6 @@ require (
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-resty/resty/v2 v2.13.1 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/godror/knownpb v0.1.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {

getDevicePerformanceScores(m.logger, m.client, devices)

err = getDeviceChannelUtilization(m.client, devices, collectionPeriod)
networkHealthService := &NetworkHealthServiceWrapper{
service: m.client.Networks,
}

err = getDeviceChannelUtilization(networkHealthService, devices, collectionPeriod)
if err != nil {
return fmt.Errorf("getDeviceChannelUtilization failed; %w", err)
}
Expand Down
224 changes: 224 additions & 0 deletions x-pack/metricbeat/module/meraki/device_health/device_health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
package device_health

import (
"bytes"
"fmt"
"io"
"net/http"
"testing"
"time"

"github.com/go-resty/resty/v2"
meraki "github.com/meraki/dashboard-api-go/v3/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIsEmpty(t *testing.T) {
Expand Down Expand Up @@ -65,3 +75,217 @@ func TestIsEmpty(t *testing.T) {
})
}
}

func TestGetDeviceChannelUtilization(t *testing.T) {
tests := []struct {
name string
client NetworkHealthService
devices map[Serial]*Device
wantErr bool
validate func(t *testing.T, devices map[Serial]*Device)
}{
{
name: "successful data retrieval",
client: &SuccessfulMockNetworkHealthService{},
devices: map[Serial]*Device{
"serial-1": {
details: &meraki.ResponseItemOrganizationsGetOrganizationDevices{
ProductType: "wireless",
NetworkID: "network-1",
},
},
"serial-2": {
details: &meraki.ResponseItemOrganizationsGetOrganizationDevices{
ProductType: "wireless",
NetworkID: "network-2",
},
},
},
validate: func(t *testing.T, devices map[Serial]*Device) {
assert.NotNil(t, devices["serial-1"].wifi0)
assert.Equal(t, 1.0, *devices["serial-1"].wifi0.Utilization80211)
assert.Equal(t, 1.1, *devices["serial-1"].wifi0.UtilizationNon80211)
assert.Equal(t, 1.2, *devices["serial-1"].wifi0.UtilizationTotal)
assert.NotNil(t, devices["serial-2"].wifi1)
assert.Equal(t, 2.0, *devices["serial-2"].wifi1.Utilization80211)
assert.Equal(t, 2.1, *devices["serial-2"].wifi1.UtilizationNon80211)
assert.Equal(t, 2.2, *devices["serial-2"].wifi1.UtilizationTotal)
},
},
{
name: "multiple buckets use first entry",
client: &MultipleBucketsMockNetworkHealthService{},
devices: map[Serial]*Device{
"serial-3": {
details: &meraki.ResponseItemOrganizationsGetOrganizationDevices{
ProductType: "wireless",
NetworkID: "network-3",
},
},
},
validate: func(t *testing.T, devices map[Serial]*Device) {
assert.NotNil(t, devices["serial-3"].wifi0)
assert.Equal(t, 3.0, *devices["serial-3"].wifi0.Utilization80211)
assert.Equal(t, 3.1, *devices["serial-3"].wifi0.UtilizationNon80211)
assert.Equal(t, 3.2, *devices["serial-3"].wifi0.UtilizationTotal)
assert.Nil(t, devices["serial-3"].wifi1)
},
},
{
name: "MR 27.0 error skips network",
client: &MR27ErrorMockNetworkHealthService{},
devices: map[Serial]*Device{
"serial-4": {
details: &meraki.ResponseItemOrganizationsGetOrganizationDevices{
ProductType: "wireless",
NetworkID: "network-4",
},
},
},
validate: func(t *testing.T, devices map[Serial]*Device) {
assert.Nil(t, devices["serial-4"].wifi0)
assert.Nil(t, devices["serial-4"].wifi1)
},
},
{
name: "other errors propagate",
client: &GenericErrorMockNetworkHealthService{},
devices: map[Serial]*Device{
"serial-5": {
details: &meraki.ResponseItemOrganizationsGetOrganizationDevices{
ProductType: "wireless",
NetworkID: "network-5",
},
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
devicesCopy := make(map[Serial]*Device, len(tt.devices))
for k, v := range tt.devices {
devicesCopy[k] = &Device{
details: v.details,
wifi0: v.wifi0,
wifi1: v.wifi1,
}
}

err := getDeviceChannelUtilization(tt.client, devicesCopy, time.Second)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)

if tt.validate != nil {
tt.validate(t, devicesCopy)
}
})
}
}

// SuccessfulMockNetworkHealthService returns valid utilization data
type SuccessfulMockNetworkHealthService struct{}

func (m *SuccessfulMockNetworkHealthService) GetNetworkNetworkHealthChannelUtilization(networkID string, params *meraki.GetNetworkNetworkHealthChannelUtilizationQueryParams) (*meraki.ResponseNetworksGetNetworkNetworkHealthChannelUtilization, *resty.Response, error) {
wifi0utilization80211 := 1.0
wifi0utilizationNon80211 := 1.1
wifi0utilizationTotal := 1.2

wifi1utilization80211 := 2.0
wifi1utilizationNon80211 := 2.1
wifi1utilizationTotal := 2.2

return &meraki.ResponseNetworksGetNetworkNetworkHealthChannelUtilization{
meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilization{
Serial: "serial-1",
Wifi0: &[]meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilizationWifi0{
{
Utilization80211: &wifi0utilization80211,
UtilizationNon80211: &wifi0utilizationNon80211,
UtilizationTotal: &wifi0utilizationTotal,
},
},
Wifi1: &[]meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilizationWifi1{
{
Utilization80211: &wifi1utilization80211,
UtilizationNon80211: &wifi1utilizationNon80211,
UtilizationTotal: &wifi1utilizationTotal,
},
},
},
meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilization{
Serial: "serial-2",
Wifi0: &[]meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilizationWifi0{
{
Utilization80211: &wifi0utilization80211,
UtilizationNon80211: &wifi0utilizationNon80211,
UtilizationTotal: &wifi0utilizationTotal,
},
},
Wifi1: &[]meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilizationWifi1{
{
Utilization80211: &wifi1utilization80211,
UtilizationNon80211: &wifi1utilizationNon80211,
UtilizationTotal: &wifi1utilizationTotal,
},
},
},
}, &resty.Response{}, nil
}

// MultipleBucketsMockNetworkHealthService returns multiple utilization buckets
type MultipleBucketsMockNetworkHealthService struct{}

func (m *MultipleBucketsMockNetworkHealthService) GetNetworkNetworkHealthChannelUtilization(networkID string, params *meraki.GetNetworkNetworkHealthChannelUtilizationQueryParams) (*meraki.ResponseNetworksGetNetworkNetworkHealthChannelUtilization, *resty.Response, error) {
wifi0util_80211 := 3.0
wifi0util_non80211 := 3.1
wifi0util_total := 3.2

return &meraki.ResponseNetworksGetNetworkNetworkHealthChannelUtilization{
meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilization{
Serial: "serial-3",
Wifi0: &[]meraki.ResponseItemNetworksGetNetworkNetworkHealthChannelUtilizationWifi0{
{ // First bucket will be used
Utilization80211: &wifi0util_80211,
UtilizationNon80211: &wifi0util_non80211,
UtilizationTotal: &wifi0util_total,
},
{ // Second bucket will be ignored
Utilization80211: &wifi0util_80211,
UtilizationNon80211: &wifi0util_non80211,
UtilizationTotal: &wifi0util_total,
},
},
},
}, &resty.Response{}, nil
}

// MR27ErrorMockNetworkHealthService simulates the MR 27.0 version error
type MR27ErrorMockNetworkHealthService struct{}

func (m *MR27ErrorMockNetworkHealthService) GetNetworkNetworkHealthChannelUtilization(networkID string, params *meraki.GetNetworkNetworkHealthChannelUtilizationQueryParams) (*meraki.ResponseNetworksGetNetworkNetworkHealthChannelUtilization, *resty.Response, error) {
r := &resty.Response{}
bodyContent := []byte("This endpoint is only available for networks on MR 27.0 or above.")
r.SetBody(bodyContent)
r.RawResponse = &http.Response{
Body: io.NopCloser(bytes.NewBuffer(bodyContent)),
}
return nil, r, fmt.Errorf("MR 27.0 error")
}

// GenericErrorMockNetworkHealthService simulates generic errors
type GenericErrorMockNetworkHealthService struct{}

func (m *GenericErrorMockNetworkHealthService) GetNetworkNetworkHealthChannelUtilization(networkID string, params *meraki.GetNetworkNetworkHealthChannelUtilizationQueryParams) (*meraki.ResponseNetworksGetNetworkNetworkHealthChannelUtilization, *resty.Response, error) {
r := &resty.Response{}
bodyContent := []byte("Internal Server Error")
r.SetBody(bodyContent)
r.RawResponse = &http.Response{
Body: io.NopCloser(bytes.NewBuffer(bodyContent)),
}
return nil, r, fmt.Errorf("mock API error")
}
Loading

0 comments on commit cd883f5

Please sign in to comment.