Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Transport.Tests/Export/ExportSecretPolicyTests.cs
T
Joseph Doherty 8c5d70f687 fix(transport): secret-bearing bundle export needs Administrator; scrub data-connection move reply (MGT-3, MGT-4, WP1 task 1.5)
MGT-3: a Designer could read SMTP/SMS credentials, external-system auth,
DB connection strings and data-connection configuration through bundle
export, values the management API returns to no one. Both export
surfaces now resolve the selection (dependencies included) via the new
IBundleExporter.CountSecretsAsync / ExportSecretPolicy and require
Administrator when it carries any secret; a secret-free export stays a
Designer operation. The ExportBundle gate becomes any-of [Designer,
Administrator] - a deliberate change to the frozen authz matrix. A new
ManagementAuthorizationException maps to ManagementUnauthorized.

MGT-4: data-connection move replies with DataConnectionPublicShape like
its siblings (the audit after-state switched with task 1.4).

Tests: matrix entry, Designer+secrets refused (nothing exported), Admin
and secret-free Designer succeed, UI wizard refuses a Designer, policy
counts every SecretsBlock field, move reply carries no secret.
2026-12-03 10:04:28 -05:00

56 lines
2.8 KiB
C#

using ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
using ZB.MOM.WW.ScadaBridge.Transport.Export;
namespace ZB.MOM.WW.ScadaBridge.Transport.Tests.Export;
/// <summary>
/// Code review 2026-12-02 MGT-3: <see cref="ExportSecretPolicy.CountSecrets"/> decides whether
/// an export needs the Administrator role, on both the management API and the Central UI. It
/// must count exactly the fields the serializer puts in a <c>SecretsBlock</c>.
/// </summary>
public class ExportSecretPolicyTests
{
private static ResolvedExport Resolved(
IReadOnlyList<ExternalSystemDefinition>? es = null,
IReadOnlyList<DatabaseConnectionDefinition>? db = null,
IReadOnlyList<SmtpConfiguration>? smtp = null,
IReadOnlyList<SmsConfiguration>? sms = null,
IReadOnlyList<DataConnection>? dc = null) =>
new(Array.Empty<TemplateFolder>(), Array.Empty<Template>(), Array.Empty<SharedScript>(),
es ?? Array.Empty<ExternalSystemDefinition>(), Array.Empty<ExternalSystemMethod>(),
db ?? Array.Empty<DatabaseConnectionDefinition>(), Array.Empty<NotificationList>(),
smtp ?? Array.Empty<SmtpConfiguration>(), Array.Empty<ApiMethod>(), Array.Empty<ManifestContentEntry>())
{
SmsConfigs = sms ?? Array.Empty<SmsConfiguration>(),
DataConnections = dc ?? Array.Empty<DataConnection>(),
};
/// <summary>A secret-free closure needs no Administrator.</summary>
[Fact]
public void SecretFreeExport_CountsZero()
{
var es = new ExternalSystemDefinition("ERP", "https://erp", "None");
var dc = new DataConnection("Plc", "OpcUa", 1);
Assert.Equal(0, ExportSecretPolicy.CountSecrets(Resolved(es: [es], dc: [dc])));
}
/// <summary>Every secret-bearing category contributes one per non-empty secret field.</summary>
[Fact]
public void EverySecretCategory_IsCounted()
{
var es = new ExternalSystemDefinition("ERP", "https://erp", "ApiKey") { AuthConfiguration = "{\"key\":\"k\"}" };
var db = new DatabaseConnectionDefinition("Mes", "Server=x;Password=y");
var smtp = new SmtpConfiguration("smtp.example.com", "Basic", "a@b") { Credentials = "u:p" };
var sms = new SmsConfiguration("AC1", "+15550000000") { AuthToken = "tok" };
var dc = new DataConnection("Plc", "OpcUa", 1) { PrimaryConfiguration = "{}", BackupConfiguration = "{}" };
Assert.Equal(6, ExportSecretPolicy.CountSecrets(Resolved([es], [db], [smtp], [sms], [dc])));
}
}