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
@@ -31,5 +31,13 @@ public sealed class DataExport
/// <summary>A2 (used by A2b): per-export opt-in for remote acknowledgement of Galaxy conditions, which execute as the
/// origin's service identity (plan §5.6, Q2). Valid only with <c>AlarmAcknowledge</c>.</summary>
public bool AllowGalaxyServiceAck { get; set; }
/// <summary>
/// #1084: per-export opt-in for writes that act under the connection key's own identity (Galaxy SecuredWrite attributes,
/// written as the key's <c>/api-keys</c> Galaxy account). Without it the origin hands the driver NO caller for a write
/// admitted through this export, so a caller-aware driver refuses an identity-bearing write exactly as it would an
/// anonymous one — even with the driver's own <c>MxAccess.SecuredWritesOverDataConnections</c> on. Both are needed.
/// Valid only with <c>Write</c>. Default false (fail closed).
/// </summary>
public bool AllowSecuredWrites { get; set; }
public byte[] RowVersion { get; set; } = [];
}
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ZB.MOM.WW.OtOpcUa.Configuration.Migrations
{
/// <inheritdoc />
public partial class AddDataExportAllowSecuredWrites : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// #1084: the export-level opt-in for secured (caller-identity) writes over a data connection. Non-null with a
// false default, so every existing export keeps refusing them and no row needs a backfill. No check constraint:
// "only with Write" is a validator rule (DataExportSecuredWriteInvalid), like AllowGalaxyServiceAck's.
migrationBuilder.AddColumn<bool>(
name: "AllowSecuredWrites",
table: "DataExport",
type: "bit",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AllowSecuredWrites",
table: "DataExport");
}
}
}
@@ -500,6 +500,9 @@ namespace ZB.MOM.WW.OtOpcUa.Configuration.Migrations
b.Property<bool>("AllowGalaxyServiceAck")
.HasColumnType("bit");
b.Property<bool>("AllowSecuredWrites")
.HasColumnType("bit");
b.Property<int>("AllowedOperations")
.HasColumnType("int");
@@ -349,6 +349,10 @@ public static class DataConnectionValidator
// must stay false; a deployed row from a newer central that carries both is tolerated (the flag is inert here).
if (export.AllowGalaxyServiceAck && !export.AllowedOperations.HasFlag(DataOperations.AlarmAcknowledge))
Add("DataExportGalaxyAckInvalid", id, "Galaxy service acknowledgement can be allowed only on an export that grants AlarmAcknowledge.");
// #1084: the secured-write (caller-identity) opt-in qualifies the Write grant and means nothing without it. Checked
// at authoring, at the deploy gate and on a deployed row alike (a deployed row breaking it fails closed).
if (export.AllowSecuredWrites && !export.AllowedOperations.HasFlag(DataOperations.Write))
Add("DataExportSecuredWriteInvalid", id, "Secured writes can be allowed only on an export that grants Write.");
// #942: an alarm-bearing tag is never a value (#917: an unsupported value node, hidden from Browse; #896: no UNS
// variable), and its condition placements sit under the tag's folder / the referencing equipment, never under the
// tag. So an export rooted at the tag (RawTag) grants nothing at all, and one rooted at a reference to it
@@ -29,10 +29,16 @@ public sealed record WriteItem(string CorrelationId, string OperationId, DataVie
/// recheck the original key's current principal and credential version against authority and its
/// original expiry even when the epoch is unchanged. ApiKeyId and CredentialVersion are non-secret
/// identifiers, never the bearer credential.
/// <para>
/// <paramref name="AllowsSecuredWrite"/> (#1084): whether an export covering the node opts in to secured (caller-identity)
/// writes (<c>DataExport.AllowSecuredWrites</c>). Only then does the runtime hand the driver the connection's caller; false
/// (the default — fail closed) dispatches the write with no caller, so a caller-aware driver refuses an identity-bearing
/// write exactly as it would an anonymous one.
/// </para>
/// </summary>
public sealed record DataWriteAdmission(string PrincipalId, DataView View, long AuthorityEpoch, long CatalogRevision,
string ServingIncarnation, string DriverIncarnation, string OperationId, long MonotonicExpiry,
string ApiKeyId, DateTimeOffset? CredentialExpiresUtc, string CredentialVersion)
string ApiKeyId, DateTimeOffset? CredentialExpiresUtc, string CredentialVersion, bool AllowsSecuredWrite = false)
{
public void Validate()
{
@@ -39,8 +39,9 @@ public enum WriteCallerOrigin
/// attributes, written under the calling API key's Galaxy account). The runtime calls this overload instead
/// of <see cref="IWritable.WriteAsync" /> whenever the write carries a <see cref="WriteCaller" />. Both data planes
/// supply one: an OPC UA write from its session, a gRPC data-connection write from the connection's authenticated
/// key (#1074) — except through <c>IWriteOutcomeCapability</c>, which takes precedence on the gRPC path and carries
/// no caller. It must keep
/// key (#1074), and only when the origin export opts in (<c>DataExport.AllowSecuredWrites</c>, #1084). On the gRPC path
/// a driver that also implements <c>IWriteOutcomeCapability</c> is called through that instead — with a caller, through
/// <c>ICallerAwareWriteOutcomeCapability</c> (#1091). It must keep
/// every other behaviour of <see cref="IWritable.WriteAsync" />; a caller the driver cannot map is refused
/// per write (e.g. <c>BadUserAccessDenied</c>), never silently written under another identity.
/// </summary>