feat(webui): audit feed API, export and read_audit grant

This commit is contained in:
sam committed 2026-10-25 22:01:02 +11:00
1 parent 6ffbf0e2a2
commit 717f9d5219
23 files changed
+969 -626

No files matched your search

@@ -10,66 +10,6 @@ import (
"time"
)
const auditForRecords = `-- name: AuditForRecords :many
SELECT id, at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id
FROM sys_audit
WHERE table_name = $1::text AND record_key = ANY($2::text[])
ORDER BY id DESC
LIMIT $3::bigint
`
type AuditForRecordsParams struct {
TableName string
RecordKeys []string
EntryLimit int64
}
type AuditForRecordsRow struct {
ID int64
At time.Time
TableName string
RecordKey string
Field string
OldValue []byte
NewValue []byte
RecordVersion int64
Actor string
Source string
RequestID string
}
func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]AuditForRecordsRow, error) {
rows, err := q.db.Query(ctx, auditForRecords, arg.TableName, arg.RecordKeys, arg.EntryLimit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AuditForRecordsRow
for rows.Next() {
var i AuditForRecordsRow
if err := rows.Scan(
&i.ID,
&i.At,
&i.TableName,
&i.RecordKey,
&i.Field,
&i.OldValue,
&i.NewValue,
&i.RecordVersion,
&i.Actor,
&i.Source,
&i.RequestID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertAudit = `-- name: InsertAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id)
VALUES (now(), $1::text, $2::text, $3::text,
@@ -24,7 +24,6 @@ INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value,
VALUES (now(), sqlc.arg(table_name)::text, sqlc.arg(record_key)::text, sqlc.arg(field)::text,
sqlc.arg(old_value)::jsonb, sqlc.arg(new_value)::jsonb, sqlc.arg(record_version)::bigint,
sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text, sqlc.arg(org_id)::text);
-- name: TryAuditPruneLock :one
-- The retention sweep's transaction-scoped lock. A second sweep skips its
-- tick rather than queuing deletes behind this one.
@@ -41,10 +40,3 @@ WHERE id IN (
ORDER BY at
LIMIT sqlc.arg(batch)::bigint
);
-- name: AuditForRecords :many
SELECT id, at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id
FROM sys_audit
WHERE table_name = sqlc.arg(table_name)::text AND record_key = ANY(sqlc.arg(record_keys)::text[])
ORDER BY id DESC
LIMIT sqlc.arg(entry_limit)::bigint;
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"math"
"strings"
"time"
@@ -86,29 +85,6 @@ func checkResourceKey(orgID, kind string) error {
return nil
}
// Audit returns the field-level audit of the named records of one table,
// newest first. A limit of zero or less returns every entry.
func (s *Resources) Audit(ctx context.Context, table string, keys []string, limit int) ([]storecontract.AuditEntry, error) {
if limit <= 0 {
limit = math.MaxInt32
}
rows, err := postgresdb.New(s.pool).AuditForRecords(ctx, postgresdb.AuditForRecordsParams{
TableName: table, RecordKeys: keys, EntryLimit: int64(limit),
})
if err != nil {
return nil, err
}
entries := make([]storecontract.AuditEntry, 0, len(rows))
for _, row := range rows {
entries = append(entries, storecontract.AuditEntry{
ID: row.ID, Table: row.TableName, RecordKey: row.RecordKey, Field: row.Field,
OldValue: row.OldValue, NewValue: row.NewValue, Version: row.RecordVersion,
Actor: row.Actor, Source: row.Source, RequestID: row.RequestID, At: row.At,
})
}
return entries, nil
}
// PutResource applies one control-plane resource write, idempotent per
// request ID.
func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) {