Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/Deployments.razor
T
Joseph Doherty 47d148daf9 fix(adminui): never slice a DB-sourced string with the range operator (#504)
`Scripts.razor` rendered a hash prefix as `@s.SourceHash[..12]…`. `SourceHash`
is a plain nvarchar(64) with no length floor, so any value shorter than 12
characters threw ArgumentOutOfRangeException out of BuildRenderTree — unhandled
in Blazor, which takes the WHOLE page to an HTTP 500, not just the offending
row. Live-reproduced on docker-dev, where two Script rows carry `h1` and
`h-abs-hr200`: /scripts was a hard 500.

New `Components/Shared/DisplayText.Abbreviate(string?, int)`:
- null/blank renders the Admin UI's em-dash placeholder rather than throwing;
- a value already within budget renders verbatim with NO ellipsis, so the
  display never implies text that isn't there (the old markup appended "…"
  unconditionally, outside the slice — `h1` would have shown as `h1…`);
- a maxLength < 1 is clamped, so a display bug cannot become a page crash.

Applied to every unguarded range-operator slice over a DB-sourced string found
by sweeping the AdminUI for `[..N]`:

  Scripts.razor                    SourceHash    (the reported crash)
  Certificates.razor               Thumbprint    (read off the on-disk store)
  Deployments.razor  x2            RevisionHash
  Clusters/ClusterOverview.razor   RevisionHash

The other `[..N]` hits are safe and untouched: Guid.ToString("N") slices
(always 32 chars) and the `Length > 60 ? [..60]` ternaries that self-guard.

NOTE — the issue text blamed the docker-dev seed; that was wrong and is
corrected on #504. `seed-clusters.sql` inserts only ServerCluster, ClusterNode
and LdapGroupRoleMapping. The short-hash rows came from earlier live-gate
authoring. A freshly seeded rig is fine; the trigger is any Script row that did
not go through ScriptEdit / UnsTreeService.HashSource — REST-API authored,
hand-inserted, or migrated in.

Tests: 14 new in DisplayTextTests, covering the short/null/blank/clamped cases
and a never-throws sweep over every value x budget combination. AdminUI suite
739/739.

Live-verified on docker-dev (AdminUI has no bUnit): /scripts 500 -> 200 across
both central nodes, rendering `hash=h1` and `hash=h-abs-hr200` in full;
/deployments still truncates 64-char hashes at 12 with the ellipsis;
/certificates and /clusters/MAIN unchanged.
2026-07-26 09:43:48 -04:00

136 lines
4.4 KiB
Plaintext

@page "/deployments"
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.Commons.Interfaces
@using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin
@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.ControlPlane.AdminOperations
@attribute [Authorize(Policy = AdminUiPolicies.ConfigEditor)]
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject IAdminOperationsClient AdminOps
@inject AuthenticationStateProvider AuthState
@rendermode InteractiveServer
<PageTitle>Deployments</PageTitle>
<h1>Deployments</h1>
<div class="d-flex align-items-center gap-3 mb-3">
<button class="btn btn-primary" @onclick="StartDeploymentAsync" disabled="@_busy">
@(_busy ? "Deploying…" : "Deploy current configuration")
</button>
@if (_drift is not null)
{
<span class="badge @(_drift.Value ? "bg-warning text-dark" : "bg-success")">
@(_drift.Value ? "Configuration drift" : "In sync")
</span>
}
</div>
@if (_lastMessage is not null)
{
<div class="alert @(_lastSuccess ? "alert-success" : "alert-danger")">
@_lastMessage
</div>
}
<table class="table table-striped table-sm">
<thead>
<tr>
<th>Deployment</th>
<th>Revision</th>
<th>Status</th>
<th>Created by</th>
<th>Created (UTC)</th>
<th>Sealed (UTC)</th>
</tr>
</thead>
<tbody>
@foreach (var d in _deployments)
{
<tr>
<td><code>@Short(d.DeploymentId)</code></td>
<td><code>@DisplayText.Abbreviate(d.RevisionHash, 12)</code></td>
<td>@d.Status</td>
<td>@d.CreatedBy</td>
<td>@d.CreatedAtUtc.ToString("u")</td>
<td>@(d.SealedAtUtc?.ToString("u") ?? "—")</td>
</tr>
}
</tbody>
</table>
@code {
private IReadOnlyList<Deployment> _deployments = Array.Empty<Deployment>();
private bool _busy;
private bool _lastSuccess;
private string? _lastMessage;
private bool? _drift;
protected override async Task OnInitializedAsync()
{
await ReloadAsync();
}
private async Task ReloadAsync()
{
await using var db = await DbFactory.CreateDbContextAsync();
_deployments = await db.Deployments
.AsNoTracking()
.OrderByDescending(d => d.CreatedAtUtc)
.Take(50)
.ToListAsync();
// Drift: if no sealed deployment yet, no drift to report. Otherwise compare the latest
// sealed revision hash to a fresh snapshot of the live-edit state.
var latestSealed = _deployments.FirstOrDefault(d => d.Status == DeploymentStatus.Sealed);
if (latestSealed is null)
{
_drift = null;
return;
}
var current = await ConfigComposer.SnapshotAndFlattenAsync(db);
_drift = !string.Equals(current.RevisionHash, latestSealed.RevisionHash, StringComparison.Ordinal);
}
private async Task StartDeploymentAsync()
{
_busy = true;
_lastMessage = null;
try
{
var auth = await AuthState.GetAuthenticationStateAsync();
var createdBy = auth.User.Identity?.Name ?? "(anonymous)";
var result = await AdminOps.StartDeploymentAsync(
createdBy: createdBy,
ct: CancellationToken.None);
_lastSuccess = result.Outcome == StartDeploymentOutcome.Accepted;
_lastMessage = result.Outcome switch
{
StartDeploymentOutcome.Accepted => $"Deployment {Short(result.DeploymentId!.Value.Value)} dispatched (rev {DisplayText.Abbreviate(result.RevisionHash!.Value.Value, 12)}).",
StartDeploymentOutcome.AnotherDeploymentInFlight => result.Message ?? "Another deployment is already in flight.",
StartDeploymentOutcome.NoChanges => "No changes detected since the last sealed deployment.",
_ => result.Message ?? "Deployment rejected.",
};
await ReloadAsync();
}
catch (Exception ex)
{
_lastSuccess = false;
_lastMessage = $"Deploy failed: {ex.Message}";
}
finally
{
_busy = false;
}
}
private static string Short(Guid id) => id.ToString("N")[..8];
}