CultNetDatabaseServer: a peer's put or delete commits only when its write authority grants it
CultNetDatabaseServerOptions.RemoteWriteAuthority is asked once per received write, after the schema resolves and before the shard does; none configured refuses every remote write; an authority that throws refuses and is logged. A refusal applies, publishes, logs and forwards nothing. The host's own CultNetDatabase.ApplyPutAsync/ApplyDeleteAsync ask no remote authority. Existing server tests whose subject is not the grant pass a test-local granting authority. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cf1e4f7a9d
commit
b4f494b7a0
8 changed files with 480 additions and 10 deletions
|
|
@ -1015,11 +1015,16 @@ namespace GameCult.Networking
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a raw document mutation after checking shard authority.
|
||||
/// Applies a raw document mutation after checking shard authority. This is the host's own door (and replication's):
|
||||
/// it asks no remote write authority. A peer's put goes through <see cref="CultNetDatabaseServer"/>, which does.
|
||||
/// </summary>
|
||||
/// <exception cref="CultNetUnownedSchemaException">No shard owns the schema and key; nothing was written.</exception>
|
||||
/// <exception cref="CultNetShardLogException">The change committed and was published, but the shard log could not record it.</exception>
|
||||
public async Task<object> ApplyPutAsync(CultNetDocumentPutRawMessage message)
|
||||
public Task<object> ApplyPutAsync(CultNetDocumentPutRawMessage message) => ApplyPutAsync(message, authorize: null);
|
||||
|
||||
// The peer door: the same apply, with the server's authorize callback asked once the schema is resolved and before the
|
||||
// shard is. The callback throws to refuse. The public overload is the host's own door and asks no remote authority.
|
||||
internal async Task<object> ApplyPutAsync(CultNetDocumentPutRawMessage message, Action<string, CultRecordKey>? authorize)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (message == null) throw new ArgumentNullException(nameof(message));
|
||||
|
|
@ -1030,6 +1035,7 @@ namespace GameCult.Networking
|
|||
|
||||
var key = new CultRecordKey(message.Document.RecordKey);
|
||||
var descriptor = _documents.ResolveDescriptorForRawDocument(message.Document);
|
||||
authorize?.Invoke(descriptor.SchemaId, key);
|
||||
var shard = ResolveShardInternal(descriptor, key);
|
||||
EnsurePrimary(shard, descriptor.SchemaId, key, message.ShardEpoch);
|
||||
var document = _documents.DeserializeRawDocument(message.Document);
|
||||
|
|
@ -1039,17 +1045,22 @@ namespace GameCult.Networking
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies a raw document delete after checking shard authority.
|
||||
/// Applies a raw document delete after checking shard authority. This is the host's own door: it asks no remote
|
||||
/// write authority. A peer's delete goes through <see cref="CultNetDatabaseServer"/>, which does.
|
||||
/// </summary>
|
||||
/// <exception cref="CultNetUnownedSchemaException">No shard owns the schema and key; nothing was written.</exception>
|
||||
/// <exception cref="CultNetShardLogException">The change committed and was published, but the shard log could not record it.</exception>
|
||||
public Task ApplyDeleteAsync(CultNetDocumentDeleteMessage message)
|
||||
public Task ApplyDeleteAsync(CultNetDocumentDeleteMessage message) => ApplyDeleteAsync(message, authorize: null);
|
||||
|
||||
// The peer door; see the put overload.
|
||||
internal Task ApplyDeleteAsync(CultNetDocumentDeleteMessage message, Action<string, CultRecordKey>? authorize)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (message == null) throw new ArgumentNullException(nameof(message));
|
||||
|
||||
var key = new CultRecordKey(message.RecordKey);
|
||||
var descriptor = _documents.ResolveDescriptorForSchemaId(message.SchemaId);
|
||||
authorize?.Invoke(descriptor.SchemaId, key);
|
||||
var shard = ResolveShardInternal(descriptor, key);
|
||||
EnsurePrimary(shard, descriptor.SchemaId, key, message.ShardEpoch);
|
||||
var previous = _cache.Get(key);
|
||||
|
|
|
|||
|
|
@ -288,11 +288,52 @@ namespace GameCult.Networking
|
|||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// The one question asked of the write authority per received write. None configured refuses; an authority that throws
|
||||
// refuses too (fail closed) and the fault is logged as an error.
|
||||
private Action<string, CultRecordKey> Authorize(
|
||||
CultNetServerPeer peer,
|
||||
CultNetRemoteWriteOperation operation,
|
||||
string? shardId,
|
||||
long? shardEpoch)
|
||||
{
|
||||
return (schemaId, key) =>
|
||||
{
|
||||
var request = new CultNetRemoteWriteRequest(peer, operation, schemaId, key, shardId, shardEpoch);
|
||||
var authority = _options.RemoteWriteAuthority;
|
||||
if (authority == null)
|
||||
{
|
||||
throw new CultNetRemoteWriteRefusedException(request, "no remote write authority is configured");
|
||||
}
|
||||
|
||||
CultNetRemoteWriteDecision decision;
|
||||
try
|
||||
{
|
||||
decision = authority.Decide(request);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_server.Logger.LogError($"CultNet remote write authority failed: {ex.Message}");
|
||||
throw new CultNetRemoteWriteRefusedException(request, "the write authority failed");
|
||||
}
|
||||
|
||||
if (decision == null || !decision.IsGranted)
|
||||
{
|
||||
throw new CultNetRemoteWriteRefusedException(request, decision?.Reason);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private async Task HandlePutAsync(CultNetDocumentPutRawMessage message, CultNetServerPeer peer)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _database.ApplyPutAsync(message).ConfigureAwait(false);
|
||||
await _database.ApplyPutAsync(message, Authorize(peer, CultNetRemoteWriteOperation.Put, message.ShardId, message.ShardEpoch))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (CultNetRemoteWriteRefusedException ex)
|
||||
{
|
||||
_server.Logger.LogWarning($"CultNet raw put refused: {ex.Message}");
|
||||
peer.SendCultNet(CultNetErrorMessage.ForRemoteWriteRefused(ex.Request, ex.Reason));
|
||||
}
|
||||
catch (CultNetShardAuthorityException ex)
|
||||
{
|
||||
|
|
@ -323,7 +364,13 @@ namespace GameCult.Networking
|
|||
{
|
||||
try
|
||||
{
|
||||
await _database.ApplyDeleteAsync(message).ConfigureAwait(false);
|
||||
await _database.ApplyDeleteAsync(message, Authorize(peer, CultNetRemoteWriteOperation.Delete, message.ShardId, message.ShardEpoch))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (CultNetRemoteWriteRefusedException ex)
|
||||
{
|
||||
_server.Logger.LogWarning($"CultNet raw delete refused: {ex.Message}");
|
||||
peer.SendCultNet(CultNetErrorMessage.ForRemoteWriteRefused(ex.Request, ex.Reason));
|
||||
}
|
||||
catch (CultNetShardAuthorityException ex)
|
||||
{
|
||||
|
|
|
|||
113
src/GameCult.Networking/CultNetRemoteWriteAuthority.cs
Normal file
113
src/GameCult.Networking/CultNetRemoteWriteAuthority.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System;
|
||||
using GameCult.Caching;
|
||||
|
||||
namespace GameCult.Networking
|
||||
{
|
||||
/// <summary>
|
||||
/// Decides whether a connected peer may put or delete a record in the database a
|
||||
/// <see cref="CultNetDatabaseServer"/> serves. Transport admission (the connection key, a completed handshake) is
|
||||
/// never a grant; this is the one owner of that decision.
|
||||
/// </summary>
|
||||
public interface ICultNetRemoteWriteAuthority
|
||||
{
|
||||
/// <summary>
|
||||
/// Decides one peer write. Called once per received put or delete, after the schema is resolved and before the
|
||||
/// shard is, so a refusal applies, publishes, logs and forwards nothing.
|
||||
/// </summary>
|
||||
CultNetRemoteWriteDecision Decide(CultNetRemoteWriteRequest request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The kind of write a peer asked for.
|
||||
/// </summary>
|
||||
public enum CultNetRemoteWriteOperation
|
||||
{
|
||||
/// <summary>Put a record.</summary>
|
||||
Put,
|
||||
/// <summary>Delete a record.</summary>
|
||||
Delete
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One peer write, as the authority sees it.
|
||||
/// </summary>
|
||||
public sealed class CultNetRemoteWriteRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a request.
|
||||
/// </summary>
|
||||
public CultNetRemoteWriteRequest(
|
||||
CultNetServerPeer peer,
|
||||
CultNetRemoteWriteOperation operation,
|
||||
string schemaId,
|
||||
CultRecordKey recordKey,
|
||||
string? shardId,
|
||||
long? shardEpoch)
|
||||
{
|
||||
Peer = peer;
|
||||
Operation = operation;
|
||||
SchemaId = schemaId;
|
||||
RecordKey = recordKey;
|
||||
ShardId = shardId;
|
||||
ShardEpoch = shardEpoch;
|
||||
}
|
||||
|
||||
/// <summary>Gets the peer connection the message arrived on (endpoint and transport only).</summary>
|
||||
public CultNetServerPeer Peer { get; }
|
||||
|
||||
/// <summary>Gets the operation.</summary>
|
||||
public CultNetRemoteWriteOperation Operation { get; }
|
||||
|
||||
/// <summary>Gets the canonical schema id the database resolved for the write, never the alias the peer sent.</summary>
|
||||
public string SchemaId { get; }
|
||||
|
||||
/// <summary>Gets the record key.</summary>
|
||||
public CultRecordKey RecordKey { get; }
|
||||
|
||||
/// <summary>Gets the shard id as the peer sent it.</summary>
|
||||
public string? ShardId { get; }
|
||||
|
||||
/// <summary>Gets the shard epoch as the peer sent it.</summary>
|
||||
public long? ShardEpoch { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An authority's answer to one <see cref="CultNetRemoteWriteRequest"/>.
|
||||
/// </summary>
|
||||
public sealed class CultNetRemoteWriteDecision
|
||||
{
|
||||
private CultNetRemoteWriteDecision(bool isGranted, string? reason)
|
||||
{
|
||||
IsGranted = isGranted;
|
||||
Reason = reason;
|
||||
}
|
||||
|
||||
/// <summary>Gets whether the write may proceed.</summary>
|
||||
public bool IsGranted { get; }
|
||||
|
||||
/// <summary>Gets the refusal reason, when there is one.</summary>
|
||||
public string? Reason { get; }
|
||||
|
||||
/// <summary>Grants the write.</summary>
|
||||
public static CultNetRemoteWriteDecision Grant() => new(true, null);
|
||||
|
||||
/// <summary>Refuses the write.</summary>
|
||||
public static CultNetRemoteWriteDecision Refuse(string? reason = null) => new(false, reason);
|
||||
}
|
||||
|
||||
// Thrown by the server's authorize callback through the database's remote apply when the authority refuses; the
|
||||
// server's handlers are its one catcher.
|
||||
internal sealed class CultNetRemoteWriteRefusedException : Exception
|
||||
{
|
||||
public CultNetRemoteWriteRefusedException(CultNetRemoteWriteRequest request, string? reason)
|
||||
: base(reason ?? "the write was not authorized")
|
||||
{
|
||||
Request = request;
|
||||
Reason = reason;
|
||||
}
|
||||
|
||||
public CultNetRemoteWriteRequest Request { get; }
|
||||
|
||||
public string? Reason { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -511,7 +511,7 @@ namespace GameCult.Networking
|
|||
[Key("routingHint")] public CultNetShardRoutingHint? RoutingHint { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the machine-readable refusal code (R-N), e.g. <c>selection_invalid</c>,
|
||||
/// <c>cursor_stale</c>, <c>cursor_invalid</c>, <c>reference_outside_target</c>, <c>unowned_schema</c>. Additive to
|
||||
/// <c>cursor_stale</c>, <c>cursor_invalid</c>, <c>reference_outside_target</c>, <c>unowned_schema</c>, <c>write_not_authorized</c>. Additive to
|
||||
/// <c>cultnet.error.v0</c>; a v0 peer that does not know this field ignores it.
|
||||
/// </summary>
|
||||
[Key("code")] public string? Code { get; set; }
|
||||
|
|
@ -543,6 +543,18 @@ namespace GameCult.Networking
|
|||
Details = new CultNetErrorDetails { Field = "schemaId", Value = ex.SchemaId }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Builds the wire error for a peer write its authority refused: code <c>write_not_authorized</c>, details
|
||||
/// <c>{ field: "recordKey", value }</c>.
|
||||
/// </summary>
|
||||
public static CultNetErrorMessage ForRemoteWriteRefused(CultNetRemoteWriteRequest request, string? reason) =>
|
||||
new CultNetErrorMessage
|
||||
{
|
||||
Error = string.IsNullOrWhiteSpace(reason) ? "write_not_authorized" : $"write_not_authorized: {reason}",
|
||||
Code = "write_not_authorized",
|
||||
Details = new CultNetErrorDetails { Field = "recordKey", Value = request.RecordKey.Value }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Builds the wire error for a cursor refusal (R-N): code <c>cursor_stale</c> with details
|
||||
/// <c>{ asOf, current }</c>, or code <c>cursor_invalid</c> with no details.
|
||||
|
|
|
|||
|
|
@ -34,6 +34,13 @@ namespace GameCult.Networking
|
|||
/// Gets or sets the forwarder used when non-primary write forwarding is enabled.
|
||||
/// </summary>
|
||||
public ICultNetShardWriteForwarder? WriteForwarder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the authority asked for every peer put and delete. None configured refuses them all: a connection
|
||||
/// key admits a peer to the transport, never to write. A write forwarded to a primary is judged here for the
|
||||
/// original peer and again at the primary for the forwarder's connection.
|
||||
/// </summary>
|
||||
public ICultNetRemoteWriteAuthority? RemoteWriteAuthority { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Reference in a new issue