Public Access
M3's teams, sharing and ACLs. Teams with roles, a public-key directory, the append-only key log served for clients to check it against, team-owned vaults, and vault key grants wrapped by a client and stored opaquely by the server. VaultAccessService resolves team membership to PermissionFlags, so a viewer may pull and may not push; the desktop client reads and syncs every vault it holds a key for, and a real TEAMS screen replaces the one that said it did not exist. No migration: team, team_membership, vault.team_id and vault_key_grant have all been there since the first one, which is what carrying two unused tables bought. Membership is authorisation. A grant is access. The obvious model is one concept — "access", with a role attached, handed out by the server — and this architecture cannot implement it: a vault key is sealed to each member's X25519 key, and only a client holding the plaintext can seal it for somebody else. So "give Bob access" decomposes into a database write and a wrap, which happen on different machines. Adding a member makes the server serve them the vault; it cannot make it readable. VaultSummary.WrappedVaultKey is null in the meantime and the vault appears in their list saying it is waiting for a key, because hiding it until a grant existed would have been tidier and would have implied the server was the thing granting access. The screen says the same thing after every add, in the status line. ADR 0009 records the whole decision. Sharing verifies or refuses. A directory lookup is a claim by the server about a third party's public key, and wrapping to an unverified claim hands the vault to whoever made it — no amount of transport security helps, because the server is inside the threat model. KeyLogAudit reads the whole log, recomputes every entry's hash from its own contents, checks the chain from genesis, and refuses unless the offered key appears in it unchanged. There is no override flag: one that exists gets used on the day the log is briefly unreachable, and the resulting grant is indistinguishable from a correct one afterwards. What it still cannot promise is that the key is the right person's, so the fingerprint comes back for an out-of-band comparison and the success message says so every time. A test corrupts the fake server's log by one byte and watches the client refuse rather than warn. The roles are only the ones that are enforceable. There is no ConnectOnly, despite the design asking for one and TeamRole having room: SSH terminates on the client, so a session needs the credential's plaintext on that machine, and "may connect but may not read the key" cannot be enforced here. Shipping it as an option in a dropdown would have been a lie. Connect rides along with Read and is documented as an interface hint. Removal is named for what it does — it revokes grants and flags the vault for rekey, and claims nothing about what is already on somebody's laptop. Three things are deliberately absent, and each is a refusal rather than an omission. The rekey itself, because re-wrapping every item's data key under a new vault key needs a client holding the current one; the server records that a rotation is owed and the interface reports it, which is more honest than a button that only appears to do it. Ownership transfer, because allowing an owner to be removed without one leaves a team nobody can administer. And cross-vault host key trust: a pin in a team vault is listed but not consulted at connect time, because any member with Write could otherwise pre-approve a fingerprint another member's client then trusts silently for a host in their own vault. Scoping trust properly needs a scope on the SSH connect path, which IKnownHostStore has not got; until then the narrow direction is the safe one and the cost is in the README rather than hidden. Reading now spans vaults and writing still does not. Every list on the vault and hosts screens covers each vault the keyring opened, rows carry the vault they came from, and an edit goes back to that vault rather than to the active one — writing it to the active vault would fork the item and only show up when a colleague wondered why their change never arrived. A new item goes wherever a picker says, defaulting to the personal vault and never moving on its own, because an item filed into a team's vault is visible to that team and moving it back means deleting and retyping. The sidebar heading stops naming one vault once there are two, and each row names its own. The server checks what it can and nothing it cannot. It will not record a grant for a key its recipient no longer holds, for a superseded generation, or for somebody who is not in the team — each of those would otherwise surface days later at the far end as a tag failure indistinguishable from corruption. It does not verify the wrap or the signature, and the grant service says so: that would be a convenience and never the boundary, and would put an asymmetric implementation on a machine that is supposed to hold no keys. Two bugs the tests found. TeamsViewModel's busy gate blocked its own reload, so a team created a moment earlier was missing from the list it had just been added to. And syncing every vault turned a failure from an exception into a report, which made a background pass announce an unreachable vault once a minute — the exact behaviour AnAutomaticPassThatFails_LeavesTheStatusAlone exists to prevent. The fact is recorded and the message swallowed, as it was before; pressing Sync still names the vault and the reason. Also fixes a build break this branch started with: QuickConnectTests was never updated when M2 added ISftpSessionFactory to the shell's constructor, so nothing built at all.
286 lines
10 KiB
C#
286 lines
10 KiB
C#
using DodoSSH.Client.Api;
|
|
using DodoSSH.Client.Auth;
|
|
using DodoSSH.Client.Session;
|
|
using DodoSSH.Client.Sync;
|
|
using DodoSSH.Contracts;
|
|
|
|
namespace DodoSSH.Client.App.Tests;
|
|
|
|
/// <summary>
|
|
/// A signed-in server, without the signing in.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Stands in for a <c>ServerConnection</c> so the shell's state machine can be driven end to end. The
|
|
/// account half stores what it is given and reports it back, because the provisioner re-reads <c>/me</c>
|
|
/// after enrolling and a stub that echoed the request would make that check meaningless. The sync half
|
|
/// applies pushes and serves them back as a change log, which is enough for the shell — the interesting
|
|
/// conflict behaviour is covered in <c>DodoSSH.Client.Sync.Tests</c> against a server that enforces
|
|
/// version checks.
|
|
/// </remarks>
|
|
internal sealed partial class FakeVaultServer : IVaultServer, IAccountApi, ISyncApi, IKeyBindingAuthorizer
|
|
{
|
|
private readonly List<SyncChange> log = [];
|
|
|
|
/// <remarks>
|
|
/// Keyed on the entity type as well as the id, as the server's tables and the client's cache both are.
|
|
/// Ids are UUIDv7 so a collision between two types will not happen by accident — but a fake that would
|
|
/// treat a host and a key with one id as one row is a fake that could make a real bug pass.
|
|
/// </remarks>
|
|
private readonly Dictionary<(SyncEntityType Type, Guid EntityId), SyncChange> rows = [];
|
|
|
|
private KeyStatement? statement;
|
|
private byte[]? wrappedPrivateKey;
|
|
private KdfParameters? kdfParameters;
|
|
private VaultSummary? personalVault;
|
|
|
|
internal Guid UserId { get; } = Guid.Parse("0192f0c8-4444-7aaa-8bbb-dddddddddddd");
|
|
|
|
internal int EnrollmentCount { get; private set; }
|
|
|
|
internal int PushCount { get; private set; }
|
|
|
|
internal bool IsEnrolled => statement is not null;
|
|
|
|
internal int LiveRowCount => rows.Values.Count(row => row.Operation != SyncOperation.Delete);
|
|
|
|
/// <summary>Device wraps registered after enrollment, keyed on the device public key.</summary>
|
|
internal Dictionary<string, byte[]> RegisteredDevices { get; } = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>The id issued for each registered public key, so revocation has something to name.</summary>
|
|
private readonly Dictionary<string, Guid> deviceIds = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>When set, the next sign-in throws — how an unreachable server is exercised.</summary>
|
|
internal Exception? SignInFailure { get; set; }
|
|
|
|
/// <summary>
|
|
/// When set, every synchronisation throws.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A server that answers but fails, as distinct from no server at all. The two are handled quite
|
|
/// differently by a background pass: one is expected and silent, the other has to not overwrite
|
|
/// whatever the user was reading.
|
|
/// </remarks>
|
|
internal Exception? SyncFailure { get; set; }
|
|
|
|
/// <inheritdoc />
|
|
public Uri ServerUrl { get; } = new("https://dodossh.example");
|
|
|
|
/// <inheritdoc />
|
|
public IAccountApi Account => this;
|
|
|
|
/// <inheritdoc />
|
|
public ISyncApi Sync => this;
|
|
|
|
/// <inheritdoc />
|
|
public IKeyBindingAuthorizer KeyBinding => this;
|
|
|
|
/// <inheritdoc />
|
|
public SyncOptions SyncOptions => SyncOptions.Default;
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
// Nothing to release; the shell disposes this on lock and on shutdown, and both paths have to be
|
|
// safe to run more than once.
|
|
}
|
|
|
|
// ---- Identity provider ----
|
|
|
|
/// <inheritdoc />
|
|
public Task<string> AuthorizeKeyBindingAsync(string bindingNonce, CancellationToken cancellationToken) =>
|
|
Task.FromResult("stub-id-token");
|
|
|
|
// ---- Account ----
|
|
|
|
/// <inheritdoc />
|
|
public Task<MeResponse> GetMeAsync(CancellationToken cancellationToken) =>
|
|
Task.FromResult(new MeResponse(
|
|
UserId,
|
|
"https://idp.example/realms/dodossh",
|
|
"alice",
|
|
"alice@example.com",
|
|
"Alice Example",
|
|
EnrollmentRequired: !IsEnrolled,
|
|
KeyGeneration: statement?.KeyGeneration,
|
|
WrappedPrivateKey: wrappedPrivateKey,
|
|
KdfParameters: kdfParameters,
|
|
|
|
// Team vaults alongside the personal one, in the order the real /me returns them: this is
|
|
// where a vault somebody shared arrives, and a fake that only ever reported the personal one
|
|
// would make a refresh that admits a new vault untestable.
|
|
Vaults: personalVault is null ? [] : [personalVault, .. teamVaults.Values]));
|
|
|
|
/// <inheritdoc />
|
|
public Task<EnrollmentResponse> EnrollAsync(
|
|
EnrollmentRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
EnrollmentCount++;
|
|
|
|
statement = request.Statement;
|
|
wrappedPrivateKey = request.WrappedPrivateKey;
|
|
kdfParameters = request.KdfParameters;
|
|
|
|
// The enrolling account joins the directory and the key log, as it does on the real server. Both
|
|
// are what a later share reads: this client verifies its own entry as part of verifying anyone's.
|
|
RegisterSelf(request.Statement, request.StatementSignature);
|
|
|
|
personalVault = new VaultSummary(
|
|
request.PersonalVault.VaultId,
|
|
request.PersonalVault.Name,
|
|
IsPersonal: true,
|
|
TeamId: null,
|
|
KeyGeneration: 1,
|
|
Permissions: 31,
|
|
request.PersonalVault.WrappedVaultKey,
|
|
RekeyRequired: false);
|
|
|
|
return Task.FromResult(new EnrollmentResponse(
|
|
UserId,
|
|
KeyGeneration: 1,
|
|
Fingerprint: new byte[32],
|
|
request.PersonalVault.VaultId,
|
|
DeviceId: null,
|
|
KeyLogSequence: 1));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
/// <remarks>
|
|
/// Records the wrap so a test can assert it reached the server, and refuses before enrollment as the
|
|
/// real endpoint's <c>Auth.EnrolledPolicy</c> does.
|
|
/// </remarks>
|
|
public Task<RegisterDeviceResponse> RegisterDeviceAsync(
|
|
RegisterDeviceRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (!IsEnrolled)
|
|
{
|
|
throw new DodoSshApiException(
|
|
System.Net.HttpStatusCode.Forbidden,
|
|
ProblemCodes.EnrollmentRequired,
|
|
"This account has no identity key yet.");
|
|
}
|
|
|
|
var key = Convert.ToHexString(request.PublicKey);
|
|
|
|
RegisteredDevices[key] = request.WrappedPrivateKey;
|
|
|
|
// One id per public key, as the real service issues, so a revocation can name the device that was
|
|
// actually registered rather than one this fake invented on the way past.
|
|
if (!deviceIds.TryGetValue(key, out var deviceId))
|
|
{
|
|
deviceId = Guid.CreateVersion7();
|
|
deviceIds[key] = deviceId;
|
|
}
|
|
|
|
return Task.FromResult(new RegisterDeviceResponse(deviceId, DateTimeOffset.UnixEpoch));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<bool> RevokeDeviceAsync(Guid deviceId, CancellationToken cancellationToken)
|
|
{
|
|
var key = deviceIds.FirstOrDefault(entry => entry.Value == deviceId).Key;
|
|
|
|
if (key is null)
|
|
{
|
|
return Task.FromResult(false);
|
|
}
|
|
|
|
deviceIds.Remove(key);
|
|
|
|
// With its wrap, as the foreign key's cascade does on the real server.
|
|
RegisteredDevices.Remove(key);
|
|
|
|
return Task.FromResult(true);
|
|
}
|
|
|
|
// ---- Sync ----
|
|
|
|
/// <inheritdoc />
|
|
public Task<SyncPullResponse> SyncPullAsync(
|
|
Guid vaultId,
|
|
SyncPullRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (SyncFailure is { } failure)
|
|
{
|
|
return Task.FromException<SyncPullResponse>(failure);
|
|
}
|
|
|
|
var after = request.Cursor is null
|
|
? 0
|
|
: long.Parse(request.Cursor.AsSpan("app-v1:".Length), provider: null);
|
|
|
|
var page = log.Where(change => change.ChangeSequence > after).ToList();
|
|
var next = page.Count > 0 ? page[^1].ChangeSequence : after;
|
|
|
|
return Task.FromResult(new SyncPullResponse(
|
|
page,
|
|
$"app-v1:{next}",
|
|
HasMore: false,
|
|
ServerTime: DateTimeOffset.FromUnixTimeSeconds(1_750_000_000),
|
|
CurrentKeyGeneration: 1));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<SyncPushResponse> SyncPushAsync(
|
|
Guid vaultId,
|
|
SyncPushRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
PushCount++;
|
|
|
|
var results = new List<SyncPushResult>(request.Operations.Count);
|
|
|
|
foreach (var operation in request.Operations)
|
|
{
|
|
results.Add(Apply(operation));
|
|
}
|
|
|
|
return Task.FromResult(new SyncPushResponse(results, $"app-v1:{log.Count}"));
|
|
}
|
|
|
|
private SyncPushResult Apply(SyncPushOperation operation)
|
|
{
|
|
rows.TryGetValue((operation.EntityType, operation.EntityId), out var existing);
|
|
|
|
var current = existing?.Operation == SyncOperation.Delete ? null : existing;
|
|
|
|
if (operation.ExpectedVersion != current?.Version)
|
|
{
|
|
return new SyncPushResult(
|
|
operation.OperationId,
|
|
SyncOperationStatus.Conflict,
|
|
current?.Version,
|
|
current?.ChangeSequence,
|
|
current,
|
|
null);
|
|
}
|
|
|
|
var sequence = log.Count + 1;
|
|
|
|
var change = new SyncChange(
|
|
operation.EntityType,
|
|
operation.EntityId,
|
|
operation.Operation,
|
|
Version: (current?.Version ?? 0) + 1,
|
|
ChangeSequence: sequence,
|
|
Payload: operation.Operation == SyncOperation.Delete ? null : operation.Payload,
|
|
PlaintextFields: operation.Operation == SyncOperation.Delete
|
|
? null
|
|
: operation.PlaintextFields,
|
|
UpdatedAt: DateTimeOffset.FromUnixTimeSeconds(1_750_000_000 + sequence));
|
|
|
|
rows[(operation.EntityType, operation.EntityId)] = change;
|
|
log.Add(change);
|
|
|
|
return new SyncPushResult(
|
|
operation.OperationId,
|
|
SyncOperationStatus.Applied,
|
|
change.Version,
|
|
sequence,
|
|
null,
|
|
null);
|
|
}
|
|
}
|