GET /admin-api/expiring?within=<minutes> (1..1440, 30 by default) lists the dated
entries that stop being enforced inside the window, nearest first, and each
address-shaped row carries thenAnswers: the real DecisionEngine.Decide over a
snapshot with only that entry removed. An exemption about to lapse therefore says
which ban is waiting underneath it ("block_operator by 203.0.113.16/28"), and a
block that stands alone says the source goes free. A path rule and a path budget
have no single answer: thenAnswers is null and whyNoAnswer points at
/admin-api/decide?ip=&path= instead of the surface guessing one. Entries that
never expire are not listed; alreadyExpired counts the dated rows whose moment
already passed, which is what separates "nothing expires soon" from "there are no
dated entries". The listing reads the database, the read-only token can ask it,
and six asks write nothing.
Rulings:
- the probe address for a CIDR is its network address, which is inside the group
it describes; a pattern is not probed at all.
- the window is capped at a day because that is the longest thing an operator
plans for; out of bounds is a 400 naming 1..1440.
- the console prints the window it asked for as the first line of the answer and
empties the box before the call, so neither a scene nor an operator on a slow
link can read the previous answer as the new one.
Two product defects fell out of the gates on the way:
- the alert push worker was registered as a hosted service above the schema step,
and hosted services start in registration order: on a cold start its first
queue sweep read alert_push_queue out of a database this process had not
created, and "SQLite Error 1: no such table" became alertWebhookLastError -
the state page blaming the webhook endpoint for the edge's own startup order.
The notifier is now registered below the warmer and the order is asserted.
- a listing the edge could not answer was drawn as an empty one: with two of the
page's fourteen calls answering an error, the observation line printed "0
sources the edge has recorded" and the streak line "0 of 0 standing streaks
listed" over a map holding twelve sources. A count read off a failed answer is
0, and 0 says there is nothing to see. Every count-bearing note and its table
now say NOT READ with the error beside them.
The gate's own traps, caught and fixed: the budget scene cleared the engine ban
its probes caused after the probe that ban was failing, and its reopen retry loop
banned itself three refusals deep; the alerts assertion read a row written off the
response before the next refresh drew it.
gates: build 0 warnings / 0 errors, unit 377/377, integration 375/375 in 35 s,
console 142 assertions 0 failed, smoke passed 217 failed 0, compose 324 passed
0 failed against the built image at 262 MB, 0 containers left behind.
Refs OPEN-72.
78 lines
3.7 KiB
C#
78 lines
3.7 KiB
C#
using System.Net.Http;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using OpenWaf.Edge.Startup;
|
|
using OpenWaf.Infrastructure.Persistence;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// Startup order, measured rather than intended.
|
|
///
|
|
/// Hosted services are started in registration order, and the schema step is one
|
|
/// of them. The alert push queue's worker was registered above it, so on a cold
|
|
/// start its first queue sweep read <c>alert_push_queue</c> out of a database this
|
|
/// same process had not created yet. Nothing crashed — the worker counts what it
|
|
/// cannot read — but the sentence it left behind was the operator's answer:
|
|
/// <c>alertWebhookLastError</c> carried "SQLite Error 1: 'no such table:
|
|
/// alert_push_queue'", and the state page blamed the webhook endpoint for the
|
|
/// edge's own startup order. A failure that names the wrong component is worse
|
|
/// than no failure, because it sends the operator to fix the wrong thing.
|
|
/// </summary>
|
|
public sealed class StartupOrderTests
|
|
{
|
|
private static async Task<JsonElement> StateAsync(EdgeFixture f)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, "/admin-api/state");
|
|
request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken);
|
|
using var response = await f.Client.SendAsync(request);
|
|
response.EnsureSuccessStatusCode();
|
|
return JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement.Clone();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_schema_step_is_registered_before_every_worker_that_touches_its_tables()
|
|
{
|
|
await using var f = new EdgeFixture(alertWebhookPath: "/hook");
|
|
var hosts = f.Factory.Services.GetServices<IHostedService>().ToList();
|
|
|
|
int IndexOf<T>() => hosts.FindIndex(h => h is T);
|
|
var warmer = IndexOf<EdgeStateWarmer>();
|
|
Assert.True(warmer >= 0, "the schema step is not a hosted service of this edge");
|
|
|
|
// Every one of these reads or writes a table the warmer creates. The
|
|
// assertion is on the order, not on the count: a worker added tomorrow
|
|
// above the warmer reproduces the defect this test exists for.
|
|
foreach (var (name, index) in new (string, int)[]
|
|
{
|
|
("AlertNotifier", IndexOf<AlertNotifier>()),
|
|
("ObservationWriter", IndexOf<ObservationWriter>()),
|
|
("RetentionWorker", IndexOf<RetentionWorker>()),
|
|
("NetblockWatcher", IndexOf<NetblockWatcher>()),
|
|
("PendingBanRetryWorker", IndexOf<PendingBanRetryWorker>())
|
|
})
|
|
{
|
|
Assert.True(index >= 0, $"{name} is not a hosted service of this edge");
|
|
Assert.True(warmer < index,
|
|
$"{name} starts at {index}, before the schema step at {warmer}: "
|
|
+ $"its first read can hit a table this process has not created. Hosts: "
|
|
+ string.Join(", ", hosts.Select(h => h.GetType().Name)));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_cold_start_with_a_webhook_configured_blames_nobody_for_the_edge_own_startup()
|
|
{
|
|
await using var f = new EdgeFixture(alertWebhookPath: "/hook");
|
|
// The queue sweep runs the moment the worker starts. Give it the moment
|
|
// it would have taken to fail, then ask what the edge thinks went wrong:
|
|
// an empty queue is not a failure, and a missing table is.
|
|
await Task.Delay(500);
|
|
var state = await StateAsync(f);
|
|
Assert.Equal(0, state.GetProperty("alertWebhookFailures").GetInt32());
|
|
Assert.Equal(JsonValueKind.Null, state.GetProperty("alertWebhookLastError").ValueKind);
|
|
Assert.Equal(0, state.GetProperty("alertPushPending").GetInt32());
|
|
}
|
|
}
|