Audit trail and impersonation#
The audit trail#
Every write through Vector (create, update, delete, restore, attach,
detach, actions, bulk and inline edits, imports, repeater rows) is
recorded in vector_audit with the actor, and the impersonator when
there is one. Creates, updates, reverts and membership changes carry the
field-level changes the writer observed, each value as the record
serialized it, so GET /vector/r/{resource}/{id}/audit answers a JSON
object as an object; deletes, restores and action runs are recorded as
the event alone (action:<name> for an action). The resource’s
updated-at stamp is never recorded, since every save sets it: a create
keeps its created-at stamp, and a save that changes nothing else
records no entry. A
record’s Audit shows it, limited to the fields the viewer may see,
each value drawn as the record page draws that field, and each change
named by who made it. The Audit page shows it across resources
(below).
The changes are stored as JSON on Vector’s own connection, so a Json
field’s value nests only as deep as that connection stores, less the
two levels an entry wraps around it
(How deep a value nests). A record can still hold a deeper value saved before Vector
refused one; when an update replaces it, the entry records that earlier
value as {"truncated": "too_deep"}, and the Audit shows that marker in
its place.
Action runs are also logged, once per run, in vector_action_events.
The entry is written after the write commits, in statements of its
own, since Vector’s tables may live on another connection than the
record’s. A failure between the two leaves the write stored without
its entry, and the request answers 500: a client that retries a
create makes a second record. The record is read back before anything
is logged, so a failed read-back leaves neither the log row nor the
entry.
A write that came through another channel than the admin’s own hand
carries its origin: the AI assistant’s approved proposals are
recorded with origin assistant (The assistant), and the Audit shows them
via assistant. Code that writes on another channel’s behalf registers
new ActorContext($user, $impersonatorId, origin: 'my-channel') (a
lowercase token of at most 40 characters) on the request scope before
anything that writes is resolved. origin is null for every other
write, and GET /vector/r/{resource}/{id}/audit returns it on each
entry. The column comes with the vector_write_origin migration
vector:install publishes.
Who made a change#
Each entry carries actorId and impersonatorId, and beside them
actorName and impersonatorName: the user your
ActorResolverInterface (Authentication) rebuilds from the id, by the
display name it answers as a VectorShellUserInterface, the name the
shell greets them with. Each id is looked up once per request. Without
a resolver, for a user who no longer exists, or one with no name, the
name is null and the admin shows the id.
A record’s own audit (GET /vector/r/{resource}/{id}/audit) shows
these names to whoever may view that record: no permission beyond the
record’s view is asked. Browsing the audit across resources on the
Audit page needs the audit permission of its own (see below).
The audit page#
The Audit page, at /audit, lists the audit across resources,
newest first: the time, who made the change (and who impersonated
them), the resource, the record, the event, the origin and the fields
it changed. An entry opens to show each field’s earlier and later
value. The record is a link while it exists. A filter bar narrows it by
resource, record, event, actor, origin, a date range in the viewer’s
own days, and changes made while impersonating; the filters live in the
page’s URL, so a link or a reload shows the same entries. Load more
reads the next page. On a small screen each entry is a card. Nothing on
the page reverts: revert stays on the record’s own Audit.
Who may browse it#
Nobody, until the application says so.
Kinetis\Vector\Audit\AuditPolicyInterface decides, for a request
Vector’s vector middleware group already admitted:
Method |
Decides |
|---|---|
|
Whether this user may browse the audit across resources. |
Vector binds ClosedAuditPolicy, which answers false. Refused, the
descriptor’s capabilities.audit is false, the menu has no Audit
entry, GET /vector/audit answers 403 whatever it asks, and the
vector_audit MCP tool refuses. A record’s own Audit is not affected:
it follows that record’s view (Authorization). Bind your own
policy in bootstrap.php, which runs after Vector’s bootstrap:
use Kinetis\Vector\Audit\AuditPolicyInterface;
use Kinetis\Vector\Auth\ActorContext;
final readonly class AdminAuditPolicy implements AuditPolicyInterface
{
// AppUser stands for your own user class, as the Vector actor carries it
public function view(ActorContext $actor): bool
{
return $actor->user instanceof AppUser && $actor->user->isAdmin;
}
}
$app->instance(AuditPolicyInterface::class, new AdminAuditPolicy());
The policy holds no request state (one instance per worker). The demo’s App\Auth\DemoAuditPolicy lets admins
browse.
What a viewer sees#
Being let in does not widen what the viewer sees of the records. Every entry is cut as the record’s own Audit cuts it, and the same service answers the page and the MCP tool:
Resources. Only entries of resources the viewer may list (
viewAny) are read. Filtering for another resource, or one that does not exist, finds nothing rather than an error, so the answer does not tell which resources exist.Records. On each page, one check per resource reads the records its entries name as a record page would: through the resource’s
scope()for this viewer, trashed records included, and its policy’sview. An entry of a record kept from the viewer is left out. An entry of a record that no longer exists (deleted, or force-deleted from the trash) is shown, withlinkedfalse, so it is not linked. A record that no longer exists can no longer be checked againstscope(), so on a resource that overridesscope()its entries keep the event, the actor, the time, the resource and the record id, and their changes are empty: no earlier or later value of another tenant’s record reaches the viewer. On a resource without ascope()of its own, nothing could have kept the record from them, and its entries are shown in full.Fields. An entry’s changes keep only the fields the viewer may see on that resource (
viewField), and never a password field, whatever the policy says.
Entries of no resource#
Some features record under a name of their own: the queue dashboard as
vector-queues (Queue dashboard), impersonation as
vector-impersonation (below), AI provider keys as
vector-ai:credentials (Setup). An entry of such a name is
shown only to a viewer who also passes that feature’s own gate, as it
was recorded, never linked:
Name |
Shown to |
|---|---|
|
Whoever the queue dashboard is there for: a queue configured, and |
|
Whoever may browse the audit. |
|
Whoever may configure AI: |
An entry under any other name that is no resource is never shown. A
package that records under a name of its own registers its gate, an
AuditSourceInterface, from its package bootstrap:
use Kinetis\Vector\Audit\AuditSources;
AuditSources::register($app, 'my-feature', MyFeatureAuditSource::class);
The class is resolved from the request scope and answers
visible(ActorContext $actor): bool.
Paging#
Pages run on the entry id, newest first, 50 entries a page, and each
answers a nextCursor to pass back for the next. A cursor goes on
where its page ended, so entries recorded meanwhile never shift a page.
When the record check leaves entries out, up to three further reads
fill the page; past that a page can come back short, even empty, with a
nextCursor that goes on after the last entry read. nextCursor is
null once nothing is left. The endpoint is REST API’s
GET /vector/audit.
Retention#
Nothing prunes vector_audit on its own, and how long to keep it is
yours to decide. vector:audit:prune --days=<n> deletes every entry
recorded more than n days ago (1 to 36500, required), 1000 entries a
statement, so no statement holds its locks for long, and prints how
many it deleted. Run it from your scheduler, for example daily:
vendor/bin/kinetis vector:audit:prune --days=365
The vector_audit_browse and vector_audit_resource_index migrations
vector:install publishes add the indexes the page and the prune read
through: one on created_at, one on (resource, event, id) and one on
(resource, id); an upgrade publishes them as
Upgrading says.
Revert#
POST /vector/r/{resource}/{id}/audit/{entry}/revert puts back what one
update (or earlier revert) changed:
each field of the entry the user may see and edit gets its earlier value, as recorded: a JSON object,
{}and a nested{}included, is put back as an object where the field keeps objects (JSON columns);the write goes through the update path: policy, validation, conditions, dependent choices and hooks all apply;
it is pinned to the record’s current version, and recorded as a
revert.
If any of those fields changed since the entry, the revert answers 409
and writes nothing, so it never overwrites a later edit. Collections and
read-only fields are left alone. Other events (create, delete,
attach, actions) cannot be reverted and are refused with
not_revertable, as is an entry that recorded a field’s earlier value
as {"truncated": "too_deep"}: there is no value to put back, so the
revert answers 422 and writes nothing rather than writing the marker.
Impersonation#
An admin can act as another user to see what they see. Vector owns the
endpoints, the permission check and the audit; your application owns
the credential, through ImpersonationInterface:
Method |
Does |
|---|---|
|
Whether this user may impersonate at all. |
|
Switches the credential: a new |
|
Restores the impersonator’s own credential. |
|
The target’s id and name, or null when they do not exist. |
Register it on the request scope, typically in your authentication
middleware, which knows the mode. The user object reports who is
impersonating it through ImpersonatedUserInterface, and every audit
and log row records both.
Each start and stop is an audit entry under vector-impersonation:
event impersonate:start or impersonate:stop, the admin as the actor
and the impersonated user as the record. It is written before the
credential changes, so an impersonation the audit could not record
does not happen.
The UI never renews an impersonation token, so its lifetime is yours to bound. The demo’s lasts no longer than the token that started it; in cookie mode, impersonation lasts as long as the session (see the known limits).
In the UI, set usersResource in app.config to the resource whose
records are users; its record pages then offer Impersonate.
See also#
Records: a record’s Audit and revert from a user’s side.
Authorization: the policies every entry is cut by.
The assistant: the assistant’s read-only
vector_audittool.Authentication: the two credential modes.