// Audit feed: the read model over every durable trail the State Store owns, // mapped into internal/domain/audit's row shape. Every entry is redacted here, // before it crosses the gRPC boundary; undecodable JSON is dropped rather than // passed through. package postgres import ( "cmp" "context" "encoding/json" "fmt" "slices" "time" "github.com/jackc/pgx/v5/pgtype" "github.com/samcharles93/archie-core/internal/domain/audit" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" "github.com/samcharles93/archie-core/internal/webhookguard" ) var _ audit.Store = (*Store)(nil) // auditFieldJSON is one field of a change set on the wire: two JSON values, // either of which is null when that side of the change is absent. type auditFieldJSON struct { Old json.RawMessage `json:"old"` New json.RawMessage `json:"new"` } // ListAudit returns one org-scoped page of the feed. The org comes from the // request context; a request that names no org reads the system org, which is // the org an unattributed call acts in. func (s *Store) ListAudit(ctx context.Context, filter audit.Filter, cursor audit.Cursor, limit int) (audit.Page, error) { if err := filter.Normalize(); err != nil { return audit.Page{}, err } limit = audit.NormalizeLimit(limit) orgID := org.DefaultOrgID if scoped, ok := org.Scope(ctx); ok { orgID = scoped } rows, err := s.queries().ListAudit(ctx, postgresdb.ListAuditParams{ PageLimit: int64(limit + 1), OrgID: string(orgID), InstanceAdmin: audit.InstanceAdmin(ctx), Categories: filter.Categories, FromAt: auditTime(filter.From), ToAt: auditTime(filter.To), Actor: auditText(filter.Actor), Actions: filter.Actions, Kinds: filter.Kinds, TargetKinds: filter.TargetKinds, TargetIds: filter.TargetIDs, Severities: filter.Severities, Outcomes: filter.Outcomes, Q: auditText(filter.Query), CursorAt: auditTime(cursor.At), CursorRank: int32(cursor.Rank), CursorID: cursor.ID, }) if err != nil { return audit.Page{}, fmt.Errorf("list audit: %w", err) } page := audit.Page{} if len(rows) > limit { page.HasMore = true rows = rows[:limit] } page.Entries = make([]audit.Entry, len(rows)) for i := range rows { page.Entries[i] = auditEntry(rows[i]) } if page.HasMore { last := page.Entries[len(page.Entries)-1] page.NextCursor = audit.Cursor{ At: last.At, Rank: last.Rank, ID: last.SourceID, FilterHash: filter.Hash(), }.Encode() } return page, nil } // AuditHead returns the cheap change token: the newest id of each trail the // caller can read, and the denial count sum because a coalesced denial bumps // its count in place without drawing a new id. func (s *Store) AuditHead(ctx context.Context) (string, error) { orgID := org.DefaultOrgID if scoped, ok := org.Scope(ctx); ok { orgID = scoped } row, err := s.queries().AuditHead(ctx, postgresdb.AuditHeadParams{ OrgID: string(orgID), InstanceAdmin: audit.InstanceAdmin(ctx), }) if err != nil { return "", fmt.Errorf("audit head: %w", err) } return fmt.Sprintf("%d:%d:%d:%d:%d", row.SysAuditID, row.IdentityEventID, row.AccessDenialID, row.AccessDenialCount, row.EventID), nil } // CountAudit returns how many entries a filter matches, with the same org and // instance scoping as a page. It counts change sets, not field rows, so the // number is exactly what an export of that filter would stream. func (s *Store) CountAudit(ctx context.Context, filter audit.Filter) (int64, error) { if err := filter.Normalize(); err != nil { return 0, err } orgID := org.DefaultOrgID if scoped, ok := org.Scope(ctx); ok { orgID = scoped } count, err := s.queries().CountAudit(ctx, postgresdb.CountAuditParams{ OrgID: string(orgID), InstanceAdmin: audit.InstanceAdmin(ctx), Categories: filter.Categories, FromAt: auditTime(filter.From), ToAt: auditTime(filter.To), Actor: auditText(filter.Actor), Actions: filter.Actions, Kinds: filter.Kinds, TargetKinds: filter.TargetKinds, TargetIds: filter.TargetIDs, Severities: filter.Severities, Outcomes: filter.Outcomes, Q: auditText(filter.Query), }) if err != nil { return 0, fmt.Errorf("count audit: %w", err) } return count, nil } // auditExportRecord is the value stored on a completed export's audit row. type auditExportRecord struct { Format string `json:"format"` FiltersHash string `json:"filters_hash"` Rows int64 `json:"rows"` } // RecordAuditExport appends the audit row of one completed export. The org, // actor and source come from the caller's context, as every sys_audit write // does. func (s *Store) RecordAuditExport(ctx context.Context, format, filterHash string, rows int64) error { orgID := org.DefaultOrgID if scoped, ok := org.Scope(ctx); ok { orgID = scoped } return insertAuditValues(ctx, s.queries(), "audit_export", "export", "export", nil, auditExportRecord{Format: format, FiltersHash: filterHash, Rows: rows}, 0, string(orgID)) } func auditTime(t time.Time) pgtype.Timestamptz { if t.IsZero() { return pgtype.Timestamptz{} } return pgtype.Timestamptz{Time: t, Valid: true} } func auditText(value string) pgtype.Text { if value == "" { return pgtype.Text{} } return pgtype.Text{String: value, Valid: true} } // auditEntry maps one read-model row to the domain entry, redacting every // free-form part. A field list or data document that cannot be decoded is // dropped, never forwarded unredacted. func auditEntry(row postgresdb.ListAuditRow) audit.Entry { return audit.Entry{ Rank: int(row.SourceRank), SourceID: row.SourceID, Category: audit.Category(row.Category), At: row.At, Actor: row.Actor, ActorKind: row.ActorKind, PrincipalID: row.PrincipalID, TargetKind: row.TargetKind, TargetID: row.TargetID, Kind: row.Kind, RecordVersion: row.RecordVersion, RequestID: row.RequestID, Source: row.Source, Fields: redactedFields(row.Fields), Action: row.Action, Severity: audit.Severity(row.Severity), Outcome: audit.Outcome(row.Outcome), Detail: webhookguard.RedactString(row.Detail), WorkspaceID: row.WorkspaceID, TaskID: row.TaskID, Data: redactedData(row.DataText), } } // redactedFields decodes the change set's fields object, redacts each side // under its own field name, and returns them sorted for a stable view. func redactedFields(encoded []byte) []audit.Field { if len(encoded) == 0 { return nil } var raw map[string]auditFieldJSON if err := json.Unmarshal(encoded, &raw); err != nil { return nil } fields := make([]audit.Field, 0, len(raw)) for name, side := range raw { fields = append(fields, audit.Field{ Name: name, Old: redactFieldValue(name, side.Old), New: redactFieldValue(name, side.New), }) } slices.SortFunc(fields, func(a, b audit.Field) int { return cmp.Compare(a.Name, b.Name) }) return fields } // redactFieldValue redacts one side of one field. Wrapping it under the field // name lets the existing redactor apply both its key and value rules, so a // secret reference survives as a hidden value while the field name stays. func redactFieldValue(name string, value json.RawMessage) json.RawMessage { if len(value) == 0 || string(value) == "null" { return value } wrapped, err := json.Marshal(map[string]json.RawMessage{name: value}) if err != nil { return nil } redacted, err := webhookguard.RedactPayload(wrapped) if err != nil { return nil } var out map[string]json.RawMessage if err := json.Unmarshal(redacted, &out); err != nil { return nil } return out[name] } // redactedData redacts an event's data document, dropping one that cannot be // decoded. An empty document is absent rather than an empty object. func redactedData(text string) json.RawMessage { if text == "" || text == "{}" { return nil } redacted, err := webhookguard.RedactPayload([]byte(text)) if err != nil { return nil } return redacted }