mrheinen / lophiid

A distributed honeypot for monitoring large scale web attacks
GNU General Public License v2.0
6 stars 1 forks source link

Add ratelimited event, add new time field to IP event #33

Closed mrheinen closed 1 month ago

mrheinen commented 1 month ago

PR Type

Enhancement, Bug fix


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
6 files
ip.go
Add FirstSeenAt field to IP events                                             

pkg/analysis/ip.go
  • Added FirstSeenAt field to IpEvent struct, set to current UTC time
    when processing a new event.
  • +1/-0     
    backend.go
    Implement rate-limited event logging                                         

    pkg/backend/backend.go
  • Added logic to create and store a rate-limited IP event when a request
    is not allowed by the rate limiter.
  • +8/-0     
    database.go
    Add FirstSeenAt field to IpEvent struct                                   

    pkg/database/database.go
  • Added FirstSeenAt field to IpEvent struct.
  • Updated struct tags for FirstSeenAt field.
  • +17/-15 
    shared_constants.go
    Add constant for rate-limited events                                         

    pkg/util/constants/shared_constants.go - Added new constant `IpEventRateLimited` with value "RATELIMITED".
    +1/-0     
    EventsList.vue
    Add FirstSeenAt to IP events UI                                                   

    ui/src/components/container/EventsList.vue
  • Added "First Seen" column to events table.
  • Updated event parsing to include first_seen_at field.
  • +7/-0     
    database.sql
    Update database schema for rate-limited events                     

    config/database.sql
  • Added 'RATELIMITED' to IP_EVENT_TYPE enum.
  • Added first_seen_at column to ip_event table.
  • +2/-1     
    Tests
    1 files
    backend_test.go
    Add tests for rate-limited events                                               

    pkg/backend/backend_test.go - Added test cases to verify the creation of rate-limited IP events.
    +8/-0     
    Formatting
    1 files
    ratelimit.go
    Minor code cleanup                                                                             

    pkg/backend/ratelimit/ratelimit.go - Removed an empty comment line.
    +0/-1     
    Bug fix
    1 files
    ContentForm.vue
    Fix header formatting in ContentForm                                         

    ui/src/components/container/ContentForm.vue - Modified header concatenation logic to avoid trailing newline.
    +3/-1     

    ๐Ÿ’ก PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    github-actions[bot] commented 1 month ago

    Preparing PR description...

    github-actions[bot] commented 1 month ago

    Preparing review...

    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR Reviewer Guide ๐Ÿ”

    โฑ๏ธ Estimated effort to review: 3 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ตโšชโšช
    ๐Ÿงช PR contains tests
    ๐Ÿ”’ No security concerns identified
    โšก Key issues to review

    Error Handling
    The error returned from `s.rateLimiter.AllowRequest(sReq)` is used in the `Details` field of the new IP event, but it's not checked for nil before using `err.Error()`. This could potentially cause a panic if `err` is nil. Unused Variable
    The `customHeaderTmp` variable is assigned but never used. This could be a leftover from the previous implementation or an oversight.
    codiumai-pr-agent-pro[bot] commented 1 month ago

    PR Code Suggestions โœจ

    CategorySuggestion                                                                                                                                    Score
    Error handling
    Add error handling for the AddEvent operation ___ **Consider adding error handling for the AddEvent call. If the operation fails, it's
    important to log the error or take appropriate action.** [pkg/backend/backend.go [620-626]](https://github.com/mrheinen/lophiid/pull/33/files#diff-c65bcfe9bb457434c3e69ba3f0576d7669935f350d24e2c2c58b05b4f9c510b2R620-R626) ```diff -s.ipEventManager.AddEvent(&database.IpEvent{ +if err := s.ipEventManager.AddEvent(&database.IpEvent{ IP: sReq.SourceIP, Type: constants.IpEventRateLimited, Details: err.Error(), Source: constants.IpEventSourceBackend, HoneypotIP: sReq.HoneypotIP, -}) +}); err != nil { + // Log the error or handle it appropriately + s.logger.Error("Failed to add rate limit event", "error", err) +} ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: Error handling is crucial for robust code. The suggestion correctly identifies a potential issue and provides a solution to improve reliability.
    8
    Enhancement
    Use array join method for string concatenation ___ **Consider using join() method instead of manual string concatenation for better
    readability and performance when joining headers.** [ui/src/components/container/ContentForm.vue [383-390]](https://github.com/mrheinen/lophiid/pull/33/files#diff-9b268cc1c4742f937010b6345e94bb8445f5bdb2238dfdc27d660c8cd000dcd3R383-R390) ```diff -var customHeaderTmp = ""; if (this.localContent.headers) { - var prefix = ""; - this.localContent.headers.forEach((header) => { - customHeaderTmp += prefix + header; - prefix = "\n"; - }); - this.customHeaders = customHeaderTmp; + this.customHeaders = this.localContent.headers.join("\n"); } ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 6 Why: The suggestion offers a more concise and potentially more efficient way to concatenate strings, improving code readability and possibly performance.
    6
    Best practice
    Use unsigned integer type for count field ___ **Consider using a more specific type for the Count field in the IpEvent struct. Since
    it represents a count, it should never be negative, so using uint64 instead of int64
    would be more appropriate.** [pkg/database/database.go [247]](https://github.com/mrheinen/lophiid/pull/33/files#diff-1adb887d06a44193c36fc1c5708be385f3129cd59c2f2aa555faa065941ed877R247-R247) ```diff -Count int64 `ksql:"count" json:"count" doc:"How often this event was seen"` +Count uint64 `ksql:"count" json:"count" doc:"How often this event was seen"` ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 5 Why: Using uint64 for a count field is a good practice as counts are never negative. However, this change might require updates in other parts of the codebase and database schema.
    5
    Use a configurable time zone instead of hardcoding UTC ___ **Consider using a constant or configuration value for the UTC time zone instead of
    hardcoding it. This would make the code more flexible if time zone requirements
    change in the future.** [pkg/analysis/ip.go [189]](https://github.com/mrheinen/lophiid/pull/33/files#diff-24480f2fb374402d21f92840858fefced2bab4fd547efd664c76acbce047103bR189-R189) ```diff -evt.FirstSeenAt = time.Now().UTC() +evt.FirstSeenAt = time.Now().In(config.TimeZone) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 4 Why: While using a configurable time zone could increase flexibility, UTC is a standard for storing timestamps and changing it might introduce complexity without clear benefits.
    4