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:
Joseph Doherty
2026-12-03 16:29:27 -05:00
parent fe4bbbe256
commit 762bcd7b8d
10 changed files with 229 additions and 12 deletions
@@ -0,0 +1,100 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Api;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Redundancy;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Api;
/// <summary>lmxopcua#1078: the headless failover trigger is key-gated and dispatches the same service call as the UI button.</summary>
[Trait("Category", "Unit")]
public sealed class ManualFailoverApiEndpointsTests
{
private sealed class FakeFailover(string? target) : IManualFailoverService
{
public List<string> Actors { get; } = [];
public ManualFailoverSnapshot GetSnapshot() => new(null, []);
public Task<string?> FailOverDriverPrimaryAsync(string actor) { Actors.Add(actor); return Task.FromResult(target); }
}
[Fact]
public async Task Unconfigured_key_disables_the_endpoint_and_never_dispatches()
{
var fake = new FakeFailover("akka://n1");
var r = await ManualFailoverApiEndpoints.HandleAsync(null, "anything", null, fake);
r.ShouldBeOfType<ProblemHttpResult>().StatusCode.ShouldBe(503);
fake.Actors.ShouldBeEmpty();
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("wrong")]
public async Task Missing_or_wrong_key_is_unauthorized_and_never_dispatches(string? provided)
{
var fake = new FakeFailover("akka://n1");
var r = await ManualFailoverApiEndpoints.HandleAsync("secret", provided, null, fake);
r.ShouldBeOfType<UnauthorizedHttpResult>();
fake.Actors.ShouldBeEmpty();
}
[Fact]
public async Task The_deploy_key_is_not_a_failover_credential()
{
// Distinct config keys: a deploy credential must not bounce the Primary.
ManualFailoverApiEndpoints.ConfigKey.ShouldNotBe(DeployApiEndpoints.ConfigKey);
var fake = new FakeFailover("akka://n1");
var r = await ManualFailoverApiEndpoints.HandleAsync("failover-key", "deploy-key", null, fake);
r.ShouldBeOfType<UnauthorizedHttpResult>();
fake.Actors.ShouldBeEmpty();
}
[Fact]
public async Task Valid_key_dispatches_the_service_call_once_and_names_the_leaving_node()
{
var fake = new FakeFailover("akka.tcp://sys@host:4053");
var r = await ManualFailoverApiEndpoints.HandleAsync("secret", "secret", null, fake);
var accepted = r.ShouldBeOfType<Accepted<ManualFailoverApiEndpoints.FailoverResponse>>();
accepted.Value!.LeavingNode.ShouldBe("akka.tcp://sys@host:4053");
fake.Actors.ShouldBe(["rest-api"]);
}
[Fact]
public async Task Actor_header_is_recorded_for_attribution()
{
var fake = new FakeFailover("akka://n1");
await ManualFailoverApiEndpoints.HandleAsync("secret", "secret", " gate-run ", fake);
fake.Actors.ShouldBe(["rest-api:gate-run"]);
}
[Fact]
public async Task A_refused_failover_is_409_not_success()
{
var fake = new FakeFailover(null);
var r = await ManualFailoverApiEndpoints.HandleAsync("secret", "secret", null, fake);
r.ShouldBeOfType<ProblemHttpResult>().StatusCode.ShouldBe(409);
}
[Fact]
public void Route_is_post_only_and_gated_in_the_handler_not_by_the_cookie_policy()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddSingleton<IManualFailoverService>(new FakeFailover(null));
var app = builder.Build();
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?> { [ManualFailoverApiEndpoints.ConfigKey] = "k" }).Build();
app.MapOtOpcUaManualFailoverApi(config);
var e = ((IEndpointRouteBuilder)app).DataSources.SelectMany(d => d.Endpoints).OfType<RouteEndpoint>()
.Single(x => x.Metadata.GetMetadata<IEndpointNameMetadata>()?.EndpointName == "ManualFailover");
e.RoutePattern.RawText.ShouldBe("/api/cluster/failover");
e.Metadata.GetMetadata<IHttpMethodMetadata>()!.HttpMethods.ShouldBe(["POST"]);
e.Metadata.GetMetadata<IAllowAnonymous>().ShouldNotBeNull();
}
}