test(ui): retain OtOpcUa component canaries and owned SQL proof
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
using System.Security.Claims;
|
||||
using System.Reflection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using OtUiCanary.Components;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
using ZB.MOM.WW.Secrets.Ui;
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.WebHost.UseStaticWebAssets();
|
||||
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
|
||||
builder.Services.AddDataProtection().UseEphemeralDataProtectionProvider();
|
||||
builder.Services.AddAuthorizationCore(options =>
|
||||
{
|
||||
options.AddSecretsAuthorization();
|
||||
// Fixture-only mirror of Security/ServiceCollectionExtensions.cs policy requirements;
|
||||
// no cookie login, LDAP or real principal is configured in this component host.
|
||||
options.AddPolicy(ZB.MOM.WW.OtOpcUa.Security.Auth.AdminUiPolicies.DriverOperator, p => p.RequireRole("Operator", "Administrator"));
|
||||
options.AddPolicy(ZB.MOM.WW.OtOpcUa.Security.Auth.AdminUiPolicies.FleetAdmin, p => p.RequireRole("Administrator"));
|
||||
options.AddPolicy(ZB.MOM.WW.OtOpcUa.Security.Auth.AdminUiPolicies.ConfigEditor, p => p.RequireRole("Administrator", "Designer"));
|
||||
options.AddPolicy(ZB.MOM.WW.OtOpcUa.Security.Auth.AdminUiPolicies.AuthenticatedRead, p => p.RequireAuthenticatedUser());
|
||||
});
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider, SyntheticAdministratorFixture>();
|
||||
builder.Services.AddScoped<FixtureTreeState>();
|
||||
builder.Services.AddScoped(sp => FixtureTreeProxy.Create<IRawTreeService>(sp.GetRequiredService<FixtureTreeState>()));
|
||||
builder.Services.AddScoped(sp => FixtureTreeProxy.Create<IUnsTreeService>(sp.GetRequiredService<FixtureTreeState>()));
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.IInProcessBroadcaster<ZB.MOM.WW.OtOpcUa.Commons.Messages.Logging.ScriptLogEntry>, ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.InProcessBroadcaster<ZB.MOM.WW.OtOpcUa.Commons.Messages.Logging.ScriptLogEntry>>();
|
||||
builder.Services.AddDbContextFactory<ZB.MOM.WW.OtOpcUa.Configuration.OtOpcUaConfigDbContext>(options => options.UseInMemoryDatabase("ot-ui-local-empty"));
|
||||
builder.Services.AddScoped<RawTagCsvExportReader>();
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.AdminUI.Deployments.CredentialReferencePreflightState>();
|
||||
builder.Services.AddSingleton(DispatchProxy.Create<ZB.MOM.WW.OtOpcUa.AdminUI.Browsing.IBrowserSessionService, RejectingFixture>());
|
||||
builder.Services.AddSingleton(RejectingFixture.WithRead<ZB.MOM.WW.OtOpcUa.Commons.Interfaces.IClusterRoleInfo>("MembersWithRole", (IReadOnlyList<ZB.MOM.WW.OtOpcUa.Commons.Types.NodeId>)[new("fixture-driver-with-long-local-name")]));
|
||||
builder.Services.AddSingleton<FixtureDbReadControl>();
|
||||
builder.Services.AddSingleton<IDbContextFactory<ZB.MOM.WW.OtOpcUa.Configuration.OtOpcUaConfigDbContext>>(sp => sp.GetRequiredService<FixtureDbReadControl>());
|
||||
builder.Services.AddSingleton<FixtureDeploymentState>();
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.Commons.Interfaces.IAdminOperationsClient>(sp => SyntheticOperationsFixture.WithDeployments(sp.GetRequiredService<FixtureDeploymentState>()));
|
||||
builder.Services.AddScoped(sp => new ZB.MOM.WW.OtOpcUa.AdminUI.Deployments.DeploymentDriftProbe(sp.GetRequiredService<IDbContextFactory<ZB.MOM.WW.OtOpcUa.Configuration.OtOpcUaConfigDbContext>>(), _ => Task.FromResult(FixtureDeploymentState.Revision)));
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.IInProcessBroadcaster<ZB.MOM.WW.OtOpcUa.Commons.Messages.Alerts.AlarmTransitionEvent>, ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.InProcessBroadcaster<ZB.MOM.WW.OtOpcUa.Commons.Messages.Alerts.AlarmTransitionEvent>>();
|
||||
builder.Services.AddSingleton(DispatchProxy.Create<ZB.MOM.WW.OtOpcUa.Commons.Interfaces.IFleetDiagnosticsClient, RejectingFixture>());
|
||||
// A disposable localhost-only Akka membership shell; no application actors or seed peers.
|
||||
var fixtureActors = Akka.Actor.ActorSystem.Create("ot-ui-local-only", Akka.Configuration.ConfigurationFactory.ParseString("""
|
||||
akka.actor.provider = cluster
|
||||
akka.remote.dot-netty.tcp.hostname = "127.0.0.1"
|
||||
akka.remote.dot-netty.tcp.port = 0
|
||||
akka.cluster.seed-nodes = []
|
||||
akka.loglevel = WARNING
|
||||
"""));
|
||||
builder.Services.AddSingleton(fixtureActors);
|
||||
var historianFixture = fixtureActors.ActorOf(Akka.Actor.Props.Create<ReadonlyHistorianStatusFixture>(), "historian-status-fixture");
|
||||
builder.Services.AddSingleton(RejectingFixture.WithRead<Akka.Hosting.IRequiredActor<ZB.MOM.WW.OtOpcUa.Runtime.HistorianAdapterActorKey>>("get_ActorRef", historianFixture));
|
||||
builder.Services.AddSingleton<FaultableFixtureStatusStore>();
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.IDriverStatusSnapshotStore>(sp => sp.GetRequiredService<FaultableFixtureStatusStore>());
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.IDriverResilienceStatusStore, ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.InMemoryDriverResilienceStatusStore>();
|
||||
builder.Services.AddScoped<ZB.MOM.WW.OtOpcUa.AdminUI.Hosts.DeployedDriverSet>();
|
||||
FinalGrammarFixtures.Register(builder);
|
||||
var app = builder.Build();
|
||||
app.Lifetime.ApplicationStopping.Register(() => _ = fixtureActors.Terminate());
|
||||
// Synthetic fixture-only records. This provider is in-memory, never the actual application's database.
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<ZB.MOM.WW.OtOpcUa.Configuration.OtOpcUaConfigDbContext>>();
|
||||
using var db = factory.CreateDbContext();
|
||||
db.ServerClusters.Add(new(){ClusterId="fixture-cluster",Name="Synthetic local cluster",Enterprise="fixture",Site="local",RedundancyMode=ZB.MOM.WW.OtOpcUa.Configuration.Enums.RedundancyMode.None,CreatedBy="fixture"});
|
||||
db.ClusterNodes.Add(new(){ClusterId="fixture-cluster",NodeId="fixture-node",Host="localhost",ApplicationUri="urn:fixture:local",CreatedBy="fixture"});
|
||||
db.NodeAcls.Add(new(){ClusterId="fixture-cluster",NodeAclId="fixture-acl",Principal="fixture-principal",ScopeKind=ZB.MOM.WW.OtOpcUa.Configuration.Enums.NodeAclScopeKind.Cluster,PermissionFlags=ZB.MOM.WW.OtOpcUa.Configuration.Enums.NodePermissions.ReadOnly});
|
||||
db.Scripts.Add(new() { ScriptId="fixture-script", Name="Synthetic script with a deliberately long human-readable name", SourceCode="return 1;", SourceHash=new string('a',64) });
|
||||
var diagnostic = "Synthetic local failure <script>ignored()</script> " + new string('x',5000) + " END-DIAGNOSTIC";
|
||||
var deployment = new ZB.MOM.WW.OtOpcUa.Configuration.Entities.Deployment { RevisionHash=new string('b',64), CreatedBy="local-fixture-only", Status=ZB.MOM.WW.OtOpcUa.Configuration.Enums.DeploymentStatus.PartiallyFailed, FailureReason=diagnostic };
|
||||
db.Deployments.Add(deployment);
|
||||
db.NodeDeploymentStates.Add(new() { NodeId="fixture-driver-with-long-local-name", DeploymentId=deployment.DeploymentId, Status=ZB.MOM.WW.OtOpcUa.Configuration.Enums.NodeDeploymentStatus.Failed, FailureReason=diagnostic });
|
||||
db.ConfigAuditLogs.Add(new() { Principal="local-fixture-only", EventType="SyntheticFailure", ClusterId="fixture-cluster", CorrelationId=Guid.Parse("71324bc9-ae5d-4f41-9f0b-a38a092f77d8"), DetailsJson=System.Text.Json.JsonSerializer.Serialize(new { message=diagnostic }) });
|
||||
db.SaveChanges();
|
||||
}
|
||||
app.UseStaticFiles(); app.UseAntiforgery();
|
||||
app.MapGet("/fixture/provenance", () => new { purpose="Disposable actual-component UI canary; explicit synthetic in-memory operations only, no external fleet/database/LDAP/COM connectivity", source="Authorized OtOpcUa checkout", theme=typeof(ZB.MOM.WW.Theme.ThemeShell).Assembly.GetName().Version?.ToString() });
|
||||
app.MapPost("/api/script-analysis/diagnostics", () => Results.Json(new { markers = Array.Empty<object>() }));
|
||||
app.MapPost("/fixture/alarm-feed/{id}", (string id, ZB.MOM.WW.OtOpcUa.AdminUI.Hubs.IInProcessBroadcaster<ZB.MOM.WW.OtOpcUa.Commons.Messages.Alerts.AlarmTransitionEvent> feed) =>
|
||||
{
|
||||
if(id is not ("alarm-a" or "alarm-b"))return Results.BadRequest();
|
||||
feed.Publish(new(id,"Fixture/Press","Local "+id,"Activated",500,"Local display-only event "+id,"fixture",DateTime.UtcNow));
|
||||
return Results.Ok(new {fixtureOnly=true});
|
||||
});
|
||||
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
|
||||
app.Run();
|
||||
internal sealed class SyntheticAdministratorFixture : AuthenticationStateProvider
|
||||
{
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync() => Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity([new(ClaimTypes.Name,"local-fixture-only"), new(ClaimTypes.Role,"Administrator"),new(ClaimTypes.Role,SecretsAuthorization.ManageRole)], "synthetic-fixture"))));
|
||||
}
|
||||
public class RejectingFixture : DispatchProxy
|
||||
{
|
||||
private readonly Dictionary<string, object> _reads = [];
|
||||
public static T WithRead<T>(string method, object value) where T : class
|
||||
{
|
||||
var proxy = Create<T, RejectingFixture>();
|
||||
var fixture = (RejectingFixture)(object)proxy; fixture._reads[method] = value;
|
||||
return proxy;
|
||||
}
|
||||
public static T WithReads<T>(Dictionary<string, object> reads) where T : class
|
||||
{
|
||||
var proxy = Create<T, RejectingFixture>();
|
||||
var fixture = (RejectingFixture)(object)proxy;
|
||||
foreach (var pair in reads) fixture._reads[pair.Key] = pair.Value;
|
||||
return proxy;
|
||||
}
|
||||
protected override object? Invoke(MethodInfo? method, object?[]? args) => method is not null && _reads.TryGetValue(method.Name, out var value) ? value : throw new InvalidOperationException("Disposable fixture rejects service operations.");
|
||||
}
|
||||
Reference in New Issue
Block a user