165 lines
7.2 KiB
C#
165 lines
7.2 KiB
C#
using System.Text;
|
|
using System.Text.Json.Nodes;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit")]
|
|
[Trait("Feature", "GrpcData")]
|
|
[Trait("Phase", "P03")]
|
|
public sealed class GrpcDataAuthorityProjectionTests
|
|
{
|
|
[Theory]
|
|
[InlineData("Nodes")]
|
|
[InlineData("Clusters")]
|
|
public void Malformed_membership_cannot_prevent_key_revocation_projection(string collection)
|
|
{
|
|
var root = Fixture();
|
|
root[collection]![0]!["enabled"] = false;
|
|
var projection = Parse(root, "node-a");
|
|
projection.Keys.ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
|
projection.Grants.ShouldBeEmpty();
|
|
projection.CatalogValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("export")]
|
|
[InlineData("connection")]
|
|
public void Independent_invalid_configuration_cannot_open_catalog_or_hide_revocation(string kind)
|
|
{
|
|
var root = Fixture();
|
|
root["RemoteNamespaceMappings"] = new JsonArray();
|
|
if (kind == "export")
|
|
root["DataExports"] = new JsonArray(JsonNode.Parse("""{"DataExportId":"bad-export","ClusterId":"local","Enabled":true,"Realm":1,"RootKind":"RawFolder","RootEntityId":"missing","PrincipalId":"peer","AllowedOperations":5}"""));
|
|
else
|
|
root["DataConnections"]![0]!["ExpectedNodeId"] = "node-a";
|
|
var projection = Parse(root, "node-a");
|
|
projection.Keys.ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
|
projection.CatalogValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Invalid_enabled_mapping_preserves_revoked_key_and_grants_but_drops_mapping_scope()
|
|
{
|
|
var root = Fixture();
|
|
root["RemoteNamespaceMappings"]![0]!["LocalParentId"] = "missing";
|
|
|
|
var projection = Parse(root, "node-a");
|
|
|
|
projection.Keys.ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
|
projection.Grants.Count.ShouldBe(2);
|
|
projection.CatalogValid.ShouldBeFalse();
|
|
projection.ScopePaths.ShouldNotContainKey(new AclScopeRef(NodeAclScopeKind.RemoteNamespaceMapping, "mapping-a"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Invalid_topology_preserves_revoked_key_and_grants_and_closes_catalog()
|
|
{
|
|
var root = Fixture();
|
|
root["RawFolders"]!.AsArray().Add((JsonNode?)null);
|
|
|
|
var projection = Parse(root, "node-a");
|
|
|
|
projection.Keys.ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
|
projection.Grants.Count.ShouldBe(2);
|
|
projection.CatalogValid.ShouldBeFalse();
|
|
projection.ScopePaths.ShouldNotContainKey(new AclScopeRef(NodeAclScopeKind.RemoteNamespaceMapping, "mapping-a"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Eligible_single_cluster_node_has_real_cluster_identity_and_mapping_scope()
|
|
{
|
|
var projection = Parse(Fixture(), "node-a");
|
|
|
|
projection.CatalogValid.ShouldBeTrue();
|
|
projection.ClusterId.ShouldBe("local");
|
|
projection.Grants.Count.ShouldBe(2);
|
|
projection.ScopePaths[new AclScopeRef(NodeAclScopeKind.RemoteNamespaceMapping, "mapping-a")]
|
|
.ShouldHaveSingleItem().Segments.ShouldBe(["raw", "Local", "Remote"]);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("unknown")]
|
|
[InlineData("missing-nodes")]
|
|
[InlineData("disabled-node")]
|
|
[InlineData("maintenance-node")]
|
|
[InlineData("disabled-cluster")]
|
|
public void Missing_or_ineligible_membership_cannot_grant_data_surface(string condition)
|
|
{
|
|
var root = Fixture();
|
|
if (condition == "missing-nodes") root.Remove("Nodes");
|
|
if (condition == "disabled-node") root["Nodes"]![0]!["Enabled"] = false;
|
|
if (condition == "maintenance-node") root["Nodes"]![0]!["MaintenanceMode"] = true;
|
|
if (condition == "disabled-cluster") root["Clusters"]![0]!["Enabled"] = false;
|
|
|
|
var projection = Parse(root, condition == "unknown" ? "unknown" : "node-a");
|
|
|
|
projection.Keys.ShouldHaveSingleItem().Enabled.ShouldBeFalse();
|
|
projection.Grants.ShouldBeEmpty();
|
|
projection.ScopePaths.ShouldBeEmpty();
|
|
projection.CatalogValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Legacy_single_cluster_artifact_keeps_legacy_authority_scope(bool emptyNewArrays)
|
|
{
|
|
var root = Fixture();
|
|
root.Remove("DataConnections");
|
|
root.Remove("DataExports");
|
|
root.Remove("RemoteNamespaceMappings");
|
|
root.Remove("Nodes");
|
|
if (emptyNewArrays)
|
|
{
|
|
root["DataConnections"] = new JsonArray();
|
|
root["DataExports"] = new JsonArray();
|
|
root["RemoteNamespaceMappings"] = new JsonArray();
|
|
}
|
|
|
|
var projection = Parse(root, "unknown");
|
|
|
|
projection.ClusterId.ShouldBeNull();
|
|
projection.Grants.Count.ShouldBe(2);
|
|
projection.CatalogValid.ShouldBeTrue();
|
|
}
|
|
|
|
private static DataPlaneAuthorityProjection Parse(JsonObject root, string nodeId) =>
|
|
DeploymentArtifact.ParseDataPlaneAuthority(Encoding.UTF8.GetBytes(root.ToJsonString()), nodeId);
|
|
|
|
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}
|
|
],
|
|
"ApiKeys": [{"ApiKeyId":"revoked","SecretHash":"hash","Principal":"p","Enabled":false}],
|
|
"NodeAcls": [
|
|
{"NodeAclId":"local-grant","ClusterId":"local","Principal":"p","ScopeKind":7,"ScopeId":"local-root","PermissionFlags":2},
|
|
{"NodeAclId":"mapped-grant","ClusterId":"local","Principal":"p","ScopeKind":11,"ScopeId":"mapping-a","PermissionFlags":2}
|
|
],
|
|
"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": [],
|
|
"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();
|
|
}
|