Public Access
Completes the client half of SSH keys: they sync alongside hosts, appear in their own list, and can be selected to authenticate a connection instead of typing a password. The reconciler and the repository were Host-typed throughout, so the choice was to generalise them or to keep a second copy per item type. Generalised, because ItemReconciler's whole premise is that the pull and the push paths must answer the same collision the same way — two copies would drift the first time one of them was fixed. What is genuinely per-type now arrives through IItemKind<TSecret>: the cipher, the merge, the plaintext columns, and the noun to use when telling a person what happened to their item. Generic where the server's IItemKind is not, and for the reason that reverses there — the client needs the concrete type, because it merges field by field. The pull filter is derived from the same registry that builds the reconcilers. That is the specific failure being designed out: an item type that encrypts, merges and lists perfectly and is never once requested from the server, so it works on the machine that made it and exists nowhere else. No client cache migration. The item table's primary key and the outbox's unique index already carry the entity type, and AadResourceTypes already mapped SshKey — so a host and a key may share an id and never see each other's rows, which SshKeySyncTests now arranges deliberately. A key hands the server nothing in plaintext. There is a public_key_fingerprint column and it would be accepted; leaving it null is deliberate. A fingerprint is not secret but it is a stable identifier for a key pair, so filling it would let an operator tell which of their users hold the same key and correlate one across vaults, for a column nothing reads. The design allows itself one plaintext concession — the relay address, which the relay cannot work without — and this is not that. A key is chosen per connection rather than bound to a host, which works the way ssh -i does. Binding one needs a field on HostSecret and therefore a payload schema bump, which makes every host written afterwards read-only on an older build; worth doing deliberately rather than as a side effect of adding keys. Three things this found, all of them by being falsified rather than by review: - Making the reconciler generic silently turned a record comparison into reference equality, because == on a type parameter is not value equality. The effect would have been a conflict recorded on every pass for an unacknowledged create that had in fact landed. Sabotaging the fix left all 73 tests passing — nothing covered that branch — so ConflictMatrixTests now has AnUnacknowledgedCreateThatDidLand_IsDroppedQuietly, which fails without it. - A test asserting that a blank passphrase reaches SSH.NET as null was vacuous: it exercised the editor, not the credential path, and passed with the guard deleted. Resolved by making SshKeySecret.Passphrase normalise an empty string to null, so there is one spelling of one state — which also keeps two clients from producing different payload bytes for an identical key. That exposed a wider gap: SshKeySecret, its codec and its merge had no direct unit tests at all. They have 25 now. - The reason first given for that normalisation was false. It claimed SSH.NET rejects a passphrase supplied for an unprotected key; measured against a real sshd it ignores it and authenticates anyway. Corrected everywhere it was stated and recorded in docs/platform-flags.md. The same test file also closes a real hole: SshPrivateKeyCredential had never been exercised against a server, because the existing key test builds SSH.NET's auth method directly and bypasses the path a vault-held key actually takes. Only one editor may be open at a time. Both sit in the same 340-pixel column as Auto rows and their heights together exceed it at the window's minimum size, so two open editors put the lower one's Save and Cancel past the bottom edge — the same failure this window already shipped once with the setup screens. Expressed as a state rule because that is the only form of it this repository can check: nothing here loads a .axaml. The refusal keeps what was typed, since in the key editor that is a pasted private key the user may have nowhere else. The end-to-end slice now carries a key as well as a host, so both item types go through the real API, the real PostgreSQL and the real crypto in one pass — the three hand-kept mappings between enums that do not line up are the reason that is worth doing rather than trusting the unit suites. 735 tests green, including the container-backed SSH and end-to-end suites. Zero warnings, dotnet format clean.
303 lines
10 KiB
C#
303 lines
10 KiB
C#
using DodoSSH.Client.Domain;
|
|
using DodoSSH.Client.Storage;
|
|
using DodoSSH.Crypto;
|
|
|
|
namespace DodoSSH.Client.Sync.Tests;
|
|
|
|
/// <summary>
|
|
/// One machine: its own cache, its own outbox, its own view of the vault.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A separate SQLite database per device, because the whole subject of these tests is two caches
|
|
/// diverging and being reconciled. Sharing one would make every conflict test vacuous.
|
|
/// </remarks>
|
|
internal sealed class SyncDevice : IDisposable
|
|
{
|
|
private readonly ClientCacheFactory factory;
|
|
private readonly MasterKey master;
|
|
private readonly LocalCacheProtector protector;
|
|
|
|
private SyncDevice(
|
|
string name,
|
|
ClientCacheFactory factory,
|
|
MasterKey master,
|
|
LocalCacheProtector protector,
|
|
VaultKeyring keyring,
|
|
FakeVaultServer server,
|
|
SyncOptions options)
|
|
{
|
|
Name = name;
|
|
this.factory = factory;
|
|
this.master = master;
|
|
this.protector = protector;
|
|
Keyring = keyring;
|
|
|
|
Items = new ItemStore(factory, protector);
|
|
Outbox = new OutboxStore(factory, protector, TimeProvider.System);
|
|
SyncState = new SyncStateStore(factory);
|
|
Conflicts = new ConflictStore(factory, protector, TimeProvider.System);
|
|
Hosts = new HostRepository(Items, Outbox, keyring);
|
|
SshKeys = new SshKeyRepository(Items, Outbox, keyring);
|
|
|
|
Engine = new SyncEngine(
|
|
server, Items, Outbox, SyncState, Conflicts, keyring, TimeProvider.System, options);
|
|
}
|
|
|
|
internal string Name { get; }
|
|
|
|
internal VaultKeyring Keyring { get; }
|
|
|
|
internal ItemStore Items { get; }
|
|
|
|
internal OutboxStore Outbox { get; }
|
|
|
|
internal SyncStateStore SyncState { get; }
|
|
|
|
internal ConflictStore Conflicts { get; }
|
|
|
|
internal HostRepository Hosts { get; }
|
|
|
|
internal SshKeyRepository SshKeys { get; }
|
|
|
|
internal SyncEngine Engine { get; }
|
|
|
|
internal static async Task<SyncDevice> CreateAsync(
|
|
string name,
|
|
UserSecretBundle bundle,
|
|
StoredVault vault,
|
|
FakeVaultServer server,
|
|
SyncOptions options)
|
|
{
|
|
var cache = ClientCacheFactory.ForMemory($"sync-{name}-{Guid.CreateVersion7():N}");
|
|
|
|
try
|
|
{
|
|
await cache.MigrateAsync(TestContext.Current.CancellationToken);
|
|
|
|
var derived = MasterKey.Derive(
|
|
$"passphrase-{name}", new byte[CryptoSpec.SaltSize], SyncHarness.CheapProfile);
|
|
|
|
// Opened through the real grant, so the keyring, the wrap and the AAD are all exercised.
|
|
var keyring = VaultKeyring.Open(bundle, [vault]);
|
|
|
|
return new SyncDevice(
|
|
name, cache, derived, LocalCacheProtector.From(derived), keyring, server, options);
|
|
}
|
|
catch
|
|
{
|
|
cache.Dispose();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
internal Task<SyncReport> SyncAsync() =>
|
|
Engine.SyncAsync(SyncHarness.VaultId, TestContext.Current.CancellationToken);
|
|
|
|
internal Task<ItemListing<HostSecret>> ListAsync() =>
|
|
Hosts.ListAsync(SyncHarness.VaultId, TestContext.Current.CancellationToken);
|
|
|
|
internal async Task<IReadOnlyList<HostSecret>> HostsSortedAsync()
|
|
{
|
|
var listing = await ListAsync();
|
|
|
|
return [.. listing.Items.Select(h => h.Secret).OrderBy(h => h.Label, StringComparer.Ordinal)];
|
|
}
|
|
|
|
internal async Task<VaultItem<HostSecret>> FindAsync(Guid entityId)
|
|
{
|
|
var listing = await ListAsync();
|
|
|
|
return listing.Items.SingleOrDefault(host => host.EntityId == entityId)
|
|
?? throw new InvalidOperationException($"{Name} cannot see host {entityId}.");
|
|
}
|
|
|
|
internal Task<Guid> CreateAsync(HostSecret host) =>
|
|
Hosts.CreateAsync(SyncHarness.VaultId, host, TestContext.Current.CancellationToken);
|
|
|
|
internal Task UpdateAsync(Guid entityId, HostSecret host) =>
|
|
Hosts.UpdateAsync(SyncHarness.VaultId, entityId, host, TestContext.Current.CancellationToken);
|
|
|
|
internal Task DeleteAsync(Guid entityId) =>
|
|
Hosts.DeleteAsync(SyncHarness.VaultId, entityId, TestContext.Current.CancellationToken);
|
|
|
|
// ---- The same four operations, on SSH keys ----
|
|
|
|
internal Task<ItemListing<SshKeySecret>> ListKeysAsync() =>
|
|
SshKeys.ListAsync(SyncHarness.VaultId, TestContext.Current.CancellationToken);
|
|
|
|
internal async Task<VaultItem<SshKeySecret>> FindKeyAsync(Guid entityId)
|
|
{
|
|
var listing = await ListKeysAsync();
|
|
|
|
return listing.Items.SingleOrDefault(key => key.EntityId == entityId)
|
|
?? throw new InvalidOperationException($"{Name} cannot see key {entityId}.");
|
|
}
|
|
|
|
internal Task<Guid> CreateKeyAsync(SshKeySecret key) =>
|
|
SshKeys.CreateAsync(SyncHarness.VaultId, key, TestContext.Current.CancellationToken);
|
|
|
|
internal Task UpdateKeyAsync(Guid entityId, SshKeySecret key) =>
|
|
SshKeys.UpdateAsync(SyncHarness.VaultId, entityId, key, TestContext.Current.CancellationToken);
|
|
|
|
internal Task DeleteKeyAsync(Guid entityId) =>
|
|
SshKeys.DeleteAsync(SyncHarness.VaultId, entityId, TestContext.Current.CancellationToken);
|
|
|
|
internal Task<IReadOnlyList<StoredConflict>> ConflictsAsync() =>
|
|
Conflicts.ListAsync(SyncHarness.VaultId, false, TestContext.Current.CancellationToken);
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
Keyring.Dispose();
|
|
protector.Dispose();
|
|
master.Dispose();
|
|
factory.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// One user, one vault, two machines and a server.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Both devices share the identity bundle, which is what a single user on a laptop and a desktop
|
|
/// actually looks like: one enrolled key pair, one vault grant, two independent local caches. That is
|
|
/// also the cheapest realistic setup in which every conflict case can be produced.
|
|
/// </remarks>
|
|
internal sealed class SyncHarness : IDisposable
|
|
{
|
|
internal static readonly Argon2Profile CheapProfile =
|
|
Argon2Profile.FromStoredParameters(memoryKibibytes: 8 * 1024, passes: 1, parallelism: 1);
|
|
|
|
private readonly UserSecretBundle bundle;
|
|
|
|
private SyncHarness(UserSecretBundle bundle, FakeVaultServer server, SyncDevice first, SyncDevice second)
|
|
{
|
|
this.bundle = bundle;
|
|
Server = server;
|
|
First = first;
|
|
Second = second;
|
|
}
|
|
|
|
internal static Guid VaultId { get; } = Guid.Parse("0192f0c8-7777-7c3d-8e4f-5a6b7c8d9e0f");
|
|
|
|
internal FakeVaultServer Server { get; }
|
|
|
|
/// <summary>The laptop.</summary>
|
|
internal SyncDevice First { get; }
|
|
|
|
/// <summary>The desktop.</summary>
|
|
internal SyncDevice Second { get; }
|
|
|
|
internal static async Task<SyncHarness> CreateAsync(SyncOptions? options = null)
|
|
{
|
|
var effective = options ?? SyncOptions.Default;
|
|
|
|
var identity = UserSecretBundle.Create(DateTimeOffset.FromUnixTimeSeconds(1_700_000_000));
|
|
|
|
try
|
|
{
|
|
var vaultKey = VaultKeys.Create();
|
|
var wrapped = VaultKeys.WrapTo(vaultKey, identity.EncryptionPublicKey, VaultId, 1);
|
|
|
|
// The plaintext key is not retained: each device unwraps the grant itself, as it would after
|
|
// an ordinary unlock.
|
|
System.Security.Cryptography.CryptographicOperations.ZeroMemory(vaultKey);
|
|
|
|
var vault = new StoredVault(
|
|
VaultId, "Personal", IsPersonal: true, TeamId: null, KeyGeneration: 1,
|
|
Permissions: 31, wrapped, RekeyRequired: false);
|
|
|
|
var server = new FakeVaultServer(VaultId);
|
|
|
|
var first = await SyncDevice.CreateAsync("laptop", identity, vault, server, effective);
|
|
|
|
try
|
|
{
|
|
var second = await SyncDevice.CreateAsync("desktop", identity, vault, server, effective);
|
|
return new SyncHarness(identity, server, first, second);
|
|
}
|
|
catch
|
|
{
|
|
first.Dispose();
|
|
throw;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
identity.Dispose();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>Brings both devices up to date, twice, so the result is a settled state.</summary>
|
|
/// <remarks>
|
|
/// Twice because one pass per device is not enough for a change made on one to be merged on the
|
|
/// other and then pushed back. Asserting on a settled state rather than on an intermediate one is
|
|
/// what makes "the two devices converge" a meaningful claim.
|
|
/// </remarks>
|
|
internal async Task SettleAsync()
|
|
{
|
|
for (var round = 0; round < 2; round++)
|
|
{
|
|
await First.SyncAsync();
|
|
await Second.SyncAsync();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
First.Dispose();
|
|
Second.Dispose();
|
|
bundle.Dispose();
|
|
}
|
|
|
|
// ---- Builders ----
|
|
|
|
internal static HostSecret Host(
|
|
string label,
|
|
string hostname = "db.internal",
|
|
int port = 22,
|
|
string? username = "deploy",
|
|
string? notes = null,
|
|
(string Name, string Value)[]? options = null,
|
|
bool relayEnabled = false) =>
|
|
new()
|
|
{
|
|
Label = label,
|
|
Hostname = hostname,
|
|
Port = port,
|
|
Username = username,
|
|
Notes = notes,
|
|
Options = options is null
|
|
? HostOptions.Empty
|
|
: HostOptions.Create(options.Select(o => new HostOption(o.Name, o.Value))),
|
|
RelayEnabled = relayEnabled,
|
|
};
|
|
|
|
/// <summary>
|
|
/// An SSH key whose material is a plausible shape but not a real key.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Not a valid Ed25519 key, and deliberately so: nothing in the sync path parses the material, and a
|
|
/// real private key checked into a test repository is a real private key on the internet regardless of
|
|
/// what it was used for. <c>SshKeySecret.TryValidate</c> only requires the armour, and the tests that
|
|
/// need a key SSH.NET can actually load live in <c>DodoSSH.Client.Ssh.Tests</c> where one is generated.
|
|
/// </remarks>
|
|
internal static SshKeySecret Key(
|
|
string label,
|
|
string material = "deploy-key-material",
|
|
string? passphrase = null,
|
|
string? publicKey = null,
|
|
string? notes = null) =>
|
|
new()
|
|
{
|
|
Label = label,
|
|
PrivateKeyPem = $"-----BEGIN OPENSSH PRIVATE KEY-----\n{material}\n"
|
|
+ "-----END OPENSSH PRIVATE KEY-----\n",
|
|
Passphrase = passphrase,
|
|
PublicKey = publicKey,
|
|
Notes = notes,
|
|
};
|
|
}
|