Restructure solution into folders and fix persistence issues
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37987a394a
commit
7c39cb5562
62 files changed
+219
-151
No files matched your search
@@ -0,0 +1,144 @@
|
||||
using Flypaper.Core;
|
||||
using Flypaper.Core.Settings;
|
||||
using Flypaper.Core.Strategies;
|
||||
using Flypaper.Http;
|
||||
using Flypaper.Http.Matching;
|
||||
using Flypaper.Storage;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Flypaper.Storage.Settings;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.Storage;
|
||||
|
||||
public sealed class SettingsOverrideTests : IDisposable
|
||||
{
|
||||
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-test-{Guid.NewGuid():N}.db");
|
||||
private IHost? _host;
|
||||
|
||||
[Fact]
|
||||
public async Task Db_overrides_startup_values_for_whitelisted_keys_only()
|
||||
{
|
||||
await using (var db = new FlypaperDbContext(new DbContextOptionsBuilder<FlypaperDbContext>().UseSqlite($"Data Source={_dbPath}").Options))
|
||||
{
|
||||
await db.Database.MigrateAsync();
|
||||
db.Settings.AddRange(
|
||||
new SettingEntity { Key = SettingKeys.MaxConnections, Value = "5" },
|
||||
new SettingEntity { Key = SettingKeys.TrapPaths, Value = "/custom\n/other/*" },
|
||||
new SettingEntity { Key = SettingKeys.ExtraKnownHosts, Value = "extra.example" },
|
||||
new SettingEntity { Key = "Flypaper:Admin:Password", Value = "evil" });
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
var host = BuildHost();
|
||||
await host.Services.MigrateFlypaperDatabaseAsync();
|
||||
|
||||
var core = host.Services.GetRequiredService<IOptionsMonitor<FlypaperOptions>>().CurrentValue;
|
||||
var http = host.Services.GetRequiredService<IOptionsMonitor<HttpTrapOptions>>().CurrentValue;
|
||||
var configuration = host.Services.GetRequiredService<IConfiguration>();
|
||||
|
||||
core.MaxConnections.ShouldBe(5);
|
||||
core.MaxConnectionsPerIp.ShouldBe(10);
|
||||
http.TrapPaths.ShouldBe(["/custom", "/other/*"]);
|
||||
http.KnownHosts.ShouldBe(["dashboard.lan", "extra.example"]);
|
||||
http.TrustedProxies.ShouldBe(["172.18.0.0/16"]);
|
||||
configuration["Flypaper:Admin:Password"].ShouldBe("secret");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Missing_db_overrides_leave_startup_values_untouched()
|
||||
{
|
||||
var host = BuildHost();
|
||||
await host.Services.MigrateFlypaperDatabaseAsync();
|
||||
|
||||
host.Services.GetRequiredService<IOptionsMonitor<FlypaperOptions>>().CurrentValue.MaxConnections.ShouldBe(100);
|
||||
host.Services.GetRequiredService<IOptionsMonitor<HttpTrapOptions>>().CurrentValue.TrapPaths.ShouldBe(["/.env"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Saving_reloads_trap_matcher_and_reset_restores_startup_values()
|
||||
{
|
||||
var host = BuildHost();
|
||||
await host.Services.MigrateFlypaperDatabaseAsync();
|
||||
var matcher = host.Services.GetRequiredService<TrapMatcher>();
|
||||
var store = host.Services.GetRequiredService<RuntimeSettingsStore>();
|
||||
|
||||
matcher.Match("dashboard.lan", "/custom").ShouldBe(TrapMatch.None);
|
||||
matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.TrapPath);
|
||||
matcher.Match("extra.example", "/").ShouldBe(TrapMatch.UnknownHost);
|
||||
|
||||
await store.SaveAsync(new RuntimeSettings(
|
||||
["/custom"],
|
||||
TimeSpan.FromSeconds(3),
|
||||
50,
|
||||
4,
|
||||
TimeSpan.FromMinutes(10),
|
||||
["extra.example"]));
|
||||
|
||||
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();
|
||||
|
||||
matcher.Match("dashboard.lan", "/custom").ShouldBe(TrapMatch.None);
|
||||
matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.TrapPath);
|
||||
(await store.HasOverridesAsync()).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Saved_empty_trap_path_list_disables_path_traps()
|
||||
{
|
||||
var host = BuildHost();
|
||||
await host.Services.MigrateFlypaperDatabaseAsync();
|
||||
var matcher = host.Services.GetRequiredService<TrapMatcher>();
|
||||
|
||||
await host.Services.GetRequiredService<RuntimeSettingsStore>().SaveAsync(new RuntimeSettings(
|
||||
[],
|
||||
TimeSpan.FromSeconds(3),
|
||||
50,
|
||||
4,
|
||||
TimeSpan.FromMinutes(10),
|
||||
[]));
|
||||
|
||||
matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.None);
|
||||
}
|
||||
|
||||
private IHost BuildHost()
|
||||
{
|
||||
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { DisableDefaults = true });
|
||||
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["ConnectionStrings:Flypaper"] = $"Data Source={_dbPath}",
|
||||
["Flypaper:MaxConnections"] = "100",
|
||||
["Flypaper:MaxConnectionsPerIp"] = "10",
|
||||
["Flypaper:Http:TrapPaths:0"] = "/.env",
|
||||
["Flypaper:Http:KnownHosts:0"] = "dashboard.lan",
|
||||
["Flypaper:Http:TrustedProxies:0"] = "172.18.0.0/16",
|
||||
["Flypaper:Admin:Password"] = "secret"
|
||||
});
|
||||
builder.AddFlypaperStorage();
|
||||
builder.Services.AddLogging();
|
||||
builder.Services.AddFlypaperCore(builder.Configuration);
|
||||
builder.Services.AddFlypaperHttp(builder.Configuration);
|
||||
|
||||
return _host = builder.Build();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_host?.Dispose();
|
||||
SqliteConnection.ClearAllPools();
|
||||
foreach (var suffix in new[] { "", "-wal", "-shm" })
|
||||
{
|
||||
File.Delete(_dbPath + suffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user