-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
430 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 dataprovider | ||
|
||
import "github.com/elastic/beats/v7/libbeat/beat" | ||
|
||
type ElasticCommonDataProvider interface { | ||
GetElasticCommonData() (map[string]interface{}, error) | ||
} | ||
|
||
type enricher struct { | ||
dataprovider ElasticCommonDataProvider | ||
} | ||
|
||
func NewEnricher(dataprovider ElasticCommonDataProvider) *enricher { | ||
return &enricher{ | ||
dataprovider: dataprovider, | ||
} | ||
} | ||
|
||
func (e *enricher) EnrichEvent(event *beat.Event) error { | ||
ecsData, err := e.dataprovider.GetElasticCommonData() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range ecsData { | ||
_, err := event.PutValue(k, v) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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 dataprovider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEnrichSuccess(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data map[string]interface{} | ||
expected map[string]string | ||
}{ | ||
{ | ||
name: "single value", | ||
data: map[string]interface{}{ | ||
"a-field": "a-value", | ||
}, | ||
expected: map[string]string{ | ||
"a-field": "a-value", | ||
}, | ||
}, | ||
{ | ||
name: "multiple values", | ||
data: map[string]interface{}{ | ||
"some-field": "some-value", | ||
"other-field": "other-value", | ||
}, | ||
expected: map[string]string{ | ||
"some-field": "some-value", | ||
"other-field": "other-value", | ||
}, | ||
}, | ||
{ | ||
name: "internal object", | ||
data: map[string]interface{}{ | ||
"some-field": "some-value", | ||
"more-fields": map[string]interface{}{ | ||
"internal-field": "internal-value", | ||
}, | ||
}, | ||
expected: map[string]string{ | ||
"some-field": "some-value", | ||
"more-fields.internal-field": "internal-value", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dp := NewMockElasticCommonDataProvider(t) | ||
dp.EXPECT().GetElasticCommonData().Return(tt.data, nil) | ||
|
||
ev := &beat.Event{ | ||
Fields: map[string]interface{}{}, | ||
} | ||
err := NewEnricher(dp).EnrichEvent(ev) | ||
assert.NoError(t, err) | ||
|
||
for key, expectedValue := range tt.expected { | ||
actualValue, err := ev.GetValue(key) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedValue, actualValue) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEnrichError(t *testing.T) { | ||
dp := NewMockElasticCommonDataProvider(t) | ||
dp.EXPECT().GetElasticCommonData().Return(nil, assert.AnError) | ||
|
||
ev := &beat.Event{ | ||
Fields: map[string]interface{}{}, | ||
} | ||
err := NewEnricher(dp).EnrichEvent(ev) | ||
assert.Error(t, err) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.