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

Cherry-pick #10950 to 7.x: [Filebeat] [Netflow] fix field name conversion to snake case #11033

Merged
merged 1 commit into from
Mar 2, 2019
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
24 changes: 15 additions & 9 deletions x-pack/filebeat/input/netflow/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (
)

var fieldNameConverter = caseConverter{
conversion: make(map[string]string),
conversion: map[string]string{
// Special handled fields

// VRFname should be VRFName
"VRFname": "vrf_name",
},
}

type caseConverter struct {
Expand Down Expand Up @@ -50,24 +55,24 @@ func (c *caseConverter) ToSnakeCase(orig record.Map) common.MapStr {
// format. This function is tailored to some specifics of NetFlow field names.
// Don't reuse it.
func CamelCaseToSnakeCase(in string) string {
// Lowercase those few fields that are already snake-cased
// skip those few fields that are already snake-cased
if strings.ContainsRune(in, '_') {
return strings.ToLower(in)
}

out := make([]rune, 0, len(in)+4)
runes := []rune(in)
upperStrike := 1
for pos, r := range runes {
upperCount := 1
for _, r := range runes {
lr := unicode.ToLower(r)
isUpper := lr != r
if isUpper {
if upperStrike == 0 {
if upperCount == 0 {
out = append(out, '_')
}
upperStrike++
upperCount++
} else {
if upperStrike > 2 {
if upperCount > 2 {
// Some magic here:
// NetFlow usually lowercases all but the first letter of an
// acronym (Icmp) Except when it is 2 characters long: (IP).
Expand All @@ -77,9 +82,10 @@ func CamelCaseToSnakeCase(in string) string {
// postNATSourceIPv4Address : post_nat_source_ipv4_address
// selectorIDTotalFlowsObserved : selector_id_total_flows_...
out = append(out, '_')
out[pos], out[pos-1] = out[pos-1], out[pos]
n := len(out) - 1
out[n], out[n-1] = out[n-1], out[n]
}
upperStrike = 0
upperCount = 0
}
out = append(out, lr)
}
Expand Down
36 changes: 36 additions & 0 deletions x-pack/filebeat/input/netflow/case_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package netflow

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCamelCaseToSnakeCase(t *testing.T) {
for _, testCase := range [][2]string{
{"aBCDe", "a_bc_de"},
{"postNATSourceIPv4Address", "post_nat_source_ipv4_address"},
{"selectorIDTotalFlowsObserved", "selector_id_total_flows_observed"},
{"engineId", "engine_id"},
{"samplerRandomInterval", "sampler_random_interval"},
{"dot1qVlanId", "dot1q_vlan_id"},
{"messageMD5Checksum", "message_md5_checksum"},
{"hashIPPayloadSize", "hash_ip_payload_size"},
{"upperCILimit", "upper_ci_limit"},
{"virtualStationUUID", "virtual_station_uuid"},
{"selectorIDTotalFlowsObserved", "selector_id_total_flows_observed"},
{"postMCastLayer2OctetDeltaCount", "post_mcast_layer2_octet_delta_count"},
{"IPSecSPI", "ip_sec_spi"},
{"VRFname", "vrf_name"},
} {
s, found := fieldNameConverter.conversion[testCase[0]]
if !found {
s = CamelCaseToSnakeCase(testCase[0])
}
assert.Equal(t, testCase[1], s)
}
}
Loading