Measured on the shipped image (openwaf:ci 08bebf43, the tree with the
storage-failure fix):
DELETE /admin-api/countries/ZZ -> 404 {"error":"no country rule for \"ZZ\""}
DELETE /admin-api/allow/999999 -> 404, Content-Length: 0
Same status, two qualities of answer. The silent half is what the console then
had to invent an explanation for: apiRaw read EVERY 404 as
"admin api disabled — set OpenWaf__Admin__Token" and threw, so removing a row
that another tab had already removed told the operator to go configure a token
that is already configured. And because the throw landed in a catch that says
nothing, the status line ended up reading "refreshed" in the success colour —
measured in the console gate before the fix:
FAIL a missing row is reported as a missing row… — ok: refreshed 4:50:18 AM
After the fix, on the image built from this tree (4823e9ae):
DELETE /admin-api/allow/999999 -> 404 {"error":"no allow entry with id 999999"}
The page now shows that sentence instead of guessing:
ok a missing row is reported as a missing row, not as the admin API being switched off
— warn: no allow entry with id 999999
Whether the admin surface is switched off at all is no longer inferred from a
status code: the core read (/admin-api/state) is checked for a non-200 and the
edge's own body is shown — "admin api disabled: set OpenWaf__Admin__Token",
which the API already says and which a new test pins.
While in this handler, the most destructive button on the page was found doing
worse than lying: "clear the automatic bans" fired the DELETE and read nothing
back, so cleared 7, cleared 0 and a refused write looked identical after the
reload. It now reports the count the edge gave ("engine bans cleared: 0"), and
the note for the action just taken survives the refresh that action itself
triggered — load() used to overwrite it with "refreshed" inside a second, which
is shorter than the time it takes to read.
Gates on this tree (macOS): build 0 warnings / 0 errors, unit 128/128,
integration 109/109 (3 new in NotFoundExplanationTests), smoke 19/0,
compose 43/0, image 261 MB, console 47/47 (2 new assertions).
Dents: allow 404 silent again -> 2 tests fail; apiRaw reading every 404 as
"disabled" -> 2 console assertions fail; clearEngine reading nothing back -> 1;
the action note not held across its own refresh -> 1.
107 lines
5.0 KiB
C#
107 lines
5.0 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// A 404 is an answer, and until now this API gave two different qualities of
|
|
/// answer and the page guessed across them.
|
|
///
|
|
/// Measured on the shipped image (openwaf:ci, 08bebf43):
|
|
/// DELETE /admin-api/countries/ZZ → 404 {"error":"no country rule for \"ZZ\""}
|
|
/// DELETE /admin-api/allow/999999 → 404, Content-Length: 0
|
|
/// Same status, one explained and one silent. The silent half is what the
|
|
/// console then had to invent an explanation for: apiRaw read every 404 as
|
|
/// "admin api disabled — set OpenWaf__Admin__Token" and threw, so removing a row
|
|
/// that another tab had already removed told the operator to configure a token
|
|
/// that is already configured — and because the throw landed in a catch that
|
|
/// said nothing, the status line ended up reading "refreshed" in the success
|
|
/// colour. Measured in the console gate before the fix:
|
|
/// FAIL a missing row is reported as a missing row… — ok: refreshed 4:50:18 AM
|
|
/// </summary>
|
|
public sealed class NotFoundExplanationTests
|
|
{
|
|
private static async Task<(HttpStatusCode Status, string Body)> AdminAsync(
|
|
EdgeFixture f, HttpMethod method, string url, string? json = null)
|
|
{
|
|
using var request = new HttpRequestMessage(method, url);
|
|
if (json is not null)
|
|
request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
|
|
request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken);
|
|
using var response = await f.Client.SendAsync(request);
|
|
return (response.StatusCode, await response.Content.ReadAsStringAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_missing_allow_entry_names_the_id_that_was_not_there()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
|
|
var (status, body) = await AdminAsync(f, HttpMethod.Delete, "/admin-api/allow/999999");
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, status);
|
|
Assert.NotEmpty(body);
|
|
Assert.Equal("no allow entry with id 999999",
|
|
JsonDocument.Parse(body).RootElement.GetProperty("error").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task An_allow_entry_that_is_there_is_removed_with_no_body_and_the_row_goes()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
|
|
var (created, _) = await AdminAsync(f, HttpMethod.Post, "/admin-api/allow",
|
|
"""{"match":"203.0.113.77/32","note":"probe"}""");
|
|
Assert.Equal(HttpStatusCode.NoContent, created);
|
|
|
|
var listed = await AdminAsync(f, HttpMethod.Get, "/admin-api/allow");
|
|
var id = JsonDocument.Parse(listed.Body).RootElement[0].GetProperty("id").GetInt64();
|
|
|
|
var (removed, removedBody) = await AdminAsync(f, HttpMethod.Delete, $"/admin-api/allow/{id}");
|
|
Assert.Equal(HttpStatusCode.NoContent, removed);
|
|
Assert.Equal("", removedBody);
|
|
|
|
var after = await AdminAsync(f, HttpMethod.Get, "/admin-api/allow");
|
|
Assert.Equal("[]", after.Body);
|
|
|
|
// Removing it twice is the stale-tab case, and it names the id.
|
|
var (again, againBody) = await AdminAsync(f, HttpMethod.Delete, $"/admin-api/allow/{id}");
|
|
Assert.Equal(HttpStatusCode.NotFound, again);
|
|
Assert.Contains($"no allow entry with id {id}", againBody);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_closed_admin_surface_says_so_in_the_body_not_only_in_the_status()
|
|
{
|
|
// The console no longer guesses "the admin API is off" from a 404; it
|
|
// shows what the edge said. That is only honest while the edge does say
|
|
// it, so this pins the sentence.
|
|
// "Closed" has to be asked for in both places. Program reads the token
|
|
// from configuration and falls back to OPENWAF_ADMIN_TOKEN when that is
|
|
// blank, and the gate scripts export a throwaway value into the test
|
|
// process — so a blank configured token with the environment set is an
|
|
// OPEN surface (measured: the request came back 401, not 404). Only this
|
|
// fixture leaves its configured token blank, so clearing the variable
|
|
// for the duration of this test cannot change what another one sees.
|
|
var saved = Environment.GetEnvironmentVariable("OPENWAF_ADMIN_TOKEN");
|
|
Environment.SetEnvironmentVariable("OPENWAF_ADMIN_TOKEN", null);
|
|
try
|
|
{
|
|
await using var f = new EdgeFixture(adminToken: "");
|
|
|
|
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);
|
|
var body = await response.Content.ReadAsStringAsync();
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
Assert.Equal("admin api disabled: set OpenWaf__Admin__Token",
|
|
JsonDocument.Parse(body).RootElement.GetProperty("error").GetString());
|
|
}
|
|
finally
|
|
{
|
|
if (saved is not null) Environment.SetEnvironmentVariable("OPENWAF_ADMIN_TOKEN", saved);
|
|
}
|
|
}
|
|
}
|