Public Access
Sync SSH keys as a vault item type, over a shared write path
The private key now lives in the vault as ciphertext, syncs between a user's machines, and is stored on the server so it can later be shared — sharing itself needs M3's signed grants; this is the storage that makes it possible. More was already reserved than expected: SyncEntityType.SshKey, CryptoSpec.AadResourceType.SshKey, ChangeEntityType.SshKey, SyncPlaintextFields.PublicKeyFingerprint, and SshPrivateKeyCredential wired through PrivateKeyFile over a MemoryStream so a key never touches disk. The frozen contract and crypto spec needed no change at all. What was missing was the server. Rather than copy the push path per item type — version check, change-log append, exactly-once receipt, advisory lock — it is now written once over IVaultItem, with everything type-specific behind IItemKind: which table, which plaintext columns, and what those columns must satisfy. Ten copies of that logic by M5, with a fix applied to nine, is the outcome this avoids. The refactor landed first with no behaviour change, so all 66 existing Host tests were the regression net, and they stayed green. An interface rather than a base class, deliberately: EF Core maps an inheritance hierarchy when it can see one, so a mapped base would quietly become a table-per-hierarchy discriminator across item types — the very arrangement per-type tables exist to avoid. ssh_key mirrors host and pointedly has no relay trio. That is the argument for separate tables rather than one wide item table: the columns a host needs are columns a key must never have, and a shared table could only make them nullable and trust the code. A key carrying a relay target is refused with a reason rather than silently dropped. A key hydrates PlaintextFields as null, not an empty instance — the difference is visible on the wire, because an all-defaults instance still serialises "relayEnabled": false and invites a reader to believe the setting exists and is off. It has none. Two things now defended by tests rather than by comments. Each kind states its own ChangeEntityType instead of casting: the two enums agree numerically but do not even share member names (Host against SshHost), and filing key changes under the host type is silent sync corruption — sabotaging it fails three tests. And EntityTypeAlignmentTests asserts the two enums stay aligned in both directions and in count, which nothing did before. The client half is next: SshKeySecret, its codec and merge, the cipher, a repository, and the UI. Note for that work — SyncEntityType.SshKey is 3 while AadResourceType.SshKey is 6, so a cast between them would seal key ciphertext as a vault and nothing would fail.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using DodoSSH.Contracts;
|
||||
using DodoSSH.Domain;
|
||||
|
||||
namespace DodoSSH.Api.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The two entity-type enums have to agree, and nothing but this makes them.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <c>SyncService</c> converts between the wire's <see cref="SyncEntityType"/> and the change log's
|
||||
/// <see cref="ChangeEntityType"/> with a raw <c>(ChangeEntityType)(int)</c> cast in both directions. That
|
||||
/// works only because two independently maintained enums in two assemblies happen to number their members
|
||||
/// identically — and they do not even name them identically: <c>Host = 1</c> against <c>SshHost = 1</c>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If they ever drift, nothing fails loudly. Changes get filed in the log under a different item type's
|
||||
/// name, and the next delta pull loads rows of the wrong type — or none — for entities the client asked
|
||||
/// about. That is silent data loss on the sync path, which is precisely the failure this project has
|
||||
/// designed everything else to avoid, so the alignment gets a test rather than a comment.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Numeric alignment only. The two lists are deliberately <em>not</em> aligned with
|
||||
/// <c>CryptoSpec.AadResourceType</c>, which also carries None/User/Device/Vault and therefore numbers the
|
||||
/// same item types differently — asserting three-way equality would be asserting something false.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class EntityTypeAlignmentTests
|
||||
{
|
||||
[Fact]
|
||||
public void EveryWireEntityType_HasAChangeLogTypeWithTheSameValue()
|
||||
{
|
||||
var changeValues = Enum.GetValues<ChangeEntityType>().Select(value => (int)value).ToHashSet();
|
||||
|
||||
foreach (var wire in Enum.GetValues<SyncEntityType>())
|
||||
{
|
||||
changeValues.ShouldContain(
|
||||
(int)wire,
|
||||
$"SyncEntityType.{wire} = {(int)wire} has no ChangeEntityType with that value, so "
|
||||
+ "SyncService's cast would produce an undefined enum value.");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EveryChangeLogType_HasAWireTypeWithTheSameValue()
|
||||
{
|
||||
// The other direction matters just as much: a pull converts stored changes back to wire types, so a
|
||||
// change-log type with no wire counterpart would be served to clients as an undefined enum.
|
||||
var wireValues = Enum.GetValues<SyncEntityType>().Select(value => (int)value).ToHashSet();
|
||||
|
||||
foreach (var change in Enum.GetValues<ChangeEntityType>())
|
||||
{
|
||||
wireValues.ShouldContain(
|
||||
(int)change,
|
||||
$"ChangeEntityType.{change} = {(int)change} has no SyncEntityType with that value.");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TheTwoEnums_HaveTheSameNumberOfMembers()
|
||||
{
|
||||
// Catches a member added to one list only, which the two checks above would miss if it reused a
|
||||
// value already present in the other.
|
||||
Enum.GetValues<SyncEntityType>().Length.ShouldBe(Enum.GetValues<ChangeEntityType>().Length);
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// Spot-checked by name as well, because the pairing that is easiest to get wrong is the one whose names
|
||||
/// differ. Someone adding an item type may reasonably assume the lists are name-matched, notice
|
||||
/// <c>Host</c> has no <c>Host</c> on the other side, and renumber to "fix" it.
|
||||
/// </remarks>
|
||||
[Fact]
|
||||
public void TheDifferentlyNamedPair_IsTheOneThatMatches()
|
||||
{
|
||||
((int)SyncEntityType.Host).ShouldBe((int)ChangeEntityType.SshHost);
|
||||
((int)SyncEntityType.SshKey).ShouldBe((int)ChangeEntityType.SshKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user