-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[add_session_metadata processor] Enrich events with user and group names #39537
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
db54e65
Rename UpdateDB to SyncDB
mjwolf 47b6a04
[add_session_metadata processor] Populate events with user/group names
mjwolf d9c71cd
Merge remote-tracking branch 'upstream/main' into session-view-username
mjwolf 58a614b
Add user and group names to tests
mjwolf 43fe82a
Update changelog
mjwolf 3231260
Merge remote-tracking branch 'upstream/main' into session-view-names
mjwolf 2da9c7d
Remove caching from getUserName
mjwolf 0b5885c
remove unneeded methods
mjwolf 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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. | ||
|
||
//go:build linux | ||
|
||
package processdb | ||
|
||
import ( | ||
"os/user" | ||
"sync" | ||
) | ||
|
||
type cval struct { | ||
name string | ||
found bool | ||
} | ||
|
||
type namesCache struct { | ||
mutex sync.RWMutex | ||
users map[string]cval | ||
groups map[string]cval | ||
} | ||
|
||
// newNamesCache will return a new namesCache, which can be used to get mappings | ||
// of user and group IDs to names. | ||
func newNamesCache() *namesCache { | ||
u := namesCache{ | ||
users: make(map[string]cval), | ||
groups: make(map[string]cval), | ||
} | ||
return &u | ||
} | ||
|
||
// getUserName will return the name associated with the user ID, if it exists | ||
func (u *namesCache) getUserName(id string) (string, bool) { | ||
u.mutex.Lock() | ||
defer u.mutex.Unlock() | ||
|
||
val, ok := u.users[id] | ||
if ok { | ||
return val.name, val.found | ||
} | ||
user, err := user.LookupId(id) | ||
cval := cval{} | ||
if err != nil { | ||
cval.name = "" | ||
cval.found = false | ||
} else { | ||
cval.name = user.Username | ||
cval.found = true | ||
} | ||
return cval.name, cval.found | ||
} | ||
|
||
// getGroupName will return the name associated with the group ID, if it exists | ||
func (u *namesCache) getGroupName(id string) (string, bool) { | ||
u.mutex.Lock() | ||
defer u.mutex.Unlock() | ||
|
||
val, ok := u.groups[id] | ||
if ok { | ||
return val.name, val.found | ||
} | ||
group, err := user.LookupGroupId(id) | ||
cval := cval{} | ||
if err != nil { | ||
cval.name = "" | ||
cval.found = false | ||
} else { | ||
cval.name = group.Name | ||
cval.found = true | ||
} | ||
return cval.name, cval.found | ||
mjwolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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.
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.
If I get this correctly before we return it we should save
cval
in the map right? That said, I think that the cache sure helps with the performance (especially if we are talking about LDAP-based users) but on the other hand it can affect the reliability of the data, e.g.I just renamed a user but the Id remains the same so better to search for it every-time?!?
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.
I've removed all the caching here, it turns out it has no benefit. I've also tried changing the username as the benchmarking ran, and it returned the new username immediately.