Add data model, DbContext and initial migration (M1)

Schema for identity, vaults, grants, hosts and the sync change log, verified against a
real PostgreSQL 18 container rather than an in-memory provider: partial unique indexes,
CHECK constraints, citext and identity-always columns are all provider behaviour that an
in-memory fake would not exercise.

Invariants pushed into the database, so they hold even when application code has a bug:
- ck_host_relay_target is a security boundary, not tidiness. A host may carry a plaintext
  hostname and port ONLY when relay is deliberately enabled. Both directions are tested;
  the important one is that relay-disabled hosts cannot carry an address, since otherwise
  a bug would silently give the server infrastructure visibility it was never granted.
- ck_vault_owner: exactly one of owner_user_id or team_id, or permission resolution would
  have no defined answer.
- ck_vault_key_grant_recipient: member grants name a user; recovery and escrow grants are
  wrapped to a key and must not.
- ck_user_key_wrap_kdf: a password-derived wrap without its parameters is permanently
  unopenable, so a partial write is rejected outright.

Present from the first migration on purpose:
- GrantKind (Member/Recovery/Escrow). Recovery cannot be bolted on later — every vault
  created before it existed would be unrecoverable by design.
- team and team_membership, though team features are M3. Adding them later would mean
  introducing a foreign key on a live vault table.
- Host.ContentKeyId, reserved for per-item content keys wrapped to individual users.
- user_key as its own table, so key rotation does not require altering the user row.

Two things verified rather than assumed:
- Npgsql's UseXminAsConcurrencyToken helper no longer exists in EF 10, so xmin is mapped
  directly in XminConcurrency. The generated migration *looks* like it creates an xmin
  column; it does not. Confirmed by inspecting pg_attribute (attnum -2, a system column)
  and by grepping the emitted DDL. A test pins both, because had it created a real column
  PostgreSQL would have rejected the name.
- EF Core is now pinned centrally. The Npgsql provider asks for 10.0.4 while
  EntityFrameworkCore.Design pulls 10.0.10, and because Design is PrivateAssets=all that
  higher version does not flow to referencing projects — producing a CS1705 in any test
  project referencing Infrastructure.

Also commits artifacts/schema/v0.1.sql, the idempotent script, as the baseline for future
upgrade tests.

Verified: 0 warnings, 122 tests pass (27 new against Postgres), format clean.
This commit is contained in:
2026-07-28 14:17:37 +02:00
parent 06d04b490b
commit eaf68c86b0
24 changed files with 5843 additions and 0 deletions
+165
View File
@@ -0,0 +1,165 @@
namespace DodoSSH.Domain;
/// <summary>Lifecycle state of a user account.</summary>
public enum UserStatus
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Normal, active account.</summary>
Active = 1,
/// <summary>Sign-in blocked, data retained.</summary>
Suspended = 2,
/// <summary>Offboarded. Grants revoked; audit history retained.</summary>
Deprovisioned = 3,
}
/// <summary>
/// Which key the user's secret bundle is wrapped under.
/// </summary>
/// <remarks>
/// Every kind wraps the <em>same</em> bundle, which is what makes a passphrase change a
/// single-row update instead of a re-encryption of the whole vault. See docs/crypto.md §3.
/// </remarks>
public enum UserKeyWrapKind
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Wrapped under a key derived from the vault passphrase.</summary>
Passphrase = 1,
/// <summary>Sealed to one enrolled device's public key.</summary>
Device = 2,
/// <summary>Wrapped under a key derived from the printable recovery code.</summary>
Recovery = 3,
/// <summary>Sealed to a team break-glass key. Opt-in; M5.</summary>
Escrow = 4,
}
/// <summary>Operating system family of an enrolled device, for display only.</summary>
public enum DevicePlatform
{
/// <summary>Unknown or unreported.</summary>
Unspecified = 0,
/// <summary>Windows.</summary>
Windows = 1,
/// <summary>macOS.</summary>
MacOs = 2,
/// <summary>Linux.</summary>
Linux = 3,
}
/// <summary>A member's role within a team.</summary>
public enum TeamRole
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Read-only.</summary>
Viewer = 10,
/// <summary>Ordinary member.</summary>
Member = 20,
/// <summary>May manage members and create vaults.</summary>
Admin = 30,
/// <summary>Sole owner. Transferable.</summary>
Owner = 40,
}
/// <summary>State of a team membership.</summary>
public enum MembershipStatus
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Invited but not yet accepted.</summary>
Invited = 1,
/// <summary>Active member.</summary>
Active = 2,
/// <summary>Revoked. Retained so audit history stays resolvable.</summary>
Revoked = 3,
}
/// <summary>Whether a vault belongs to one user or to a team.</summary>
public enum VaultOwnerKind
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Owned by a single user.</summary>
Personal = 1,
/// <summary>Owned by a team.</summary>
Team = 2,
}
/// <summary>Why a vault key grant exists.</summary>
/// <remarks>
/// Present from the first migration on purpose. Recovery is not a feature that can be bolted on
/// later: the schema has to allow a vault key to be wrapped to something other than a member
/// from the outset, or every existing vault becomes unrecoverable by design.
/// </remarks>
public enum GrantKind
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Wrapped to a member's identity key.</summary>
Member = 1,
/// <summary>Wrapped to a recovery key held by the vault owner.</summary>
Recovery = 2,
/// <summary>Wrapped to a team break-glass key. Opt-in; M5.</summary>
Escrow = 3,
}
/// <summary>State of a vault key grant.</summary>
public enum GrantState
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Usable.</summary>
Active = 1,
/// <summary>
/// The recipient's identity key changed or the vault was rekeyed, so this grant must be
/// re-wrapped by a member holding Share before the recipient can read the vault again.
/// </summary>
AwaitingRewrap = 2,
/// <summary>
/// Revoked. Blocks future reads only; anything already downloaded is already gone. See
/// ADR 0001.
/// </summary>
Revoked = 3,
}
/// <summary>Why a vault needs rekeying.</summary>
public enum RekeyReason
{
/// <summary>No rekey pending.</summary>
None = 0,
/// <summary>A member was removed.</summary>
MemberRemoved = 1,
/// <summary>A member's identity key was rotated.</summary>
KeyRotated = 2,
/// <summary>An operator or member requested it.</summary>
Requested = 3,
}