feat(admin): #1078 headless POST /api/cluster/failover (key-gated, same service+audit) and unattended SCM-restart gate
fix(install): #1075 worker identity gets read+execute (not Modify) on <PeCacheDir>\pe; re-run replaces the old grant
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.ControlPlane.Redundancy;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Headless trigger for the manual failover the cluster redundancy page's "Trigger failover" button
|
||||
/// performs (lmxopcua#1078), so scripted gates such as
|
||||
/// <c>scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1</c> can run unattended.
|
||||
/// It calls the same <see cref="IManualFailoverService.FailOverDriverPrimaryAsync"/> — peer guard and the
|
||||
/// <c>cluster.manual-failover</c> audit event included — and adds no failover logic of its own.
|
||||
/// <para>
|
||||
/// Auth follows <see cref="DeployApiEndpoints"/>: a configured secret (<c>Security:FailoverApiKey</c>,
|
||||
/// a key SEPARATE from the deploy key so a deploy credential cannot bounce the Primary) checked from
|
||||
/// <c>X-Api-Key</c> in fixed time; the endpoint self-disables (503) until the key is set. The page's
|
||||
/// <c>FleetAdmin</c> policy needs an interactive LDAP login, which an unattended script has no way to
|
||||
/// perform, so holding this key is the Administrator-equivalent grant. The audit actor is
|
||||
/// <c>rest-api</c> (or <c>rest-api:<X-Actor></c> when the optional header is sent).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static class ManualFailoverApiEndpoints
|
||||
{
|
||||
/// <summary>Configuration key holding the failover API secret.</summary>
|
||||
public const string ConfigKey = "Security:FailoverApiKey";
|
||||
|
||||
/// <summary>Optional header naming who/what is triggering (recorded in the audit actor).</summary>
|
||||
public const string ActorHeader = "X-Actor";
|
||||
|
||||
/// <summary>Maps <c>POST /api/cluster/failover</c>. Acts on the Primary of THIS node's own mesh.</summary>
|
||||
/// <param name="app">The endpoint route builder.</param>
|
||||
/// <param name="config">Application configuration (read for the failover API key).</param>
|
||||
/// <returns>The same <paramref name="app"/> for chaining.</returns>
|
||||
public static IEndpointRouteBuilder MapOtOpcUaManualFailoverApi(this IEndpointRouteBuilder app, IConfiguration config)
|
||||
{
|
||||
var configuredKey = config[ConfigKey];
|
||||
|
||||
app.MapPost("/api/cluster/failover", (HttpContext http, IManualFailoverService failover) =>
|
||||
HandleAsync(
|
||||
configuredKey,
|
||||
http.Request.Headers[DeployApiEndpoints.ApiKeyHeader].ToString(),
|
||||
http.Request.Headers[ActorHeader].ToString(),
|
||||
failover))
|
||||
.AllowAnonymous() // gated by the API key, not the cookie auth fallback
|
||||
.DisableAntiforgery() // machine endpoint, not a browser form post
|
||||
.WithName("ManualFailover");
|
||||
return app;
|
||||
}
|
||||
|
||||
/// <summary>The endpoint's decision logic, separated from routing so it is unit-testable.</summary>
|
||||
/// <param name="configuredKey">The configured secret; empty disables the endpoint.</param>
|
||||
/// <param name="providedKey">The caller's <c>X-Api-Key</c>.</param>
|
||||
/// <param name="actorHeader">The caller's optional <c>X-Actor</c>.</param>
|
||||
/// <param name="failover">The manual-failover service the UI button also calls.</param>
|
||||
/// <returns>503 when disabled, 401 on a wrong key, 409 when refused (no peer), else 202 naming the leaving node.</returns>
|
||||
public static async Task<IResult> HandleAsync(
|
||||
string? configuredKey, string? providedKey, string? actorHeader, IManualFailoverService failover)
|
||||
{
|
||||
if (string.IsNullOrEmpty(configuredKey))
|
||||
return Results.Problem(
|
||||
$"Manual failover API is disabled. Set {ConfigKey} to enable it.",
|
||||
statusCode: StatusCodes.Status503ServiceUnavailable);
|
||||
|
||||
if (!DeployApiEndpoints.IsAuthorized(providedKey, configuredKey))
|
||||
return Results.Unauthorized();
|
||||
|
||||
var actor = string.IsNullOrWhiteSpace(actorHeader) ? "rest-api" : $"rest-api:{actorHeader.Trim()}";
|
||||
var target = await failover.FailOverDriverPrimaryAsync(actor);
|
||||
return target is null
|
||||
? Results.Problem("Failover refused: no driver peer is available to take over.",
|
||||
statusCode: StatusCodes.Status409Conflict)
|
||||
: Results.Accepted(value: new FailoverResponse("Accepted", target));
|
||||
}
|
||||
|
||||
/// <summary>Response body for an accepted failover.</summary>
|
||||
/// <param name="Outcome">Always <c>Accepted</c>.</param>
|
||||
/// <param name="LeavingNode">The address of the Primary asked to leave the cluster.</param>
|
||||
public sealed record FailoverResponse(string Outcome, string LeavingNode);
|
||||
}
|
||||
@@ -818,6 +818,8 @@ if (hasAdmin)
|
||||
app.MapOtOpcUaHubs();
|
||||
// Headless deploy trigger for CI/scripts (API-key gated; disabled until Security:DeployApiKey set).
|
||||
app.MapOtOpcUaDeployApi(app.Configuration);
|
||||
// Headless manual failover (#1078; API-key gated; disabled until Security:FailoverApiKey set).
|
||||
app.MapOtOpcUaManualFailoverApi(app.Configuration);
|
||||
}
|
||||
|
||||
// Mesh gRPC ingress: the LocalDb passive sync endpoint, the ConfigServe artifact + central
|
||||
|
||||
Reference in New Issue
Block a user