Skip to content

Commit

Permalink
Update purge audit to purge both audit_log_ext and audit_log
Browse files Browse the repository at this point in the history
Signed-off-by: stonezdj <[email protected]>
  • Loading branch information
stonezdj committed Feb 10, 2025
1 parent fee92c5 commit b972aa9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/jobservice/job/impl/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/pkg/audit"
"github.com/goharbor/harbor/src/pkg/auditext"
)

// Job defines the purge job
Expand All @@ -29,6 +30,7 @@ type Job struct {
includeOperations []string
dryRun bool
auditMgr audit.Manager
auditExtMgr auditext.Manager
}

// MaxFails is implementation of same method in Interface.
Expand Down Expand Up @@ -106,11 +108,19 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
if j.retentionHour > common.MaxAuditRetentionHour {
j.retentionHour = common.MaxAuditRetentionHour
}
// TODO: Remove the previous purge logic when the audit_log is not used anymore
n, err := j.auditMgr.Purge(ormCtx, j.retentionHour, j.includeOperations, j.dryRun)
if err != nil {
logger.Errorf("failed to purge audit log, error: %v", err)
return err
}
// purge the audit_log_ext table
n2, err2 := j.auditExtMgr.Purge(ormCtx, j.retentionHour, j.includeOperations, j.dryRun)
if err2 != nil {
logger.Errorf("failed to purge audit log ext, error: %v", err2)
return err2
}
n += n2
logger.Infof("Purge operation parameter, retention_hour=%v, include_operations:%v, dry_run:%v",
j.retentionHour, j.includeOperations, j.dryRun)
if j.dryRun {
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/auditext/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ func permitEventTypes(includeEventTypes []string) []string {
event := strings.ToLower(e)
if utils.StringInSlice(event, model.EventTypes) {
filterEvents = append(filterEvents, e)
} else if event == model.OtherEvents { // include all other events
filterEvents = append(filterEvents, model.OtherEventTypes...)
}
}
return filterEvents
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/auditext/dao/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,9 @@ func TestPermitEventTypes(t *testing.T) {
t.Errorf("permitEventTypes failed")
}

// test other event types
otherEventTypes := permitEventTypes([]string{"create_artifact", "delete_artifact", "pull_artifact", "other_events"})
if len(otherEventTypes) != len(model.EventTypes) {
t.Errorf("permitOtherEventTypes failed, it should include all event types")
}
}
17 changes: 17 additions & 0 deletions src/pkg/auditext/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
beego_orm "github.com/beego/beego/v2/client/orm"
)

const OtherEvents = "other_events"

func init() {
beego_orm.RegisterModel(&AuditLogExt{})
}
Expand Down Expand Up @@ -60,3 +62,18 @@ var EventTypes = []string{
"delete_robot",
"update_configure",
}

// OtherEventTypes defines the types of other audit log event types excludes previous EventTypes: create_artifact, delete_artifact, pull_artifact
var OtherEventTypes = []string{
"create_project",
"delete_project",
"delete_repository",
"login_user",
"logout_user",
"create_user",
"delete_user",
"update_user",
"create_robot",
"delete_robot",
"update_configure",
}

0 comments on commit b972aa9

Please sign in to comment.