BackgroundAudit creates an audit log for a background event. The audit log is committed upon invocation.
(ctx context.Context, p *BackgroundAuditParams[T])
| 525 | // BackgroundAudit creates an audit log for a background event. |
| 526 | // The audit log is committed upon invocation. |
| 527 | func BackgroundAudit[T Auditable](ctx context.Context, p *BackgroundAuditParams[T]) { |
| 528 | ip := database.ParseIP(p.IP) |
| 529 | |
| 530 | diff := Diff(p.Audit, p.Old, p.New) |
| 531 | var err error |
| 532 | diffRaw, err := json.Marshal(diff) |
| 533 | if err != nil { |
| 534 | p.Log.Warn(ctx, "marshal diff", slog.Error(err)) |
| 535 | diffRaw = []byte("{}") |
| 536 | } |
| 537 | |
| 538 | if p.Time.IsZero() { |
| 539 | p.Time = dbtime.Now() |
| 540 | } else { |
| 541 | // NOTE(mafredri): dbtime.Time does not currently enforce UTC. |
| 542 | p.Time = dbtime.Time(p.Time.In(time.UTC)) |
| 543 | } |
| 544 | if p.AdditionalFields == nil { |
| 545 | p.AdditionalFields = json.RawMessage("{}") |
| 546 | } |
| 547 | |
| 548 | auditLog := database.AuditLog{ |
| 549 | ID: uuid.New(), |
| 550 | Time: p.Time, |
| 551 | UserID: p.UserID, |
| 552 | OrganizationID: requireOrgID[T](ctx, p.OrganizationID, p.Log), |
| 553 | Ip: ip, |
| 554 | UserAgent: sql.NullString{Valid: p.UserAgent != "", String: p.UserAgent}, |
| 555 | ResourceType: either(p.Old, p.New, ResourceType[T], p.Action), |
| 556 | ResourceID: either(p.Old, p.New, ResourceID[T], p.Action), |
| 557 | ResourceTarget: either(p.Old, p.New, ResourceTarget[T], p.Action), |
| 558 | Action: p.Action, |
| 559 | Diff: diffRaw, |
| 560 | // #nosec G115 - Safe conversion as HTTP status code is expected to be within int32 range (typically 100-599) |
| 561 | StatusCode: int32(p.Status), |
| 562 | RequestID: p.RequestID, |
| 563 | AdditionalFields: p.AdditionalFields, |
| 564 | } |
| 565 | err = p.Audit.Export(ctx, auditLog) |
| 566 | if err != nil { |
| 567 | p.Log.Error(ctx, "export audit log", |
| 568 | slog.F("audit_log", auditLog), |
| 569 | slog.Error(err), |
| 570 | ) |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | type WorkspaceBuildBaggage struct { |
| 575 | IP string |
no test coverage detected