feat(grpc-data): #1084 secured writes over a data connection need an export-level opt-in; #1091 Galaxy reports gRPC write outcomes definitively

#1084 - DataExport.AllowSecuredWrites (default false; migration AddDataExportAllowSecuredWrites, a bit NOT NULL DEFAULT 0
column, no constraint => no MigrationPreChecks twin; the preflight's legacy-schema tolerance covers the missing column).
- Valid only with Write: validator code DataExportSecuredWriteInvalid at authoring, the deploy gate and on a deployed
  row (an artifact row breaking it fails closed), modelled on AllowGalaxyServiceAck.
- LocalExportCatalog keeps it per rule (counts only with Write) and answers AllowsSecuredWrite(principal, node): an
  export of the principal, in the node's realm, granting Write, opted in, whose root contains the node.
- DataWriteExecutor stamps DataWriteAdmission.AllowsSecuredWrite (new, default false) from the catalog generation that
  admitted the write (re-checked by the dispatch gate's commit), never from the request.
- DriverInstanceActor.RichCaller returns null without it, so the driver gets NO caller: the driver option
  MxAccess.SecuredWritesOverDataConnections alone no longer lets a forwarding consumer cluster's link key write
  SecuredWrite attributes. Fail closed by default.
- AdminUI export editor: "Allow secured writes under this peer's key identity" checkbox (enabled only with Write).

#1091 (a) - GalaxyDriver implements IWriteOutcomeCapability + ICallerAwareWriteOutcomeCapability. GalaxyWriteOutcomes
maps BadUserAccessDenied and the OnWriteComplete verdicts BadNotWritable/BadTypeMismatch/BadOutOfRange/
BadConfigurationError to DriverConfirmedFailure, Good to AcceptedUnconfirmed (a provisional Good is indistinguishable),
everything else to OutcomeUnknown. A refused secured write is now a definite BadUserAccessDenied, not OutcomeUnknown.
- RunRichWriteAsync precedence: caller-aware outcome overload (caller present) -> outcome overload -> ICallerAwareWritable
  (caller present) -> IWritable. Both earlier behaviours kept; the OPC UA path is unchanged.

Tests (each failed before its fix where applicable):
- End to end through the runtime path (artifact -> catalog -> DataWriteRuntime executor -> ActorDataWriteRoute ->
  DriverInstanceActor behind the real dispatch gate -> real GalaxyDriver): export opt-in x driver option matrix; only
  both commit as the key's identity, either alone refuses BadUserAccessDenied definitively with no identity lookup.
  Ignoring the flag in RichCaller fails 3 tests.
- Catalog scope (root containment, Write, principal), artifact round-trip + fail-closed row, validator, migration SQL +
  no pending model changes, AdminUI checkbox enable/save (bUnit), actor precedence, Galaxy outcome mapping.
- ScriptForeignKeysAndExternalIdUniquenessMigrationTests now migrate to THEIR migration, not the latest.

Docs: DataConnections.md (two-gate table), Galaxy.md, security.md, rules/drivers.md.
This commit is contained in:
Joseph Doherty
2026-12-04 14:58:16 -05:00
parent 2f22003061
commit 8e2a793000
23 changed files with 2704 additions and 53 deletions
@@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.MxGateway.Client;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Browse;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Config;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Health;
@@ -38,7 +39,7 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy;
/// </para>
/// </remarks>
public sealed class GalaxyDriver
: IDriver, ITagDiscovery, IReadable, IWritable, ICallerAwareWritable, ISubscribable, IRediscoverable, IHostConnectivityProbe, IAlarmSource, IAlarmInitialStateSignal, IAlarmAcknowledgePreflight, IAlarmAcknowledgeConfirmation, ISkippedTagReporter, IDisposable, IAsyncDisposable
: IDriver, ITagDiscovery, IReadable, IWritable, ICallerAwareWritable, ICallerAwareWriteOutcomeCapability, ISubscribable, IRediscoverable, IHostConnectivityProbe, IAlarmSource, IAlarmInitialStateSignal, IAlarmAcknowledgePreflight, IAlarmAcknowledgeConfirmation, ISkippedTagReporter, IDisposable, IAsyncDisposable
{
/// <summary>Authored tags this driver could not dial — a TagConfig with no usable
/// <c>attributeRef</c> has no MXAccess address to bind.</summary>
@@ -2438,6 +2439,42 @@ public sealed class GalaxyDriver
dialledWrites, attributeRef => ResolveSecurity(ToRawPath(attributeRef)), securedCredential, cancellationToken);
}
// ===== IWriteOutcomeCapability (#1091) — the gRPC data-connection write path =====
/// <summary>
/// #1091 — <see cref="WriteAsync(IReadOnlyList{WriteRequest}, CancellationToken)"/> reported as explicit outcomes for
/// the gRPC data-connection write path (<see cref="GalaxyWriteOutcomes.From"/>): a refusal is a definite
/// <see cref="DataWriteState.DriverConfirmedFailure"/> carrying its status (a refused secured write ⇒
/// <c>BadUserAccessDenied</c>), where the legacy path reported every non-Good result as <c>OutcomeUnknown</c>.
/// </summary>
/// <param name="writes">Pairs of full reference (RawPath) + value.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>One outcome per request, in order.</returns>
public Task<IReadOnlyList<DataWriteOutcome>> WriteWithOutcomesAsync(
IReadOnlyList<WriteRequest> writes, CancellationToken cancellationToken)
=> WithOutcomesAsync(writes, caller: null, cancellationToken);
/// <summary>
/// #1091 — <see cref="WriteAsync(IReadOnlyList{WriteRequest}, WriteCaller, CancellationToken)"/> (the caller's Galaxy
/// identity for SecuredWrite attributes, under the same origin rules) reported as explicit outcomes.
/// </summary>
/// <param name="writes">Pairs of full reference (RawPath) + value.</param>
/// <param name="caller">Who issued the write.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>One outcome per request, in order.</returns>
public Task<IReadOnlyList<DataWriteOutcome>> WriteWithOutcomesAsync(
IReadOnlyList<WriteRequest> writes, WriteCaller caller, CancellationToken cancellationToken)
=> WithOutcomesAsync(writes, caller ?? throw new ArgumentNullException(nameof(caller)), cancellationToken);
private async Task<IReadOnlyList<DataWriteOutcome>> WithOutcomesAsync(
IReadOnlyList<WriteRequest> writes, WriteCaller? caller, CancellationToken cancellationToken)
{
var results = await WriteCoreAsync(writes, caller, cancellationToken).ConfigureAwait(false);
var outcomes = new DataWriteOutcome[results.Count];
for (var i = 0; i < results.Count; i++) outcomes[i] = GalaxyWriteOutcomes.From(results[i]);
return outcomes;
}
/// <summary>
/// #1074 — which data planes a SecuredWrite may run under the caller's identity on. An OPC UA session always
/// (the #1017 opt-in alone); a gRPC data connection only with <c>MxAccess.SecuredWritesOverDataConnections</c>
@@ -0,0 +1,47 @@
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
/// <summary>
/// #1091 — how a Galaxy <see cref="WriteResult"/> reads as a gRPC data-connection <see cref="DataWriteOutcome"/>. Only a
/// status that can arise solely from a definite verdict is reported as one; everything ambiguous stays
/// <see cref="DataWriteState.OutcomeUnknown"/>, because a caller treats a definite failure as "the value did not change".
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item><b>Good</b> ⇒ <see cref="DataWriteState.AcceptedUnconfirmed"/> (as the legacy path reported it): the writer
/// reports a provisional Good off an EMPTY gateway statuses array (the completion wait timed out), which the result
/// cannot tell apart from a confirmed one.</item>
/// <item><b>Definite refusals</b> ⇒ <see cref="DataWriteState.DriverConfirmedFailure"/> with the status:
/// <c>BadUserAccessDenied</c> — a local secured/verified-write refusal (no identity, origin not allowed, identity
/// unresolvable), a failed <c>AuthenticateUser</c> (no <c>WriteSecured</c> sent), the gateway refusing the caller
/// (<c>PermissionDenied</c>/<c>Unauthenticated</c>, nothing executed) or MXAccess's own access verdict (1008/1012/
/// 1013/1016/1017); and the MXAccess <c>OnWriteComplete</c> verdicts <c>BadNotWritable</c>, <c>BadTypeMismatch</c>,
/// <c>BadOutOfRange</c> and <c>BadConfigurationError</c>, which no transport/exception path of the writer produces.</item>
/// <item><b>Everything else</b> ⇒ <see cref="DataWriteState.OutcomeUnknown"/> with the status:
/// <c>BadCommunicationError</c> (protocol failure, exception, failed advise), <c>BadTimeout</c>, Uncertain
/// (Pending/Warning), <c>BadInternalError</c>, generic Bad and any other code.</item>
/// </list>
/// </remarks>
internal static class GalaxyWriteOutcomes
{
/// <summary>Maps one write result.</summary>
/// <param name="result">The writer's result.</param>
/// <returns>The data-connection outcome.</returns>
public static DataWriteOutcome From(WriteResult result)
{
ArgumentNullException.ThrowIfNull(result);
var status = result.StatusCode;
if (status == StatusCodeMap.Good) return new(DataWriteState.AcceptedUnconfirmed, status, null);
return status switch
{
StatusCodeMap.BadUserAccessDenied => new(DataWriteState.DriverConfirmedFailure, status, "Write refused: access denied."),
StatusCodeMap.BadNotWritable => new(DataWriteState.DriverConfirmedFailure, status, "Write refused: not writable."),
StatusCodeMap.BadTypeMismatch => new(DataWriteState.DriverConfirmedFailure, status, "Write refused: type mismatch."),
StatusCodeMap.BadOutOfRange => new(DataWriteState.DriverConfirmedFailure, status, "Write refused: out of range."),
StatusCodeMap.BadConfigurationError => new(DataWriteState.DriverConfirmedFailure, status, "Write refused: configuration error."),
_ => new(DataWriteState.OutcomeUnknown, status, "Write outcome unknown."),
};
}
}