Skip to content

Commit 08eaadb

Browse files
authored
[filebeat] Add preserve_original_event option to o365audit input (#26273)
* Add preserve_original_event option to o365audit input * Use String method from MapStr * Add test
1 parent 2ebf83e commit 08eaadb

5 files changed

Lines changed: 51 additions & 0 deletions

File tree

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
823823
- Make `filestream` input GA. {pull}26127[26127]
824824
- Add new `parser` to `filestream` input: `container`. {pull}26115[26115]
825825
- Add support for ISO8601 timestamps in Zeek fileset {pull}25564[25564]
826+
- Add `preserve_original_event` option to `o365audit` input. {pull}26273[26273]
826827

827828
*Heartbeat*
828829

x-pack/filebeat/docs/inputs/input-o365audit.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ default is `2000`, as this is the server-side limit per tenant.
133133
The maximum time window that API allows in a single query. Defaults to `24h`
134134
to match Microsoft's documented limit.
135135

136+
===== `api.preserve_original_event`
137+
138+
Controls whether the original o365 audit object will be kept in `event.original`
139+
or not. Defaults to `false`.
140+
136141
[id="{beatname_lc}-input-{type}-common-options"]
137142
include::../../../../filebeat/docs/inputs/input-common-options.asciidoc[]
138143

x-pack/filebeat/input/o365audit/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ type APIConfig struct {
8383
// duplicates.
8484
SetIDFromAuditRecord bool `config:"set_id_from_audit_record"`
8585

86+
// PreserveOriginalEvent controls whether the original o365 audit object
87+
// will be kept in `event.original` or not.
88+
PreserveOriginalEvent bool `config:"preserve_original_event"`
89+
8690
// MaxQuerySize is the maximum time window that can be queried. The default
8791
// is 24h.
8892
MaxQuerySize time.Duration `config:"max_query_size" validate:"positive"`

x-pack/filebeat/input/o365audit/input.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ func (env apiEnvironment) toBeatEvent(doc common.MapStr) beat.Event {
253253
b.SetID(id)
254254
}
255255
}
256+
if env.Config.PreserveOriginalEvent {
257+
b.PutValue("event.original", doc.String())
258+
}
256259
if len(errs) > 0 {
257260
msgs := make([]string, len(errs))
258261
for idx, e := range errs {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package o365audit
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/elastic/beats/v7/libbeat/common"
14+
)
15+
16+
func TestPreserveOriginalEvent(t *testing.T) {
17+
env := apiEnvironment{
18+
Config: APIConfig{PreserveOriginalEvent: false},
19+
}
20+
21+
doc := common.MapStr{
22+
"field1": "val1",
23+
}
24+
25+
event := env.toBeatEvent(doc)
26+
27+
v, err := event.GetValue("event.original")
28+
require.EqualError(t, err, "key not found")
29+
assert.Nil(t, v)
30+
31+
env.Config.PreserveOriginalEvent = true
32+
33+
event = env.toBeatEvent(doc)
34+
35+
v, err = event.GetValue("event.original")
36+
require.NoError(t, err)
37+
assert.JSONEq(t, `{"field1":"val1"}`, v.(string))
38+
}

0 commit comments

Comments
 (0)