feat: deploy scoped grpc data connection configuration
This commit is contained in:
+228
@@ -0,0 +1,228 @@
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.DataConnections;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
[Trait("Feature", "GrpcData")]
|
||||
[Trait("Phase", "P02")]
|
||||
public sealed class GrpcDataArtifactProjectionTests
|
||||
{
|
||||
private const string NodeA = "node-a:4840";
|
||||
private const string NodeB = "node-b:4840";
|
||||
private static readonly DateTime Timestamp = new(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
[Fact]
|
||||
public void New_collections_and_nested_sets_have_stable_bytes_independent_of_enumeration()
|
||||
{
|
||||
var first = Snapshot();
|
||||
var second = Snapshot(reverse: true);
|
||||
first.DataConnections.Count.ShouldBe(2);
|
||||
first.DataConnections[0].ConsumerNodeIds.Count.ShouldBe(2);
|
||||
|
||||
var a = ConfigComposer.Compose(first);
|
||||
var b = ConfigComposer.Compose(second);
|
||||
|
||||
a.Blob.ShouldBe(b.Blob);
|
||||
a.RevisionHash.ShouldBe(b.RevisionHash);
|
||||
using var json = JsonDocument.Parse(Inflate(a.Blob));
|
||||
json.RootElement.GetProperty("DataConnections").EnumerateArray()
|
||||
.Select(x => x.GetProperty("DataConnectionId").GetString()).ShouldBe(["connection-a", "connection-z"]);
|
||||
json.RootElement.GetProperty("DataExports").EnumerateArray()
|
||||
.Select(x => x.GetProperty("DataExportId").GetString()).ShouldBe(["export-a", "export-z"]);
|
||||
json.RootElement.GetProperty("RemoteNamespaceMappings").EnumerateArray()
|
||||
.Select(x => x.GetProperty("RemoteNamespaceMappingId").GetString()).ShouldBe(["mapping-a", "mapping-z"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ordered_endpoint_preference_is_preserved_and_changes_the_revision()
|
||||
{
|
||||
var first = Snapshot();
|
||||
var second = Snapshot();
|
||||
second.DataConnections[0].Endpoints.Reverse();
|
||||
|
||||
var a = ConfigComposer.Compose(first);
|
||||
var b = ConfigComposer.Compose(second);
|
||||
|
||||
b.RevisionHash.ShouldNotBe(a.RevisionHash);
|
||||
DeploymentArtifact.ParseDataConnections(a.Blob, NodeA).Single(x => x.DataConnectionId == "connection-a")
|
||||
.Endpoints.ShouldBe(["https://node-b:7443", "https://node-b-backup:7443"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("connection")]
|
||||
[InlineData("export")]
|
||||
[InlineData("mapping")]
|
||||
[InlineData("connection-revision")]
|
||||
[InlineData("export-revision")]
|
||||
[InlineData("mapping-revision")]
|
||||
[InlineData("grant")]
|
||||
[InlineData("mapping-acl")]
|
||||
[InlineData("maintenance")]
|
||||
public void Disable_revision_authority_and_maintenance_edits_change_the_deployed_hash(string edit)
|
||||
{
|
||||
var before = Snapshot();
|
||||
var after = Snapshot();
|
||||
switch (edit)
|
||||
{
|
||||
case "connection": after.DataConnections[0].Enabled = false; break;
|
||||
case "export": after.DataExports[0].Enabled = false; break;
|
||||
case "mapping": after.RemoteNamespaceMappings[0].Enabled = false; break;
|
||||
case "connection-revision": after.DataConnections[0].Revision++; break;
|
||||
case "export-revision": after.DataExports[0].Revision++; break;
|
||||
case "mapping-revision": after.RemoteNamespaceMappings[0].Revision++; break;
|
||||
case "grant": after.DataExports[0].AllowedOperations = DataOperations.Browse; break;
|
||||
case "mapping-acl": after.NodeAcls[0].PermissionFlags = NodePermissions.Browse; break;
|
||||
case "maintenance": after.ClusterNodes[1].MaintenanceMode = true; break;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(edit));
|
||||
}
|
||||
|
||||
ConfigComposer.Compose(after).RevisionHash.ShouldNotBe(ConfigComposer.Compose(before).RevisionHash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Connection_credentials_serialize_as_references_while_existing_api_key_hashes_survive()
|
||||
{
|
||||
var artifact = ConfigComposer.Compose(Snapshot());
|
||||
using var json = JsonDocument.Parse(Inflate(artifact.Blob));
|
||||
var connection = json.RootElement.GetProperty("DataConnections")[0];
|
||||
|
||||
connection.GetProperty("SecretReference").GetString().ShouldBe("secret:data/upstream");
|
||||
connection.GetProperty("SecretScopeClusterId").GetString().ShouldBe("cluster-local");
|
||||
connection.EnumerateObject().Select(p => p.Name).ShouldNotContain("Secret");
|
||||
connection.EnumerateObject().Select(p => p.Name).ShouldNotContain("ApiKey");
|
||||
connection.EnumerateObject().Select(p => p.Name).ShouldNotContain("PresentedSecret");
|
||||
json.RootElement.GetProperty("ApiKeys")[0].GetProperty("SecretHash").GetString()
|
||||
.ShouldBe("existing-peppered-hash");
|
||||
DeploymentArtifact.ParseDataConnections(artifact.Blob, NodeA).ShouldAllBe(c => c.SecretReference == "secret:data/upstream");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Composed_A_only_same_cluster_connection_and_mount_are_absent_on_B()
|
||||
{
|
||||
var snapshot = Snapshot();
|
||||
foreach (var connection in snapshot.DataConnections)
|
||||
{
|
||||
connection.ConsumerNodeIds = [NodeA];
|
||||
connection.TargetMode = DataTargetMode.Node;
|
||||
connection.ExpectedClusterId = "cluster-local";
|
||||
connection.ExpectedNodeId = NodeB;
|
||||
connection.EligibleTargetNodeIds = [];
|
||||
connection.Endpoints = ["https://node-b:7443"];
|
||||
}
|
||||
foreach (var mapping in snapshot.RemoteNamespaceMappings) mapping.ConsumerNodeIds = [NodeA];
|
||||
var blob = ConfigComposer.Compose(snapshot).Blob;
|
||||
|
||||
DeploymentArtifact.ParseDataConnections(blob, NodeA).Count.ShouldBe(2);
|
||||
DeploymentArtifact.ParseRemoteNamespaceMappings(blob, NodeA).Count.ShouldBe(2);
|
||||
DeploymentArtifact.ParseDataConnections(blob, NodeB).ShouldBeEmpty();
|
||||
DeploymentArtifact.ParseRemoteNamespaceMappings(blob, NodeB).ShouldBeEmpty();
|
||||
DeploymentArtifact.ParseDataConnections(blob, "unlisted:4840").ShouldBeEmpty();
|
||||
DeploymentArtifact.ParseRemoteNamespaceMappings(blob, "unlisted:4840").ShouldBeEmpty();
|
||||
DeploymentArtifact.ParseDataExports(blob, "unlisted:4840").ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Database_snapshot_carries_all_three_new_collections_into_the_artifact()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString("N")).Options;
|
||||
await using var db = new OtOpcUaConfigDbContext(options);
|
||||
var snapshot = Snapshot();
|
||||
db.ServerClusters.AddRange(snapshot.ServerClusters);
|
||||
db.ClusterNodes.AddRange(snapshot.ClusterNodes);
|
||||
db.RawFolders.AddRange(snapshot.RawFolders);
|
||||
db.DataConnections.AddRange(snapshot.DataConnections);
|
||||
db.DataExports.AddRange(snapshot.DataExports);
|
||||
db.RemoteNamespaceMappings.AddRange(snapshot.RemoteNamespaceMappings);
|
||||
await db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
|
||||
var artifact = await ConfigComposer.SnapshotAndFlattenAsync(db, TestContext.Current.CancellationToken);
|
||||
|
||||
DeploymentArtifact.ParseDataConnections(artifact.Blob, NodeA).Select(x => x.DataConnectionId).ShouldBe(["connection-a", "connection-z"]);
|
||||
DeploymentArtifact.ParseDataExports(artifact.Blob, NodeA).Select(x => x.DataExportId).ShouldBe(["export-a", "export-z"]);
|
||||
DeploymentArtifact.ParseRemoteNamespaceMappings(artifact.Blob, NodeA).Select(x => x.RemoteNamespaceMappingId).ShouldBe(["mapping-a", "mapping-z"]);
|
||||
}
|
||||
|
||||
private static DraftSnapshot Snapshot(bool reverse = false)
|
||||
{
|
||||
var connections = new[] { Connection("connection-a"), Connection("connection-z") };
|
||||
var mappings = new[] { Mapping("mapping-a", "connection-a"), Mapping("mapping-z", "connection-z") };
|
||||
var exports = new[] { Export("export-a"), Export("export-z") };
|
||||
if (reverse)
|
||||
{
|
||||
Array.Reverse(connections);
|
||||
Array.Reverse(mappings);
|
||||
Array.Reverse(exports);
|
||||
foreach (var connection in connections)
|
||||
{
|
||||
connection.ConsumerNodeIds.Reverse();
|
||||
connection.EligibleTargetNodeIds.Reverse();
|
||||
}
|
||||
foreach (var mapping in mappings) mapping.ConsumerNodeIds.Reverse();
|
||||
}
|
||||
return new DraftSnapshot
|
||||
{
|
||||
GenerationId = 0, ClusterId = "cluster-local",
|
||||
ServerClusters = [new ServerCluster { ClusterId = "cluster-local", Name = "local", Enterprise = "ent", Site = "site",
|
||||
RedundancyMode = RedundancyMode.None, CreatedBy = "test", CreatedAt = Timestamp }],
|
||||
ClusterNodes = [Node(NodeA), Node(NodeB)],
|
||||
RawFolders = [new RawFolder { RawFolderId = "local-root", ClusterId = "cluster-local", Name = "Local" }],
|
||||
DataConnections = connections, DataExports = exports, RemoteNamespaceMappings = mappings,
|
||||
NodeAcls = [new NodeAcl { NodeAclId = "mapping-grant", ClusterId = "cluster-local", Principal = "viewer",
|
||||
ScopeKind = NodeAclScopeKind.RemoteNamespaceMapping, ScopeId = "mapping-a", PermissionFlags = NodePermissions.Read }],
|
||||
ApiKeys = [new ApiKey { ApiKeyRowId = Guid.Empty, ApiKeyId = "key", Name = "key", Principal = "peer",
|
||||
SecretHash = "existing-peppered-hash", CreatedUtc = Timestamp }],
|
||||
};
|
||||
}
|
||||
|
||||
private static ClusterNode Node(string id) => new()
|
||||
{
|
||||
NodeId = id, ClusterId = "cluster-local", Host = id.Split(':')[0], ApplicationUri = "urn:" + id,
|
||||
CreatedAt = Timestamp, CreatedBy = "test",
|
||||
};
|
||||
|
||||
private static DataConnection Connection(string id) => new()
|
||||
{
|
||||
DataConnectionId = id, ClusterId = "cluster-local", Enabled = true,
|
||||
ConsumerNodeIds = [NodeA, NodeB], TargetMode = DataTargetMode.Cluster,
|
||||
ExpectedClusterId = "cluster-remote", EligibleTargetNodeIds = ["remote-a", "remote-b"],
|
||||
Endpoints = ["https://node-b:7443", "https://node-b-backup:7443"],
|
||||
SecretReference = "secret:data/upstream", SecretScopeClusterId = "cluster-local",
|
||||
AllowedOperations = DataOperations.Browse | DataOperations.Read,
|
||||
};
|
||||
|
||||
private static RemoteNamespaceMapping Mapping(string id, string connectionId) => new()
|
||||
{
|
||||
RemoteNamespaceMappingId = id, ClusterId = "cluster-local", DataConnectionId = connectionId,
|
||||
Enabled = true, ConsumerNodeIds = [NodeA, NodeB], Realm = DataRealm.Raw, RemoteRealm = DataRealm.Raw,
|
||||
LocalParentKind = "RawFolder", LocalParentId = "local-root", BranchName = id,
|
||||
RemoteRootEntityId = "remote-root", RemoteNamespaceUri = "https://zb.com/otopcua/raw", RemoteIdentifierKind = "string",
|
||||
RemoteIdentifier = "remote-root", AllowedOperations = DataOperations.Browse,
|
||||
};
|
||||
|
||||
private static DataExport Export(string id) => new()
|
||||
{
|
||||
DataExportId = id, ClusterId = "cluster-local", Enabled = true, Realm = DataRealm.Raw,
|
||||
RootKind = "RawFolder", RootEntityId = "local-root", PrincipalId = "peer",
|
||||
AllowedOperations = DataOperations.Browse | DataOperations.Read,
|
||||
};
|
||||
|
||||
private static string Inflate(byte[] blob)
|
||||
{
|
||||
using var gzip = new GZipStream(new MemoryStream(blob), CompressionMode.Decompress);
|
||||
using var reader = new StreamReader(gzip, Encoding.UTF8);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
+496
@@ -0,0 +1,496 @@
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
[Trait("Feature", "GrpcData")]
|
||||
[Trait("Phase", "P02")]
|
||||
public sealed class GrpcDataArtifactReadTests
|
||||
{
|
||||
private const string NodeA = "node-a";
|
||||
private const string NodeB = "node-b";
|
||||
|
||||
[Theory]
|
||||
[InlineData("{}")]
|
||||
[InlineData(
|
||||
"{\"Clusters\":[{\"ClusterId\":\"local\"}],\"Nodes\":[{\"NodeId\":\"node-a\",\"ClusterId\":\"local\",\"Enabled\":true}]}"
|
||||
)]
|
||||
[InlineData("{\"UnknownFutureArray\":[{\"Enabled\":true}]}")]
|
||||
public void Old_artifacts_without_new_arrays_have_no_enabled_data_surface(string json)
|
||||
{
|
||||
using var parsed = DeploymentArtifact.Open(Encoding.UTF8.GetBytes(json));
|
||||
|
||||
parsed.ParseDataConnections(NodeA).ShouldBeEmpty();
|
||||
parsed.ParseDataExports(NodeA).ShouldBeEmpty();
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA).ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_only_peer_mount_is_not_returned_to_its_same_cluster_target_B()
|
||||
{
|
||||
using var parsed = DeploymentArtifact.Open(Blob(Fixture()));
|
||||
|
||||
var connection = parsed.ParseDataConnections(NodeA).ShouldHaveSingleItem();
|
||||
connection.TargetMode.ShouldBe(DataTargetMode.Node);
|
||||
connection.ExpectedNodeId.ShouldBe(NodeB);
|
||||
connection.ExpectedClusterId.ShouldBe("local");
|
||||
parsed
|
||||
.ParseRemoteNamespaceMappings(NodeA)
|
||||
.ShouldHaveSingleItem()
|
||||
.RemoteNamespaceMappingId.ShouldBe("mapping-a");
|
||||
parsed.ParseDataConnections(NodeB).ShouldBeEmpty();
|
||||
parsed.ParseRemoteNamespaceMappings(NodeB).ShouldBeEmpty();
|
||||
// B still owns the local export catalog; import consumer scope must not suppress it.
|
||||
parsed.ParseDataExports(NodeB).ShouldHaveSingleItem().DataExportId.ShouldBe("export-a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void Unknown_node_never_inherits_a_single_clusters_new_data_surface(bool omitNodes)
|
||||
{
|
||||
var root = Fixture();
|
||||
if (omitNodes)
|
||||
root.Remove("Nodes");
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
parsed.ParseDataConnections("unlisted").ShouldBeEmpty();
|
||||
parsed.ParseDataExports("unlisted").ShouldBeEmpty();
|
||||
parsed.ParseRemoteNamespaceMappings("unlisted").ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("node-disabled")]
|
||||
[InlineData("node-maintenance")]
|
||||
[InlineData("cluster-disabled")]
|
||||
public void Ineligible_node_or_owner_cluster_has_no_new_data_surface(string state)
|
||||
{
|
||||
var root = Fixture();
|
||||
if (state == "node-disabled")
|
||||
root["Nodes"]![0]!["Enabled"] = false;
|
||||
if (state == "node-maintenance")
|
||||
root["Nodes"]![0]!["MaintenanceMode"] = true;
|
||||
if (state == "cluster-disabled")
|
||||
root["Clusters"]![0]!["Enabled"] = false;
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
parsed.ParseDataConnections(NodeA).ShouldBeEmpty();
|
||||
parsed.ParseDataExports(NodeA).ShouldBeEmpty();
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA).ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Owner_cluster_filters_connections_mappings_and_origin_local_exports()
|
||||
{
|
||||
var root = Fixture();
|
||||
root["Clusters"]!
|
||||
.AsArray()
|
||||
.Add(
|
||||
JsonNode.Parse(
|
||||
"""{"ClusterId":"other","Name":"Other","Enterprise":"ent","Site":"site","RedundancyMode":0,"CreatedBy":"test","Enabled":true}"""
|
||||
)
|
||||
);
|
||||
root["Nodes"]!
|
||||
.AsArray()
|
||||
.Add(
|
||||
JsonNode.Parse(
|
||||
"""{"NodeId":"node-c","ClusterId":"other","Host":"node-c","ApplicationUri":"urn:node-c","CreatedBy":"test","Enabled":true}"""
|
||||
)
|
||||
);
|
||||
root["RawFolders"]!
|
||||
.AsArray()
|
||||
.Add(
|
||||
JsonNode.Parse(
|
||||
"""{"RawFolderId":"other-root","ClusterId":"other","Name":"Other"}"""
|
||||
)
|
||||
);
|
||||
var foreignConnection = root["DataConnections"]![0]!.DeepClone();
|
||||
foreignConnection["DataConnectionId"] = "connection-other";
|
||||
foreignConnection["ClusterId"] = "other";
|
||||
foreignConnection["SecretScopeClusterId"] = "other";
|
||||
foreignConnection["ConsumerNodeIds"] = new JsonArray("node-c");
|
||||
root["DataConnections"]!.AsArray().Add(foreignConnection);
|
||||
var foreignMapping = root["RemoteNamespaceMappings"]![0]!.DeepClone();
|
||||
foreignMapping["RemoteNamespaceMappingId"] = "mapping-other";
|
||||
foreignMapping["ClusterId"] = "other";
|
||||
foreignMapping["DataConnectionId"] = "connection-other";
|
||||
foreignMapping["LocalParentId"] = "other-root";
|
||||
foreignMapping["ConsumerNodeIds"] = new JsonArray("node-c");
|
||||
root["RemoteNamespaceMappings"]!.AsArray().Add(foreignMapping);
|
||||
var foreignExport = root["DataExports"]![0]!.DeepClone();
|
||||
foreignExport["DataExportId"] = "export-other";
|
||||
foreignExport["ClusterId"] = "other";
|
||||
foreignExport["RootEntityId"] = "other-root";
|
||||
root["DataExports"]!.AsArray().Add(foreignExport);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
parsed
|
||||
.ParseDataConnections(NodeA)
|
||||
.Select(x => x.DataConnectionId)
|
||||
.ShouldBe(["connection-a"]);
|
||||
parsed
|
||||
.ParseRemoteNamespaceMappings(NodeA)
|
||||
.Select(x => x.RemoteNamespaceMappingId)
|
||||
.ShouldBe(["mapping-a"]);
|
||||
parsed.ParseDataExports(NodeA).Select(x => x.DataExportId).ShouldBe(["export-a"]);
|
||||
parsed
|
||||
.ParseDataExports("node-c")
|
||||
.ShouldHaveSingleItem()
|
||||
.DataExportId.ShouldBe("export-other");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disabled_rows_are_retained_within_scope_for_generation_reconciliation()
|
||||
{
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]!["Enabled"] = false;
|
||||
root["DataExports"]![0]!["Enabled"] = false;
|
||||
root["RemoteNamespaceMappings"]![0]!["Enabled"] = false;
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
parsed.ParseDataConnections(NodeA).ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
||||
parsed.ParseDataExports(NodeA).ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA).ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
||||
parsed.ParseDataConnections(NodeB).ShouldBeEmpty();
|
||||
parsed.ParseRemoteNamespaceMappings(NodeB).ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Omitted_enabled_flags_never_enable_new_configuration_implicitly()
|
||||
{
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]!.AsObject().Remove("Enabled");
|
||||
root["DataExports"]![0]!.AsObject().Remove("Enabled");
|
||||
root["RemoteNamespaceMappings"]![0]!.AsObject().Remove("Enabled");
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
parsed.ParseDataConnections(NodeA).ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
||||
parsed.ParseDataExports(NodeA).ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA).ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ConsumerNodeIds", "[]")]
|
||||
[InlineData("ConsumerNodeIds", "null")]
|
||||
[InlineData("ConsumerNodeIds", "\"node-a\"")]
|
||||
[InlineData("TargetMode", "0")]
|
||||
[InlineData("TargetMode", "999")]
|
||||
[InlineData("ExpectedNodeId", "null")]
|
||||
[InlineData("ExpectedNodeId", "\"node-a\"")]
|
||||
[InlineData("SecretReference", "\"\"")]
|
||||
[InlineData("SecretScopeClusterId", "\"foreign\"")]
|
||||
[InlineData("Endpoints", "null")]
|
||||
[InlineData("Endpoints", "[\"http://node-b:7443\"]")]
|
||||
[InlineData("AllowedOperations", "0")]
|
||||
[InlineData("AllowedOperations", "1073741824")]
|
||||
[InlineData("MaxBatchItems", "0")]
|
||||
[InlineData("Revision", "0")]
|
||||
public void Malformed_enabled_connection_fails_closed_with_row_diagnostics(
|
||||
string field,
|
||||
string value
|
||||
)
|
||||
{
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]![field] = JsonNode.Parse(value);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
var error = Should.Throw<InvalidDataException>(() => parsed.ParseDataConnections(NodeA));
|
||||
|
||||
error.Message.ShouldContain("connection-a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Realm", "0")]
|
||||
[InlineData("RemoteRealm", "2")]
|
||||
[InlineData("DataConnectionId", "\"missing-connection\"")]
|
||||
[InlineData("LocalParentId", "\"missing-parent\"")]
|
||||
[InlineData("ConsumerNodeIds", "[\"node-a\",\"node-b\"]")]
|
||||
[InlineData("AllowedOperations", "8")]
|
||||
[InlineData("MaxProjectionNodes", "0")]
|
||||
[InlineData("Revision", "0")]
|
||||
public void Malformed_enabled_mapping_is_not_installed_with_inferred_permissions(
|
||||
string field,
|
||||
string value
|
||||
)
|
||||
{
|
||||
var root = Fixture();
|
||||
root["RemoteNamespaceMappings"]![0]![field] = JsonNode.Parse(value);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
var error = Should.Throw<InvalidDataException>(() =>
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA)
|
||||
);
|
||||
|
||||
error.Message.ShouldContain("mapping-a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("PrincipalId", "\"\"")]
|
||||
[InlineData("Realm", "0")]
|
||||
[InlineData("RootEntityId", "\"missing-origin\"")]
|
||||
[InlineData("RootKind", "\"RemoteNamespaceMapping\"")]
|
||||
[InlineData("AllowedOperations", "1073741824")]
|
||||
[InlineData("Revision", "0")]
|
||||
public void Malformed_enabled_export_cannot_become_a_broad_or_reexport_grant(
|
||||
string field,
|
||||
string value
|
||||
)
|
||||
{
|
||||
var root = Fixture();
|
||||
root["DataExports"]![0]![field] = JsonNode.Parse(value);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
var error = Should.Throw<InvalidDataException>(() => parsed.ParseDataExports(NodeA));
|
||||
|
||||
error.Message.ShouldContain("export-a");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Missing_connection_array_does_not_leave_an_enabled_orphan_mount()
|
||||
{
|
||||
var root = Fixture();
|
||||
root.Remove("DataConnections");
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
var error = Should.Throw<InvalidDataException>(() =>
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA)
|
||||
);
|
||||
|
||||
error.Message.ShouldContain("mapping-a");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Presented_credentials_are_rejected_without_echoing_them_in_artifact_diagnostics()
|
||||
{
|
||||
const string presentedSecret = "presented-key-never-log-this";
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]!["Endpoints"] = new JsonArray(
|
||||
"https://peer:" + presentedSecret + "@node-b:7443"
|
||||
);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
|
||||
var error = Should.Throw<InvalidDataException>(() => parsed.ParseDataConnections(NodeA));
|
||||
|
||||
error.Message.ShouldContain("connection-a");
|
||||
error.Message.ShouldNotContain(presentedSecret);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void New_projections_are_materialized_and_survive_document_disposal()
|
||||
{
|
||||
IReadOnlyList<DataConnection> connections;
|
||||
IReadOnlyList<DataExport> exports;
|
||||
IReadOnlyList<RemoteNamespaceMapping> mappings;
|
||||
var root = Fixture();
|
||||
root["RemoteNamespaceMappings"]![0]!["ExpectedValueKind"] = (int)DataValueKind.Array;
|
||||
root["RemoteNamespaceMappings"]![0]!["ExpectedArrayElementKind"] = (int)
|
||||
DataValueKind.UInt64;
|
||||
using (var parsed = DeploymentArtifact.Open(Blob(root)))
|
||||
{
|
||||
connections = parsed.ParseDataConnections(NodeA);
|
||||
exports = parsed.ParseDataExports(NodeA);
|
||||
mappings = parsed.ParseRemoteNamespaceMappings(NodeA);
|
||||
}
|
||||
|
||||
connections.ShouldHaveSingleItem().Endpoints.ShouldBe(["https://node-b:7443"]);
|
||||
connections[0].ConsumerNodeIds.ShouldBe([NodeA]);
|
||||
connections[0].SecretReference.ShouldBe("secret:data/upstream");
|
||||
exports.ShouldHaveSingleItem().RootEntityId.ShouldBe("local-root");
|
||||
mappings.ShouldHaveSingleItem().RemoteIdentifier.ShouldBe("origin-root");
|
||||
mappings[0].WriteThrough.ShouldBeFalse();
|
||||
mappings[0].ExpectedValueKind.ShouldBe(DataValueKind.Array);
|
||||
mappings[0].ExpectedArrayElementKind.ShouldBe(DataValueKind.UInt64);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Static_facades_preserve_the_strict_scoped_projection()
|
||||
{
|
||||
var blob = Blob(Fixture());
|
||||
|
||||
DeploymentArtifact
|
||||
.ParseDataConnections(blob, NodeA)
|
||||
.ShouldHaveSingleItem()
|
||||
.DataConnectionId.ShouldBe("connection-a");
|
||||
DeploymentArtifact
|
||||
.ParseDataExports(blob, NodeA)
|
||||
.ShouldHaveSingleItem()
|
||||
.DataExportId.ShouldBe("export-a");
|
||||
DeploymentArtifact
|
||||
.ParseRemoteNamespaceMappings(blob, NodeA)
|
||||
.ShouldHaveSingleItem()
|
||||
.RemoteNamespaceMappingId.ShouldBe("mapping-a");
|
||||
DeploymentArtifact.ParseDataConnections(blob, "unlisted").ShouldBeEmpty();
|
||||
DeploymentArtifact.ParseDataExports(blob, "unlisted").ShouldBeEmpty();
|
||||
DeploymentArtifact.ParseRemoteNamespaceMappings(blob, "unlisted").ShouldBeEmpty();
|
||||
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]!["SecretReference"] = "";
|
||||
Should
|
||||
.Throw<InvalidDataException>(() =>
|
||||
DeploymentArtifact.ParseDataConnections(Blob(root), NodeA)
|
||||
)
|
||||
.Message.ShouldContain("connection-a");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Foreign_projected_mapping_cannot_be_used_as_a_remote_root()
|
||||
{
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]!["ExpectedClusterId"] = "remote";
|
||||
var foreign = root["RemoteNamespaceMappings"]![0]!.DeepClone();
|
||||
foreign["ClusterId"] = "remote";
|
||||
foreign["RemoteNamespaceMappingId"] = "origin-root";
|
||||
root["RemoteNamespaceMappings"]!.AsArray().Add(foreign);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
Should
|
||||
.Throw<InvalidDataException>(() => parsed.ParseRemoteNamespaceMappings(NodeA))
|
||||
.Message.ShouldContain("mapping-a");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Null_topology_rows_fail_closed_as_invalid_data()
|
||||
{
|
||||
var root = Fixture();
|
||||
root["Nodes"]!.AsArray().Add((JsonNode?)null);
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
Should.Throw<InvalidDataException>(() => parsed.ParseDataConnections(NodeA));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("DataConnections", "DataConnectionId", "missing")]
|
||||
[InlineData("DataConnections", "DataConnectionId", "null")]
|
||||
[InlineData("DataConnections", "DataConnectionId", "blank")]
|
||||
[InlineData("DataExports", "DataExportId", "missing")]
|
||||
[InlineData("DataExports", "DataExportId", "null")]
|
||||
[InlineData("DataExports", "DataExportId", "blank")]
|
||||
[InlineData("RemoteNamespaceMappings", "RemoteNamespaceMappingId", "missing")]
|
||||
[InlineData("RemoteNamespaceMappings", "RemoteNamespaceMappingId", "null")]
|
||||
[InlineData("RemoteNamespaceMappings", "RemoteNamespaceMappingId", "blank")]
|
||||
public void Enabled_rows_require_logical_ids(string collection, string field, string shape)
|
||||
{
|
||||
var root = Fixture();
|
||||
var row = root[collection]![0]!.AsObject();
|
||||
if (shape == "missing")
|
||||
row.Remove(field);
|
||||
else if (shape == "null")
|
||||
row[field] = null;
|
||||
else
|
||||
row[field] = " ";
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
var error = Should.Throw<InvalidDataException>(() =>
|
||||
{
|
||||
if (collection == "DataConnections")
|
||||
parsed.ParseDataConnections(NodeA);
|
||||
else if (collection == "DataExports")
|
||||
parsed.ParseDataExports(NodeA);
|
||||
else
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA);
|
||||
});
|
||||
error.Message.ShouldContain("<unknown>");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Null_id_cannot_hide_other_invalid_connection_fields()
|
||||
{
|
||||
var root = Fixture();
|
||||
root["DataConnections"]![0]!["DataConnectionId"] = null;
|
||||
root["DataConnections"]![0]!["TargetMode"] = 0;
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
Should
|
||||
.Throw<InvalidDataException>(() => parsed.ParseDataConnections(NodeA))
|
||||
.Message.ShouldContain("<unknown>");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("DataConnections", "DataConnectionId")]
|
||||
[InlineData("DataExports", "DataExportId")]
|
||||
[InlineData("RemoteNamespaceMappings", "RemoteNamespaceMappingId")]
|
||||
public void Lowercase_enabled_with_null_id_is_rejected(string collection, string idName)
|
||||
{
|
||||
var root = Fixture();
|
||||
var row = root[collection]![0]!.AsObject();
|
||||
row.Remove("Enabled");
|
||||
row["enabled"] = true;
|
||||
row[idName] = null;
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
Should.Throw<InvalidDataException>(() => ReadCollection(parsed, collection));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("DataConnections", "DataConnectionId", "dataConnectionId")]
|
||||
[InlineData("DataExports", "DataExportId", "dataExportId")]
|
||||
[InlineData("RemoteNamespaceMappings", "RemoteNamespaceMappingId", "remoteNamespaceMappingId")]
|
||||
public void Case_variant_shadow_id_is_rejected(string collection, string idName, string alias)
|
||||
{
|
||||
var root = Fixture();
|
||||
root[collection]![0]!.AsObject().ContainsKey(idName).ShouldBeTrue();
|
||||
root[collection]![0]![alias] = null;
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
Should.Throw<InvalidDataException>(() => ReadCollection(parsed, collection));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("DataConnections")]
|
||||
[InlineData("DataExports")]
|
||||
[InlineData("RemoteNamespaceMappings")]
|
||||
public void Case_variant_owner_cannot_override_local_scope(string collection)
|
||||
{
|
||||
var root = Fixture();
|
||||
root[collection]![0]!["clusterId"] = "foreign";
|
||||
using var parsed = DeploymentArtifact.Open(Blob(root));
|
||||
Should.Throw<InvalidDataException>(() => ReadCollection(parsed, collection));
|
||||
}
|
||||
|
||||
private static void ReadCollection(ParsedDeploymentArtifact parsed, string collection)
|
||||
{
|
||||
if (collection == "DataConnections")
|
||||
parsed.ParseDataConnections(NodeA);
|
||||
else if (collection == "DataExports")
|
||||
parsed.ParseDataExports(NodeA);
|
||||
else
|
||||
parsed.ParseRemoteNamespaceMappings(NodeA);
|
||||
}
|
||||
|
||||
private static byte[] Blob(JsonObject root) => Encoding.UTF8.GetBytes(root.ToJsonString());
|
||||
|
||||
private static JsonObject Fixture() =>
|
||||
JsonNode
|
||||
.Parse(
|
||||
"""
|
||||
{
|
||||
"Clusters": [{"ClusterId":"local","Name":"Local","Enterprise":"ent","Site":"site","RedundancyMode":0,"CreatedBy":"test","Enabled":true}],
|
||||
"Nodes": [
|
||||
{"NodeId":"node-a","ClusterId":"local","Host":"node-a","ApplicationUri":"urn:node-a","CreatedBy":"test","Enabled":true,"MaintenanceMode":false},
|
||||
{"NodeId":"node-b","ClusterId":"local","Host":"node-b","ApplicationUri":"urn:node-b","CreatedBy":"test","Enabled":true,"MaintenanceMode":false}
|
||||
],
|
||||
"RawFolders": [{"RawFolderId":"local-root","ClusterId":"local","Name":"Local"}],
|
||||
"DataConnections": [{
|
||||
"DataConnectionId":"connection-a","ClusterId":"local","Revision":3,"Enabled":true,
|
||||
"ConsumerNodeIds":["node-a"],"TargetMode":1,"Consistency":1,
|
||||
"ExpectedClusterId":"local","ExpectedNodeId":"node-b","EligibleTargetNodeIds":[],
|
||||
"Endpoints":["https://node-b:7443"],"SecretReference":"secret:data/upstream",
|
||||
"SecretScopeClusterId":"local","AllowedOperations":5
|
||||
}],
|
||||
"DataExports": [{
|
||||
"DataExportId":"export-a","ClusterId":"local","Revision":4,"Enabled":true,
|
||||
"Realm":1,"RootKind":"RawFolder","RootEntityId":"local-root",
|
||||
"PrincipalId":"peer","AllowedOperations":5
|
||||
}],
|
||||
"RemoteNamespaceMappings": [{
|
||||
"RemoteNamespaceMappingId":"mapping-a","ClusterId":"local","DataConnectionId":"connection-a",
|
||||
"Revision":5,"Enabled":true,"ConsumerNodeIds":["node-a"],"Realm":1,"RemoteRealm":1,
|
||||
"LocalParentKind":"RawFolder","LocalParentId":"local-root","BranchName":"Remote",
|
||||
"RemoteRootEntityId":"origin-root","RemoteNamespaceUri":"https://zb.com/otopcua/raw","RemoteIdentifierKind":"string",
|
||||
"RemoteIdentifier":"origin-root","AllowedOperations":1,"WriteThrough":false
|
||||
}]
|
||||
}
|
||||
"""
|
||||
)!
|
||||
.AsObject();
|
||||
}
|
||||
Reference in New Issue
Block a user