Policy storage and audit
Some apps keep documents that must change safely: a policy, a rule set, a configuration that several people edit and that auditors ask about later. This page states what TenancyEngine provides for that, what it does not, and how to build the rest in your own application.
Requires nothing from the SDK beyond what you already use. No type on this page is unreleased.
The responsibility contract
| Concern | Owner | Notes |
|---|---|---|
| Where the policy document lives | Your app, in your app's own database | Policy documents are your business data. Every app has its own database for its own business logic. |
| Concurrent edits (optimistic concurrency) | Your app | TenancyEngine storage has no compare-and-set. See below. |
| The audit trail of policy changes | Your app, in the same database and transaction | The SDK has no API for an app to write audit events. |
| Who the actor is, and that they were allowed | TenancyEngine issues the identity and permission claim; your app verifies it | See App staff authorization. |
| Tenant, user and role changes, API key and application settings changes | TenancyEngine | Recorded in the organization audit log, readable by organization admins (see Organization audit log). Policy edits inside your app are not part of it. |
What TenancyEngine storage is, and is not
The storage client (CreateStorageGrantAsync, RequestStorageUploadUrlAsync, ConfirmStorageUploadAsync, GetStorageDownloadUrlAsync) is for files: it hands out upload and download URLs and tracks objects. It has no ETag, version number or If-Match precondition, so two writers cannot detect that they overwrote each other. Do not use it as a policy store.
Build it app-side
Store each policy as a row with a version number, and write every change in one transaction together with its audit row.
create table policy (
tenant_id uuid not null,
key text not null,
version integer not null,
document jsonb not null,
updated_by text not null,
updated_at timestamptz not null,
primary key (tenant_id, key)
);
create table policy_audit (
id bigint generated always as identity primary key,
tenant_id uuid not null,
key text not null,
version integer not null,
action text not null, -- created | updated | rolled_back
actor_sub text not null, -- the token's sub
actor_tid uuid not null, -- the token's tenant_id
before_doc jsonb,
after_doc jsonb not null,
occurred_at timestamptz not null default now(),
request_id text
);The update succeeds only if the caller edited the version it last read:
await using var tx = await conn.BeginTransactionAsync(ct);
var updated = await conn.ExecuteAsync(
"""
update policy
set document = @doc, version = version + 1, updated_by = @sub, updated_at = now()
where tenant_id = @tenant and key = @key and version = @expectedVersion
""", new { doc, sub, tenant, key, expectedVersion }, tx);
if (updated == 0)
return Results.Conflict(new { error = "policy_changed", message = "Someone else changed this policy. Reload and try again." });
await conn.ExecuteAsync(
"insert into policy_audit (tenant_id, key, version, action, actor_sub, actor_tid, before_doc, after_doc, request_id) " +
"values (@tenant, @key, @expectedVersion + 1, 'updated', @sub, @actorTenant, @before, @doc, @requestId)",
new { tenant, key, expectedVersion, sub, actorTenant, before, doc, requestId }, tx);
await tx.CommitAsync(ct);Rules that keep it trustworthy:
- Take
tenant_idand the actor from the verified token, never from the request body. - Return the new
versionto the client and require it on the next write. Show a conflict, not a silent overwrite. - Make the audit table append-only for your application's database role (no
updateordeletegrant). - Write the audit row in the same transaction as the change, so there is never a change without a record or a record without a change.
- Record the actor's
subandtenant_idfrom the token, plus the request or correlation id. Do not rely on a display name. - Keep retention and export as part of your own compliance statement: the platform's audit retention does not apply to your tables.
What the platform guarantees
- The identity in the token (
sub,tenant_id,permission) was issued by TenancyEngine for your application and environment, and a removed member cannot refresh into a new token. - Platform-side changes that affect your app (members, roles, settings) are in the organization audit log.
It does not guarantee ordering, versioning or durability of data you keep in your own database.
Not provided
A platform-owned, versioned policy store with built-in audit does not exist.