fix(state): classify audit coverage rows and keep unknown identity orgs private
This commit is contained in:
6 files changed
+65
-29
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
@@ -31,11 +32,17 @@ func insertIdentityEvent(ctx context.Context, q *postgresdb.Queries, orgID org.O
|
||||
if audit.ActorID == "" || audit.Source == "" || audit.RequestID == "" {
|
||||
return fmt.Errorf("%w: audit actor, source, and request ID are required", identity.ErrInvalid)
|
||||
}
|
||||
// New events always name their org. Only historical rows, backfilled before
|
||||
// an org was recorded, may be NULL.
|
||||
if orgID == "" {
|
||||
orgID = org.DefaultOrgID
|
||||
}
|
||||
return q.InsertIdentityEvent(ctx, postgresdb.InsertIdentityEventParams{
|
||||
IdentityID: string(id), EventType: string(ev.Type),
|
||||
FromLifecycle: string(ev.From), ToLifecycle: string(ev.To),
|
||||
DisplayName: ev.DisplayName, ActorID: string(audit.ActorID),
|
||||
Source: audit.Source, RequestID: audit.RequestID, At: at, OrgID: string(orgID),
|
||||
Source: audit.Source, RequestID: audit.RequestID, At: at,
|
||||
OrgID: pgtype.Text{String: string(orgID), Valid: true},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -2,32 +2,31 @@
|
||||
-- memberships to decide which org could see them, so removing a member erased
|
||||
-- their earlier lifecycle rows from that org's timeline. org_id on the event
|
||||
-- makes the org a first-class, immutable fact recorded when the event happens.
|
||||
--
|
||||
-- Historical rows have no recorded org. A membership row carries only the
|
||||
-- moment it started (no end), so the membership in effect at an event is the
|
||||
-- latest one created at or before it. An agent belongs to one org, so its
|
||||
-- assignment is the next best answer. A row with neither stays NULL and is
|
||||
-- visible only to an instance administrator: attributing it to the system org
|
||||
-- would show one org's history to every non-admin member of that org.
|
||||
|
||||
-- +goose Up
|
||||
ALTER TABLE identity_events ADD COLUMN org_id text NOT NULL DEFAULT 'org-sys';
|
||||
ALTER TABLE identity_events ADD CONSTRAINT identity_events_org_nonempty CHECK (org_id <> '');
|
||||
ALTER TABLE identity_events ADD COLUMN org_id text;
|
||||
ALTER TABLE identity_events ADD CONSTRAINT identity_events_org_nonempty
|
||||
CHECK (org_id IS NULL OR org_id <> '');
|
||||
|
||||
-- An agent belongs to exactly one org, so its assignment is authoritative.
|
||||
-- For a person, use the membership that was already in place when the event
|
||||
-- happened; a create writes the membership first, so its event resolves. A
|
||||
-- membership that predates the event is the best remaining answer; a person
|
||||
-- with none belongs to the system org.
|
||||
UPDATE identity_events ie SET org_id = COALESCE(
|
||||
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = ie.identity_id),
|
||||
(SELECT m.org_id FROM memberships m
|
||||
WHERE m.identity_id = ie.identity_id AND m.created_at <= ie.at
|
||||
ORDER BY m.created_at DESC LIMIT 1),
|
||||
(SELECT m.org_id FROM memberships m
|
||||
WHERE m.identity_id = ie.identity_id
|
||||
ORDER BY m.created_at ASC LIMIT 1),
|
||||
'org-sys'
|
||||
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = ie.identity_id)
|
||||
);
|
||||
|
||||
DROP INDEX idx_identity_events_at;
|
||||
-- Keep the instance-admin read ordered by time as well: its predicate has no
|
||||
-- org equality, so the org-leading index alone would not serve it.
|
||||
CREATE INDEX idx_identity_events_org_at ON identity_events (org_id, at DESC, id DESC);
|
||||
|
||||
-- +goose Down
|
||||
DROP INDEX idx_identity_events_org_at;
|
||||
CREATE INDEX idx_identity_events_at ON identity_events (at DESC, id DESC);
|
||||
ALTER TABLE identity_events DROP CONSTRAINT identity_events_org_nonempty;
|
||||
ALTER TABLE identity_events DROP COLUMN org_id;
|
||||
@@ -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 {
|
||||
|
||||
@@ -21,7 +21,13 @@
|
||||
-- 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.
|
||||
|
||||
-- name: ListAudit :many
|
||||
WITH sys_changes AS (
|
||||
@@ -45,6 +51,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 = sqlc.arg(org_id)::text
|
||||
@@ -185,7 +197,8 @@ SELECT * FROM (
|
||||
''::text AS data_text
|
||||
FROM identity_events ie
|
||||
WHERE ie.event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
|
||||
AND (sqlc.arg(instance_admin)::boolean OR ie.org_id = sqlc.arg(org_id)::text)
|
||||
AND (sqlc.arg(instance_admin)::boolean
|
||||
OR (ie.org_id IS NOT NULL AND ie.org_id = sqlc.arg(org_id)::text))
|
||||
AND (sqlc.narg(from_at)::timestamptz IS NULL OR ie.at >= sqlc.narg(from_at)::timestamptz)
|
||||
AND (sqlc.narg(to_at)::timestamptz IS NULL OR ie.at < sqlc.narg(to_at)::timestamptz)
|
||||
) si
|
||||
@@ -360,12 +373,13 @@ SELECT * FROM (
|
||||
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,
|
||||
@@ -405,7 +419,8 @@ LIMIT sqlc.arg(page_limit)::bigint;
|
||||
SELECT
|
||||
(SELECT COALESCE(max(id), 0)::bigint FROM sys_audit WHERE org_id = sqlc.arg(org_id)::text) AS sys_audit_id,
|
||||
(SELECT COALESCE(max(id), 0)::bigint FROM identity_events
|
||||
WHERE (sqlc.arg(instance_admin)::boolean OR org_id = sqlc.arg(org_id)::text)) AS identity_event_id,
|
||||
WHERE (sqlc.arg(instance_admin)::boolean
|
||||
OR (org_id IS NOT NULL AND org_id = sqlc.arg(org_id)::text))) AS identity_event_id,
|
||||
(SELECT COALESCE(max(id), 0)::bigint FROM access_denials WHERE org_id = sqlc.arg(org_id)::text) AS access_denial_id,
|
||||
(SELECT COALESCE(sum(count), 0)::bigint FROM access_denials WHERE org_id = sqlc.arg(org_id)::text) AS access_denial_count,
|
||||
(SELECT COALESCE(max(id), 0)::bigint FROM events WHERE org_id = sqlc.arg(org_id)::text) AS event_id;
|
||||
Reference in new issue
Block a user