-
Notifications
You must be signed in to change notification settings - Fork 68
Refactor nodes package #640
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
9 commits
Select commit
Hold shift + click to select a range
1c6c7ce
Remove unused redis instance
zhuoyuan-liu 44bdec5
Remove unused function
zhuoyuan-liu 5a96792
fix last_seen bug
zhuoyuan-liu 04f73ed
refactor utils functions
zhuoyuan-liu d6a1965
add test for new functions
zhuoyuan-liu 556e3c1
Add check for creation
zhuoyuan-liu d4f75cc
Remove unused types and import dependency
zhuoyuan-liu 0d93ed3
Add the node cache
zhuoyuan-liu 3de03d4
Update pkg/nodes/utils_test.go
javuto 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
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,77 @@ | ||
| package nodes | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // OsqueryNode as abstraction of a node | ||
| type OsqueryNode struct { | ||
| gorm.Model | ||
| NodeKey string `gorm:"index"` | ||
| UUID string `gorm:"index"` | ||
| Platform string | ||
| PlatformVersion string | ||
| OsqueryVersion string | ||
| Hostname string | ||
| Localname string | ||
| IPAddress string | ||
| Username string | ||
| OsqueryUser string | ||
| Environment string | ||
| CPU string | ||
| Memory string | ||
| HardwareSerial string | ||
| DaemonHash string | ||
| ConfigHash string | ||
| BytesReceived int | ||
| RawEnrollment string | ||
| LastSeen time.Time | ||
| UserID uint | ||
| EnvironmentID uint | ||
| ExtraData string | ||
| } | ||
|
|
||
| // ArchiveOsqueryNode as abstraction of an archived node | ||
| type ArchiveOsqueryNode struct { | ||
| gorm.Model | ||
| NodeKey string `gorm:"index"` | ||
| UUID string `gorm:"index"` | ||
| Trigger string | ||
| Platform string | ||
| PlatformVersion string | ||
| OsqueryVersion string | ||
| Hostname string | ||
| Localname string | ||
| IPAddress string | ||
| Username string | ||
| OsqueryUser string | ||
| Environment string | ||
| CPU string | ||
| Memory string | ||
| HardwareSerial string | ||
| ConfigHash string | ||
| DaemonHash string | ||
| BytesReceived int | ||
| RawEnrollment string | ||
| LastSeen time.Time | ||
| UserID uint | ||
| EnvironmentID uint | ||
| ExtraData string | ||
| } | ||
|
|
||
| // NodeMetadata to hold metadata for a node | ||
| type NodeMetadata struct { | ||
| IPAddress string | ||
| Username string | ||
| OsqueryUser string | ||
| Hostname string | ||
| Localname string | ||
| ConfigHash string | ||
| DaemonHash string | ||
| OsqueryVersion string | ||
| Platform string | ||
| PlatformVersion string | ||
| BytesReceived int | ||
| } |
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,80 @@ | ||
| package nodes | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/jmpsec/osctrl/pkg/cache" | ||
| ) | ||
|
|
||
| const ( | ||
| cacheName = "nodes" | ||
| // Default time-to-live for cached nodes | ||
| defaultTTL = 60 * time.Minute | ||
| // Default cleanup interval for the cache | ||
| defaultCleanupInterval = 30 * time.Minute | ||
| ) | ||
|
|
||
| // NodeCache provides cached access to OsqueryNode objects | ||
| type NodeCache struct { | ||
| // The cache itself, storing OsqueryNode objects | ||
| cache *cache.MemoryCache[OsqueryNode] | ||
|
|
||
| // Reference to the node manager for cache misses | ||
| nodes *NodeManager | ||
| } | ||
|
|
||
| // NewNodeCache creates a new node cache | ||
| func NewNodeCache(nodes *NodeManager) *NodeCache { | ||
| // Create a new cache with appropriate cleanup interval | ||
| nodeCache := cache.NewMemoryCache( | ||
| cache.WithCleanupInterval[OsqueryNode](defaultCleanupInterval), | ||
| cache.WithName[OsqueryNode](cacheName), | ||
| ) | ||
|
|
||
| return &NodeCache{ | ||
| cache: nodeCache, | ||
| nodes: nodes, | ||
| } | ||
| } | ||
|
|
||
| // GetByKey retrieves a node by node_key, using cache when available | ||
| func (nc *NodeCache) GetByKey(ctx context.Context, nodeKey string) (OsqueryNode, error) { | ||
| // Try to get from cache first | ||
| if node, found := nc.cache.Get(ctx, nodeKey); found { | ||
| return node, nil | ||
| } | ||
|
|
||
| // Not in cache, fetch from database | ||
| node, err := nc.nodes.getByKeyFromDB(nodeKey) | ||
| if err != nil { | ||
| return OsqueryNode{}, err | ||
| } | ||
|
|
||
| // Store in cache for future requests | ||
| nc.cache.Set(ctx, nodeKey, node, defaultTTL) | ||
|
|
||
| return node, nil | ||
| } | ||
|
|
||
| // InvalidateNode removes a specific node from the cache | ||
| func (nc *NodeCache) InvalidateNode(ctx context.Context, nodeKey string) { | ||
| nc.cache.Delete(ctx, nodeKey) | ||
| } | ||
|
|
||
| // InvalidateAll clears the entire cache | ||
| func (nc *NodeCache) InvalidateAll(ctx context.Context) { | ||
| nc.cache.Clear(ctx) | ||
| } | ||
|
|
||
| // UpdateNodeInCache updates a node in the cache | ||
| func (nc *NodeCache) UpdateNodeInCache(ctx context.Context, node OsqueryNode) { | ||
| nc.cache.Set(ctx, node.NodeKey, node, defaultTTL) | ||
| } | ||
|
|
||
| // Close stops the cleanup goroutine and releases resources | ||
| func (nc *NodeCache) Close() { | ||
| if nc.cache != nil { | ||
| nc.cache.Stop() | ||
| } | ||
| } |
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.
Removed the dependency for types pkg. I think the redis instance has never been used in the code. But let's keep them for now.