-
Notifications
You must be signed in to change notification settings - Fork 8
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
Css 4824/expire audit logs #1007
Css 4824/expire audit logs #1007
Conversation
internal/jimm/audit_log.go
Outdated
for { | ||
select { | ||
case <-time.After(a.calculateNextPollDuration()): | ||
deleted, err := a.db.CleanupAuditLogs(a.ctx, a.auditLogRetentionPeriod) |
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.
i think it would be very useful for us to monitor the duration of cleanup each day.. could you please add metrics for this and the number of deleted audit entries?
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.
Will do! Will update this comment when done
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.
Added histogram and haven't added total number of deleted logs, because they're gone anyway?
service.go
Outdated
if err != nil { | ||
return nil, errors.E(op, "failed to parse audit log retention period") | ||
} | ||
jimm.NewAuditLogCleanupService(ctx, s.jimm.Database, period).Start() |
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.
hmmm.. could we then just have a single method like StartAuditLogCleanupService instead of New().Start()?
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.
I prefer creation of service then start honestly, it's a nice separation of concerns, constructor vs behaviour
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.
lgtm with some tweaks
internal/jimm/audit_log.go
Outdated
0, | ||
now.Location(), | ||
) | ||
return midDayUTC.Sub(now) |
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.
A little bit confused by this function, we take the current year,month,day and the polling hours,minutes, seconds then we subtract the current time. I'll just think on this some more
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.
Good spot, negative hours will break this. It needs solving. Will update comment.
internal/jimm/audit_log.go
Outdated
// for absolute consistency within ns apart. | ||
func (a *auditLogCleanupService) calculateNextPollDuration() time.Duration { | ||
now := time.Now().UTC() | ||
nineAM := time.Date(now.Year(), now.Month(), now.Day(), pollDuration.Hours, 0, 0, 0, time.UTC) |
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.
shouldn't this be time.Date(now.Year(), now.Month(), now.Day(), pollDuration.Hours, 0, 0, 0, time.UTC).Add(0,0,1) ... so nine am tomorrow.. not today..
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.
That's handled below, but we need to check if its a negative duration first then turn it absolute
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.
also have added teststo show it work
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.
monitoring values are wrong, i think
charms/jimm/templates/jimm.env
Outdated
{% endif %} | ||
JIMM_AUDIT_LOG_RETENTION_PERIOD_IN_DAYS={{audit_retention_period}} |
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.
new line, please?
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.
Doing this now
internal/db/audit.go
Outdated
// CleanupAuditLogs cleans up audit logs after the auditLogRetentionPeriodInDays, | ||
// HARD deleting them from the database. | ||
func (d *Database) CleanupAuditLogs(ctx context.Context, auditLogRetentionPeriodInDays int) (int64, error) { | ||
retentionDate := time.Now().AddDate(0, 0, -(auditLogRetentionPeriodInDays)) |
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.
we might run into problems testing because time is calculated here.. would be better to pass in the cut-off date.. better for testability
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.
This function is present in both this and #1008 so please discuss and align them
internal/db/audit.go
Outdated
// HARD deleting them from the database. | ||
func (d *Database) CleanupAuditLogs(ctx context.Context, auditLogRetentionPeriodInDays int) (int64, error) { | ||
retentionDate := time.Now().AddDate(0, 0, -(auditLogRetentionPeriodInDays)) | ||
duration := time.Since(time.Now()) |
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.
this doesn't work.. instead
start := time.Now()
internal/db/audit.go
Outdated
Unscoped(). | ||
Where("time < ?", retentionDate). | ||
Delete(&dbmodel.AuditLogEntry{}) | ||
servermon.QueryTimeAuditLogCleanUpHistogram.Observe(duration.Seconds()) |
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.
then
servermon.QueryTimeAuditLogCleanUpHistogram.Observe(time.Since(start))
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.
Fixed aha
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.
LGTM.. though i'd still pass in the cutoff time to the db method..
Done that now |
if nineAMDuration < 0 { | ||
// Add 24 hours, flip it to an absolute duration, i.e., -10h == 10h | ||
// and subtract it from 24 hours to calculate 9am tomorrow | ||
d = time.Hour*24 - nineAMDuration.Abs() |
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.
This is the same as time.Hour*24 + nineAMDuration
since we've already checked that nineAMDuration
is negative.
// and subtract it from 24 hours to calculate 9am tomorrow | ||
d = time.Hour*24 - nineAMDuration.Abs() | ||
} else { | ||
d = nineAMDuration.Abs() |
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.
We don't need the .Abs()
here because we've already checked if it's negative.
c.Assert(logs, qt.HasLen, 3) | ||
|
||
jimm.PollDuration.Hours = now.Hour() | ||
jimm.PollDuration.Minutes = now.Minute() |
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.
Setting these values isn't doing anything since we don't use them in the function.
Description
Cleans up audit logs by a configuration option set in DAYS. Hardcoded the UTC timezone to cleanup at 9AM every day.
Engineering checklist
Check only items that apply
Test instructions
Notes for code reviewers