Skip to content

Commit

Permalink
Added cleanup for audit events
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Dec 20, 2024
1 parent a3c01d9 commit 3842f63
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
31 changes: 27 additions & 4 deletions internal/server/audit_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package server

import (
"cmp"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -38,8 +39,8 @@ func (s *Server) initAuditDB(connectString string) error {
return err
}

cleanupTicker := time.NewTicker(5 * time.Minute)
go s.auditCleanup(cleanupTicker)
cleanupTicker := time.NewTicker(1 * time.Hour)
go s.auditCleanupLoop(cleanupTicker)
return nil
}

Expand Down Expand Up @@ -105,11 +106,28 @@ func (s *Server) InsertAuditEvent(event *types.AuditEvent) error {
}

func (s *Server) cleanupEvents() error {
// TODO: Implement cleanup
httpCleanupTime := time.Now().Add(-time.Duration(s.config.System.HttpEventRetentionDays) * 24 * time.Hour).UnixNano()
nonHttpCleanupTime := time.Now().Add(-time.Duration(s.config.System.NonHttpEventRetentionDays) * 24 * time.Hour).UnixNano()

httpResult, err := s.auditDB.Exec(`delete from audit where event_type = "http" and create_time < ?`, httpCleanupTime)
if err != nil {
return err
}
nonHttpResult, err := s.auditDB.Exec(`delete from audit where event_type != "http" and create_time < ?`, nonHttpCleanupTime)
if err != nil {
return err
}

httpDeleted, err1 := httpResult.RowsAffected()
nonHttpDeleted, err2 := nonHttpResult.RowsAffected()
if cmp.Or(err1, err2) != nil {
return cmp.Or(err1, err2)
}
s.Info().Msgf("audit cleanup: http deleted %d, non-http deleted %d", httpDeleted, nonHttpDeleted)
return nil
}

func (s *Server) auditCleanup(cleanupTicker *time.Ticker) {
func (s *Server) auditCleanupLoop(cleanupTicker *time.Ticker) {
err := s.cleanupEvents()
if err != nil {
fmt.Fprintf(os.Stderr, "error cleaning up audit entries %s", err)
Expand Down Expand Up @@ -200,6 +218,11 @@ func (server *Server) handleStatus(next http.Handler) http.Handler {
if err != nil {
return
}

if app.AppConfig.Audit.SkipHttpEvents {
// http event auditing is disabled for this app
return
}
redactUrl = app.AppConfig.Audit.RedactUrl
}

Expand Down
4 changes: 4 additions & 0 deletions internal/system/clace.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ disable_unknown_domains = true # disable unknown domains, if default domain
root_serve_list_apps = "auto" # "auto" means serve list_apps app for default domain, "disable" means don't server for any domain,
# any other value means server for specified domain

http_event_retention_days = 90 # number of days to retain http events
non_http_event_retention_days = 180 # number of days to retain non-http (system, action, custom) events

[plugin."store.in"]
db_connection = "sqlite:$CL_HOME/metadata/clace_app_store.db"

Expand Down Expand Up @@ -105,3 +108,4 @@ fs.file_access = ["$TEMPDIR", "/tmp"]

# Audit related settings
audit.redact_url = false
audit.skip_http_events = false
6 changes: 5 additions & 1 deletion internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ type FS struct {
}

type Audit struct {
RedactUrl bool `toml:"redact_url"`
RedactUrl bool `toml:"redact_url"`
SkipHttpEvents bool `toml:"skip_http_events"`
}

type Container struct {
Expand Down Expand Up @@ -198,6 +199,9 @@ type SystemConfig struct {
DefaultDomain string `toml:"default_domain"`
DisableUnknownDomains bool `toml:"disable_unknown_domains"`
RootServeListApps string `toml:"root_serve_list_apps"`

HttpEventRetentionDays int `toml:"http_event_retention_days"`
NonHttpEventRetentionDays int `toml:"non_http_event_retention_days"`
}

// GitAuth is a github auth config entry
Expand Down

0 comments on commit 3842f63

Please sign in to comment.