Skip to content
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

Merged
merged 17 commits into from
Jul 25, 2023

Conversation

ale8k
Copy link
Contributor

@ale8k ale8k commented Jul 19, 2023

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

  • Documentation updated
  • Covered by unit tests
  • Covered by integration tests

Test instructions

Notes for code reviewers

charms/jimm-k8s/config.yaml Outdated Show resolved Hide resolved
charms/jimm/config.yaml Outdated Show resolved Hide resolved
internal/db/audit.go Outdated Show resolved Hide resolved
internal/db/audit.go Outdated Show resolved Hide resolved
internal/jimm/audit_log.go Outdated Show resolved Hide resolved
internal/jimm/audit_log.go Outdated Show resolved Hide resolved
for {
select {
case <-time.After(a.calculateNextPollDuration()):
deleted, err := a.db.CleanupAuditLogs(a.ctx, a.auditLogRetentionPeriod)
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

internal/jimm/audit_log_test.go Show resolved Hide resolved
service.go Outdated Show resolved Hide resolved
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()
Copy link
Contributor

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()?

Copy link
Contributor Author

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

Copy link
Contributor

@kian99 kian99 left a 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

charms/jimm-k8s/config.yaml Outdated Show resolved Hide resolved
charms/jimm-k8s/src/charm.py Outdated Show resolved Hide resolved
internal/db/audit.go Outdated Show resolved Hide resolved
docker-compose.yaml Outdated Show resolved Hide resolved
internal/db/audit.go Outdated Show resolved Hide resolved
internal/jimm/audit_log.go Outdated Show resolved Hide resolved
internal/jimm/audit_log.go Show resolved Hide resolved
internal/jimm/audit_log_test.go Outdated Show resolved Hide resolved
0,
now.Location(),
)
return midDayUTC.Sub(now)
Copy link
Contributor

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

Copy link
Contributor Author

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.

charms/jimm-k8s/config.yaml Outdated Show resolved Hide resolved
charms/jimm/config.yaml Outdated Show resolved Hide resolved
charms/jimm/config.yaml Outdated Show resolved Hide resolved
internal/db/auditlog_test.go Show resolved Hide resolved
// 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)
Copy link
Contributor

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..

Copy link
Contributor Author

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

Copy link
Contributor Author

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

internal/jimm/audit_log.go Outdated Show resolved Hide resolved
Copy link
Contributor

@alesstimec alesstimec left a 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

{% endif %}
JIMM_AUDIT_LOG_RETENTION_PERIOD_IN_DAYS={{audit_retention_period}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this now

// 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))
Copy link
Contributor

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

Copy link
Contributor

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

// 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())
Copy link
Contributor

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()

Unscoped().
Where("time < ?", retentionDate).
Delete(&dbmodel.AuditLogEntry{})
servermon.QueryTimeAuditLogCleanUpHistogram.Observe(duration.Seconds())
Copy link
Contributor

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))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed aha

internal/jimm/audit_log.go Show resolved Hide resolved
Copy link
Contributor

@alesstimec alesstimec left a 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..

@kian99 kian99 mentioned this pull request Jul 25, 2023
3 tasks
@ale8k
Copy link
Contributor Author

ale8k commented Jul 25, 2023

LGTM.. though i'd still pass in the cutoff time to the db method..

Done that now

@ale8k ale8k merged commit a4d3325 into canonical:feature-rebac Jul 25, 2023
7 checks passed
internal/jimm/audit_log.go Show resolved Hide resolved
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()
Copy link
Contributor

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()
Copy link
Contributor

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()
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants