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,180 @@
|
||||
namespace DodoSSH.Client.Domain.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The credential record, its codec and its merge.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Deliberately shorter than <see cref="SshKeySecretTests"/>. The two types have the same shape and the same
|
||||
/// merge strategy, so what is covered here is what is specific to a credential: that an empty password is
|
||||
/// refused while a password of spaces is not, that "no username" has one spelling, and that the password
|
||||
/// never appears in a conflict log.
|
||||
/// </remarks>
|
||||
public sealed class CredentialSecretTests
|
||||
{
|
||||
[Fact]
|
||||
public void AnEmptyUsername_IsTheSameAsNone()
|
||||
{
|
||||
// One spelling of "use the host's username", for the same reasons SshKeySecret.Passphrase normalises:
|
||||
// identical credentials encode identically, and "does this override the host?" has one answer.
|
||||
Credential(username: string.Empty).Username.ShouldBeNull();
|
||||
Credential(username: null).Username.ShouldBeNull();
|
||||
Credential(username: "postgres").Username.ShouldBe("postgres");
|
||||
|
||||
Credential(username: string.Empty).ShouldBe(Credential(username: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void APasswordOfSpaces_IsAPassword()
|
||||
{
|
||||
// IsNullOrWhiteSpace would refuse this, and refusing it would lock someone out of a host over a
|
||||
// validation opinion. Only genuinely empty is refused.
|
||||
Credential(password: " ").TryValidate(out var reason).ShouldBeTrue(reason);
|
||||
Credential(password: string.Empty).TryValidate(out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "hunter2", "needs a name")]
|
||||
[InlineData(" ", "hunter2", "needs a name")]
|
||||
[InlineData("db", "", "needs a password")]
|
||||
public void AnInvalidCredential_SaysWhatIsWrongWithIt(string label, string password, string expected)
|
||||
{
|
||||
var credential = new CredentialSecret { Label = label, Password = password };
|
||||
|
||||
credential.TryValidate(out var reason).ShouldBeFalse();
|
||||
reason.ShouldNotBeNull().ShouldContain(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ACredential_SurvivesARoundTrip()
|
||||
{
|
||||
var credential = Credential(username: "postgres") with { Notes = "rotate in June" };
|
||||
|
||||
var encoded = CredentialSecretCodec.Encode(credential);
|
||||
|
||||
CredentialSecretCodec.TryDecode(encoded, out var document).ShouldBeTrue();
|
||||
|
||||
document.ShouldNotBeNull();
|
||||
document.Credential.ShouldBe(credential);
|
||||
document.SchemaVersion.ShouldBe(CredentialSecretCodec.CurrentSchemaVersion);
|
||||
document.IsReadOnly.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodingIsDeterministic()
|
||||
{
|
||||
CredentialSecretCodec.Encode(Credential())
|
||||
.ShouldBe(CredentialSecretCodec.Encode(Credential()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnEmptyUsernameIsNotWrittenAtAll()
|
||||
{
|
||||
CredentialSecretCodec.Encode(Credential(username: string.Empty))
|
||||
.ShouldBe(CredentialSecretCodec.Encode(Credential(username: null)));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("not json")]
|
||||
[InlineData("{}")]
|
||||
[InlineData("""{"schemaVersion":1,"label":"db"}""")]
|
||||
[InlineData("""{"schemaVersion":1,"password":"hunter2"}""")]
|
||||
[InlineData("""{"schemaVersion":0,"label":"db","password":"hunter2"}""")]
|
||||
public void APayloadThatIsNotACredential_DoesNotDecode(string json)
|
||||
{
|
||||
CredentialSecretCodec
|
||||
.TryDecode(System.Text.Encoding.UTF8.GetBytes(json), out var document)
|
||||
.ShouldBeFalse();
|
||||
|
||||
document.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ACredentialFromANewerClient_IsReadableButNotWritable()
|
||||
{
|
||||
var payload = System.Text.Encoding.UTF8.GetBytes(
|
||||
"""
|
||||
{"schemaVersion":99,"label":"db","password":"hunter2","totpSeed":"something new"}
|
||||
""");
|
||||
|
||||
CredentialSecretCodec.TryDecode(payload, out var document).ShouldBeTrue();
|
||||
|
||||
document.ShouldNotBeNull().IsReadOnly.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EachSideEditingADifferentField_KeepsBoth()
|
||||
{
|
||||
var ancestor = Credential();
|
||||
var local = ancestor with { Label = "db-primary" };
|
||||
var remote = ancestor with { Notes = "from the desktop" };
|
||||
|
||||
var merged = CredentialSecretMerge.Merge(ancestor, local, remote);
|
||||
|
||||
merged.HasConflicts.ShouldBeFalse();
|
||||
merged.Merged.Label.ShouldBe("db-primary");
|
||||
merged.Merged.Notes.ShouldBe("from the desktop");
|
||||
merged.Merged.Password.ShouldBe(ancestor.Password);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BothSidesChangingThePassword_NeverPutsEitherInTheConflictLog()
|
||||
{
|
||||
// The reason CredentialSecretMerge redacts, and the case for it is if anything plainer than for a
|
||||
// key: a discarded password is very often still the live password on some other system.
|
||||
var ancestor = Credential();
|
||||
var local = ancestor with { Password = "LAPTOP-SECRET" };
|
||||
var remote = ancestor with { Password = "DESKTOP-SECRET" };
|
||||
|
||||
var merged = CredentialSecretMerge.Merge(ancestor, local, remote);
|
||||
|
||||
var conflict = merged.Conflicts.ShouldHaveSingleItem();
|
||||
conflict.Field.ShouldBe(nameof(CredentialSecret.Password));
|
||||
|
||||
var kept = conflict.Kept.ShouldNotBeNull();
|
||||
var discarded = conflict.Discarded.ShouldNotBeNull();
|
||||
|
||||
foreach (var reported in new[] { kept, discarded })
|
||||
{
|
||||
reported.ShouldNotContain("LAPTOP-SECRET");
|
||||
reported.ShouldNotContain("DESKTOP-SECRET");
|
||||
}
|
||||
|
||||
// Redacting the report must not redact the value.
|
||||
merged.Merged.Password.ShouldBeOneOf("LAPTOP-SECRET", "DESKTOP-SECRET");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AUsernameClash_IsShownInFull()
|
||||
{
|
||||
// Not a secret, and knowing which account the merge dropped is the whole use of the notice.
|
||||
var ancestor = Credential();
|
||||
var local = ancestor with { Username = "postgres" };
|
||||
var remote = ancestor with { Username = "deploy" };
|
||||
|
||||
var merged = CredentialSecretMerge.Merge(ancestor, local, remote);
|
||||
|
||||
var conflict = merged.Conflicts.ShouldHaveSingleItem();
|
||||
conflict.Field.ShouldBe(nameof(CredentialSecret.Username));
|
||||
|
||||
new[] { conflict.Kept, conflict.Discarded }.ShouldBe(["deploy", "postgres"], ignoreOrder: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AUsernameClashingWithItsRemoval_SaysWhichSideHadNone()
|
||||
{
|
||||
var ancestor = Credential(username: "root");
|
||||
var local = ancestor with { Username = null };
|
||||
var remote = ancestor with { Username = "deploy" };
|
||||
|
||||
var merged = CredentialSecretMerge.Merge(ancestor, local, remote);
|
||||
|
||||
var conflict = merged.Conflicts.ShouldHaveSingleItem();
|
||||
conflict.Kept.ShouldBe("deploy");
|
||||
conflict.Discarded.ShouldBe("(none)");
|
||||
}
|
||||
|
||||
private static CredentialSecret Credential(
|
||||
string password = "hunter2",
|
||||
string? username = null) =>
|
||||
new() { Label = "db", Password = password, Username = username };
|
||||
}
|
||||
Reference in New Issue
Block a user