Add data management, randomized HTML drip and public README
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3671019e60
commit
f5d8ca7484
46 files changed
+1601
-172
No files matched your search
@@ -0,0 +1,36 @@
|
||||
using Flypaper.Storage;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class MigrationTests : IDisposable
|
||||
{
|
||||
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-migration-{Guid.NewGuid():N}.db");
|
||||
|
||||
[Fact]
|
||||
public async Task Removes_obsolete_drip_interval_override_from_existing_databases()
|
||||
{
|
||||
await using var db = new FlypaperDbContext(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite($"Data Source={_dbPath}").Options);
|
||||
var migrator = db.GetService<IMigrator>();
|
||||
await migrator.MigrateAsync("AddOrigin");
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"INSERT INTO \"Settings\" (\"Key\", \"Value\") VALUES ('Flypaper:Drip:Interval', '00:00:10'), ('Flypaper:MaxConnections', '50')");
|
||||
|
||||
await db.Database.MigrateAsync();
|
||||
|
||||
(await db.Settings.Select(s => s.Key).ToListAsync()).ShouldBe(["Flypaper:MaxConnections"]);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SqliteConnection.ClearAllPools();
|
||||
foreach (var suffix in new[] { "", "-wal", "-shm" })
|
||||
{
|
||||
File.Delete(_dbPath + suffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Settings;
|
||||
using Flypaper.Core.Strategies;
|
||||
using Flypaper.Http;
|
||||
using Flypaper.Http.Matching;
|
||||
using Flypaper.Storage;
|
||||
@@ -74,16 +73,16 @@ public sealed class SettingsOverrideTests : IDisposable
|
||||
|
||||
await store.SaveAsync(new RuntimeSettings(
|
||||
["/custom"],
|
||||
TimeSpan.FromSeconds(3),
|
||||
50,
|
||||
4,
|
||||
TimeSpan.FromMinutes(10),
|
||||
["extra.example"]));
|
||||
["extra.example"],
|
||||
90,
|
||||
true));
|
||||
|
||||
matcher.Match("dashboard.lan", "/custom").ShouldBe(TrapMatch.TrapPath);
|
||||
matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.None);
|
||||
matcher.Match("extra.example", "/").ShouldBe(TrapMatch.None);
|
||||
host.Services.GetRequiredService<IOptionsMonitor<DripOptions>>().CurrentValue.Interval.ShouldBe(TimeSpan.FromSeconds(3));
|
||||
(await store.HasOverridesAsync()).ShouldBeTrue();
|
||||
|
||||
await store.ResetAsync();
|
||||
@@ -102,11 +101,12 @@ public sealed class SettingsOverrideTests : IDisposable
|
||||
|
||||
await host.Services.GetRequiredService<RuntimeSettingsStore>().SaveAsync(new RuntimeSettings(
|
||||
[],
|
||||
TimeSpan.FromSeconds(3),
|
||||
50,
|
||||
4,
|
||||
TimeSpan.FromMinutes(10),
|
||||
[]));
|
||||
[],
|
||||
90,
|
||||
true));
|
||||
|
||||
matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.None);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Storage;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Storage.Maintenance;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class TrapDataMaintenanceTests : IDisposable
|
||||
{
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 14, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-maintenance-{Guid.NewGuid():N}.db");
|
||||
private readonly IDbContextFactory<FlypaperDbContext> _dbContextFactory;
|
||||
private readonly TrapDataMaintenance _maintenance;
|
||||
|
||||
public TrapDataMaintenanceTests()
|
||||
{
|
||||
_dbContextFactory = new TestDbContextFactory($"Data Source={_dbPath}");
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
db.Database.Migrate();
|
||||
_maintenance = new TrapDataMaintenance(_dbContextFactory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Deletes_only_events_from_private_networks()
|
||||
{
|
||||
await SeedAsync(
|
||||
("203.0.113.9", 1),
|
||||
("192.168.1.20", 1),
|
||||
("172.19.0.1", 1),
|
||||
("127.0.0.1", 1),
|
||||
("fd00::1", 1),
|
||||
("fe80::1", 1),
|
||||
("172.32.0.1", 1),
|
||||
("8.8.8.8", 1));
|
||||
|
||||
(await _maintenance.DeletePrivateAsync()).ShouldBe(5);
|
||||
|
||||
(await RemainingIpsAsync()).ShouldBe(["203.0.113.9", "172.32.0.1", "8.8.8.8"], ignoreOrder: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Deletes_all_events_but_keeps_settings()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 1), ("8.8.8.8", 2));
|
||||
await using (var db = await _dbContextFactory.CreateDbContextAsync())
|
||||
{
|
||||
db.Settings.Add(new SettingEntity { Key = "Flypaper:MaxConnections", Value = "5" });
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
(await _maintenance.DeleteAllAsync()).ShouldBe(2);
|
||||
await _maintenance.VacuumAsync();
|
||||
|
||||
await using var check = await _dbContextFactory.CreateDbContextAsync();
|
||||
(await check.TrapEvents.CountAsync()).ShouldBe(0);
|
||||
(await check.Settings.CountAsync()).ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reports_count_oldest_event_and_file_size()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 10), ("8.8.8.8", 3));
|
||||
|
||||
var stats = await _maintenance.GetStatsAsync();
|
||||
|
||||
stats.Count.ShouldBe(2);
|
||||
stats.Oldest.ShouldBe(Now.UtcDateTime.AddDays(-10));
|
||||
stats.DatabaseBytes.ShouldNotBeNull().ShouldBeGreaterThan(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Retention_deletes_events_older_than_configured_days()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 100), ("203.0.113.10", 91), ("203.0.113.11", 89), ("203.0.113.12", 1));
|
||||
|
||||
(await CreateCleanup(days: 90).CleanupAsync(CancellationToken.None)).ShouldBe(2);
|
||||
|
||||
(await RemainingIpsAsync()).ShouldBe(["203.0.113.11", "203.0.113.12"], ignoreOrder: true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Retention_of_zero_days_keeps_everything()
|
||||
{
|
||||
await SeedAsync(("203.0.113.9", 4000));
|
||||
|
||||
(await CreateCleanup(days: 0).CleanupAsync(CancellationToken.None)).ShouldBe(0);
|
||||
|
||||
(await RemainingIpsAsync()).ShouldBe(["203.0.113.9"]);
|
||||
}
|
||||
|
||||
private RetentionCleanup CreateCleanup(int days) => new(
|
||||
_maintenance,
|
||||
TestOptions.Monitor(new RetentionOptions { Days = days }),
|
||||
new FixedTimeProvider(Now),
|
||||
NullLogger<RetentionCleanup>.Instance);
|
||||
|
||||
private async Task SeedAsync(params (string Ip, int DaysAgo)[] events)
|
||||
{
|
||||
await using var db = await _dbContextFactory.CreateDbContextAsync();
|
||||
foreach (var (ip, daysAgo) in events)
|
||||
{
|
||||
var started = Now.AddDays(-daysAgo);
|
||||
db.TrapEvents.Add(TrapEventEntity.From(new TrapEvent(
|
||||
Guid.NewGuid(),
|
||||
TrapProtocol.Http,
|
||||
IPAddress.Parse(ip),
|
||||
"drip",
|
||||
started,
|
||||
started.AddSeconds(5),
|
||||
5,
|
||||
TrapOutcome.ClientAborted)));
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private async Task<List<string>> RemainingIpsAsync()
|
||||
{
|
||||
await using var db = await _dbContextFactory.CreateDbContextAsync();
|
||||
return await db.TrapEvents.Select(e => e.RemoteIp).ToListAsync();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SqliteConnection.ClearAllPools();
|
||||
foreach (var suffix in new[] { "", "-wal", "-shm" })
|
||||
{
|
||||
File.Delete(_dbPath + suffix);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TestDbContextFactory(string connectionString) : IDbContextFactory<FlypaperDbContext>
|
||||
{
|
||||
public FlypaperDbContext CreateDbContext() =>
|
||||
new(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite(connectionString).Options);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user