Skip to content

Commit 4e3987b

Browse files
mergify[bot]gpop63shmsr
authored
[meraki] improve defensive checks to prevent panics (#47950) (#47952)
* check if device exists in the map * add fragment * add more safety checks * Fix more potential nil panic(s) * Fix more potential nil panic(s) * update fragment --------- (cherry picked from commit 1c8215c) Co-authored-by: Gabriel Pop <94497545+gpop63@users.noreply.github.com> Co-authored-by: subham sarkar <subham.sarkar@elastic.co>
1 parent 8678007 commit 4e3987b

File tree

5 files changed

+76
-16
lines changed

5 files changed

+76
-16
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# REQUIRED
2+
# Kind can be one of:
3+
# - breaking-change: a change to previously-documented behavior
4+
# - deprecation: functionality that is being removed in a later release
5+
# - bug-fix: fixes a problem in a previous version
6+
# - enhancement: extends functionality but does not break or fix existing behavior
7+
# - feature: new functionality
8+
# - known-issue: problems that we are aware of in a given version
9+
# - security: impacts on the security of a product or a user’s deployment.
10+
# - upgrade: important information for someone upgrading from a prior version
11+
# - other: does not fit into any of the other categories
12+
kind: bug-fix
13+
14+
# REQUIRED for all kinds
15+
# Change summary; a 80ish characters long description of the change.
16+
summary: Improve defensive checks to prevent panics in meraki module
17+
18+
# REQUIRED for breaking-change, deprecation, known-issue
19+
# Long description; in case the summary is not enough to describe the change
20+
# this field accommodate a description without length limits.
21+
# description:
22+
23+
# REQUIRED for breaking-change, deprecation, known-issue
24+
# impact:
25+
26+
# REQUIRED for breaking-change, deprecation, known-issue
27+
# action:
28+
29+
# REQUIRED for all kinds
30+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
31+
component: metricbeat
32+
33+
# AUTOMATED
34+
# OPTIONAL to manually add other PR URLs
35+
# PR URL: A link the PR that added the changeset.
36+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
37+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
38+
# Please provide it if you are adding a fragment for a different PR.
39+
# pr: https://github.com/owner/repo/1234
40+
41+
# AUTOMATED
42+
# OPTIONAL to manually add other issue URLs
43+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
44+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
45+
# issue: https://github.com/owner/repo/1234

x-pack/metricbeat/module/meraki/device_health/device_health_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ func TestGetDeviceChannelUtilization(t *testing.T) {
4242
},
4343
},
4444
validate: func(t *testing.T, devices map[Serial]*Device) {
45-
assert.NotNil(t, devices["ABC123"].bandUtilization)
45+
device, ok := devices["ABC123"]
46+
require.True(t, ok, "device ABC123 should exist in the map")
47+
require.NotNil(t, device, "device ABC123 should not be nil")
48+
assert.NotNil(t, device.bandUtilization)
4649

47-
band1, ok := devices["ABC123"].bandUtilization["2.4"]
50+
band1, ok := device.bandUtilization["2.4"]
4851
assert.NotNil(t, band1)
4952
assert.True(t, ok)
5053
assert.Equal(t, 45.0, *band1.Wifi.Percentage)
5154
assert.Equal(t, 10.0, *band1.NonWifi.Percentage)
5255
assert.Equal(t, 55.0, *band1.Total.Percentage)
5356

54-
band2, ok := devices["ABC123"].bandUtilization["5"]
57+
band2, ok := device.bandUtilization["5"]
5558
assert.NotNil(t, band2)
5659
assert.True(t, ok)
5760
assert.Equal(t, 10.0, *band2.Wifi.Percentage)

x-pack/metricbeat/module/meraki/device_health/devices.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,20 @@ func reportDeviceMetrics(reporter mb.ReporterV2, organizationID string, devices
336336

337337
if device.bandUtilization != nil {
338338
for band, v := range device.bandUtilization {
339+
if v == nil {
340+
continue
341+
}
339342
// Avoid nested object mappings
340343
metricBand := strings.ReplaceAll(band, ".", "_")
341-
metric[fmt.Sprintf("device.channel_utilization.%s.utilization_80211", metricBand)] = v.Wifi.Percentage
342-
metric[fmt.Sprintf("device.channel_utilization.%s.utilization_non_80211", metricBand)] = v.NonWifi.Percentage
343-
metric[fmt.Sprintf("device.channel_utilization.%s.utilization_total", metricBand)] = v.Total.Percentage
344+
if v.Wifi != nil {
345+
metric[fmt.Sprintf("device.channel_utilization.%s.utilization_80211", metricBand)] = v.Wifi.Percentage
346+
}
347+
if v.NonWifi != nil {
348+
metric[fmt.Sprintf("device.channel_utilization.%s.utilization_non_80211", metricBand)] = v.NonWifi.Percentage
349+
}
350+
if v.Total != nil {
351+
metric[fmt.Sprintf("device.channel_utilization.%s.utilization_total", metricBand)] = v.Total.Percentage
352+
}
344353
}
345354
}
346355

x-pack/metricbeat/module/meraki/device_health/switchports.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func getDeviceSwitchports(client *sdk.Client, organizationID string, devices map
7676
}
7777
}
7878

79-
devices[Serial(device.Serial)].switchports = switchports
79+
if d, ok := devices[Serial(device.Serial)]; ok && d != nil {
80+
d.switchports = switchports
81+
}
8082
}
8183

8284
return nil

x-pack/metricbeat/module/meraki/device_health/uplinks.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ func getDeviceUplinks(client *sdk.Client, organizationID string, devices map[Ser
7272

7373
for _, device := range *applicanceUplinks {
7474
deviceObj, ok := devices[Serial(device.Serial)]
75-
if device.HighAvailability != nil && ok && deviceObj != nil {
76-
devices[Serial(device.Serial)].haStatus = device.HighAvailability
75+
if !ok || deviceObj == nil {
76+
continue
77+
}
78+
79+
if device.HighAvailability != nil {
80+
deviceObj.haStatus = device.HighAvailability
7781
}
7882

7983
if device.Uplinks != nil {
@@ -96,9 +100,7 @@ func getDeviceUplinks(client *sdk.Client, organizationID string, devices map[Ser
96100
uplinks = append(uplinks, uplink)
97101
}
98102

99-
if ok && deviceObj != nil {
100-
devices[Serial(device.Serial)].uplinks = uplinks
101-
}
103+
deviceObj.uplinks = uplinks
102104
}
103105
}
104106

@@ -161,9 +163,8 @@ func getDeviceUplinks(client *sdk.Client, organizationID string, devices map[Ser
161163
uplinks = append(uplinks, uplink)
162164
}
163165

164-
deviceObj, ok := devices[Serial(device.Serial)]
165-
if ok && deviceObj != nil {
166-
devices[Serial(device.Serial)].uplinks = uplinks
166+
if deviceObj, ok := devices[Serial(device.Serial)]; ok && deviceObj != nil {
167+
deviceObj.uplinks = uplinks
167168
}
168169
}
169170

@@ -192,7 +193,7 @@ func reportUplinkMetrics(reporter mb.ReporterV2, organizationID string, devices
192193
if uplink == nil {
193194
continue
194195
}
195-
if uplink.lossAndLatency != nil {
196+
if uplink.lossAndLatency != nil && uplink.lossAndLatency.TimeSeries != nil {
196197
// each loss and latency metric can have multiple values per collection.
197198
// we report each value as it's own (smaller) metric event, containing
198199
// the identifying device/uplink fields.

0 commit comments

Comments
 (0)