using Shouldly; using Akka.Actor; using Xunit; using System.Text; using Microsoft.Extensions.Logging.Abstractions; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; using ZB.MOM.WW.OtOpcUa.Commons.Types; using ZB.MOM.WW.OtOpcUa.Core.Authorization; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; using ZB.MOM.WW.OtOpcUa.Configuration.Enums; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; using ZB.MOM.WW.OtOpcUa.OpcUaServer; using ZB.MOM.WW.OtOpcUa.Runtime.Drivers; using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa; using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; [Trait("Feature", "GrpcData")] [Trait("Category", "Unit")] [Trait("Phase", "P03")] [Collection("opcua-publish-meters")] public sealed class GrpcDataCatalogActivationTests : RuntimeActorTestBase { [Fact] public void Real_activation_token_rejects_old_mapping_even_after_same_authority_noop_rebuild() { var authority = new DataPlaneAuthorityState(); var installed = authority.Apply("cluster", "first", [], [new NodeAcl { NodeAclId = "grant", ClusterId = "cluster", Principal = "peer", ScopeKind = NodeAclScopeKind.Cluster, PermissionFlags = NodePermissions.Read }], new Dictionary>()); var catalog = new CatalogActivation(); var first = catalog.Begin("first"); catalog.AuthorityInstalled(first, installed.GenerationId); catalog.Complete(first, true); var clock = new ManualTimeProvider(); var serving = new ServingIdentity("remote", "incarnation", "Primary", 7, 9); var view = new DataView(new DataAddress("remote-cluster", "https://zb.com/otopcua/raw", "string", "remote/source"), DataRealm.Raw, "mapping", 1, "Plant/Mount/Source"); var subject = new MappedNamespaceSubject(view, new NodeScope { ClusterId = "cluster", Kind = NodeHierarchyKind.Raw, Segments = ["Plant", "Mount", "Source"] }, ["parent", "mapping"], serving, DataOperations.Read); var resolver = new MappedSubjectResolver([subject], 7, catalog.CaptureAuthority()); var lease = new MetadataAuthorityLease(serving, new MetadataLease(7, 9, TimeSpan.FromSeconds(10)), clock.GetTimestamp(), TimeSpan.FromSeconds(10), TimeSpan.Zero, clock); var policy = new DataOperationPolicy(authority, new TriePermissionEvaluator(authority.Tries, clock), clock); var caller = new DataCallerIdentity("peer", null, null, null); policy.AuthorizeMapped(caller, OpcUaOperation.Read, view, serving, subject, resolver, lease).ShouldBeTrue(); var next = catalog.Begin("same-authority"); policy.AuthorizeMapped(caller, OpcUaOperation.Read, view, serving, subject, resolver, lease).ShouldBeFalse(); catalog.AuthorityInstalled(next, installed.GenerationId); catalog.Complete(next, true); policy.AuthorizeMapped(caller, OpcUaOperation.Read, view, serving, subject, resolver, lease).ShouldBeFalse(); } [Fact] public void Valid_revocation_is_installed_even_when_topology_planning_throws() { var catalog = new CatalogActivation(); var authority = new DataPlaneAuthorityState(); var sink = NullOpcUaAddressSpaceSink.Instance; var revoke = false; var hooks = new OpcUaPublishActor.TestHooks { DataCatalog = catalog, ParseAuthority = (_, _) => new DataPlaneAuthorityProjection("cluster", [new ApiKey { ApiKeyId = "key", Name = "key", Principal = "peer", SecretHash = "hash", Enabled = !revoke }], [], new Dictionary>()), BeforeComposition = () => { if (revoke) throw new InvalidDataException("topology invalid"); } }; var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(sink, new AddressSpaceApplier(sink, NullLogger.Instance), hooks, dataPlaneAuthority: authority)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId(), DeploymentId.NewId(), Encoding.UTF8.GetBytes("{}"), ReportOutcome: true)); ExpectMsg(TimeSpan.FromSeconds(15), cancellationToken: TestContext.Current.CancellationToken); authority.Current.FindUsableKey("key", DateTime.UtcNow).ShouldNotBeNull(); var installed = authority.Current.GenerationId; revoke = true; actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId(), DeploymentId.NewId(), Encoding.UTF8.GetBytes("{\"Revoked\":true}"), ReportOutcome: true)); ExpectMsg(TimeSpan.FromSeconds(15), cancellationToken: TestContext.Current.CancellationToken) .Degraded.ShouldBeTrue(); authority.Current.GenerationId.ShouldBeGreaterThan(installed); authority.Current.FindUsableKey("key", DateTime.UtcNow).ShouldBeNull(); catalog.Current.IsReady.ShouldBeFalse(); } [Fact] public void Real_apply_publishes_authority_only_revision_and_closes_on_next_authority_parse_failure() { var catalog = new CatalogActivation(); var authority = new DataPlaneAuthorityState(); var sink = NullOpcUaAddressSpaceSink.Instance; var fail = false; var hooks = new OpcUaPublishActor.TestHooks { DataCatalog = catalog, ParseAuthority = (_, _) => fail ? throw new InvalidDataException("authority invalid") : new DataPlaneAuthorityProjection("cluster", [], [], new Dictionary>()) }; var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(sink, new AddressSpaceApplier(sink, NullLogger.Instance), hooks, dataPlaneAuthority: authority)); actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId(), DeploymentId.NewId(), Encoding.UTF8.GetBytes("{}"), ReportOutcome: true)); ExpectMsg(TimeSpan.FromSeconds(15), cancellationToken: TestContext.Current.CancellationToken); catalog.Current.IsReady.ShouldBeTrue(); var installed = authority.Current.GenerationId; fail = true; actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(CorrelationId.NewId(), DeploymentId.NewId(), Encoding.UTF8.GetBytes("{\"ChangedAuthority\":true}"), ReportOutcome: true)); ExpectMsg(TimeSpan.FromSeconds(15), cancellationToken: TestContext.Current.CancellationToken); catalog.Current.IsReady.ShouldBeFalse(); authority.Current.GenerationId.ShouldBe(installed); } [Fact] public void Staging_revokes_old_generation_before_topology_and_failure_cannot_restore_it() { var catalog = new CatalogActivation(); var first = catalog.Begin("first"); catalog.AuthorityInstalled(first, 1); catalog.Complete(first, topologySucceeded: true).ShouldBeTrue(); catalog.Current.IsReady.ShouldBeTrue(); var next = catalog.Begin("revocation"); catalog.Current.IsReady.ShouldBeFalse(); catalog.AuthorityInstalled(next, 2); catalog.Complete(next, topologySucceeded: false).ShouldBeFalse(); catalog.Current.AuthorityEpoch.ShouldBe(2); catalog.Current.IsReady.ShouldBeFalse(); catalog.Complete(first, topologySucceeded: true).ShouldBeFalse(); catalog.Current.Revision.ShouldBe("revocation"); } [Fact] public void Empty_topology_authority_only_revision_requires_matching_installed_authority() { var catalog = new CatalogActivation(); var next = catalog.Begin("acl-only"); catalog.Complete(next, topologySucceeded: true).ShouldBeFalse(); catalog.AuthorityInstalled(next, 4); catalog.Complete(next, topologySucceeded: true).ShouldBeTrue(); catalog.Current.CatalogEpoch.ShouldBeGreaterThan(0); catalog.Current.AuthorityEpoch.ShouldBe(4); catalog.Current.IsReady.ShouldBeTrue(); } }