Closed mrheinen closed 1 month ago
Preparing PR description...
This prepares for moving to using a database client with generics
Preparing review...
β±οΈ Estimated effort to review: 4 π΅π΅π΅π΅βͺ |
π§ͺ PR contains tests |
π No security concerns identified |
β‘ Key issues to review API Changes Significant changes to the DatabaseClient interface, including removal of several methods and addition of new ones. This may affect other parts of the codebase that depend on these methods. Data Model Changes New fields (ExtVersion and ExtUuid) added to Content, ContentRule, and Application structs. Ensure these changes are properly reflected in the database schema and all relevant code. Error Handling The error handling in the LookupIP function has changed. Ensure that all possible error cases are properly handled and logged. |
Category | Suggestion | Score |
Performance |
Implement pagination for loading rules to improve performance with large datasets___ **Consider adding pagination to theLoadRules function to avoid potential performance issues when dealing with a large number of rules. This can be achieved by implementing a batch processing approach.** [pkg/backend/backend.go [742-743]](https://github.com/mrheinen/lophiid/pull/36/files#diff-c65bcfe9bb457434c3e69ba3f0576d7669935f350d24e2c2c58b05b4f9c510b2R742-R743) ```diff -// TODO: Add logic that allowes only active rules to be selected here -rules, err := s.dbClient.SearchContentRules(0, 9999999, "") +const batchSize = 1000 +var allRules []database.ContentRule +offset := 0 +for { + rules, err := s.dbClient.SearchContentRules(offset, batchSize, "") + if err != nil { + return err + } + allRules = append(allRules, rules...) + if len(rules) < batchSize { + break + } + offset += batchSize +} + ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 9Why: Implementing pagination for loading rules is crucial for performance when dealing with large datasets, preventing potential memory issues. | 9 |
Error handling |
Improve error handling for honeypot search results___ **Consider adding error handling for the case when no honeypots are found in theSearchHoneypots method. This will provide more robust error handling and prevent potential nil pointer dereferences.** [pkg/database/database.go [652-660]](https://github.com/mrheinen/lophiid/pull/36/files#diff-1adb887d06a44193c36fc1c5708be385f3129cd59c2f2aa555faa065941ed877R652-R660) ```diff dms, err := s.dbClient.SearchHoneypots(0, 1, fmt.Sprintf("ip:%s", sReq.HoneypotIP)) -if err != nil || len(hps) == 0 { - slog.Warn("error finding honeypot", slog.String("error", err.Error()), slog.String("honeypot", sReq.HoneypotIP)) +if err != nil { + slog.Error("error searching for honeypot", slog.String("error", err.Error()), slog.String("honeypot", sReq.HoneypotIP)) + matchedRule = s.safeRules.Get()[0] +} else if len(dms) == 0 { + slog.Warn("no honeypot found", slog.String("honeypot", sReq.HoneypotIP)) matchedRule = s.safeRules.Get()[0] } else { - matchedRule.ContentID = hps[0].DefaultContentID + matchedRule.ContentID = dms[0].DefaultContentID matchedRule.ID = 0 } ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 8Why: The suggestion improves error handling and provides more detailed logging, which enhances debugging and robustness of the code. | 8 |
Best practice |
Use pointer receiver for setter method to modify the original struct___ **Consider using a pointer receiver for theSetModelID method of Content struct. This allows for modifying the original struct instance rather than a copy.** [pkg/database/database.go [88]](https://github.com/mrheinen/lophiid/pull/36/files#diff-1adb887d06a44193c36fc1c5708be385f3129cd59c2f2aa555faa065941ed877R88-R88) ```diff +func (c *Content) SetModelID(id int64) { c.ID = id } func (c *Content) SetModelID(id int64) { c.ID = id } ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 7Why: Using a pointer receiver for the SetModelID method is a good practice as it allows modifying the original struct instance, improving efficiency. | 7 |
Code structure |
Restructure database query and error handling for improved readability___ **Consider moving the database query outside of the error handling block to improvecode readability and avoid unnecessary nesting. This will make the logic flow more clear and easier to maintain.** [pkg/whois/rdap.go [166-177]](https://github.com/mrheinen/lophiid/pull/36/files#diff-e28100c5c0bcf097b7a414bde2c3d8f1b0fa075281de07b908ace5e022520848R166-R177) ```diff hps, err := c.dbClient.SearchWhois(0, 1, fmt.Sprintf("ip:%s", ip)) if err != nil { slog.Error("Failed to query whois in database", slog.String("error", err.Error())) - -} else { - if len(hps) != 0 { - // Update the cache to recored we already have an entry. In the future we - // might want to look at the age of the entry. - c.ipCache.Store(ip, true) - return nil - } + return err } +if len(hps) != 0 { + // Update the cache to record we already have an entry. In the future we + // might want to look at the age of the entry. + c.ipCache.Store(ip, true) + return nil +} + ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 6Why: The suggestion improves code readability and structure, making it easier to maintain, but doesn't address a critical issue or bug. | 6 |
User description
Update and cleanup databaseclient and it's usage.
PR Type
Enhancement, Bug fix
Description
Changes walkthrough π
8 files
api_server.go
Streamline API endpoints and remove bulk retrieval
cmd/api/api_server.go
endpoints
server.go
Refactor API server handlers and remove bulk retrieval methods
pkg/api/server.go
'GetWhoisByIP'
backend.go
Refactor backend to use search-based queries
pkg/backend/backend.go
instead of 'GetHoneypotByIP'
'GetDownloadBySum'
'GetContentRules'
database.go
Refactor database models and client interface
pkg/database/database.go
external version and UUID fields
rdap.go
Update WHOIS lookup to use search-based query
pkg/whois/rdap.go
RuleForm.vue
Update RuleForm to use new app search endpoint
ui/src/components/container/RuleForm.vue - Updated API endpoint for loading apps to use segment-based search
RulesList.vue
Update RulesList to use new app search endpoint
ui/src/components/container/RulesList.vue - Updated API endpoint for loading apps to use segment-based search
database.sql
Update database schema for external versioning
config/database.sql
and app tables
1 files
backend_test.go
Update backend tests for new search-based queries
pkg/backend/backend_test.go
handling
1 files
SEARCH_KEYWORDS.md
Update search keywords documentation
SEARCH_KEYWORDS.md
Application sections
1 files
BUILD.bazel
Remove KSQL dependency from API package
pkg/api/BUILD.bazel - Removed dependency on 'com_github_vingarcia_ksql'
1 files
BUILD.bazel
Add Bazel build file for database rules
pkg/database/rules/BUILD.bazel - Added new Bazel build file for database rules package