Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Configuration.Tests/DataConnections/GrpcDataSecuredWriteExportConfigurationTests.cs
T
Joseph Doherty 8e2a793000 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.
2026-12-04 14:58:16 -05:00

61 lines
3.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests.DataConnections;
/// <summary>
/// #1084: <see cref="DataExport.AllowSecuredWrites"/> — the export-level opt-in a secured (caller-identity) write over a data
/// connection needs on top of the driver's own <c>MxAccess.SecuredWritesOverDataConnections</c>. Default off, valid only with
/// the Write grant it qualifies, added by a default-false column.
/// </summary>
[Trait("Category", "Unit"), Trait("Feature", "GrpcData")]
public sealed class GrpcDataSecuredWriteExportConfigurationTests
{
[Fact]
public void A_new_export_does_not_allow_secured_writes()
{
new DataExport().AllowSecuredWrites.ShouldBeFalse("fail closed: the opt-in defaults off");
}
[Fact]
public void AllowSecuredWrites_is_valid_only_with_Write_at_authoring_and_on_a_deployed_row()
{
var draft = GrpcDataReservedOperationAuthoringTests.Snapshot(DataOperations.Browse | DataOperations.Read);
DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
draft.DataExports[0].AllowSecuredWrites = true;
DataConnectionValidator.Validate(draft).ShouldContain(x => x.Code == "DataExportSecuredWriteInvalid" && x.Context == "export");
DataConnectionValidator.ValidateDeployed(draft).ShouldContain(x => x.Code == "DataExportSecuredWriteInvalid" && x.Context == "export");
// With the Write grant the flag authors and deploys cleanly.
draft.DataExports[0].AllowedOperations = DataOperations.Browse | DataOperations.Read | DataOperations.Write;
DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
DataConnectionValidator.ValidateDeployed(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
// A disabled export is not validated (it grants nothing either way).
draft.DataExports[0].AllowedOperations = DataOperations.Read;
draft.DataExports[0].Enabled = false;
DataConnectionValidator.Validate(draft).ShouldNotContain(x => x.Code == "DataExportSecuredWriteInvalid");
}
[Fact]
public void Migration_adds_a_default_false_column_reversibly_and_no_constraint()
{
using var db = new OtOpcUaConfigDbContext(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
.UseSqlServer("Server=127.0.0.1,1;Database=design;Integrated Security=true;TrustServerCertificate=true").Options);
var migrations = db.Database.GetMigrations().ToArray();
var index = Array.FindIndex(migrations, x => x.EndsWith("_AddDataExportAllowSecuredWrites", StringComparison.Ordinal));
index.ShouldBeGreaterThan(0);
var migrator = db.GetService<IMigrator>();
var up = migrator.GenerateScript(migrations[index - 1], migrations[index]);
up.ShouldContain("ALTER TABLE [DataExport] ADD [AllowSecuredWrites] bit NOT NULL DEFAULT CAST(0 AS bit)");
up.ShouldNotContain("CHECK", Case.Sensitive, "no constraint, so the upgrade preflight needs no MigrationPreChecks twin");
var down = migrator.GenerateScript(migrations[index], migrations[index - 1]);
down.ShouldContain("DROP COLUMN [AllowSecuredWrites]");
db.Database.HasPendingModelChanges().ShouldBeFalse();
}
}