generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 237
Add windows event regex filtering to CWAgent #1764
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cd878d0
Add event ID filtering for windows logs to CWAgent
Paamicky f6d5cdf
mend
Paamicky 119c7d7
mend
Paamicky de9a111
mend
Paamicky 037fe7f
mend
Paamicky 5dd88d6
update just for integ test
Paamicky 144fac4
Merge branch 'main' into feature/windows-eventId-filtering
Paamicky 2276ab2
fix: resolve workflow lint issues
Paamicky 1ab8a1a
Merge branch 'main' into feature/windows-eventId-filtering
Paamicky 5a26a15
Merge branch 'main' into feature/windows-eventId-filtering
Paamicky 218f290
Merge branch 'main' into feature/windows-eventId-filtering
Paamicky 90f466e
changed cutoffPeriod to const
Paamicky 10f9f0a
Add eventid check to the Agent TOML
Paamicky 94fcd20
Add windows event regex filtering
Paamicky 8f70ab5
Merge branch 'main' into feature/windows-regex-filtering
Paamicky 9fb255f
Fix merge conflicts
Paamicky b76632c
Merge branch 'main' into feature/windows-regex-filtering
Paamicky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -1361,4 +1361,4 @@ jobs: | |
| else | ||
| cd terraform/eks/addon/gpu | ||
| fi | ||
| terraform destroy -auto-approve | ||
| terraform destroy -auto-approve | ||
This file contains hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
plugins/inputs/windows_event_log/wineventlog/eventFilter_test.go
This file contains hidden or 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,93 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package wineventlog | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestEventLogFilterInit(t *testing.T) { | ||
| exp := "(foo|bar|baz)" | ||
| filter, err := initEventLogFilter(includeFilterType, exp) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, filter.expressionP) | ||
| filter, err = initEventLogFilter(excludeFilterType, exp) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, filter.expressionP) | ||
| } | ||
|
|
||
| func TestLogEventFilterInitInvalidType(t *testing.T) { | ||
| _, err := initEventLogFilter("something wrong", "(foo|bar|baz)") | ||
| assert.Error(t, err) | ||
| } | ||
|
|
||
| func TestLogEventFilterInitInvalidRegex(t *testing.T) { | ||
| _, err := initEventLogFilter(excludeFilterType, "abc)") | ||
| assert.Error(t, err) | ||
| } | ||
|
|
||
| func TestLogEventFilterShouldPublishInclude(t *testing.T) { | ||
| exp := "(foo|bar|baz)" | ||
| filter, err := initEventLogFilter(includeFilterType, exp) | ||
| assert.NoError(t, err) | ||
|
|
||
| assertShouldPublish(t, filter, "foo bar baz") | ||
| assertShouldNotPublish(t, filter, "something else") | ||
| } | ||
|
|
||
| func TestEventLogFilterShouldPublishExclude(t *testing.T) { | ||
| exp := "(foo|bar|baz)" | ||
| filter, err := initEventLogFilter(excludeFilterType, exp) | ||
| assert.NoError(t, err) | ||
|
|
||
| assertShouldNotPublish(t, filter, "foo bar baz") | ||
| assertShouldPublish(t, filter, "something else") | ||
| } | ||
|
|
||
| func BenchmarkEventLogFilterShouldPublish(b *testing.B) { | ||
| exp := "(foo|bar|baz)" | ||
| filter, err := initEventLogFilter(excludeFilterType, exp) | ||
| assert.NoError(b, err) | ||
| b.ResetTimer() | ||
|
|
||
| msg := "foo bar baz" | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| filter.ShouldPublish(msg) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkEventLogFilterShouldNotPublish(b *testing.B) { | ||
| exp := "(foo|bar|baz)" | ||
| filter, err := initEventLogFilter(excludeFilterType, exp) | ||
| assert.NoError(b, err) | ||
| b.ResetTimer() | ||
|
|
||
| msg := "something else" | ||
|
|
||
| for i := 0; i < b.N; i++ { | ||
| filter.ShouldPublish(msg) | ||
| } | ||
| } | ||
|
|
||
| func initEventLogFilter(filterType, expressionStr string) (EventFilter, error) { | ||
| filter := EventFilter{ | ||
| Type: filterType, | ||
| Expression: expressionStr, | ||
| } | ||
| err := filter.init() | ||
| return filter, err | ||
| } | ||
|
|
||
| func assertShouldPublish(t *testing.T, filter EventFilter, msg string) { | ||
| res := filter.ShouldPublish(msg) | ||
| assert.True(t, res) | ||
| } | ||
|
|
||
| func assertShouldNotPublish(t *testing.T, filter EventFilter, msg string) { | ||
| res := filter.ShouldPublish(msg) | ||
| assert.False(t, res) | ||
| } |
50 changes: 50 additions & 0 deletions
50
plugins/inputs/windows_event_log/wineventlog/eventfilter.go
This file contains hidden or 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 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package wineventlog | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "regexp" | ||
| ) | ||
|
|
||
| const ( | ||
| includeFilterType = "include" | ||
| excludeFilterType = "exclude" | ||
| ) | ||
|
|
||
| var ( | ||
| validFilterTypes = []string{includeFilterType, excludeFilterType} | ||
| validFilterTypesSet = map[string]struct{}{ | ||
| includeFilterType: {}, | ||
| excludeFilterType: {}, | ||
| } | ||
| ) | ||
|
|
||
| type EventFilter struct { | ||
| Type string `toml:"type"` | ||
| Expression string `toml:"expression"` | ||
| expressionP *regexp.Regexp | ||
| } | ||
|
|
||
| func (filter *EventFilter) init() error { | ||
| if _, ok := validFilterTypesSet[filter.Type]; !ok { | ||
| return fmt.Errorf("filter type %s is incorrect, valid types are: %v", filter.Type, validFilterTypes) | ||
| } | ||
|
|
||
| var err error | ||
| if filter.expressionP, err = regexp.Compile(filter.Expression); err != nil { | ||
| return fmt.Errorf("filter regex has issue, regexp: Compile( %v ): %v", filter.Expression, err.Error()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (filter *EventFilter) ShouldPublish(message string) bool { | ||
| if filter.expressionP == nil { | ||
| log.Printf("E! [wineventlog] Filter regex is invalid, expression: %s", filter.Expression) | ||
Paamicky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false | ||
| } | ||
| match := filter.expressionP.MatchString(message) | ||
| return (filter.Type == includeFilterType) == match | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
translator/config/sampleSchema/invalidLogWindowsEventsWithInvalidFilterType.json
This file contains hidden or 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,37 @@ | ||
| { | ||
| "logs": { | ||
| "logs_collected": { | ||
| "windows_events": { | ||
| "collect_list": [ | ||
| { | ||
| "event_name": "System", | ||
| "event_ids": [2300], | ||
| "log_group_name": "System", | ||
| "log_stream_name": "System", | ||
| "filters": [ | ||
| { | ||
| "type": "invalid", | ||
| "expression": "(TRACE|DEBUG)" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "event_name": "Application", | ||
| "event_levels": [ | ||
| "ERROR" | ||
| ], | ||
| "log_group_name": "Application", | ||
| "log_stream_name": "Application", | ||
| "filters": [ | ||
| { | ||
| "type": "include", | ||
| "expression": 40 | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "log_stream_name": "LOG_STREAM_NAME" | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: unnecessary change