Closed amirmalka closed 7 months ago
PR Description updated to latest commit (https://github.com/kubescape/node-agent/commit/4de6593d6054995abff1c9d42e10a6139fb25300)
⏱️ Estimated effort to review [1-5] | 3, because the PR involves changes in data structures and their management, which requires a detailed understanding of the existing system and the implications of these changes. The changes are spread across multiple files and include modifications to core functionalities like exec tracking and rule processing, which are critical for the application's performance and correctness. |
🧪 Relevant tests | Yes |
🔍 Possible issues | Possible Bug: The removal of sorting for `execEvent.Args` in `R0001UnexpectedProcessLaunched` and `execCall.Args` in the application profile might lead to inconsistencies in exec matching if the args are not always provided in the same order. This could potentially allow unexpected processes to be launched without being detected. |
Data Integrity: The change from sets to slices for exec tracking might introduce issues with duplicate exec entries if not handled properly. The PR seems to address this by using a hash function to generate unique identifiers for execs, but this needs thorough testing to ensure no duplicates are missed and no false unique entries are created. | |
🔒 Security concerns | No |
Category | Suggestions |
Possible issue |
Add nil check before using the
___
**Consider checking for nil before using |
Enhancement |
Standardize the data structures for execs storage to improve type safety and code clarity.___ **Use a consistent data structure forsavedExecs and toSaveExecs to ensure type safety and avoid confusion.** [pkg/applicationprofilemanager/v1/applicationprofile_manager.go [46-51]](https://github.com/kubescape/node-agent/pull/265/files#diff-fc815317651e17975c117749e7661127dbcde82fd9d4d36ebc76cb5b09b3c54eR46-R51) ```diff -savedExecs maps.SafeMap[string, *maps.SafeMap[string, []string]] -toSaveExecs maps.SafeMap[string, *maps.SafeMap[string, []string]] +savedExecs maps.SafeMap[string, maps.SafeMap[string, []string]] +toSaveExecs maps.SafeMap[string, maps.SafeMap[string, []string]] ``` |
Use a configurable parameter for domain suffix checks to enhance flexibility.___ **Replace the hardcoded domain suffix with a configurable parameter to enhance flexibilityand maintainability.** [pkg/ruleengine/v1/r0005_unexpected_domain_request.go [71-72]](https://github.com/kubescape/node-agent/pull/265/files#diff-fe45fd0bea6a18b7edee9a1e285e10ce183b9ae962a0d5e441c4bfbd33ab47afR71-R72) ```diff -if strings.HasSuffix(domainEvent.DNSName, "svc.cluster.local.") { +if strings.HasSuffix(domainEvent.DNSName, config.ClusterDomainSuffix) { return nil } ``` | |
Maintainability |
Refactor execs setting logic into a method to reduce duplication and improve maintainability.___ **Instead of manually checking and setting each exec, consider using a method thatencapsulates this logic to reduce code duplication and improve maintainability.** [pkg/applicationprofilemanager/v1/applicationprofile_manager.go [456-461]](https://github.com/kubescape/node-agent/pull/265/files#diff-fc815317651e17975c117749e7661127dbcde82fd9d4d36ebc76cb5b09b3c54eR456-R461) ```diff -toSaveExecs.Range(func(uniqueExecIdentifier string, v []string) bool { - if !am.toSaveExecs.Get(watchedContainer.K8sContainerID).Has(uniqueExecIdentifier) { - am.toSaveExecs.Get(watchedContainer.K8sContainerID).Set(uniqueExecIdentifier, v) - } - return true -}) +toSaveExecs.Range(am.safeSetExecs(watchedContainer.K8sContainerID)) ``` |
Refactor repetitive patch operations into a loop or function for better maintainability.___ **Consider using a loop or a function to handle repetitive patch operations to enhance codemaintainability and reduce errors.** [pkg/utils/utils.go [328-336]](https://github.com/kubescape/node-agent/pull/265/files#diff-81ddbadfb415ccbb9c7af84f11668d1aa5e53c34025bf86d4702f16b4e42f045R328-R336) ```diff -existingPatch = append(existingPatch, - PatchOperation{ +patchOps := []PatchOperation{ + { Op: "replace", Path: "/metadata/annotations/" + EscapeJSONPointerElement(helpersv1.StatusMetadataKey), Value: string(watchedContainer.status), }, - PatchOperation{ + { Op: "replace", Path: "/metadata/annotations/" + EscapeJSONPointerElement(helpersv1.CompletionMetadataKey), + } +} +existingPatch = append(existingPatch, patchOps...) ``` | |
Bug |
Properly initialize new
___
**Ensure that the new |
Performance |
Optimize the setting of execs in the map by using a method that checks and sets atomically.___ **Use a more efficient method for setting execs in thetoSaveExecs map to avoid unnecessary checks and improve performance.** [pkg/applicationprofilemanager/v1/applicationprofile_manager.go [456-461]](https://github.com/kubescape/node-agent/pull/265/files#diff-fc815317651e17975c117749e7661127dbcde82fd9d4d36ebc76cb5b09b3c54eR456-R461) ```diff toSaveExecs.Range(func(uniqueExecIdentifier string, v []string) bool { - if !am.toSaveExecs.Get(watchedContainer.K8sContainerID).Has(uniqueExecIdentifier) { - am.toSaveExecs.Get(watchedContainer.K8sContainerID).Set(uniqueExecIdentifier, v) - } + am.toSaveExecs.Get(watchedContainer.K8sContainerID).SetIfAbsent(uniqueExecIdentifier, v) return true }) ``` |
Best practice |
Check for errors when writing to a hash to ensure data integrity.___ **Ensure that thehash.Write method's error is checked to handle potential failures in hash computation.** [pkg/utils/utils.go [458]](https://github.com/kubescape/node-agent/pull/265/files#diff-81ddbadfb415ccbb9c7af84f11668d1aa5e53c34025bf86d4702f16b4e42f045R458-R458) ```diff -hash.Write([]byte(fmt.Sprintf("%s;%v", path, args))) +if _, err := hash.Write([]byte(fmt.Sprintf("%s;%v", path, args))); err != nil { + return "", err +} ``` |
Add logging for ignored in-cluster communications to improve traceability.___ **Consider adding a debug or info log statement before returning nil for better traceabilityof the decision-making process.** [pkg/ruleengine/v1/r0005_unexpected_domain_request.go [71-72]](https://github.com/kubescape/node-agent/pull/265/files#diff-fe45fd0bea6a18b7edee9a1e285e10ce183b9ae962a0d5e441c4bfbd33ab47afR71-R72) ```diff if strings.HasSuffix(domainEvent.DNSName, "svc.cluster.local.") { + log.Info("Ignoring in-cluster communication for domain:", domainEvent.DNSName) return nil } ``` |
:sparkles: Artifacts are available here.
:sparkles: Artifacts are available here.
:sparkles: Artifacts are available here.
:sparkles: Artifacts are available here.
:sparkles: Artifacts are available here.
Summary:
:sparkles: Artifacts are available here.
:sparkles: Artifacts are available here.
User description
Application profile fixes:
Rule Manager:
Overview
Type
Enhancement, Bug fix
Description
ApplicationProfileManager
to use slices instead of sets for efficiency and better management.R0001UnexpectedProcessLaunched
by removing unnecessary sorting.R0005UnexpectedDomainRequest
.CalculateSHA256FileExecHash
for generating unique identifiers for execs, with corresponding tests.Changes walkthrough
applicationprofile_manager.go
Refactor exec tracking from set to slice for efficiency
pkg/applicationprofilemanager/v1/applicationprofile_manager.go
mapset.Set[string]
to
[]string
to handle execs more efficiently.saveProfile
,ReportFileExec
, and error handling sections.various lifecycle methods.
r0001_unexpected_process_launched.go
Simplify exec comparison in process event rule
pkg/ruleengine/v1/r0001_unexpected_process_launched.go
handling logic.
r0005_unexpected_domain_request.go
Enhance domain request rule to ignore in-cluster DNS
pkg/ruleengine/v1/r0005_unexpected_domain_request.go - Added a check to ignore in-cluster DNS requests.
applicationprofile.go
Update utility functions for new execs data structure
pkg/utils/applicationprofile.go
CreateCapabilitiesPatchOperations
andEnrichApplicationProfileContainer
to support the new execs datastructure.
utils.go
Add utility function for exec identification
pkg/utils/utils.go
CalculateSHA256FileExecHash
to generateunique identifiers for execs.
applicationprofile_manager_test.go
Update tests for new exec tracking logic
pkg/applicationprofilemanager/v1/applicationprofile_manager_test.go
reports.
logic for execs.
utils_test.go
Implement tests for new exec hash utility function
pkg/utils/utils_test.go
CalculateSHA256FileExecHash
utility function.