Adds the plumbing for the fleet-aggregation feature without moving any
data yet. Same portal binary now supports two modes selected via
Application:RunMode (Client | Admin).
Backend
- New AdminDbContext (identity + branding shared via SharedSchemaConfiguration
helper + fleet schema). AppDbContext keeps existing identity + branding +
monitoring + rates; renamed implicitly the "Client" context. Only one is
registered with DI per RunMode.
- IWhiteLabelStore interface implemented by both contexts so BrandingService
works in either mode.
- Fleet entities: Customer, FleetSite, FleetDevice, FleetPowerMeasurement,
IngestEvent (all in the new fleet schema). Migration in Migrations/Admin/.
- CustomerService: 32-byte random token, SHA-256 hash stored, plaintext
shown once on create + rotate. Token lookup is a single O(log N) indexed
query.
- RunModeGuards: refuses Admin without conn string; refuses Client+push
without URL/token; refuses cross-DB pointing (Client at admin_fleet DB
with fleet.Customers, or Admin at customer DB with monitoring.PowerMeasurements).
- Endpoint maps now branch on RunMode:
Client → sites/measurements/rates/admin-sites/admin-rates
Admin → admin/customers
Shared → auth, users, branding, grafana, admin-config, app/info, health
- /api/app/info (anonymous) returns {runMode, applicationName, version} so
the SPA can drive nav without re-fetching auth state.
Frontend
- AppInfoProvider + useAppInfo hook fetch /api/app/info once on load.
- AdminCustomersPage with create / edit / rotate-token / delete.
- TokenShownOnceModal: shows token once, copy-to-clipboard, "I've stored
it" confirmation gate before closing.
- AppLayout nav swaps Sites <-> Customers based on RunMode and shows a
FLEET ADMIN tag in the header when in Admin mode.
Tests
- 11 new tests: CustomerTokenTests (5) + RunModeGuardsTests (6).
- 51/51 passing locally.
Verified
- dotnet build + dotnet test clean (zero errors, one EF1002 warning
suppressed in Phase 11 already).
- Client mode docker rebuild: no regressions, /api/app/info returns
Client, login works, /api/sites/ works.
- Admin mode spun up on port 8090 against a fresh admin_fleet DB:
/api/app/info returns Admin, customer ABC0001 registered, 64-char
token returned, list shows the row.
- Cross-DB guard: Client run against admin_fleet refuses with explicit
"is pointed at a database that contains fleet.Customers" error.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
81 lines
3.3 KiB
C#
81 lines
3.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Tau.Acuvim.Portal.Configuration;
|
|
using Tau.Acuvim.Portal.Data;
|
|
|
|
namespace Tau.Acuvim.Portal.Services;
|
|
|
|
// Startup-time validation that prevents the obvious "pointed dev at prod DB" /
|
|
// "Admin mode against a Client DB" mistakes.
|
|
public static class RunModeGuards
|
|
{
|
|
public static void ValidateConfig(ApplicationOptions app, DatabaseOptions database, FleetIngestOptions fleet)
|
|
{
|
|
if (app.RunMode == RunMode.Admin)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(database.ConnectionString))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"RunMode=Admin requires Database:ConnectionString. " +
|
|
"AutoProvisionLocalTimescaleDb is not honoured for Admin (no obvious local default for a fleet DB).");
|
|
}
|
|
}
|
|
|
|
if (app.RunMode == RunMode.Client && fleet.Enabled)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(fleet.Url) || string.IsNullOrWhiteSpace(fleet.Token))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"FleetIngest:Enabled=true requires both FleetIngest:Url and FleetIngest:Token. " +
|
|
"Disable FleetIngest or provide both via env vars / secrets.");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Refuse to run Client against an Admin DB (fleet.Customers exists) or vice versa.
|
|
// Cheap query — only runs once at startup.
|
|
public static async Task ValidateDatabaseShapeAsync(
|
|
IServiceProvider services,
|
|
RunMode runMode,
|
|
CancellationToken ct = default)
|
|
{
|
|
using var scope = services.CreateScope();
|
|
|
|
if (runMode == RunMode.Client)
|
|
{
|
|
var db = scope.ServiceProvider.GetService<AppDbContext>();
|
|
if (db is null) return;
|
|
try
|
|
{
|
|
var hasFleet = await db.Database
|
|
.SqlQuery<int>($@"SELECT 1 AS ""Value"" FROM information_schema.tables WHERE table_schema='fleet' AND table_name='Customers' LIMIT 1")
|
|
.ToListAsync(ct);
|
|
if (hasFleet.Count > 0)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"RunMode=Client is pointed at a database that contains fleet.Customers (an Admin DB). " +
|
|
"Check Database:ConnectionString.");
|
|
}
|
|
}
|
|
catch (Microsoft.EntityFrameworkCore.DbUpdateException) { /* db not ready, migration runs next */ }
|
|
}
|
|
else
|
|
{
|
|
var db = scope.ServiceProvider.GetService<AdminDbContext>();
|
|
if (db is null) return;
|
|
try
|
|
{
|
|
var hasMonitoring = await db.Database
|
|
.SqlQuery<int>($@"SELECT 1 AS ""Value"" FROM information_schema.tables WHERE table_schema='monitoring' AND table_name='PowerMeasurements' LIMIT 1")
|
|
.ToListAsync(ct);
|
|
if (hasMonitoring.Count > 0)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"RunMode=Admin is pointed at a database that contains monitoring.PowerMeasurements (a Client DB). " +
|
|
"Check Database:ConnectionString.");
|
|
}
|
|
}
|
|
catch (Microsoft.EntityFrameworkCore.DbUpdateException) { /* db not ready */ }
|
|
}
|
|
}
|
|
}
|