Bind an SSH key to a host instead of picking one per connection

A host now names the key it authenticates with, or none, as a field in its
encrypted payload — so the choice follows the host to every machine rather than
being made again each time somebody connects. The per-connection "Use key"
switch it replaces was a stopgap for not having this, and keeping both would
have left two mechanisms answering one question.

This is the first payload schema version bump, and it does not work the obvious
way. A host is written at the *lowest* schema version that can represent it: one
that binds a key is written at 2, one that does not is still written at 1, byte
for byte as it was before the field existed. The version is what makes an older
client refuse to edit an item, so stamping 2 unconditionally would mean
upgrading a single machine and renaming a single host made that host uneditable
on every machine that had not upgraded yet. Confining the cost to the hosts that
actually use the field is the difference between a team noticing a bump and a
team being blocked by one. HostSecretCodec states the rule so the next field
added follows it, and a test pins the version-1 bytes against a literal rather
than against the codec, because the claim is about history: every host already in
every vault has to re-encode to what it encoded before, or the first sync after
an upgrade would push the whole vault as changed.

A binding is an item id, not a copy of the key — a second copy of a private key
is one that goes stale — which means the reference can dangle when the key is
deleted on another machine. Both places that meets are handled the same way, by
refusing rather than falling back:

- Connecting to a host whose key is gone is refused outright. A host somebody
  deliberately set up for key-only access must not quietly start offering a
  password.
- Opening such a host in the editor keeps the binding, selected, labelled as
  missing. The quieter version of the same failure is someone editing the port
  and saving, silently converting the host to password authentication with
  nothing ever having said so.

Two things this found by being falsified:

- The merge was untested for the new field, and "just take the server's value"
  passed the entire suite — a local binding change would have been discarded with
  no conflict recorded. HostSecretMergeTests already had a test written for
  exactly this class of omission; it simply had not been extended.

- Adding a nullable field exposed a defect in HostSecretMerge.Field: it
  short-circuited when the discarded value was null, so the formatter never ran
  for the one case where null is a value rather than an absence, and a field
  whose absence has a name could not report it. Now the formatter always runs,
  and "no key" appears in the conflict log where an empty string used to.

Also fixes eight nullable warnings in SyncEndpointTests left by the server-side
SSH key commit, which had omitted the null-forgiving operator the rest of that
file uses. They were invisible until an unrelated change forced the project to
recompile.

The end-to-end slice now binds its host to its key, so a schema-version-2
payload goes through the real API, the real PostgreSQL and back out on a second
machine.

745 tests green. Zero warnings, dotnet format clean.
This commit is contained in:
2026-07-29 20:42:51 +02:00
parent e3fd3e1728
commit 70b3290a77
12 changed files with 523 additions and 105 deletions
+8 -8
View File
@@ -623,14 +623,14 @@ public sealed class SyncEndpointTests(ApiFixture fixture)
new SyncPushRequest([KeyOperation(keyId, expectedVersion: null, envelope: [9, 8, 7])]));
var results = await pushed.Content.ReadContractAsync<SyncPushResponse>();
results.Results.ShouldHaveSingleItem().Status.ShouldBe(SyncOperationStatus.Applied);
results!.Results.ShouldHaveSingleItem().Status.ShouldBe(SyncOperationStatus.Applied);
var pulled = await client.PostContractAsync(
PullUrl(vaultId),
new SyncPullRequest(null, null, [SyncEntityType.SshKey]));
var page = await pulled.Content.ReadContractAsync<SyncPullResponse>();
var change = page.Changes.ShouldHaveSingleItem();
var change = page!.Changes.ShouldHaveSingleItem();
change.EntityType.ShouldBe(SyncEntityType.SshKey);
change.EntityId.ShouldBe(keyId);
@@ -654,10 +654,10 @@ public sealed class SyncEndpointTests(ApiFixture fixture)
var pushed = await client.PostContractAsync(PushUrl(vaultId), new SyncPushRequest([operation]));
var result = (await pushed.Content.ReadContractAsync<SyncPushResponse>()).Results.ShouldHaveSingleItem();
var result = (await pushed.Content.ReadContractAsync<SyncPushResponse>())!.Results.ShouldHaveSingleItem();
result.Status.ShouldBe(SyncOperationStatus.Invalid);
result.Detail.ShouldContain("no relay target");
result.Detail.ShouldNotBeNull().ShouldContain("no relay target");
}
/// <remarks>
@@ -682,7 +682,7 @@ public sealed class SyncEndpointTests(ApiFixture fixture)
KeyOperation(keyId, expectedVersion: null, envelope: [2, 2]),
]));
(await pushed.Content.ReadContractAsync<SyncPushResponse>()).Results
(await pushed.Content.ReadContractAsync<SyncPushResponse>())!.Results
.ShouldAllBe(result => result.Status == SyncOperationStatus.Applied);
var pulled = await client.PostContractAsync(
@@ -691,7 +691,7 @@ public sealed class SyncEndpointTests(ApiFixture fixture)
var page = await pulled.Content.ReadContractAsync<SyncPullResponse>();
page.Changes.Count.ShouldBe(2);
page!.Changes.Count.ShouldBe(2);
page.Changes.Select(change => change.ChangeSequence)
.ShouldBeInOrder(Shouldly.SortDirection.Ascending);
@@ -733,7 +733,7 @@ public sealed class SyncEndpointTests(ApiFixture fixture)
PlaintextFields: null),
]));
(await deleted.Content.ReadContractAsync<SyncPushResponse>()).Results
(await deleted.Content.ReadContractAsync<SyncPushResponse>())!.Results
.ShouldHaveSingleItem().Status.ShouldBe(SyncOperationStatus.Applied);
var pulled = await client.PostContractAsync(
@@ -741,7 +741,7 @@ public sealed class SyncEndpointTests(ApiFixture fixture)
new SyncPullRequest(null, null, [SyncEntityType.SshKey]));
var page = await pulled.Content.ReadContractAsync<SyncPullResponse>();
var last = page.Changes[^1];
var last = page!.Changes[^1];
last.Operation.ShouldBe(SyncOperation.Delete);
last.Payload.ShouldBeNull("a tombstone must not ship the key material it replaced");