using DodoSSH.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace DodoSSH.Infrastructure.Configurations;
/// Maps .
public sealed class HostConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("host");
builder.HasKey(h => h.Id);
// Client-generated UUIDv7: items must be creatable offline, with their ids.
builder.Property(h => h.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(h => h.Payload).IsRequired();
builder.Property(h => h.Hostname).HasMaxLength(255);
builder.HasIndex(h => new { h.VaultId, h.ChangeSequence });
builder.HasIndex(h => h.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_host_vault_live");
// The relay resolves its target from this row, so an address is stored only when relay is
// deliberately enabled for the host. Enforced in the database rather than in application
// code: a bug that let a host carry a plaintext address without opting in would silently
// widen what the server can see. See ADR 0004.
builder.ToTable(t => t.HasCheckConstraint(
"ck_host_relay_target",
"""
(relay_enabled AND hostname IS NOT NULL AND port IS NOT NULL)
OR (NOT relay_enabled AND hostname IS NULL AND port IS NULL)
"""));
builder.ToTable(t => t.HasCheckConstraint(
"ck_host_port_range",
"port IS NULL OR (port BETWEEN 1 AND 65535)"));
builder.ToTable(t => t.HasCheckConstraint(
"ck_host_version",
"version >= 1"));
}
}
/// Maps .
///
/// Mirrors in everything the two types share, and deliberately has no
/// analogue of the relay CHECK: a key carries no address, which is the reason it is its own table.
///
public sealed class SshKeyConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("ssh_key");
builder.HasKey(k => k.Id);
// Client-generated UUIDv7: keys must be creatable offline, with their ids.
builder.Property(k => k.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(k => k.Payload).IsRequired();
// SHA256:base64 of a 32-byte digest is 50 characters; the ceiling leaves room for another
// algorithm without a migration, and refuses anything that is plainly not a fingerprint.
builder.Property(k => k.PublicKeyFingerprint).HasMaxLength(128);
builder.HasIndex(k => new { k.VaultId, k.ChangeSequence });
builder.HasIndex(k => k.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_ssh_key_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_ssh_key_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// The narrowest shape any item table takes — shared now with — and
/// deliberately so: no relay CHECK, and unlike not even a fingerprint
/// column. There is nothing about a password that is safe to hold in the clear, so there is nothing here but
/// the envelope and its bookkeeping.
///
public sealed class CredentialConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("credential");
builder.HasKey(c => c.Id);
// Client-generated UUIDv7: credentials must be creatable offline, with their ids.
builder.Property(c => c.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(c => c.Payload).IsRequired();
builder.HasIndex(c => new { c.VaultId, c.ChangeSequence });
builder.HasIndex(c => c.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_credential_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_credential_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// Identical to , and the sameness is the statement: a pin has no
/// plaintext column either. It would have been easy to add a host column here for a "known hosts"
/// screen to list without decrypting, and that column would have handed the operator the map of every user's
/// estate. The client decrypts its own vault; it does not need the server's help to sort a list.
///
public sealed class KnownHostKeyConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("known_host_key");
builder.HasKey(k => k.Id);
// Client-generated UUIDv7: a pin must be recordable offline, with its id.
builder.Property(k => k.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(k => k.Payload).IsRequired();
builder.HasIndex(k => new { k.VaultId, k.ChangeSequence });
builder.HasIndex(k => k.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_known_host_key_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_known_host_key_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// The same shape again, and by now the sameness is the design rather than a coincidence: most item types
/// hold nothing but an envelope and its bookkeeping. This one is worth a sentence anyway, because it is a
/// type where plaintext columns would have been tempting and are not defensible — a name here would
/// let the server order a list it never draws, and a parent_id would draw the shape of every user's
/// estate, both in exchange for telling the operator how each of them files their machines. Groups nest
/// through a pointer inside the payload; nothing about that is visible from this table.
///
public sealed class HostGroupConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("host_group");
builder.HasKey(g => g.Id);
// Client-generated UUIDv7: a group must be creatable offline, with its id, because the host that
// names it is created offline too and needs something to point at.
builder.Property(g => g.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(g => g.Payload).IsRequired();
builder.HasIndex(g => new { g.VaultId, g.ChangeSequence });
builder.HasIndex(g => g.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_host_group_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_host_group_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// again, down to the index names. The one thing worth stating is what
/// is not here: no host_tag join table, because membership is a set inside each host's
/// payload — see — so there is nothing to join and no second table to keep in step.
///
public sealed class TagConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("tag");
builder.HasKey(t => t.Id);
// Client-generated UUIDv7: a tag must be creatable offline, with its id, because the host that
// wears it is created offline too and needs something to point at.
builder.Property(t => t.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(t => t.Payload).IsRequired();
builder.HasIndex(t => new { t.VaultId, t.ChangeSequence });
builder.HasIndex(t => t.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_tag_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_tag_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// Identical to . A snippet's label would be harmless on its own and the
/// commands beside it would not be, and splitting one out would give the server half a map for no feature.
///
public sealed class SnippetConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("snippet");
builder.HasKey(s => s.Id);
// Client-generated UUIDv7: a snippet must be writable offline, with its id.
builder.Property(s => s.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(s => s.Payload).IsRequired();
builder.HasIndex(s => new { s.VaultId, s.ChangeSequence });
builder.HasIndex(s => s.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_snippet_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_snippet_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
///
/// The same envelope-and-bookkeeping shape as every other item kind, and this is the one where holding
/// something in the clear would have been most useful and least defensible: a started_at column would
/// let the server order a log without decrypting it, and would hand the operator every user's working hours.
/// The client sorts its own log.
///
///
/// The live index is not decoration here. This is the only table that grows without a person adding
/// anything to it — a busy user produces entries all day — so the query retention runs, "everything in this
/// vault that is not already a tombstone", is the one that has to stay cheap.
///
///
public sealed class ConnectionLogEntryConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("connection_log_entry");
builder.HasKey(e => e.Id);
// Client-generated UUIDv7, and here the ordering it carries is load-bearing rather than incidental:
// a log is read newest-first, and the id is the only creation time an item has.
builder.Property(e => e.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(e => e.Payload).IsRequired();
builder.HasIndex(e => new { e.VaultId, e.ChangeSequence });
builder.HasIndex(e => e.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_connection_log_entry_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_connection_log_entry_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
public sealed class ActivityLogEntryConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("activity_log_entry");
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(e => e.Payload).IsRequired();
builder.HasIndex(e => new { e.VaultId, e.ChangeSequence });
builder.HasIndex(e => e.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_activity_log_entry_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_activity_log_entry_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// again, which is the right comparison: a secret access key is a
/// password, and the endpoint beside it is an address on somebody's own network as often as it is Amazon's.
/// Neither belongs in a column.
///
public sealed class ObjectStoreConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("object_store");
builder.HasKey(o => o.Id);
// Client-generated UUIDv7: a bucket must be addable offline, with its id.
builder.Property(o => o.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(o => o.Payload).IsRequired();
builder.HasIndex(o => new { o.VaultId, o.ChangeSequence });
builder.HasIndex(o => o.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_object_store_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_object_store_version",
"version >= 1"));
}
}
///
/// Maps .
///
///
/// again: a key and the address it goes to, neither of which belongs
/// in a column.
///
public sealed class SpelunkKeyConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("spelunk_key");
builder.HasKey(s => s.Id);
// Client-generated UUIDv7: a key must be addable offline, with its id.
builder.Property(s => s.Id).ValueGeneratedNever();
builder.UseXminConcurrencyToken();
builder.Property(s => s.Payload).IsRequired();
builder.HasIndex(s => new { s.VaultId, s.ChangeSequence });
builder.HasIndex(s => s.VaultId)
.HasFilter("deleted_at_utc IS NULL")
.HasDatabaseName("ix_spelunk_key_vault_live");
builder.ToTable(t => t.HasCheckConstraint(
"ck_spelunk_key_version",
"version >= 1"));
}
}
/// Maps .
public sealed class SyncChangeConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("sync_change");
builder.HasKey(c => c.Sequence);
// Identity ALWAYS rather than BY DEFAULT: nothing may supply its own sequence value, or
// cursor ordering stops meaning anything.
builder.Property(c => c.Sequence).UseIdentityAlwaysColumn();
builder.Property(c => c.EntityType).HasConversion();
builder.Property(c => c.Operation).HasConversion();
// The delta-pull access path: everything after a cursor, for one vault.
builder.HasIndex(c => new { c.VaultId, c.Sequence });
// Latest change for a given entity, used when resolving a conflict.
builder.HasIndex(c => new { c.VaultId, c.EntityId, c.Sequence })
.IsDescending(false, false, true);
}
}
/// Maps .
public sealed class SyncOperationReceiptConfiguration : IEntityTypeConfiguration
{
///
public void Configure(EntityTypeBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ToTable("sync_operation_receipt");
// The client-generated operation id is the key, which is what makes a retried push
// exactly-once per operation rather than per batch.
builder.HasKey(r => r.OperationId);
builder.Property(r => r.OperationId).ValueGeneratedNever();
builder.HasIndex(r => new { r.VaultId, r.CreatedAtUtc });
}
}