285015c9de
1. RawTreeService.UpdateTagAsync refuses the edit that makes a tag alarm-bearing while it roots an enabled DataExport (RawTag, or UnsTagReference to it) with the #942 DataExportRootAlarmBearing text (shared constants). DataConnectionAdminService refuses only errors a save/delete introduces or that remain on the saved row, so a pre-existing invalid row no longer blocks unrelated rows (two invalid rows used to deadlock); deploy gate unchanged. 2. Origin: GetMetadata/Read/Subscribe/condition admission answer a node inside the grant that the origin ACL hides (no Browse and no Read) NotFound, like a missing one (Browse omits it; HistoryRead/Write already uniform). Denied stays for visible nodes. Consumer renewal treats NotFound like Denied (#939 Refused), so tightening the ACL on a projected node still reads BadUserAccessDenied.
161 lines
11 KiB
C#
161 lines
11 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Acls;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P09")]
|
|
public sealed class GrpcDataAuthoringTests
|
|
{
|
|
[Fact]
|
|
public async Task Editor_saves_a_disabled_draft_and_read_only_user_cannot_modify_it()
|
|
{
|
|
var factory = AclScopeTestDb.Factory(Guid.NewGuid().ToString());
|
|
var editor = new DataConnectionAdminService(factory, new Auth(true), new User());
|
|
var row = new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" };
|
|
await editor.SaveAsync("MAIN", row, TestContext.Current.CancellationToken);
|
|
var loaded = (await editor.LoadAsync(TestContext.Current.CancellationToken)).DataConnections.ShouldHaveSingleItem();
|
|
loaded.Enabled.ShouldBeFalse();
|
|
var reader = new DataConnectionAdminService(factory, new Auth(false), new User());
|
|
await Should.ThrowAsync<UnauthorizedAccessException>(() => reader.DeleteAsync("MAIN", loaded, TestContext.Current.CancellationToken));
|
|
(await editor.LoadAsync(TestContext.Current.CancellationToken)).DataConnections.Count.ShouldBe(1);
|
|
}
|
|
[Fact]
|
|
public async Task A_secret_value_is_rejected_by_the_authoritative_validator()
|
|
{
|
|
var service = new DataConnectionAdminService(AclScopeTestDb.Factory(Guid.NewGuid().ToString()), new Auth(true), new User());
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer", SecretReference = "raw-key" }, TestContext.Current.CancellationToken));
|
|
(await service.LoadAsync(TestContext.Current.CancellationToken)).DataConnections.ShouldBeEmpty();
|
|
}
|
|
[Fact]
|
|
public async Task Edits_advance_revision_and_stale_edits_or_deletes_are_refused()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var service = new DataConnectionAdminService(AclScopeTestDb.Factory(Guid.NewGuid().ToString()), new Auth(true), new User());
|
|
await service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" }, ct);
|
|
var original = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
var stale = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
original.ExpectedClusterId = "OTHER";
|
|
await service.SaveAsync("MAIN", original, ct);
|
|
(await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem().Revision.ShouldBe(2);
|
|
await Should.ThrowAsync<DbUpdateConcurrencyException>(() => service.SaveAsync("MAIN", stale, ct));
|
|
await Should.ThrowAsync<DbUpdateConcurrencyException>(() => service.DeleteAsync("MAIN", stale, ct));
|
|
var current = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
await service.DeleteAsync("MAIN", current, ct);
|
|
(await service.LoadAsync(ct)).DataConnections.ShouldBeEmpty();
|
|
}
|
|
[Fact]
|
|
public async Task Row_identity_cannot_be_moved_renamed_or_deleted_by_a_forged_id()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var service = new DataConnectionAdminService(AclScopeTestDb.Factory(Guid.NewGuid().ToString()), new Auth(true), new User());
|
|
await service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" }, ct);
|
|
var row = (await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem();
|
|
row.DataConnectionId = "other";
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("MAIN", row, ct));
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.DeleteAsync("MAIN", row, ct));
|
|
row.ClusterId = "OTHER";
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("OTHER", row, ct));
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.DeleteAsync("OTHER", row, ct));
|
|
(await service.LoadAsync(ct)).DataConnections.ShouldHaveSingleItem().DataConnectionId.ShouldBe("peer");
|
|
}
|
|
[Fact]
|
|
public async Task Read_only_cannot_create_or_enable_write()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var factory = AclScopeTestDb.Factory(Guid.NewGuid().ToString());
|
|
var reader = new DataConnectionAdminService(factory, new Auth(false), new User());
|
|
var row = new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer", Enabled = true,
|
|
AllowedOperations = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.DataOperations.Write };
|
|
await Should.ThrowAsync<UnauthorizedAccessException>(() => reader.SaveAsync("MAIN", row, ct));
|
|
(await reader.LoadAsync(ct)).DataConnections.ShouldBeEmpty();
|
|
}
|
|
[Fact]
|
|
public async Task Connection_with_enabled_mapping_cannot_be_deleted()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var name = Guid.NewGuid().ToString();
|
|
var factory = AclScopeTestDb.Factory(name);
|
|
await using (var db = await factory.CreateDbContextAsync(ct))
|
|
{
|
|
AclScopeTestDb.Seed(db);
|
|
db.ClusterNodes.Add(new ClusterNode { NodeId = "A", ClusterId = "MAIN", Host = "a", ApplicationUri = "urn:a", CreatedBy = "test" });
|
|
db.DataConnections.Add(new DataConnection { DataConnectionId = "peer", ClusterId = "MAIN", Enabled = true,
|
|
ConsumerNodeIds = ["A"], TargetMode = DataTargetMode.Node, ExpectedClusterId = "REMOTE", ExpectedNodeId = "B",
|
|
Endpoints = ["https://peer.example"], SecretReference = "secret:data/peer", SecretScopeClusterId = "MAIN",
|
|
AllowedOperations = DataOperations.Read });
|
|
db.RemoteNamespaceMappings.Add(new RemoteNamespaceMapping { RemoteNamespaceMappingId = "mount", ClusterId = "MAIN", Enabled = true,
|
|
DataConnectionId = "peer", ConsumerNodeIds = ["A"], Realm = DataRealm.Raw, RemoteRealm = DataRealm.Raw,
|
|
LocalParentKind = "Cluster", LocalParentId = "MAIN", BranchName = "remote", RemoteRootEntityId = "root",
|
|
RemoteNamespaceUri = V3NodeIds.RawNamespaceUri, RemoteIdentifierKind = "String", RemoteIdentifier = "root", AllowedOperations = DataOperations.Read });
|
|
await db.SaveChangesAsync(ct);
|
|
}
|
|
var service = new DataConnectionAdminService(factory, new Auth(true), new User());
|
|
var snapshot = await service.LoadAsync(ct);
|
|
DataConnectionValidator.Validate(snapshot).ShouldBeEmpty();
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.DeleteAsync("MAIN", snapshot.DataConnections.ShouldHaveSingleItem(), ct));
|
|
(await service.LoadAsync(ct)).DataConnections.Count.ShouldBe(1);
|
|
}
|
|
/// <summary>
|
|
/// #957: a pre-existing invalid row (here two exports rooted at a tag that became alarm-bearing, #942) no longer blocks
|
|
/// saving or deleting unrelated data-connection rows. A mutation is refused only for an error it introduces or one
|
|
/// still attached to the row it saves; the deploy gate keeps validating the whole draft.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task A_pre_existing_invalid_export_does_not_block_unrelated_rows_957()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var factory = AclScopeTestDb.Factory(Guid.NewGuid().ToString());
|
|
await using (var db = await factory.CreateDbContextAsync(ct))
|
|
{
|
|
AclScopeTestDb.Seed(db);
|
|
db.Tags.Single(t => t.TagId == AclScopeTestDb.TagId).TagConfig = "{\"alarm\":{\"alarmType\":\"AlarmCondition\",\"severity\":700}}";
|
|
foreach (var id in new[] { "bad-1", "bad-2" })
|
|
db.DataExports.Add(new DataExport { DataExportId = id, ClusterId = "MAIN", Enabled = true, Revision = 1, Realm = DataRealm.Raw,
|
|
RootKind = "RawTag", RootEntityId = AclScopeTestDb.TagId, PrincipalId = "peer", AllowedOperations = DataOperations.Read });
|
|
await db.SaveChangesAsync(ct);
|
|
}
|
|
var service = new DataConnectionAdminService(factory, new Auth(true), new User());
|
|
var baseline = DataConnectionValidator.Validate(await service.LoadAsync(ct));
|
|
baseline.Select(e => (e.Code, e.Context)).Order().ShouldBe([
|
|
(DataConnectionValidator.ExportRootAlarmBearingCode, "bad-1"), (DataConnectionValidator.ExportRootAlarmBearingCode, "bad-2")]);
|
|
|
|
// An unrelated row saves (it used to be refused with the exports' error).
|
|
await service.SaveAsync("MAIN", new DataConnection { ClusterId = "MAIN", DataConnectionId = "peer" }, ct);
|
|
// A change that introduces its own error is still refused.
|
|
await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("MAIN",
|
|
new DataConnection { ClusterId = "MAIN", DataConnectionId = "other", SecretReference = "raw-key" }, ct));
|
|
// Re-saving an invalid row without fixing it is refused: the row being saved must be valid.
|
|
var snapshot = await service.LoadAsync(ct);
|
|
var bad1 = snapshot.DataExports.Single(x => x.DataExportId == "bad-1");
|
|
(await Should.ThrowAsync<InvalidOperationException>(() => service.SaveAsync("MAIN", bad1, ct)))
|
|
.Message.ShouldContain("alarm-bearing");
|
|
// With two invalid rows, either can be deleted (before, each blocked deleting the other).
|
|
await service.DeleteAsync("MAIN", bad1, ct);
|
|
var after = await service.LoadAsync(ct);
|
|
after.DataExports.Select(x => x.DataExportId).ShouldBe(["bad-2"]);
|
|
after.DataConnections.Select(x => x.DataConnectionId).ShouldBe(["peer"]);
|
|
// The deploy gate still refuses the remaining one.
|
|
DraftValidator.Validate(after).ShouldContain(e => e.Code == DataConnectionValidator.ExportRootAlarmBearingCode && e.Context == "bad-2");
|
|
}
|
|
|
|
private sealed class User : AuthenticationStateProvider
|
|
{
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity("test"))));
|
|
}
|
|
private sealed class Auth(bool allowed) : IAuthorizationService
|
|
{
|
|
public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object? resource, IEnumerable<IAuthorizationRequirement> requirements) => Task.FromResult(allowed ? AuthorizationResult.Success() : AuthorizationResult.Failed());
|
|
public Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object? resource, string policyName) => Task.FromResult(allowed ? AuthorizationResult.Success() : AuthorizationResult.Failed());
|
|
}
|
|
}
|