Public Access
The WebView sat inside the Hosts grid, so navigating to Files or the keychain hid every open terminal and the strip that named them. A connection you had opened was invisible from four of the five screens. The window now has two surfaces rather than one: a nav rail that says which page you are on, and a terminal strip that is always there and switches the whole content area to a shell. Screen keeps meaning "which page" and never becomes a sixth kind of page, which is why this is two properties instead of one enum with a terminal member in it. Every screen lives inside one wrapper panel that collapses when a terminal is showing. That is not tidiness — the WebView hosts a Win32 child window that composites above everything Avalonia draws, so a screen left visible over its rectangle is a screen sliced in half, and this window has shipped that defect once already. One decision point, IsTerminalShowing, and a nested panel rather than five compound bindings nobody would remember to extend. The focus choreography is the part no test in this repo can see. Every reveal path now focuses in the same turn the WebView appeared, so all three of them post at DispatcherPriority.Loaded and let the native control re-push its bounds first. Going the other way had a real bug: the screen-changed branch called a bare Focus() where it had to release the keyboard from the native child, so switching from a terminal to Files silently ate the first keystrokes. Rare before this commit and the primary gesture after it. The tab strip grew a cross inside each tab, a plus that opens the quick-connect palette, and middle-click close. Nested buttons are correct here: Avalonia handles a left press on the cross and deliberately does not handle other buttons, which is exactly what lets middle-click bubble up from the cross as well as the tab. The test is PointerUpdateKind rather than IsMiddleButtonPressed, because the latter reports button state and is also true for a left press made while the middle button happens to be held. The handler is on the tab and not the strip, so the background closes nothing by construction. Plus opens the palette rather than a flyout, since a menu dropping into the WebView's rectangle may or may not composite above a child HWND and this repo does not make rendering claims it has not photographed. Everything a user reads now says keychain. The wire, the database and the cryptographic spec still say vault, deliberately: renaming those is a migration and a protocol change for a word. That split is written down rather than left to be rediscovered as an inconsistency. Four things that were squeezed into the keychain's category rail, or into nothing at all, now have screens. Pinned host keys get one, with fingerprints never truncated and a filter that matches them, because comparing what you have against what the operator published is the whole workflow; the approved date is read out of the item's UUIDv7 rather than added as a column, and says so, since it means first approval and not last use. Keys can be generated in the client, which needed the openssh-key-v1 container written by hand — there is no BCL or NSec helper, and the PKCS#8 route is unverified in the SSH library this uses. The armour carries no passphrase: encrypting it needs bcrypt_pbkdf, which is Blowfish with a swizzle, in a project whose crypto is otherwise entirely libsodium, for a protection the key's own remarks argue is redundant inside a vault. Generation fills the existing editor and stops, so SAVE stays the one thing that writes. ~/.ssh/config can be imported behind a preview that is ticked per row and writes nothing until the button; IdentityFile records the path and imports the key material only on an explicit opt-in, because reading somebody's private key into a vault is precisely the act this product exists to make deliberate. Match blocks and ProxyJump are reported rather than obeyed — one cannot be evaluated statically and the other has nothing behind it to route with, and a preview that implied otherwise would be worse than one that admits it. Files can be dragged in all four directions that are honestly available. Remote to Explorer does not ship and is not pretended to: the shell wants the bytes during the drop, which needs a virtual file and a native COM data object, outside what Avalonia offers. Note for the next person that Avalonia 12 replaced the drag model outright — DataObject and DataFormats are no-op stubs and IDataObject is not in the reference assembly, so every tutorial written for 11 does not compile here. Hosts can be grouped, flat and never nested. A parent id merged as a scalar lets two offline clients each re-parent A under B and B under A, producing a cycle inside an encrypted payload that no server can police and every reader would have to detect for ever. Membership lives in that payload rather than in the one plaintext concession ADR 0001 allows, whose test is that the relay cannot function without it — nothing on the server reads a group, so what plaintext would hand over is a clustering of the estate for nothing. The plaintext column reserved for it is dropped, provably always null, and the server now refuses a client that sends one; it was never populated, was copied on apply, and was not cleared on delete, so a group id would have outlived the host it described. Snippets insert through xterm rather than through the pump, because xterm is the only thing that knows whether the remote has bracketed paste on, and that is what makes a shell treat embedded newlines as text instead of as execute. The host process moves opaque bytes and never parses output, so it would have to guess, and guessing wrong runs every line. Running is off by default and the copy says the text goes into whatever is there — the terminal has no notion of being at a prompt, and may be in vi or at a password prompt with echo off, so the Enter the user presses themselves is the entire safety property. Connections and keychain changes are recorded as synced encrypted items, which is what makes them auditable by a team later and costs the server knowledge of connection rate and timing from row counts alone. ADR 0001 already concedes it cannot hide that class of metadata; the trade is now written into it rather than left implicit. A connection entry is written once, at close, which is what makes a synced log tractable: nothing to merge, one outbox row, no chance of colliding with itself. Live sessions come from memory, not from the log. The write is void by contract and posts to a bounded channel, because putting an encrypt-and-write on the teardown path of every session is how closing the application comes to take four seconds. A ticket opened before a lock still closes afterwards, since a shell outlives the vault. The activity log hooks the one generic repository every kind writes through, so it cannot miss a caller — which is also why the log kinds themselves declare they are not audited, or the first entry would write an entry about writing an entry. It records the names of the fields that changed and never their values; a log with an old password in it would be a plaintext credential store with no vault around it. Retention is 90 days or 5,000 entries, whichever bites first, pruned on the sync loop rather than on a second timer. That log traffic then broke the status line, which is worth recording because the fix is a shape and not a patch: background sync counted its own log rows as pushed items, so the quiet rule stopped being quiet and every action's message was overwritten a second later by a sync report. The report now separates log rows from user items and the rule reads the latter. S3 buckets appear as a remote in the file browser, behind the same interface an SFTP session implements, so the queue and both panes did not have to learn what they are talking to. Uploads go through a pipe, because the queue wants to write and the SDK wants to read; memory is then bounded by the part size instead of buffering a file to disk twice. Finally, the Windows device key store moved out of the session project, which was the one thing keeping it from being portable — everything else in it is platform-neutral, and a Windows CNG dependency in the middle of the vault code meant a second head could not reference it without dragging Windows along. The seam that made the move free was already there. docs/android-port.md is the audit behind that: what ports, what does not, in order of cost, the four decisions taken, and an inventory of every screen and state the interface has to carry, written so a design can be made from it directly. dotnet build, dotnet test and dotnet format --verify-no-changes are all clean: 1240 tests at zero warnings, including the end-to-end suite against real containers. The manual checks that headless Avalonia cannot make — the drag from Explorer, a generated key against a real host, twelve tabs at the minimum window width — are listed in docs/manual-checks.md and are still outstanding.
990 lines
34 KiB
C#
990 lines
34 KiB
C#
using DodoSSH.Contracts;
|
|
using DodoSSH.Domain;
|
|
using DodoSSH.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DodoSSH.Api.Features.Sync;
|
|
|
|
/// <summary>
|
|
/// Everything about one item type that the shared write path cannot know.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The push path is the most dangerous code in the server: it holds the version check, the change-log
|
|
/// append, the receipt that makes a retry exactly-once, and the advisory lock that keeps the sequence in
|
|
/// commit order. Duplicating that per item type would mean ten copies of it by M5, and a fix applied to nine
|
|
/// of them. So it is written once over <see cref="IVaultItem"/>, and everything genuinely type-specific —
|
|
/// which table, which plaintext columns, what those columns must satisfy — arrives through here.
|
|
/// </para>
|
|
/// <para>
|
|
/// Deliberately not generic. A generic <c>IItemKind<TItem></c> reads better in isolation and would
|
|
/// force every method on the write path to be generic too, including the async ones, for no benefit: the
|
|
/// path never needs the concrete type, only the shared shape. The downcast each implementation performs is
|
|
/// contained to one class per type and is guaranteed by construction, because the same class both creates
|
|
/// and queries the rows.
|
|
/// </para>
|
|
/// </remarks>
|
|
internal interface IItemKind
|
|
{
|
|
/// <summary>The type as the wire contract names it.</summary>
|
|
SyncEntityType WireType { get; }
|
|
|
|
/// <summary>The type as the change log names it.</summary>
|
|
/// <remarks>
|
|
/// Stated rather than cast. The two enums do agree numerically — and they have different member names
|
|
/// for the same value, <c>Host</c> against <c>SshHost</c> — but that alignment is hand-kept and nothing
|
|
/// in the type system defends it. Writing it out per kind means adding a type cannot silently file its
|
|
/// changes under another type's name.
|
|
/// </remarks>
|
|
ChangeEntityType ChangeType { get; }
|
|
|
|
/// <summary>Finds one item by id, across every vault, so a cross-vault id can be refused.</summary>
|
|
Task<IVaultItem?> FindAsync(DodoDbContext database, Guid id, CancellationToken cancellationToken);
|
|
|
|
/// <summary>Loads the rows behind a set of change-log entries, scoped to one vault.</summary>
|
|
Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken);
|
|
|
|
/// <summary>Creates an empty row of this type and tracks it.</summary>
|
|
IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId);
|
|
|
|
/// <summary>Rejects plaintext fields this type may not carry.</summary>
|
|
bool ValidateFields(SyncPlaintextFields fields, out string error);
|
|
|
|
/// <summary>Copies this type's plaintext columns out of the request.</summary>
|
|
void ApplyFields(IVaultItem item, SyncPlaintextFields fields);
|
|
|
|
/// <summary>Clears plaintext columns that must not outlive the item.</summary>
|
|
void ClearFieldsOnDelete(IVaultItem item);
|
|
|
|
/// <summary>Reads this type's plaintext columns back for a pull, or null when it has none.</summary>
|
|
SyncPlaintextFields? Hydrate(IVaultItem item);
|
|
}
|
|
|
|
/// <summary>The item types this server can synchronise, by wire type.</summary>
|
|
internal static class ItemKinds
|
|
{
|
|
private static readonly Dictionary<SyncEntityType, IItemKind> Supported =
|
|
new[]
|
|
{
|
|
(IItemKind)new HostKind(), new SshKeyKind(), new CredentialKind(), new KnownHostKeyKind(),
|
|
new HostGroupKind(), new SnippetKind(),
|
|
new ConnectionLogEntryKind(), new ActivityLogEntryKind(), new ObjectStoreKind(),
|
|
}.ToDictionary(kind => kind.WireType);
|
|
|
|
/// <summary>The kind for a wire type, or null when this server does not synchronise it yet.</summary>
|
|
/// <remarks>
|
|
/// Null rather than a throw. Every entity type in the contract is reachable by a newer client, and the
|
|
/// push endpoint answers per operation — so an unsupported type has to become one <c>Invalid</c> result
|
|
/// with a reason, not a failed batch that also rejects the operations this server did understand.
|
|
/// </remarks>
|
|
internal static IItemKind? For(SyncEntityType type) =>
|
|
Supported.TryGetValue(type, out var kind) ? kind : null;
|
|
}
|
|
|
|
/// <summary>Hosts: the one item type with a deliberate plaintext concession.</summary>
|
|
internal sealed class HostKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.Host;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.SshHost;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.Hosts.SingleOrDefaultAsync(h => h.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.Hosts
|
|
.Where(h => h.VaultId == vaultId && ids.Contains(h.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var host = new SshHost { Id = id, VaultId = vaultId };
|
|
|
|
database.Hosts.Add(host);
|
|
|
|
return host;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mirrors the database CHECK so a bad request is a clear 200-with-Invalid rather than a constraint
|
|
/// violation surfacing as a 500.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The group check comes <em>first</em>, ahead of the relay branch, and that placement is the point: the
|
|
/// relay branch returns early on its happy path, so a check placed after it would apply to non-relay
|
|
/// hosts only — leaving the one field this refusal exists for reachable by exactly the hosts most likely
|
|
/// to carry it.
|
|
/// </para>
|
|
/// <para>
|
|
/// <c>SyncPlaintextFields.GroupId</c> is part of a frozen wire contract and cannot be removed from it, so
|
|
/// refusing it here is what actually keeps the value out of the database. The column it used to be copied
|
|
/// into was dropped when groups landed; see <see cref="VaultHostGroup"/> for why membership travels inside
|
|
/// the payload instead.
|
|
/// </para>
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.GroupId is not null)
|
|
{
|
|
error = "A host's group is inside its encrypted payload; the server does not store one.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.RelayEnabled)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fields.Hostname) || fields.Port is null)
|
|
{
|
|
error = "Relay-enabled hosts require both a hostname and a port.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.Port is < 1 or > 65535)
|
|
{
|
|
error = "Port must be between 1 and 65535.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
if (fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A hostname or port may only be supplied when relay is enabled for the host.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
var host = (SshHost)item;
|
|
|
|
host.RelayEnabled = fields.RelayEnabled;
|
|
host.Hostname = fields.RelayEnabled ? fields.Hostname : null;
|
|
host.Port = fields.RelayEnabled ? fields.Port : null;
|
|
}
|
|
|
|
/// <remarks>
|
|
/// The address goes with the item. Leaving it would keep the server able to resolve a host the user
|
|
/// believes they deleted.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
var host = (SshHost)item;
|
|
|
|
host.RelayEnabled = false;
|
|
host.Hostname = null;
|
|
host.Port = null;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item)
|
|
{
|
|
var host = (SshHost)item;
|
|
|
|
return new SyncPlaintextFields(
|
|
RelayEnabled: host.RelayEnabled,
|
|
Hostname: host.Hostname,
|
|
Port: host.Port);
|
|
}
|
|
}
|
|
|
|
/// <summary>SSH keys: ciphertext and nothing else.</summary>
|
|
internal sealed class SshKeyKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.SshKey;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.SshKey;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.SshKeys.SingleOrDefaultAsync(k => k.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.SshKeys
|
|
.Where(k => k.VaultId == vaultId && ids.Contains(k.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var key = new VaultSshKey { Id = id, VaultId = vaultId };
|
|
|
|
database.SshKeys.Add(key);
|
|
|
|
return key;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refuses the relay columns outright.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A key is not something the server dials, so an address on one is either a client bug or an attempt to
|
|
/// get the server to store something it has no reason to hold. Refused with a reason rather than
|
|
/// silently dropped, because a client that thinks it is storing a field and is not will eventually be
|
|
/// surprised by its absence.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "An SSH key has no relay target; relay fields may only be set on a host.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
// Only the fingerprint, and only because the contract reserved it. Everything that identifies the
|
|
// key to a person — its label, its comment — is inside the ciphertext.
|
|
((VaultSshKey)item).PublicKeyFingerprint = fields.PublicKeyFingerprint;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item) =>
|
|
((VaultSshKey)item).PublicKeyFingerprint = null;
|
|
|
|
/// <remarks>
|
|
/// Null rather than an all-defaults instance, and the difference is visible on the wire: a
|
|
/// <c>SyncPlaintextFields</c> with nothing set still serialises <c>relayEnabled: false</c>, which
|
|
/// invites a reader to believe this type has a relay setting that happens to be off. It has none.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) =>
|
|
((VaultSshKey)item).PublicKeyFingerprint is { } fingerprint
|
|
? new SyncPlaintextFields(PublicKeyFingerprint: fingerprint)
|
|
: null;
|
|
}
|
|
|
|
/// <summary>Credentials: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// As strict as a kind gets about plaintext — <see cref="KnownHostKeyKind"/> is the other one — and the
|
|
/// reason is not symmetry. A key at least has a fingerprint that is public by nature; a password has no part
|
|
/// that is safe to expose, so this kind accepts no plaintext fields at all and hydrates none.
|
|
/// </remarks>
|
|
internal sealed class CredentialKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.Credential;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.Credential;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.Credentials.SingleOrDefaultAsync(c => c.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.Credentials
|
|
.Where(c => c.VaultId == vaultId && ids.Contains(c.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var credential = new VaultCredential { Id = id, VaultId = vaultId };
|
|
|
|
database.Credentials.Add(credential);
|
|
|
|
return credential;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refuses every plaintext field there is.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Refused with a reason rather than silently dropped, so a client that believes it is storing something
|
|
/// finds out now rather than when the field turns out to be missing.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A credential has no relay target; relay fields may only be set on a host.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A credential has no public key.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <remarks>
|
|
/// Always null, which is a stronger statement than an empty record: this type has no plaintext columns,
|
|
/// so there is nothing a pull could hydrate even in principle.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|
|
|
|
/// <summary>Known host keys: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// As strict as <see cref="CredentialKind"/> about plaintext, for a reason that is about aggregation rather
|
|
/// than secrecy. A fingerprint is published so it can be compared and an address may already sit in a
|
|
/// relay-enabled host's columns — but the set of endpoints one user has approved is a map of their estate,
|
|
/// and this server has nothing to do with it.
|
|
/// </remarks>
|
|
internal sealed class KnownHostKeyKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.KnownHostKey;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.KnownHostKey;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.KnownHostKeys.SingleOrDefaultAsync(k => k.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.KnownHostKeys
|
|
.Where(k => k.VaultId == vaultId && ids.Contains(k.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var knownHost = new VaultKnownHostKey { Id = id, VaultId = vaultId };
|
|
|
|
database.KnownHostKeys.Add(knownHost);
|
|
|
|
return knownHost;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refuses every plaintext field there is.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The relay fields are refused although this type is the one that <em>does</em> hold an address, and
|
|
/// that is the point: the address belongs in the ciphertext. A client sending it here is either confused
|
|
/// or trying to get the server to keep a list it has no business keeping, and either way it should be
|
|
/// told rather than have the value quietly dropped.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A known host key is not something the server dials; its address stays encrypted.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A known host key's fingerprint stays inside its payload.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <remarks>
|
|
/// Always null, as for a credential: this type has no plaintext columns, so there is nothing a pull could
|
|
/// hydrate even in principle.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|
|
|
|
/// <summary>Host groups: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// The kind that closes a hole rather than opening one. <c>SyncPlaintextFields</c> has carried a
|
|
/// <c>GroupId</c> since the contract was frozen and <see cref="HostKind"/> used to copy it into a column;
|
|
/// nothing ever sent one, and now nothing may. The group itself arrives here as ciphertext with no name the
|
|
/// server can read, which is the same answer <see cref="KnownHostKeyKind"/> gives for the same reason.
|
|
/// </remarks>
|
|
internal sealed class HostGroupKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.HostGroup;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.HostGroup;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.HostGroups.SingleOrDefaultAsync(g => g.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.HostGroups
|
|
.Where(g => g.VaultId == vaultId && ids.Contains(g.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var group = new VaultHostGroup { Id = id, VaultId = vaultId };
|
|
|
|
database.HostGroups.Add(group);
|
|
|
|
return group;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refuses every plaintext field there is, including the one named after this type.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A <c>GroupId</c> on a group would be a parent pointer, and groups are flat — see
|
|
/// <see cref="VaultHostGroup"/> for why nesting merged by a scalar three-way merge can produce a cycle
|
|
/// nothing is able to repair. Refusing it here means a client that grows a tree cannot store one by
|
|
/// accident.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A host group is not something the server dials.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.GroupId is not null || fields.ParentId is not null)
|
|
{
|
|
error = "Host groups are flat, and a group's name is inside its payload.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A host group has no public key.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|
|
|
|
/// <summary>Snippets: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// A label column here would sort a list this server never draws, and the commands beside that label describe
|
|
/// the estate as precisely as a list of hostnames would. So this kind is as strict as
|
|
/// <see cref="CredentialKind"/>, and for the aggregation reason rather than the secrecy one.
|
|
/// </remarks>
|
|
internal sealed class SnippetKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.Snippet;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.Snippet;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.Snippets.SingleOrDefaultAsync(s => s.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.Snippets
|
|
.Where(s => s.VaultId == vaultId && ids.Contains(s.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var snippet = new VaultSnippet { Id = id, VaultId = vaultId };
|
|
|
|
database.Snippets.Add(snippet);
|
|
|
|
return snippet;
|
|
}
|
|
|
|
/// <summary>Refuses every plaintext field there is.</summary>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A snippet is not something the server dials.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.GroupId is not null || fields.ParentId is not null || fields.RelatedId is not null)
|
|
{
|
|
error = "A snippet's contents, including anything it is scoped to, stay inside its payload.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A snippet has no public key.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|
|
|
|
/// <summary>Connection log entries: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The strictest kind here, and the one where a plaintext column would have been most tempting: a
|
|
/// <c>started_at</c> would let this server order and prune a log without any client's help. It gets none,
|
|
/// because a timestamp column on this table is a record of when each user works, and the times are the
|
|
/// interesting part of a connection log even when the hostnames are sealed.
|
|
/// </para>
|
|
/// <para>
|
|
/// <b>The server cannot enforce write-once, and does not pretend to.</b> That an entry is created and never
|
|
/// updated is a client rule — see <see cref="VaultConnectionLogEntry"/> — and the shared write path would
|
|
/// accept an upsert with a correct <c>expectedVersion</c> like any other. Adding a refusal here would be a
|
|
/// guarantee about payload semantics this server cannot read.
|
|
/// </para>
|
|
/// </remarks>
|
|
internal sealed class ConnectionLogEntryKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.ConnectionLogEntry;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.ConnectionLogEntry;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.ConnectionLog.SingleOrDefaultAsync(e => e.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.ConnectionLog
|
|
.Where(e => e.VaultId == vaultId && ids.Contains(e.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var entry = new VaultConnectionLogEntry { Id = id, VaultId = vaultId };
|
|
|
|
database.ConnectionLog.Add(entry);
|
|
|
|
return entry;
|
|
}
|
|
|
|
/// <summary>Refuses every plaintext field there is.</summary>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A log entry is not something the server dials; what was connected to stays encrypted.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.GroupId is not null || fields.ParentId is not null || fields.RelatedId is not null)
|
|
{
|
|
error = "A log entry names what it is about inside its payload.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.Kind is not null || fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A log entry carries no plaintext fields at all.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|
|
|
|
/// <summary>Activity log entries: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// As strict as <see cref="ConnectionLogEntryKind"/>. <c>SyncPlaintextFields.Kind</c> exists and would fit
|
|
/// "which sort of item this entry is about" exactly, which is why it is refused by name: a column recording
|
|
/// that a user created four SSH keys last Tuesday is a description of the keychain, assembled from facts
|
|
/// that each look harmless.
|
|
/// </remarks>
|
|
internal sealed class ActivityLogEntryKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.ActivityLogEntry;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.ActivityLogEntry;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.ActivityLog.SingleOrDefaultAsync(e => e.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.ActivityLog
|
|
.Where(e => e.VaultId == vaultId && ids.Contains(e.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var entry = new VaultActivityLogEntry { Id = id, VaultId = vaultId };
|
|
|
|
database.ActivityLog.Add(entry);
|
|
|
|
return entry;
|
|
}
|
|
|
|
/// <summary>Refuses every plaintext field there is.</summary>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A log entry is not something the server dials.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.GroupId is not null || fields.ParentId is not null || fields.RelatedId is not null)
|
|
{
|
|
error = "Which item a log entry is about stays inside its payload.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.Kind is not null || fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A log entry carries no plaintext fields at all.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|
|
|
|
/// <summary>Object stores: an envelope and nothing else.</summary>
|
|
/// <remarks>
|
|
/// As strict as <see cref="CredentialKind"/>, because it holds the same class of thing. A secret access key
|
|
/// is a password; the endpoint beside it is, for everybody self-hosting, an address on their own network. The
|
|
/// relay does not dial a bucket, so ADR 0004's one concession has no analogue here and there is nothing to
|
|
/// weigh.
|
|
/// </remarks>
|
|
internal sealed class ObjectStoreKind : IItemKind
|
|
{
|
|
/// <inheritdoc />
|
|
public SyncEntityType WireType => SyncEntityType.ObjectStore;
|
|
|
|
/// <inheritdoc />
|
|
public ChangeEntityType ChangeType => ChangeEntityType.ObjectStore;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IVaultItem?> FindAsync(
|
|
DodoDbContext database,
|
|
Guid id,
|
|
CancellationToken cancellationToken) =>
|
|
await database.ObjectStores.SingleOrDefaultAsync(o => o.Id == id, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<Guid, IVaultItem>> LoadAsync(
|
|
DodoDbContext database,
|
|
Guid vaultId,
|
|
Guid[] ids,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var rows = await database.ObjectStores
|
|
.Where(o => o.VaultId == vaultId && ids.Contains(o.Id))
|
|
.ToListAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
return rows.ToDictionary(row => row.Id, row => (IVaultItem)row);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IVaultItem Add(DodoDbContext database, Guid id, Guid vaultId)
|
|
{
|
|
var store = new VaultObjectStore { Id = id, VaultId = vaultId };
|
|
|
|
database.ObjectStores.Add(store);
|
|
|
|
return store;
|
|
}
|
|
|
|
/// <summary>Refuses every plaintext field there is.</summary>
|
|
/// <remarks>
|
|
/// The relay fields are refused although this type <em>does</em> hold an address, exactly as they are for
|
|
/// a pinned host key: the address belongs in the ciphertext, and a client sending it here is either
|
|
/// confused or trying to get the server to keep a list of where its users store data.
|
|
/// </remarks>
|
|
/// <inheritdoc />
|
|
public bool ValidateFields(SyncPlaintextFields fields, out string error)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(fields);
|
|
|
|
error = string.Empty;
|
|
|
|
if (fields.RelayEnabled || fields.Hostname is not null || fields.Port is not null)
|
|
{
|
|
error = "A bucket is not something the server dials; its endpoint stays encrypted.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.GroupId is not null || fields.ParentId is not null || fields.RelatedId is not null)
|
|
{
|
|
error = "A bucket's contents are inside its payload.";
|
|
return false;
|
|
}
|
|
|
|
if (fields.Kind is not null || fields.PublicKeyFingerprint is not null)
|
|
{
|
|
error = "A bucket carries no plaintext fields at all.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <remarks>Nothing to copy: this type has no plaintext columns to copy anything into.</remarks>
|
|
/// <inheritdoc />
|
|
public void ApplyFields(IVaultItem item, SyncPlaintextFields fields)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ClearFieldsOnDelete(IVaultItem item)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public SyncPlaintextFields? Hydrate(IVaultItem item) => null;
|
|
}
|