Public Access
Sync credentials as a vault item type, and bind one to a host
Closes the largest remaining M1 gap in the data layer: a username and password can live in the vault, sync between machines, and be named by a host as how it authenticates. What is not here is the interface for creating one — see the end of this message. The third item type, and the first one that cost almost nothing to add. Server: a VaultCredential row, an EF configuration, a migration, and a CredentialKind. Client: a secret, a codec, a merge, a cipher, a kind, a repository facade and a session property. No new reconciliation logic, no change to the sync engine, no client cache migration. That was the whole point of the item-kind seam, and this is the evidence it holds. The narrowest type of the three on plaintext, and not for symmetry. A host has a deliberate concession — the relay needs an address it can resolve. A key has a fingerprint, public by nature, which this client still declines to send. A password has no part that is safe to expose: not its length, not a hash, not a hint. So CredentialKind refuses every plaintext field there is, hydrates none, and the table has no column to put one in. HostSecret.CredentialId is the password counterpart of SshKeyId, and the two are mutually exclusive. SSH itself would happily try a key and fall back to a password, but a host naming both leaves "how does this authenticate?" without a single answer — the interface, the connect path and the user would each be free to guess differently. TryValidate refuses it. One consequence was not anticipated: "a full host" stops being a coherent idea, which is what broke AFullHost_RoundTrips and is now written into that test. The schema version became a ladder rather than a maximum: credential-bound is 3, key-bound is 2, neither is still 1. Adding credentials therefore does not drag every key-bound host in every vault onto a version that clients understanding keys perfectly well would refuse to edit. A test pins exactly that, because it is the property the whole content-dependent-version rule exists to provide, and the obvious implementation would quietly lose it. Two tests had become false and said so: - Push_AnUnsupportedEntityType_IsInvalidNotAFailedBatch used Credential as its example of a type this server does not implement. It now asks the server's own registry what is still missing, so it cannot go stale again, and skips with a reason if that set ever empties. - ThePullFilterNamesEveryTypeThisBuildSynchronises pinned the exact list, which is what it is for. Also fixes ten nullable warnings — eight in SyncEndpointTests, two in a test file added earlier today. Neither set was introduced here; both were invisible until an unrelated change forced their project to recompile, which means the zero-warning claims made earlier in this work only ever covered what happened to be rebuilt. 777 tests green. Zero warnings, dotnet format clean. Not done, and deliberately: the credential interface. The vault column is 340 pixels wide and already holds two lists and two editors, kept from clipping its own buttons at the window's minimum height only by the one-editor-at-a-time rule added earlier today. A third list and a third editor would recreate that defect rather than avoid it, so the column needs a shape decision first. Credentials sync; they cannot yet be created in the interface.
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
using DodoSSH.Client.Storage;
|
||||
using DodoSSH.Contracts;
|
||||
using static DodoSSH.Client.Sync.Tests.SyncHarness;
|
||||
|
||||
namespace DodoSSH.Client.Sync.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Credentials through the two-machine harness.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Shorter still than <see cref="SshKeySyncTests"/>, and that is the payoff of the shared reconciler: the six
|
||||
/// collision outcomes are one implementation and are already exercised. What is left to check per type is its
|
||||
/// cipher, what it tells the server, that its items cannot be confused with another type's, and that the one
|
||||
/// thing which must never be logged is not logged.
|
||||
/// </remarks>
|
||||
public sealed class CredentialSyncTests : IAsyncLifetime
|
||||
{
|
||||
private SyncHarness harness = null!;
|
||||
|
||||
private static CancellationToken Token => TestContext.Current.CancellationToken;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask InitializeAsync() => harness = await CreateAsync();
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
harness.Dispose();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ACredentialCreatedOnOneMachine_ReachesTheOther()
|
||||
{
|
||||
var entityId = await harness.First.CreateCredentialAsync(
|
||||
Credential("prod-db", password: "hunter2", username: "postgres", notes: "rotate in June"));
|
||||
|
||||
await harness.SettleAsync();
|
||||
|
||||
var seen = await harness.Second.FindCredentialAsync(entityId);
|
||||
|
||||
seen.Secret.Label.ShouldBe("prod-db");
|
||||
seen.Secret.Password.ShouldBe("hunter2");
|
||||
seen.Secret.Username.ShouldBe("postgres");
|
||||
seen.Secret.Notes.ShouldBe("rotate in June");
|
||||
seen.HasUnsyncedChanges.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThePull_AsksForAllThreeTypes()
|
||||
{
|
||||
await harness.First.SyncAsync();
|
||||
|
||||
var asked = harness.Server.LastPullTypes.ShouldNotBeNull();
|
||||
|
||||
asked.ShouldContain(SyncEntityType.Host);
|
||||
asked.ShouldContain(SyncEntityType.SshKey);
|
||||
asked.ShouldContain(SyncEntityType.Credential);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ACredentialHandsTheServerNothingInPlaintext()
|
||||
{
|
||||
var entityId = await harness.First.CreateCredentialAsync(Credential("prod-db"));
|
||||
|
||||
var queued = await harness.First.Outbox
|
||||
.FindAsync(VaultId, SyncEntityType.Credential, entityId, Token);
|
||||
|
||||
queued.ShouldNotBeNull();
|
||||
queued.Fields.ShouldBeNull("nothing about a password is safe to hold in the clear");
|
||||
|
||||
await harness.SettleAsync();
|
||||
|
||||
var row = harness.Server.Find(entityId, SyncEntityType.Credential).ShouldNotBeNull();
|
||||
|
||||
row.Fields.RelayEnabled.ShouldBeFalse();
|
||||
row.Fields.Hostname.ShouldBeNull();
|
||||
row.Fields.PublicKeyFingerprint.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ThreeTypesSharingOneId_AreThreeItems()
|
||||
{
|
||||
// The cache keys on the type as well as the id, and each payload's AAD binds a different resource
|
||||
// type — two defences, independently. Arranged on the server because the repositories mint UUIDv7s
|
||||
// and would never collide.
|
||||
var sharedId = Guid.CreateVersion7();
|
||||
|
||||
harness.First.Keyring.TryGet(VaultId, out var vaultKey, out var generation).ShouldBeTrue();
|
||||
|
||||
harness.Server.ExternalUpsert(
|
||||
sharedId,
|
||||
HostCipher.Seal(Host("prod-db"), vaultKey.Span, sharedId, generation, itemVersion: 1),
|
||||
new SyncPlaintextFields(),
|
||||
SyncEntityType.Host);
|
||||
|
||||
harness.Server.ExternalUpsert(
|
||||
sharedId,
|
||||
SshKeyCipher.Seal(Key("deploy"), vaultKey.Span, sharedId, generation, itemVersion: 1),
|
||||
null,
|
||||
SyncEntityType.SshKey);
|
||||
|
||||
harness.Server.ExternalUpsert(
|
||||
sharedId,
|
||||
CredentialCipher.Seal(
|
||||
Credential("db-login"), vaultKey.Span, sharedId, generation, itemVersion: 1),
|
||||
null,
|
||||
SyncEntityType.Credential);
|
||||
|
||||
await harness.Second.SyncAsync();
|
||||
|
||||
(await harness.Second.ListAsync()).Items.ShouldHaveSingleItem()
|
||||
.Secret.Label.ShouldBe("prod-db");
|
||||
(await harness.Second.ListKeysAsync()).Items.ShouldHaveSingleItem()
|
||||
.Secret.Label.ShouldBe("deploy");
|
||||
|
||||
var credentials = await harness.Second.ListCredentialsAsync();
|
||||
|
||||
credentials.Items.ShouldHaveSingleItem().Secret.Label.ShouldBe("db-login");
|
||||
credentials.Unreadable.ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TwoMachinesEditingDifferentFields_BothSurvive()
|
||||
{
|
||||
var entityId = await harness.First.CreateCredentialAsync(Credential("prod-db"));
|
||||
await harness.SettleAsync();
|
||||
|
||||
await harness.First.UpdateCredentialAsync(entityId, Credential("prod-db-primary"));
|
||||
await harness.Second.UpdateCredentialAsync(
|
||||
entityId, Credential("prod-db", notes: "from the desktop"));
|
||||
|
||||
await harness.SettleAsync();
|
||||
|
||||
var first = (await harness.First.FindCredentialAsync(entityId)).Secret;
|
||||
|
||||
first.ShouldBe((await harness.Second.FindCredentialAsync(entityId)).Secret);
|
||||
first.Label.ShouldBe("prod-db-primary");
|
||||
first.Notes.ShouldBe("from the desktop");
|
||||
|
||||
(await ConflictKindsAsync()).ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BothChangedThePassword_NeitherReachesTheConflictLog()
|
||||
{
|
||||
var entityId = await harness.First.CreateCredentialAsync(Credential("prod-db"));
|
||||
await harness.SettleAsync();
|
||||
|
||||
await harness.First.UpdateCredentialAsync(entityId, Credential("prod-db", "LAPTOP-SECRET"));
|
||||
await harness.Second.UpdateCredentialAsync(entityId, Credential("prod-db", "DESKTOP-SECRET"));
|
||||
|
||||
await harness.SettleAsync();
|
||||
|
||||
(await ConflictKindsAsync()).ShouldContain(kind => kind == ConflictKind.FieldOverridden);
|
||||
|
||||
var details = await ConflictDetailsAsync();
|
||||
|
||||
details.ShouldContain(
|
||||
detail => detail.Contains("Password", StringComparison.Ordinal),
|
||||
"the user still has to be told which field clashed");
|
||||
|
||||
foreach (var detail in details)
|
||||
{
|
||||
detail.ShouldNotContain("LAPTOP-SECRET");
|
||||
detail.ShouldNotContain("DESKTOP-SECRET");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ACredentialEditedElsewhereAfterBeingDeletedHere_IsCalledACredential()
|
||||
{
|
||||
var entityId = await harness.First.CreateCredentialAsync(Credential("prod-db"));
|
||||
await harness.SettleAsync();
|
||||
|
||||
await harness.First.UpdateCredentialAsync(entityId, Credential("prod-db", notes: "still in use"));
|
||||
await harness.Second.Credentials.DeleteAsync(VaultId, entityId, Token);
|
||||
|
||||
await harness.SettleAsync();
|
||||
|
||||
(await harness.First.FindCredentialAsync(entityId)).Secret.Notes.ShouldBe("still in use");
|
||||
|
||||
var details = await ConflictDetailsAsync();
|
||||
|
||||
details.ShouldContain(
|
||||
detail => detail.Contains("This credential was edited", StringComparison.Ordinal));
|
||||
|
||||
details.ShouldNotContain(
|
||||
detail => detail.Contains("This host was edited", StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyList<ConflictKind>> ConflictKindsAsync()
|
||||
{
|
||||
var first = await harness.First.ConflictsAsync();
|
||||
var second = await harness.Second.ConflictsAsync();
|
||||
|
||||
return [.. first.Concat(second).Select(conflict => conflict.Kind)];
|
||||
}
|
||||
|
||||
private async Task<IReadOnlyList<string>> ConflictDetailsAsync()
|
||||
{
|
||||
var first = await harness.First.ConflictsAsync();
|
||||
var second = await harness.Second.ConflictsAsync();
|
||||
|
||||
return
|
||||
[
|
||||
.. first.Concat(second)
|
||||
.Select(conflict => System.Text.Encoding.UTF8.GetString(conflict.Detail)),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user