The alerting pillar ended at a table. The rows are right — the server-error
streak fires, the dry-run candidate is recorded — and then they wait for an
operator to open a page. That is the posture this project already removed for
blocking ("blocking is usually manual: a human notices abuse and adds an IP.
That never fires at 3am"), applied one level up: an alert nobody is told about
never fires at 3am either.
OpenWaf:Alerts:WebhookUrl (env fallback OPENWAF_ALERT_WEBHOOK_URL) POSTs each
raised alert as JSON — kind, sourceKey, count, lastStatusCode, lastPath,
createdAt, detail — off the request path, from a bounded queue of 1000, and only
after the row is durably written. Three attempts with a short backoff;
OpenWaf:Alerts:WebhookTimeoutSeconds (1-60, default 5) refuses startup outside
that range rather than falling back, as the other startup-parsed bounds do. The
default is empty, which is no push at all: the shipped stack behaves exactly as
it did before.
The endpoint is counted like every other write side pillar, because a webhook
that has been down for a week is otherwise indistinguishable from a quiet night:
alertWebhookFailures, alertWebhookLost, alertWebhookDelivered,
alertWebhookLastError, alertWebhookLastSuccessAt, plus alertWebhookConfigured and
alertWebhookHost — host, never the URL, since a webhook URL carries its secret
in the path and this is rendered on the operator page. The console's alert line
now ends in "pushed to <host>, N delivered" or "not pushed anywhere: each row
waits for someone to open this page", so "no alerts" and "alerts go nowhere" stay
two different sentences.
Two things the tests caught instead of the design assuming:
* the queue was BoundedChannelFullMode.DropWrite, where TryWrite returns true
and the overflow vanishes with no trace — the counter it was written to feed
stayed at zero. The mode is Wait now, chosen for what TryWrite reports when
the queue is full; nothing here awaits a write, so the response path still
cannot block.
* a refused push counted as an entry lost, which conflated a lost delivery with
a lost record. The alert row is in the table either way, so EntriesLost on
this pillar is reserved for the one case where the push is gone for good.
Measured on this tree: build 0 warnings / 0 errors, unit 151/151 (19 new),
integration 194/194 in 28 s (3 new: the push arrives with the alert in it, an
unconfigured edge sends nothing and keeps every row, an endpoint answering 500 is
counted and does not undo the alert), smoke 29/0, console 79 assertions 0 failed,
compose 209/0 against the built image at 261 MB.
90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using System.Net;
|
|
using System.Net.Sockets;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// The application behind the edge. It is deliberately dumb: it answers a
|
|
/// configured status per path and counts what it was asked. The whole point of
|
|
/// the feature is the edge reacting to what the upstream answered, so the test
|
|
/// needs to control that answer and observe whether the edge noticed.
|
|
/// </summary>
|
|
internal sealed class FakeUpstream : IAsyncDisposable
|
|
{
|
|
private readonly HttpListener _listener = new();
|
|
private readonly CancellationTokenSource _cts = new();
|
|
|
|
public string Address { get; }
|
|
public int RequestCount;
|
|
public List<string> Paths = [];
|
|
|
|
/// <summary>What the upstream was asked to hold, per request that sent a
|
|
/// body. The edge's alert push POSTs one, and the whole point of that
|
|
/// feature is what arrives at the endpoint — a path alone cannot show it.
|
|
/// Recorded only when a body was sent, so the proxy traffic that dominates
|
|
/// a run stays an empty list.</summary>
|
|
public List<string> Bodies = [];
|
|
|
|
/// <summary>What this upstream answers for a path.</summary>
|
|
public Func<string, int> StatusFor { get; set; } = _ => StatusCodes.Status404NotFound;
|
|
|
|
public static FakeUpstream Start()
|
|
{
|
|
var port = FreePort();
|
|
var upstream = new FakeUpstream($"http://127.0.0.1:{port}/");
|
|
upstream._listener.Start();
|
|
_ = upstream.LoopAsync();
|
|
return upstream;
|
|
}
|
|
|
|
private FakeUpstream(string address)
|
|
{
|
|
Address = address;
|
|
_listener.Prefixes.Add(address);
|
|
}
|
|
|
|
private async Task LoopAsync()
|
|
{
|
|
try
|
|
{
|
|
while (!_cts.IsCancellationRequested)
|
|
{
|
|
var context = await _listener.GetContextAsync();
|
|
var path = context.Request.Url?.AbsolutePath ?? "/";
|
|
Interlocked.Increment(ref RequestCount);
|
|
lock (Paths) Paths.Add(path);
|
|
if (context.Request.HasEntityBody)
|
|
{
|
|
using var reader = new System.IO.StreamReader(context.Request.InputStream);
|
|
var body = reader.ReadToEnd();
|
|
lock (Bodies) Bodies.Add(body);
|
|
}
|
|
|
|
context.Response.StatusCode = StatusFor(path);
|
|
context.Response.OutputStream.Close();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Listener closed at teardown — the expected way out.
|
|
}
|
|
}
|
|
|
|
private static int FreePort()
|
|
{
|
|
var probe = new TcpListener(IPAddress.Loopback, 0);
|
|
probe.Start();
|
|
var port = ((IPEndPoint)probe.LocalEndpoint).Port;
|
|
probe.Stop();
|
|
return port;
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
_cts.Cancel();
|
|
_listener.Close();
|
|
await Task.CompletedTask;
|
|
}
|
|
}
|