fix(state): classify audit coverage rows and keep unknown identity orgs private

This commit is contained in:
sam committed 2026-10-25 21:25:55 +11:00
1 parent 691002eb9b
commit e2a268aef1
6 files changed
+65 -29

No files matched your search

@@ -16,7 +16,8 @@ const auditHead = `-- name: AuditHead :one
SELECT
(SELECT COALESCE(max(id), 0)::bigint FROM sys_audit WHERE org_id = $1::text) AS sys_audit_id,
(SELECT COALESCE(max(id), 0)::bigint FROM identity_events
WHERE ($2::boolean OR org_id = $1::text)) AS identity_event_id,
WHERE ($2::boolean
OR (org_id IS NOT NULL AND org_id = $1::text))) AS identity_event_id,
(SELECT COALESCE(max(id), 0)::bigint FROM access_denials WHERE org_id = $1::text) AS access_denial_id,
(SELECT COALESCE(sum(count), 0)::bigint FROM access_denials WHERE org_id = $1::text) AS access_denial_count,
(SELECT COALESCE(max(id), 0)::bigint FROM events WHERE org_id = $1::text) AS event_id
@@ -75,6 +76,12 @@ WITH sys_changes AS (
bool_and(a.old_value IS NULL) AS all_old_null,
bool_and(a.new_value IS NULL) AS all_new_null,
bool_or(a.field = 'reset') AS is_reset,
count(DISTINCT a.field)::int AS field_count,
bool_or(a.field IN (
'org', 'workspace', 'role', 'org_id',
'mapping', 'binding', 'source', 'event_type',
'install', 'remove', 'credential'
)) AS whole_record_field,
string_agg(DISTINCT a.field, ' ') AS field_names
FROM sys_audit a
WHERE a.org_id = $2::text
@@ -215,7 +222,8 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
''::text AS data_text
FROM identity_events ie
WHERE ie.event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND ($17::boolean OR ie.org_id = $2::text)
AND ($17::boolean
OR (ie.org_id IS NOT NULL AND ie.org_id = $2::text))
AND ($5::timestamptz IS NULL OR ie.at >= $5::timestamptz)
AND ($6::timestamptz IS NULL OR ie.at < $6::timestamptz)
) si
@@ -390,12 +398,13 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
g.source,
g.fields,
CASE
WHEN g.record_version = 0 AND g.all_old_null THEN 'create'
WHEN g.record_version = 0 AND g.all_new_null THEN 'delete'
WHEN g.field_count = 1 AND g.whole_record_field AND g.all_old_null THEN 'create'
WHEN g.field_count = 1 AND g.whole_record_field AND g.all_new_null THEN 'delete'
ELSE 'update'
END AS action,
CASE
WHEN g.record_version = 0 AND g.all_new_null AND NOT g.all_old_null THEN 'warning'
WHEN g.field_count = 1 AND g.whole_record_field AND g.all_new_null
AND NOT g.all_old_null THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
@@ -498,7 +507,13 @@ type ListAuditRow struct {
// Identity lifecycle rows carry the org they happened in, so removing a
// member does not erase their earlier history from that org's feed. Only a
// principal with instance-wide authority sees every org's identity events;
// being confined to the system org is not that authority.
// being confined to the system org is not that authority. A historical row
// with no defensible org stays NULL and is visible only to that authority.
//
// Coverage-table change sets are classified by their whole-record field, not
// by a version: a create is a whole-record field with a null old side, a
// delete the same field with a null new side, and everything else an update.
// A partial add or remove is never a create or delete.
func (q *Queries) ListAudit(ctx context.Context, arg ListAuditParams) ([]ListAuditRow, error) {
rows, err := q.db.Query(ctx, listAudit,
arg.PageLimit,
@@ -175,7 +175,7 @@ type InsertIdentityEventParams struct {
Source string
RequestID string
At time.Time
OrgID string
OrgID pgtype.Text
}
func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEventParams) error {
@@ -258,7 +258,7 @@ type IdentityEvent struct {
Source string
RequestID string
At time.Time
OrgID string
OrgID pgtype.Text
}
type IdentitySubject struct {