feat(transport): fleet-wide bundle-unlock throttle window (register row 52)

The per-IP unlock throttle's counters were a process-local ConcurrentDictionary
on each central node, so the effective fleet cap was N x
MaxUnlockAttemptsPerIpPerHour per client (failing over to the other node handed
the client a fresh budget) and a node restart cleared the window outright.

The window now lives in the central MS SQL BundleUnlockAttempt table — the HA
store both central nodes already share, so no new infrastructure, no new port,
no consensus protocol:

- Commons: BundleUnlockAttempt entity + IBundleUnlockAttemptRepository.
- ConfigurationDatabase: entity config (one index,
  IX_BundleUnlockAttempt_Client_Attempted, serving both the windowed count and
  the prune), repository (AsNoTracking count, ExecuteDeleteAsync prune),
  DbSet, DI registration, migration AddBundleUnlockAttemptTable + the
  hand-written idempotent production script under docs/plans/sql/.
- Transport: IBundleUnlockAttemptWindow seam + SqlBundleUnlockAttemptWindow
  (singleton; resolves the scoped repository from its own scope per call, like
  the central purge singletons). BundleUnlockRateLimiter keeps the policy and
  gains TryRegisterAttemptAsync; BundleImporter.LoadAsync calls it.

Behaviour preserved from row 44: locked bundles spend no hourly budget (the
lockout check still runs first), throttle denials burn no per-bundle strike and
are NOT recorded so they cannot roll the window forward, unattributable clients
share the stricter <unattributed> bucket, and the budget counts attempts rather
than failures. Config keys and their semantics are unchanged, so the rig's 500
override stays valid.

Three deliberate trades, documented in both Transport docs:
- FAILS OPEN. A store error logs a Warning naming the client and cap, and
  permits the attempt. This is a best-effort rate cap over an already
  authenticated, Administrator-gated surface — the availability of legitimate
  imports outranks the cap, and a DB blip must not make the import wizard
  unusable. Falling back to the process-local window was considered and
  rejected: it would still block a legitimate operator's 11th import.
- Approximate. Count-then-insert is not atomic across nodes, so simultaneous
  attempts can overshoot by the number in flight — bounded, self-correcting
  within the window, and not worth provider-specific raw SQL.
- Pruned opportunistically on the unlock path (CAS-claimed at most every 15
  min per process, bounded to 1000 rows, retaining 2x the window, best-effort
  try/catch) rather than by a new background service — Transport owns no
  central scheduler. Same shape as DeploymentService's terminal-record sweep.

Tests: Transport.IntegrationTests 116 passed (+10 — two limiter instances over
one store enforcing a single combined window, restart durability, denials
burning no budget, sliding release, bucket isolation, store-failure fail-open,
caller-cancellation not swallowed, no-shared-window fallback, prune and its
negative control); ConfigurationDatabase 395 passed (+5).
This commit is contained in:
Joseph Doherty
2026-08-15 11:52:34 -04:00
parent 372ea4df1b
commit 677263af57
21 changed files with 3157 additions and 30 deletions
+5 -3
View File
@@ -7,7 +7,7 @@ The Configuration Database component is the exclusive EF Core data-access layer
The component lives in `src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/` and is central-only — site clusters never load it. Its responsibilities break down into four areas:
- **DbContext + Fluent mappings** — `ScadaBridgeDbContext` maps ~30 Commons POCO entity types to SQL Server using `IEntityTypeConfiguration<T>` classes in `Configurations/`, registered wholesale via `modelBuilder.ApplyConfigurationsFromAssembly(...)`.
- **Repository implementations** — eleven scoped repositories implement the interfaces declared in Commons, covering every domain area from template authoring to audit log ingest.
- **Repository implementations** — scoped repositories implement the interfaces declared in Commons, covering every domain area from template authoring to audit log ingest.
- **Config-change audit** — `AuditService` implements `IAuditService`, staging an `AuditLogEntry` into the change tracker so it commits atomically with the entity change; `AuditCorrelationContext` threads a `BundleImportId` through `AsyncLocal<T>` so bundle-import audit rows are correlated without cross-contaminating concurrent import sessions.
- **Partition maintenance** — `AuditLogPartitionMaintenance` implements `IPartitionMaintenance`, rolling `pf_AuditLog_Month` forward by issuing `ALTER PARTITION FUNCTION … SPLIT RANGE` for each missing future monthly boundary.
@@ -91,9 +91,11 @@ The predecessor `UX_AuditLog_EventId` — a single-column unique index on `[PRIM
**`SiteCallEntityTypeConfiguration`** maps `SiteCall` to `dbo.SiteCalls` with a `TrackedOperationId` PK stored as `varchar(36)` (GUID in `"D"` format) so the column shape matches the wire format and the site SQLite store — one consistent format for operational debugging.
**`BundleUnlockAttemptEntityTypeConfiguration`** maps `BundleUnlockAttempt` to the table of the same name — the fleet-wide sliding window behind Transport's per-client bundle-unlock throttle (register row 52; the counters used to be per-node and per-process, so the effective cap was N × the configured limit). Operational history, not audit: non-partitioned, `[PRIMARY]` filegroup, no DB-role restriction. One named index, `IX_BundleUnlockAttempt_Client_Attempted`, serves both the windowed count and the application's opportunistic prune, which is bounded and best-effort — a store failure in this path is deliberately fail-open at the Transport layer, so nothing here may block an unlock. The table deliberately records no bundle identity; the durable record of a failed unlock is the `BundleImportUnlockFailed` audit row.
### Repository implementations
All eleven repositories follow the same shape: they take `ScadaBridgeDbContext` by constructor injection, work with Commons POCO types, and never commit — callers invoke `SaveChangesAsync()` to commit the unit of work.
All repositories follow the same shape: they take `ScadaBridgeDbContext` by constructor injection, work with Commons POCO types, and never commit — callers invoke `SaveChangesAsync()` to commit the unit of work.
**`AuditLogRepository`** is the most specialized. Its `InsertIfNotExistsAsync` bypasses the change tracker and issues raw interpolated SQL because the computed columns must not appear in the INSERT column list:
@@ -152,7 +154,7 @@ services.AddConfigurationDatabase(
configuration["ScadaBridge:Database:ConfigurationDb"]!);
```
This registers `ScadaBridgeDbContext` as scoped (with the runtime encryption overload), all eleven repository interfaces bound to their implementations, `IAuditCorrelationContext` → `AuditCorrelationContext`, `IAuditService` → `AuditService`, `IInstanceLocator` → `InstanceLocator`, `IPartitionMaintenance` → `AuditLogPartitionMaintenance`, and the Data Protection key ring persisted to the database.
This registers `ScadaBridgeDbContext` as scoped (with the runtime encryption overload), every repository interface bound to its implementation, `IAuditCorrelationContext` → `AuditCorrelationContext`, `IAuditService` → `AuditService`, `IInstanceLocator` → `InstanceLocator`, `IPartitionMaintenance` → `AuditLogPartitionMaintenance`, and the Data Protection key ring persisted to the database.
The obsolete zero-argument overload throws `InvalidOperationException` at startup (marked `error: true` on the `[Obsolete]` attribute) so a misconfigured host fails fast with a clear message rather than silently producing an empty DI registration.
+9 -1
View File
@@ -131,6 +131,14 @@ The two levels interact in a specific order, and it is load-bearing (register ro
2. **Per-IP throttle next, before the decrypt.** Over budget throws `BundleUnlockThrottledException` (Commons, so the management surface can catch it and return a curated message). Because it precedes the decrypt, a denial burns **no** per-bundle strike — otherwise the throttle would be a way to lock out a bundle someone is legitimately importing. Every unlock attempt is registered, successful ones included: the documented budget counts attempts, not failures.
3. Only then the AES-GCM decrypt, whose failure increments the per-bundle counter as before.
**The per-IP window is fleet-wide (register row 52).** It used to be a process-local `ConcurrentDictionary` on each central node, which had two consequences worth naming: the effective fleet cap was N × `MaxUnlockAttemptsPerIpPerHour` per client (a client that failed over to the other node got a fresh budget), and a node restart cleared the window outright. The window now lives in the central MS SQL `BundleUnlockAttempt` table — the HA store both central nodes already share, so this needs no new infrastructure, no new port and no consensus protocol. `BundleUnlockRateLimiter` keeps the policy; `SqlBundleUnlockAttemptWindow` is the store, resolving a scoped `IBundleUnlockAttemptRepository` from its own scope per call (it is a singleton, like the limiter). Three properties are load-bearing:
- **It fails OPEN.** A store error logs a Warning naming the client and the cap, and *permits* the attempt. This is a best-effort rate cap layered over a surface that is already authenticated and Administrator-gated, so the availability of legitimate imports outranks the cap — a database blip must not make the import wizard unusable. Falling back to the old process-local window instead was considered and rejected: it would still block a legitimate operator's 11th import during an outage, which is precisely the outcome fail-open exists to prevent. The Warning is the operator's signal that the fleet cap is not currently being enforced.
- **Accuracy is deliberately approximate.** The count-then-insert pair is not atomic across the two nodes, so genuinely simultaneous attempts can overshoot the cap by up to the number in flight. A conditional single-statement insert would close that at the cost of provider-specific raw SQL; the overshoot is bounded and self-corrects within the window.
- **Rows are pruned opportunistically, not by a background service.** Transport owns no central scheduler, so the prune rides the unlock path: claimed by CAS at most once per 15 minutes per process, bounded to 1000 rows per pass, retaining `2 ×` the window so a prune racing a count can never delete a row the count still needs, and wrapped in a try/catch that logs and swallows — retention maintenance must never fail the operator's actual unlock. This is the same shape `DeploymentService` uses for terminal deployment records.
Behaviour the operator sees is otherwise unchanged: same config key, same semantics ("attempts, not failures"), same ordering relative to the per-bundle lockout, same `<unattributed>` bucket.
The client key is the caller's remote IP: the Central UI wizard reads it from `CircuitClientAddressProvider` (a scoped service that snapshots `Connection.RemoteIpAddress` at circuit establishment, since `HttpContext` is gone by the time a circuit submits a passphrase — the same pattern as `CookieAuthenticationStateProvider`), and the management endpoint stamps it on `ManagementEnvelope.ClientIp` for the CLI `transport preview` / `transport import` paths. A caller with no attributable address is counted against a single **shared** bucket rather than skipping the throttle — "unknown client" degrades to a stricter budget, never to none. As with the login throttle (`Component-Security.md`), per-client isolation is only real when ForwardedHeaders is configured for the fronting proxy; behind an untrusted proxy every caller collapses into the proxy's bucket.
```csharp
@@ -264,7 +272,7 @@ After 3 wrong passphrase attempts against the same bundle (keyed by `ContentHash
### Unlock throttled ("Too many bundle unlock attempts")
`BundleUnlockThrottledException` — this **client** (not this bundle) has used its `MaxUnlockAttemptsPerIpPerHour` budget within the trailing hour. Re-uploading does not help; the budget is keyed by IP and rolls forward on a sliding window, and the counters are process-local (a central node restart clears them, deliberately, so an outage doesn't strand legitimate operators). The denial burns no per-bundle strike, so the bundle itself is still importable once the window frees up. Raise `ScadaBridge:Transport:MaxUnlockAttemptsPerIpPerHour` if a legitimate workflow (many encrypted bundles in one sitting) trips it routinely.
`BundleUnlockThrottledException` — this **client** (not this bundle) has used its `MaxUnlockAttemptsPerIpPerHour` budget within the trailing hour. Re-uploading does not help; the budget is keyed by IP, rolls forward on a sliding window, and is **fleet-wide and restart-durable** (register row 52 — see below), so failing over to the other central node does not help either. The denial burns no per-bundle strike, so the bundle itself is still importable once the window frees up. Raise `ScadaBridge:Transport:MaxUnlockAttemptsPerIpPerHour` if a legitimate workflow (many encrypted bundles in one sitting) trips it routinely.
### BundleImportAlarmScriptUnresolved / BundleImportCompositionUnresolved warnings
@@ -0,0 +1,45 @@
-- Register row 52: the fleet-wide bundle-unlock throttle window.
--
-- Creates the central [BundleUnlockAttempt] table that both central nodes share, so
-- ScadaBridge:Transport:MaxUnlockAttemptsPerIpPerHour is a fleet cap rather than a
-- per-node, per-process one. Operational history, not audit: non-partitioned, standard
-- [PRIMARY] filegroup, no DB-role restriction, and rows are pruned opportunistically by
-- the application once they age past twice the trailing window.
--
-- Run with QUOTED_IDENTIFIER ON. Several tables in this database carry filtered indexes
-- and persisted computed columns, and SQL Server refuses DDL/DML against them with error
-- 1934 when the session has QUOTED_IDENTIFIER OFF -- which is exactly what `sqlcmd` gives
-- you by default (pass -I, or keep this header). The SETs sit in their own batch so they
-- are in force when the statements after the GO parse.
SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
GO
BEGIN TRANSACTION;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20260815154803_AddBundleUnlockAttemptTable'
)
BEGIN
EXEC(N'CREATE TABLE [BundleUnlockAttempt] (
[Id] bigint NOT NULL IDENTITY,
[ClientKey] varchar(64) NOT NULL,
[AttemptedAtUtc] datetime2 NOT NULL,
CONSTRAINT [PK_BundleUnlockAttempt] PRIMARY KEY ([Id])
)');
EXEC(N'CREATE INDEX [IX_BundleUnlockAttempt_Client_Attempted]
ON [BundleUnlockAttempt] ([ClientKey], [AttemptedAtUtc])');
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20260815154803_AddBundleUnlockAttemptTable'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260815154803_AddBundleUnlockAttemptTable', N'10.0.7');
END;
COMMIT;
GO
@@ -123,6 +123,9 @@ The configuration database stores all central system data, organized by domain a
- **Deployment Records**: Deployment history per instance (timestamp, user, status, deployed configuration snapshot).
- **System-Wide Artifact Deployment Records**: Deployment history for shared artifacts (timestamp, user, artifact type, status).
### Transport
- **Bundle Unlock Attempts**: One row per permitted bundle-unlock passphrase attempt (`ClientKey`, `AttemptedAtUtc`), forming the fleet-wide sliding window behind Transport's `MaxUnlockAttemptsPerIpPerHour` throttle — see `Component-Transport.md` (register row 52). Operational history, not audit: non-partitioned, no DB-role restriction, and rows are pruned opportunistically by the application once they age past twice the window. **Denied** attempts are deliberately not recorded (a denial burns no budget), and no bundle identity is stored — the durable record of a failed unlock is the `BundleImportUnlockFailed` audit row. Central-only: written only by the Transport component, which the host registers for central roles.
### Audit Logging
- **Audit Log Entries**: Append-only audit trail (timestamp, user, action, entity type, entity ID, entity name, state as JSON). Stores only the after-state — change history is reconstructed by comparing consecutive entries. Entries are never modified or deleted. No retention policy — retained indefinitely. Indexed on timestamp, user, entity type, entity ID, and action for efficient filtering.
+2
View File
@@ -337,6 +337,8 @@ Imports are all-or-nothing per bundle. A bundle either applies fully or not at a
- **AES-256-GCM** for content encryption with **PBKDF2-SHA256 / 600 000 iterations** (OWASP 2023+ guidance), per-bundle random salt, random IV per encryption. The GCM auth tag is verified before decryption — a wrong passphrase fails cleanly.
- **Passphrase never persisted.** It lives only inside the export/import service call path and is discarded after use. No environment variable, no log line.
- **Failed-unlock rate limit:** per-bundle 3-strike lockout (keyed by `ContentHash`, server-side); per-IP-per-hour cap (`MaxUnlockAttemptsPerIpPerHour`, default 10, configurable) to deter brute force against a stolen bundle. Each failed attempt produces a `BundleImportUnlockFailed` audit row. The two are evaluated in order — bundle lockout, then per-IP throttle, then decrypt — so hammering an already-locked bundle spends no hourly budget, and a throttle denial (`BundleUnlockThrottledException`, fail-closed with a curated message on both the UI and CLI surfaces) burns **no** per-bundle strike, since it never reaches the decryptor. Both typed exceptions live in `Commons/Types/Transport/` — `ManagementService` references Commons but not Transport, so a type declared beside the importer cannot be curated by `MapFault` and reaches the CLI as a generic internal-error message (register rows 44 and 51). Successful unlocks count against the hourly budget too: it caps attempts, not failures. The client key is the caller's remote IP — from `CircuitClientAddressProvider` (snapshotted at Blazor circuit establishment, since `HttpContext` is unavailable mid-circuit) for the Central UI, and from `ManagementEnvelope.ClientIp` for the CLI; a caller with no attributable address shares a single stricter bucket rather than bypassing the throttle. Per-client isolation is only real behind a ForwardedHeaders-trusted proxy, the same caveat the login throttle carries in `Component-Security.md`. (Register row 44: the limiter existed, was configured and unit-tested, but was never invoked until this was wired.)
**The per-IP window is fleet-wide and restart-durable (register row 52).** It is persisted in the central MS SQL `BundleUnlockAttempt` table — the HA store both central nodes already share — rather than in each node's process memory, so the documented cap is a fleet cap and not N × the cap, and a node restart no longer clears it. `BundleUnlockRateLimiter` still owns the policy; `SqlBundleUnlockAttemptWindow` is the store behind the `IBundleUnlockAttemptWindow` seam, and `IBundleUnlockAttemptRepository` (Commons interface, ConfigurationDatabase implementation) is the persistence. Three deliberate trades: **the throttle fails OPEN** — a store error logs a Warning and permits the attempt, because this is a best-effort rate cap over an already-authenticated Administrator-gated surface and the availability of legitimate imports outranks the cap; **accuracy is approximate** — count-then-insert is not atomic across nodes, so simultaneous attempts can overshoot by the number in flight, which is bounded and self-corrects within the window; and **rows are pruned opportunistically on the unlock path** (CAS-claimed at most every 15 minutes per process, bounded to 1000 rows, retaining twice the window, best-effort) rather than by a new background service, since Transport owns no central scheduler.
- **Bundle size cap** on upload (default 100 MB, configurable) to bound memory.
- **In-transit:** existing HTTPS to the Central UI; no new channel.
- **Audit trail is the chain of custody.** Every export, every import (including aborted ones at validation), and every unlock failure is audit-logged with source env, content hash, encrypted yes/no, and artifact summary.
@@ -0,0 +1,42 @@
namespace ZB.MOM.WW.ScadaBridge.Commons.Entities.Transport;
/// <summary>
/// One recorded bundle-unlock passphrase attempt, written to the central
/// <c>BundleUnlockAttempt</c> table. Rows form the trailing-hour sliding window the
/// Transport component's per-client unlock throttle
/// (<c>TransportOptions.MaxUnlockAttemptsPerIpPerHour</c>) is evaluated against.
/// </summary>
/// <remarks>
/// <para>
/// Register row 52: the throttle's counters used to be per-node and per-process, so the
/// effective fleet cap was N x limit per client and a node restart cleared the window.
/// Central MS SQL is the store both central nodes already share, so putting the window
/// there makes the cap fleet-wide and restart-durable without new infrastructure.
/// </para>
/// <para>
/// Operational history, NOT audit — the table is non-partitioned, lives on the standard
/// <c>[PRIMARY]</c> filegroup and carries no DB-role restriction. Rows are pruned
/// opportunistically once they age past the window; they are not a durable record of
/// who tried to open which bundle (that is the <c>BundleImportUnlockFailed</c> audit row's
/// job, and note this table deliberately records no bundle identity at all).
/// </para>
/// <para>
/// Persistence-ignorant POCO; the EF Core mapping lives in the Configuration Database
/// component. <see cref="AttemptedAtUtc"/> is UTC, like every timestamp in the system.
/// </para>
/// </remarks>
public sealed class BundleUnlockAttempt
{
/// <summary>Surrogate identity key assigned by the store.</summary>
public long Id { get; set; }
/// <summary>
/// The throttle bucket the attempt was counted against — the caller's remote IP, or
/// the shared <c>&lt;unattributed&gt;</c> bucket when no address could be attributed.
/// Sized for an IPv6 literal with a scope id.
/// </summary>
public required string ClientKey { get; set; }
/// <summary>The UTC instant at which the unlock attempt was registered.</summary>
public DateTime AttemptedAtUtc { get; set; }
}
@@ -0,0 +1,46 @@
namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
/// <summary>
/// Register row 52: read/write access to the central <c>BundleUnlockAttempt</c> table,
/// the fleet-wide sliding window behind the Transport component's per-client bundle-unlock
/// throttle. Both central nodes share this store, so the documented
/// <c>MaxUnlockAttemptsPerIpPerHour</c> cap is a fleet cap rather than a per-process one.
/// </summary>
/// <remarks>
/// The throttle is a best-effort rate cap layered over an already-authenticated,
/// role-gated surface. Callers are expected to treat a store failure as fail-open (the
/// availability of legitimate imports outranks the cap), so no method here is required to
/// be transactional with respect to the caller's own work.
/// </remarks>
public interface IBundleUnlockAttemptRepository
{
/// <summary>
/// Counts the attempts recorded for <paramref name="clientKey"/> at or after
/// <paramref name="sinceUtc"/> — the trailing-window depth the cap is compared against.
/// </summary>
/// <param name="clientKey">Throttle bucket (client IP, or the shared unattributed bucket).</param>
/// <param name="sinceUtc">Inclusive UTC lower bound of the trailing window.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>The number of attempts inside the window.</returns>
Task<int> CountAttemptsSinceAsync(string clientKey, DateTime sinceUtc, CancellationToken ct = default);
/// <summary>
/// Appends one attempt row for <paramref name="clientKey"/>.
/// </summary>
/// <param name="clientKey">Throttle bucket (client IP, or the shared unattributed bucket).</param>
/// <param name="attemptedAtUtc">UTC instant of the attempt.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A task that completes when the row is committed.</returns>
Task RecordAttemptAsync(string clientKey, DateTime attemptedAtUtc, CancellationToken ct = default);
/// <summary>
/// Deletes attempt rows older than <paramref name="beforeUtc"/>, up to
/// <paramref name="maxRows"/> in one bounded pass. Bounded so an opportunistic prune on
/// a request path can never turn into an unbounded delete.
/// </summary>
/// <param name="beforeUtc">Exclusive UTC upper bound — rows strictly older than this are eligible.</param>
/// <param name="maxRows">Maximum rows to delete in this pass; must be positive.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>The number of rows deleted.</returns>
Task<int> PruneAttemptsOlderThanAsync(DateTime beforeUtc, int maxRows, CancellationToken ct = default);
}
@@ -0,0 +1,46 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Transport;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Configurations;
/// <summary>
/// Maps the <see cref="BundleUnlockAttempt"/> POCO to the central
/// <c>BundleUnlockAttempt</c> table — the fleet-wide sliding window behind the Transport
/// component's per-client bundle-unlock throttle (register row 52). Operational history,
/// NOT audit, so the table is non-partitioned, standard <c>[PRIMARY]</c> filegroup, no
/// DB-role restriction.
/// </summary>
/// <remarks>
/// One named index, <c>IX_BundleUnlockAttempt_Client_Attempted</c>, serves both access
/// paths: the window count seeks on <c>ClientKey</c> and ranges on
/// <c>AttemptedAtUtc</c>, and the opportunistic prune scans the same index by time. A
/// second time-only index was deliberately not added — the table is kept small by the
/// prune, so the leading-column seek is cheap enough for the prune to share.
/// </remarks>
public class BundleUnlockAttemptEntityTypeConfiguration : IEntityTypeConfiguration<BundleUnlockAttempt>
{
/// <summary>
/// Configures the EF Core entity type mapping for <see cref="BundleUnlockAttempt"/>.
/// </summary>
/// <param name="builder">The entity type builder to configure.</param>
public void Configure(EntityTypeBuilder<BundleUnlockAttempt> builder)
{
builder.ToTable("BundleUnlockAttempt");
// Surrogate long identity assigned by the store.
builder.HasKey(a => a.Id);
// Bucket key: an IP literal or the "<unattributed>" sentinel. 64 chars covers an
// IPv6 address with a scope id; ASCII, so non-unicode (varchar) like every other
// catalog-bounded column in the schema.
builder.Property(a => a.ClientKey).HasMaxLength(64).IsUnicode(false).IsRequired();
builder.Property(a => a.AttemptedAtUtc).IsRequired();
// Window index — seeks the bucket, ranges over the trailing hour, and is
// reused by the prune's time scan. Name locked for migration discoverability.
builder.HasIndex(a => new { a.ClientKey, a.AttemptedAtUtc })
.HasDatabaseName("IX_BundleUnlockAttempt_Client_Attempted");
}
}
@@ -0,0 +1,41 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
/// <inheritdoc />
public partial class AddBundleUnlockAttemptTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BundleUnlockAttempt",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ClientKey = table.Column<string>(type: "varchar(64)", unicode: false, maxLength: 64, nullable: false),
AttemptedAtUtc = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BundleUnlockAttempt", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_BundleUnlockAttempt_Client_Attempted",
table: "BundleUnlockAttempt",
columns: new[] { "ClientKey", "AttemptedAtUtc" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BundleUnlockAttempt");
}
}
}
@@ -1716,6 +1716,31 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
b.ToTable("TemplateScripts");
});
modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Transport.BundleUnlockAttempt", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<DateTime>("AttemptedAtUtc")
.HasColumnType("datetime2");
b.Property<string>("ClientKey")
.IsRequired()
.HasMaxLength(64)
.IsUnicode(false)
.HasColumnType("varchar(64)");
b.HasKey("Id");
b.HasIndex("ClientKey", "AttemptedAtUtc")
.HasDatabaseName("IX_BundleUnlockAttempt_Client_Attempted");
b.ToTable("BundleUnlockAttempt", (string)null);
});
modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities.AuditLogRow", b =>
{
b.Property<Guid>("EventId")
@@ -0,0 +1,70 @@
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Transport;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
/// <summary>
/// EF Core implementation of <see cref="IBundleUnlockAttemptRepository"/> over the central
/// <c>BundleUnlockAttempt</c> table (register row 52). Plain reads/writes against the
/// shared <see cref="ScadaBridgeDbContext"/>; the prune uses <c>ExecuteDeleteAsync</c> so
/// no rows are materialised into the change tracker.
/// </summary>
public class BundleUnlockAttemptRepository : IBundleUnlockAttemptRepository
{
private readonly ScadaBridgeDbContext _context;
/// <summary>
/// Initializes a new instance of the <see cref="BundleUnlockAttemptRepository"/> class.
/// </summary>
/// <param name="context">The EF Core database context.</param>
public BundleUnlockAttemptRepository(ScadaBridgeDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
/// <inheritdoc />
public async Task<int> CountAttemptsSinceAsync(
string clientKey, DateTime sinceUtc, CancellationToken ct = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(clientKey);
return await _context.BundleUnlockAttempts
.AsNoTracking()
.Where(a => a.ClientKey == clientKey && a.AttemptedAtUtc >= sinceUtc)
.CountAsync(ct);
}
/// <inheritdoc />
public async Task RecordAttemptAsync(
string clientKey, DateTime attemptedAtUtc, CancellationToken ct = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(clientKey);
_context.BundleUnlockAttempts.Add(new BundleUnlockAttempt
{
ClientKey = clientKey,
AttemptedAtUtc = attemptedAtUtc,
});
await _context.SaveChangesAsync(ct);
}
/// <inheritdoc />
public async Task<int> PruneAttemptsOlderThanAsync(
DateTime beforeUtc, int maxRows, CancellationToken ct = default)
{
if (maxRows < 1)
{
throw new ArgumentOutOfRangeException(nameof(maxRows), "Prune batch size must be at least 1.");
}
// Bounded by construction: OrderBy + Take caps the delete regardless of how much
// history accumulated while the prune was failing or the node was down, so an
// opportunistic sweep on a request path can never become an unbounded delete.
return await _context.BundleUnlockAttempts
.Where(a => a.AttemptedAtUtc < beforeUtc)
.OrderBy(a => a.AttemptedAtUtc)
.Take(maxRows)
.ExecuteDeleteAsync(ct);
}
}
@@ -13,6 +13,7 @@ using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Schemas;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.SecuredWrites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Transport;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Security;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
@@ -152,6 +153,10 @@ public class ScadaBridgeDbContext : DbContext, IDataProtectionKeyContext
/// <summary>Gets the set of hourly KPI rollups (pre-aggregated long-range trend backbone).</summary>
public DbSet<KpiRollupHourly> KpiRollupHourly => Set<KpiRollupHourly>();
// Transport (bundle export/import)
/// <summary>Gets the set of recorded bundle-unlock attempts — the fleet-wide sliding window behind the per-client unlock throttle (register row 52).</summary>
public DbSet<BundleUnlockAttempt> BundleUnlockAttempts => Set<BundleUnlockAttempt>();
// Data Protection Keys (for shared ASP.NET Data Protection across nodes)
/// <summary>Gets the set of data protection keys.</summary>
public DbSet<DataProtectionKey> DataProtectionKeys => Set<DataProtectionKey>();
@@ -87,6 +87,10 @@ public static class ServiceCollectionExtensions
services.AddScoped<ISecuredWriteRepository, SecuredWriteRepository>();
services.AddScoped<ISharedSchemaRepository, SharedSchemaRepository>();
services.AddScoped<IKpiHistoryRepository, KpiHistoryRepository>();
// Register row 52: the fleet-wide bundle-unlock throttle window. Central-only in
// practice (Transport is registered in the host's Central branch), but registered
// here with every other repository so the ConfigurationDatabase surface stays uniform.
services.AddScoped<IBundleUnlockAttemptRepository, BundleUnlockAttemptRepository>();
// Auth re-arch: inbound API keys are no longer persisted in SQL Server —
// the repository now exposes only API-method access, so a plain scoped
// registration suffices (no peppered-hasher accessor to wire).
@@ -293,11 +293,18 @@ public sealed class BundleImporter : IBundleImporter
// operator is legitimately importing.
// Registered on every unlock attempt (successful ones included) because the
// documented budget counts attempts, not failures.
//
// Register row 52: the async overload consults the FLEET-WIDE window (central
// MS SQL) so the cap is not multiplied by the number of central nodes and does
// not reset on a node restart. It fails open on a store error — see
// BundleUnlockRateLimiter's remarks.
var throttleKey = string.IsNullOrWhiteSpace(clientKey)
? UnattributedClientKey
: clientKey;
var perIpCap = _options.Value.MaxUnlockAttemptsPerIpPerHour;
if (!_unlockRateLimiter.TryRegisterAttempt(throttleKey, perIpCap))
if (!await _unlockRateLimiter
.TryRegisterAttemptAsync(throttleKey, perIpCap, ct)
.ConfigureAwait(false))
{
_logger?.LogWarning(
"Bundle unlock throttled: client {ClientKey} exceeded {Cap} attempts in the trailing hour.",
@@ -1,26 +1,37 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace ZB.MOM.WW.ScadaBridge.Transport.Import;
/// <summary>
/// In-memory sliding-window rate limiter for bundle-unlock passphrase
/// attempts, keyed by client IP. The design doc (§11) declares a per-IP-per-hour cap
/// (default 10) as a brute-force defence against a stolen bundle; this class is the
/// minimal server-side implementation.
/// Sliding-window rate limiter for bundle-unlock passphrase attempts, keyed by client IP.
/// The design doc (§11) declares a per-IP-per-hour cap (default 10) as a brute-force
/// defence against a stolen bundle; this class owns that policy.
/// <para>
/// Algorithm: each key (an IP string, or any opaque caller identifier) holds a queue
/// of attempt timestamps. <see cref="TryRegisterAttempt"/> first prunes entries older
/// than the configured window, then either appends the current timestamp and returns
/// <c>true</c> if the count is still under the threshold, or refuses to append and
/// returns <c>false</c> if appending would cross it. The trailing-hour count is the
/// queue length post-prune.
/// <b>The window is fleet-wide when an <see cref="IBundleUnlockAttemptWindow"/> is
/// supplied (register row 52).</b> The counters used to be per-node AND per-process, so
/// the effective cap was N x limit per client across the central pair and a node restart
/// silently cleared the window. <see cref="TryRegisterAttemptAsync"/> now consults the
/// shared central-MS-SQL window instead, which both central nodes already share. The
/// production composition root always supplies one.
/// </para>
/// <para>
/// Storage is a process-local <see cref="ConcurrentDictionary{TKey,TValue}"/>. The
/// counters do not survive a host restart — that is by design: a restart resets the
/// brute-force window in favour of legitimate operators after an outage. Persisting
/// the counters would require a multi-node consensus story the simple in-memory
/// design avoids.
/// <b>Fail-open is deliberate.</b> A store failure in the throttle path logs a Warning and
/// PERMITS the attempt. This is a best-effort rate cap layered over a surface that is
/// already authenticated and role-gated (Administrator-only bundle import), so the
/// availability of legitimate imports outranks the cap: a database blip must not make the
/// import wizard unusable. Falling back to the process-local window instead was considered
/// and rejected — it would still block a legitimate operator's 11th import during an
/// outage, which is the outcome fail-open exists to prevent.
/// </para>
/// <para>
/// Algorithm (the process-local fallback used when no shared window is registered): each
/// key holds a queue of attempt timestamps. <see cref="TryRegisterAttempt"/> first prunes
/// entries older than the configured window, then either appends the current timestamp and
/// returns <c>true</c> if the count is still under the threshold, or refuses to append and
/// returns <c>false</c> if appending would cross it. Either way a REJECTED attempt is never
/// appended, so a denial burns no budget — the same contract the shared window implements.
/// </para>
/// <para>
/// Thread-safety: the per-key queue is protected by a per-key lock taken inside the
@@ -39,10 +50,13 @@ public sealed class BundleUnlockRateLimiter
private readonly ConcurrentDictionary<string, AttemptBucket> _buckets = new(StringComparer.OrdinalIgnoreCase);
private readonly TimeProvider _timeProvider;
private readonly TimeSpan _window;
private readonly IBundleUnlockAttemptWindow? _sharedWindow;
private readonly ILogger<BundleUnlockRateLimiter> _logger;
/// <summary>
/// Initializes a new <see cref="BundleUnlockRateLimiter"/> using the documented
/// 1-hour trailing window and the system clock. Suitable for production DI.
/// 1-hour trailing window and the system clock, with no shared window (process-local
/// counters only).
/// </summary>
public BundleUnlockRateLimiter() : this(TimeProvider.System, DefaultWindow)
{
@@ -50,11 +64,28 @@ public sealed class BundleUnlockRateLimiter
/// <summary>
/// Initializes a new <see cref="BundleUnlockRateLimiter"/> with an injected clock
/// (for deterministic tests) and a custom trailing window.
/// (for deterministic tests) and a custom trailing window, with no shared window.
/// </summary>
/// <param name="timeProvider">Clock used for both timestamping new attempts and pruning expired ones.</param>
/// <param name="window">Trailing window over which attempts are counted (typically 1 hour).</param>
public BundleUnlockRateLimiter(TimeProvider timeProvider, TimeSpan window)
: this(timeProvider, window, sharedWindow: null, logger: null)
{
}
/// <summary>
/// Initializes a new <see cref="BundleUnlockRateLimiter"/> over a fleet-wide shared
/// window (register row 52). This is the overload the production composition root uses.
/// </summary>
/// <param name="timeProvider">Clock used for the process-local fallback window.</param>
/// <param name="window">Trailing window over which attempts are counted (typically 1 hour).</param>
/// <param name="sharedWindow">Shared cross-node attempt window; when null the limiter falls back to process-local counters.</param>
/// <param name="logger">Optional logger. Used for the fail-open Warning when the shared window is unavailable.</param>
public BundleUnlockRateLimiter(
TimeProvider timeProvider,
TimeSpan window,
IBundleUnlockAttemptWindow? sharedWindow,
ILogger<BundleUnlockRateLimiter>? logger)
{
ArgumentNullException.ThrowIfNull(timeProvider);
if (window <= TimeSpan.Zero)
@@ -64,13 +95,81 @@ public sealed class BundleUnlockRateLimiter
_timeProvider = timeProvider;
_window = window;
_sharedWindow = sharedWindow;
_logger = logger ?? NullLogger<BundleUnlockRateLimiter>.Instance;
}
/// <summary>
/// Attempts to register a new passphrase try against the configured per-key
/// limit. Returns <c>true</c> when the attempt is permitted (and recorded);
/// returns <c>false</c> when the key has exhausted its budget for the trailing
/// window — the caller should reject the unlock request with a 429-equivalent.
/// Gets a value indicating whether this limiter is backed by a fleet-wide shared
/// window. False means the cap degrades to per-node, per-process counters.
/// </summary>
public bool IsFleetWide => _sharedWindow is not null;
/// <summary>
/// Registers a passphrase try against the configured per-key limit, using the
/// fleet-wide shared window when one is registered and the process-local window
/// otherwise. This is the method the importer calls.
/// </summary>
/// <param name="clientKey">Opaque caller identifier — typically the remote IP.</param>
/// <param name="maxAttemptsPerWindow">The trailing-window cap (<c>MaxUnlockAttemptsPerIpPerHour</c>).</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>
/// <c>true</c> if the attempt is permitted; <c>false</c> if the caller has exhausted
/// its budget. Also <c>true</c> when the shared window is unavailable — see the
/// class remarks on why the throttle fails open.
/// </returns>
public async Task<bool> TryRegisterAttemptAsync(
string clientKey, int maxAttemptsPerWindow, CancellationToken ct = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(clientKey);
if (maxAttemptsPerWindow < 1)
{
throw new ArgumentOutOfRangeException(
nameof(maxAttemptsPerWindow), "Limit must be at least 1.");
}
if (_sharedWindow is null)
{
return TryRegisterAttempt(clientKey, maxAttemptsPerWindow);
}
try
{
return await _sharedWindow
.TryRegisterAttemptAsync(clientKey, maxAttemptsPerWindow, _window, ct)
.ConfigureAwait(false);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
// The caller's own cancellation, not a store failure — do not swallow it.
throw;
}
catch (Exception ex)
{
// Fail OPEN: a best-effort rate cap must never take the import surface down
// with the database. The Warning is the operator's signal that the fleet cap
// is currently not being enforced.
_logger.LogWarning(
ex,
"Fleet-wide bundle-unlock throttle unavailable; permitting the unlock attempt for client "
+ "{ClientKey} without enforcing the {Cap}/hour cap.",
clientKey, maxAttemptsPerWindow);
return true;
}
}
/// <summary>
/// Attempts to register a new passphrase try against the configured per-key limit in
/// the PROCESS-LOCAL window. Returns <c>true</c> when the attempt is permitted (and
/// recorded); returns <c>false</c> when the key has exhausted its budget for the
/// trailing window — the caller should reject the unlock request with a 429-equivalent.
/// <para>
/// Register row 52: this bypasses the fleet-wide window. Callers on the unlock path
/// must use <see cref="TryRegisterAttemptAsync"/>, which routes to the shared window
/// when one is registered and falls back here otherwise. This overload remains public
/// for hosts that register Transport without a shared window, and for the tests that
/// pin the sliding-window arithmetic directly.
/// </para>
/// </summary>
/// <param name="clientKey">
/// Opaque caller identifier — typically the remote IP, but any stable per-source
@@ -0,0 +1,28 @@
namespace ZB.MOM.WW.ScadaBridge.Transport.Import;
/// <summary>
/// Register row 52: a sliding window of bundle-unlock attempts that is shared across the
/// central pair, so <c>TransportOptions.MaxUnlockAttemptsPerIpPerHour</c> is a FLEET cap
/// rather than a per-node, per-process one.
/// <para>
/// Implementations are expected to THROW on store failure. The fail-open decision belongs
/// to <see cref="BundleUnlockRateLimiter"/>, which owns the policy (log a Warning and
/// permit the attempt), not to the storage layer.
/// </para>
/// </summary>
public interface IBundleUnlockAttemptWindow
{
/// <summary>
/// Registers one unlock attempt against the shared window and reports whether it was
/// within budget. Implementations must NOT record an attempt they are rejecting —
/// a denial burns no budget, matching the in-memory limiter's contract (register
/// row 44), so a client that keeps hammering cannot extend its own lockout.
/// </summary>
/// <param name="clientKey">Throttle bucket — the caller's remote IP, or the shared unattributed bucket.</param>
/// <param name="maxAttemptsPerWindow">The trailing-window cap (<c>MaxUnlockAttemptsPerIpPerHour</c>).</param>
/// <param name="window">Trailing window the attempts are counted over (one hour in production).</param>
/// <param name="ct">Cancellation token.</param>
/// <returns><c>true</c> when the attempt was recorded (within budget); <c>false</c> when the bucket is exhausted.</returns>
Task<bool> TryRegisterAttemptAsync(
string clientKey, int maxAttemptsPerWindow, TimeSpan window, CancellationToken ct = default);
}
@@ -0,0 +1,167 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
namespace ZB.MOM.WW.ScadaBridge.Transport.Import;
/// <summary>
/// Register row 52: <see cref="IBundleUnlockAttemptWindow"/> backed by the central MS SQL
/// <c>BundleUnlockAttempt</c> table — the HA store both central nodes already share, so
/// the fleet-wide cap needs no new infrastructure, no new port and no consensus protocol.
/// <para>
/// Registered as a singleton (the limiter that consumes it is one) and therefore resolves
/// a scoped <see cref="IBundleUnlockAttemptRepository"/> from its own
/// <see cref="IServiceScopeFactory"/> scope per call, the same way the central purge
/// singletons reach the database.
/// </para>
/// <para>
/// <b>Accuracy is deliberately approximate.</b> The count-then-insert pair is not atomic
/// across the two central nodes, so genuinely simultaneous attempts can overshoot the cap
/// by up to the number in flight. A conditional single-statement insert would close that,
/// at the cost of provider-specific SQL for a best-effort rate cap layered over an
/// already-authenticated, role-gated surface. Overshoot is bounded and self-corrects
/// within the window; it is not worth raw SQL.
/// </para>
/// </summary>
public sealed class SqlBundleUnlockAttemptWindow : IBundleUnlockAttemptWindow
{
/// <summary>
/// Minimum wall-clock gap between opportunistic prunes. The prune runs on the unlock
/// path (Transport owns no central scheduler, and the design constraint was "no new
/// background service"), so it is claimed at most this often per process — the same
/// rate-limited opportunistic-sweep shape <c>DeploymentService</c> uses for terminal
/// deployment records.
/// </summary>
internal static readonly TimeSpan PruneMinInterval = TimeSpan.FromMinutes(15);
/// <summary>
/// Upper bound on rows deleted by one opportunistic prune pass. Bounded so a sweep on
/// a request path can never become a long-running delete, even if the table grew while
/// prunes were failing.
/// </summary>
internal const int PruneBatchSize = 1000;
/// <summary>
/// Multiple of the trailing window that rows are retained for before becoming
/// prunable. Kept above 1 so a prune racing a count can never delete a row the count
/// still needs.
/// </summary>
internal const int PruneRetentionWindows = 2;
private readonly IServiceScopeFactory _scopeFactory;
private readonly TimeProvider _timeProvider;
private readonly ILogger<SqlBundleUnlockAttemptWindow> _logger;
/// <summary>Ticks of the last claimed prune, as a CAS-guarded process-local claim.</summary>
private long _lastPruneTicks;
/// <summary>
/// Initializes a new <see cref="SqlBundleUnlockAttemptWindow"/>.
/// </summary>
/// <param name="scopeFactory">Scope factory used to resolve the scoped attempt repository per call.</param>
/// <param name="timeProvider">Clock used for the window boundary and the prune cadence.</param>
/// <param name="logger">Optional logger for prune diagnostics.</param>
public SqlBundleUnlockAttemptWindow(
IServiceScopeFactory scopeFactory,
TimeProvider timeProvider,
ILogger<SqlBundleUnlockAttemptWindow>? logger = null)
{
_scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
_timeProvider = timeProvider ?? throw new ArgumentNullException(nameof(timeProvider));
_logger = logger ?? NullLogger<SqlBundleUnlockAttemptWindow>.Instance;
// Seed the prune claim to "now" so a freshly started node does not prune on its
// very first unlock — the table is almost certainly already tidy, and a node that
// restarts under load would otherwise prune on every restart.
_lastPruneTicks = _timeProvider.GetUtcNow().UtcTicks;
}
/// <inheritdoc />
public async Task<bool> TryRegisterAttemptAsync(
string clientKey, int maxAttemptsPerWindow, TimeSpan window, CancellationToken ct = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(clientKey);
if (maxAttemptsPerWindow < 1)
{
throw new ArgumentOutOfRangeException(
nameof(maxAttemptsPerWindow), "Limit must be at least 1.");
}
if (window <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(window), "Window must be positive.");
}
var now = _timeProvider.GetUtcNow().UtcDateTime;
var key = clientKey.Trim();
using var scope = _scopeFactory.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IBundleUnlockAttemptRepository>();
// Two round trips rather than one conditional insert — see the class remarks on
// why the resulting overshoot is accepted. Exceptions propagate: the fail-open
// decision lives in BundleUnlockRateLimiter.
var used = await repo.CountAttemptsSinceAsync(key, now - window, ct).ConfigureAwait(false);
if (used >= maxAttemptsPerWindow)
{
// Rejected attempts are NOT recorded — a denial burns no budget, so a client
// hammering the endpoint cannot roll its own window forward indefinitely.
return false;
}
await repo.RecordAttemptAsync(key, now, ct).ConfigureAwait(false);
await TryPruneAsync(repo, now, window, ct).ConfigureAwait(false);
return true;
}
/// <summary>
/// Opportunistic, rate-limited, bounded prune of aged-out attempt rows. Best-effort:
/// any failure is logged and swallowed, because retention maintenance must never fail
/// the operator's actual unlock.
/// </summary>
/// <param name="repo">Repository resolved for the current call's scope.</param>
/// <param name="nowUtc">Current UTC instant.</param>
/// <param name="window">The trailing window; rows older than a multiple of it are prunable.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A task that completes when the prune attempt has finished (or been skipped).</returns>
private async Task TryPruneAsync(
IBundleUnlockAttemptRepository repo, DateTime nowUtc, TimeSpan window, CancellationToken ct)
{
var previous = Interlocked.Read(ref _lastPruneTicks);
if (nowUtc.Ticks - previous < PruneMinInterval.Ticks)
{
return;
}
// Claim the sweep so concurrent unlocks on this node don't all prune at once.
if (Interlocked.CompareExchange(ref _lastPruneTicks, nowUtc.Ticks, previous) != previous)
{
return;
}
try
{
var cutoff = nowUtc - (window * PruneRetentionWindows);
var deleted = await repo
.PruneAttemptsOlderThanAsync(cutoff, PruneBatchSize, ct)
.ConfigureAwait(false);
if (deleted > 0)
{
_logger.LogDebug(
"Pruned {Deleted} bundle-unlock attempt row(s) older than {Cutoff:o}.", deleted, cutoff);
}
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
throw;
}
catch (Exception ex)
{
// Never fail the unlock for a retention sweep.
_logger.LogWarning(ex, "Opportunistic bundle-unlock attempt prune failed; will retry on a later attempt.");
}
}
}
@@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport;
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation;
@@ -43,10 +44,22 @@ public static class ServiceCollectionExtensions
services.AddScoped<DependencyResolver>();
services.AddScoped<IBundleExporter, BundleExporter>();
services.AddSingleton<IBundleSessionStore, BundleSessionStore>();
// T-004: per-IP-per-hour unlock rate limiter — design doc §11. Singleton
// so the trailing-hour window is shared across every importer scope; the
// counters live in-memory and reset on host restart (by design).
services.AddSingleton<BundleUnlockRateLimiter>();
// T-004: per-IP-per-hour unlock rate limiter — design doc §11. Singleton so the
// trailing-hour window is shared across every importer scope.
//
// Register row 52: the window is FLEET-WIDE, backed by the central MS SQL
// BundleUnlockAttempt table that both central nodes already share — the counters
// used to be per-node and per-process, so the effective cap was N x limit per
// client and a restart cleared the window. The window resolves its scoped
// repository from its own scope per call, so it is safe as a singleton.
// Constructed explicitly rather than by convention so it is unambiguous which
// constructor overload production uses.
services.AddSingleton<IBundleUnlockAttemptWindow, SqlBundleUnlockAttemptWindow>();
services.AddSingleton(sp => new BundleUnlockRateLimiter(
sp.GetRequiredService<TimeProvider>(),
BundleUnlockRateLimiter.DefaultWindow,
sp.GetRequiredService<IBundleUnlockAttemptWindow>(),
sp.GetService<ILogger<BundleUnlockRateLimiter>>()));
// T-007: periodic eviction sweep so abandoned sessions clear without
// needing a fresh Get() to trigger lazy eviction.
services.AddHostedService<BundleSessionEvictionService>();
@@ -0,0 +1,90 @@
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Repositories;
/// <summary>
/// Register row 52 coverage for <see cref="BundleUnlockAttemptRepository"/> over the
/// in-memory SQLite harness — the fleet-wide bundle-unlock throttle window's persistence
/// layer. Exercises the windowed count (including the bucket-isolation and window-boundary
/// cases the throttle depends on) and the bounded prune.
/// </summary>
public class BundleUnlockAttemptRepositoryTests
{
// Fixed UTC base so the window assertions are deterministic.
private static readonly DateTime Base = new(2026, 6, 1, 12, 0, 0, DateTimeKind.Utc);
private static ScadaBridgeDbContext NewContext() => SqliteTestHelper.CreateInMemoryContext();
[Fact]
public async Task CountAttemptsSinceAsync_CountsOnlyTheGivenBucketInsideTheWindow()
{
await using var ctx = NewContext();
var repo = new BundleUnlockAttemptRepository(ctx);
// Two inside the trailing hour, one aged out, one from a different client.
await repo.RecordAttemptAsync("10.0.0.1", Base.AddMinutes(-10));
await repo.RecordAttemptAsync("10.0.0.1", Base.AddMinutes(-50));
await repo.RecordAttemptAsync("10.0.0.1", Base.AddMinutes(-90));
await repo.RecordAttemptAsync("10.0.0.2", Base.AddMinutes(-5));
var count = await repo.CountAttemptsSinceAsync("10.0.0.1", Base.AddHours(-1));
Assert.Equal(2, count);
}
[Fact]
public async Task CountAttemptsSinceAsync_IsInclusiveAtTheWindowFloor()
{
// The floor is inclusive, matching the in-memory limiter's prune predicate
// (entries at exactly the cutoff still count). Pinned because an off-by-one here
// would silently hand every client one extra attempt per window.
await using var ctx = NewContext();
var repo = new BundleUnlockAttemptRepository(ctx);
await repo.RecordAttemptAsync("10.0.0.1", Base.AddHours(-1));
Assert.Equal(1, await repo.CountAttemptsSinceAsync("10.0.0.1", Base.AddHours(-1)));
}
[Fact]
public async Task CountAttemptsSinceAsync_UnknownBucket_ReturnsZero()
{
await using var ctx = NewContext();
var repo = new BundleUnlockAttemptRepository(ctx);
Assert.Equal(0, await repo.CountAttemptsSinceAsync("<unattributed>", Base.AddHours(-1)));
}
[Fact]
public async Task PruneAttemptsOlderThanAsync_DeletesOnlyAgedRows_AndIsBounded()
{
await using var ctx = NewContext();
var repo = new BundleUnlockAttemptRepository(ctx);
for (var i = 0; i < 5; i++)
{
await repo.RecordAttemptAsync("10.0.0.1", Base.AddHours(-5).AddMinutes(i));
}
await repo.RecordAttemptAsync("10.0.0.1", Base.AddMinutes(-1));
// Bounded: only 3 of the 5 eligible rows go in this pass.
var deleted = await repo.PruneAttemptsOlderThanAsync(Base.AddHours(-2), maxRows: 3);
Assert.Equal(3, deleted);
// The recent row is untouched, and a second pass clears the remainder.
Assert.Equal(1, await repo.CountAttemptsSinceAsync("10.0.0.1", Base.AddHours(-1)));
Assert.Equal(2, await repo.PruneAttemptsOlderThanAsync(Base.AddHours(-2), maxRows: 3));
Assert.Equal(0, await repo.PruneAttemptsOlderThanAsync(Base.AddHours(-2), maxRows: 3));
}
[Fact]
public async Task PruneAttemptsOlderThanAsync_RejectsANonPositiveBatchSize()
{
await using var ctx = NewContext();
var repo = new BundleUnlockAttemptRepository(ctx);
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(
() => repo.PruneAttemptsOlderThanAsync(Base, maxRows: 0));
}
}
@@ -0,0 +1,263 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
using ZB.MOM.WW.ScadaBridge.Transport.Import;
namespace ZB.MOM.WW.ScadaBridge.Transport.IntegrationTests.Import;
/// <summary>
/// Register row 52: the bundle-unlock throttle's counters used to be per-node AND
/// per-process, so the effective fleet cap was N x <c>MaxUnlockAttemptsPerIpPerHour</c>
/// per client and a node restart silently cleared the window. These tests stand up TWO
/// independent limiter instances — the stand-in for central-a and central-b — over ONE
/// shared relational store and prove they enforce a single combined window, plus the
/// fail-open behaviour a store outage must produce.
/// </summary>
public sealed class FleetWideUnlockThrottleTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly ServiceProvider _provider;
public FleetWideUnlockThrottleTests()
{
// One open in-memory SQLite connection for the fixture's lifetime — the database
// lives exactly as long as the connection, and both "nodes" resolve scoped
// contexts over it, which is what makes the store genuinely shared.
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
var services = new ServiceCollection();
services.AddDbContext<ScadaBridgeDbContext>(opts => opts.UseSqlite(_connection));
services.AddScoped<IBundleUnlockAttemptRepository, BundleUnlockAttemptRepository>();
_provider = services.BuildServiceProvider();
using var scope = _provider.CreateScope();
scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>().Database.EnsureCreated();
}
public void Dispose()
{
_provider.Dispose();
_connection.Dispose();
}
/// <summary>
/// Builds a limiter over the shared store, standing in for one central node's
/// singleton. Each call returns a fully independent instance (its own process-local
/// fallback buckets, its own prune claim) — exactly the isolation the two real nodes
/// have.
/// </summary>
/// <param name="clock">Clock the node observes.</param>
/// <returns>A limiter whose window is the shared store.</returns>
private BundleUnlockRateLimiter NewNode(TimeProvider clock) => new(
clock,
BundleUnlockRateLimiter.DefaultWindow,
new SqlBundleUnlockAttemptWindow(
_provider.GetRequiredService<IServiceScopeFactory>(), clock),
logger: null);
[Fact]
public async Task TwoNodesSharingOneStore_EnforceASingleCombinedWindow()
{
// The headline row-52 contract: attempts spread across both central nodes count
// against ONE budget. Before this, five attempts per node were both allowed.
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var nodeA = NewNode(clock);
var nodeB = NewNode(clock);
for (var i = 0; i < 5; i++)
{
Assert.True(await nodeA.TryRegisterAttemptAsync("10.0.0.1", 10));
Assert.True(await nodeB.TryRegisterAttemptAsync("10.0.0.1", 10));
}
// The combined budget of 10 is now spent — on EITHER node.
Assert.False(await nodeA.TryRegisterAttemptAsync("10.0.0.1", 10));
Assert.False(await nodeB.TryRegisterAttemptAsync("10.0.0.1", 10));
}
[Fact]
public async Task DeniedAttempts_BurnNoBudget()
{
// Register row 44's contract, preserved through the move to the shared store: a
// rejected attempt is not recorded, so hammering cannot roll the window forward
// and extend the client's own lockout.
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var nodeA = NewNode(clock);
for (var i = 0; i < 3; i++)
{
Assert.True(await nodeA.TryRegisterAttemptAsync("10.0.0.1", 3));
}
for (var i = 0; i < 5; i++)
{
Assert.False(await nodeA.TryRegisterAttemptAsync("10.0.0.1", 3));
}
// Exactly the 3 permitted attempts were persisted — the 5 denials added nothing.
using var scope = _provider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IBundleUnlockAttemptRepository>();
var recorded = await repo.CountAttemptsSinceAsync(
"10.0.0.1", clock.GetUtcNow().UtcDateTime.AddHours(-1));
Assert.Equal(3, recorded);
}
[Fact]
public async Task TheWindowIsRestartDurable()
{
// A node restart used to clear the window outright. A brand-new limiter instance
// (the restarted process) must still see the budget the old one spent.
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var before = NewNode(clock);
for (var i = 0; i < 3; i++)
{
Assert.True(await before.TryRegisterAttemptAsync("10.0.0.1", 3));
}
var afterRestart = NewNode(clock);
Assert.False(await afterRestart.TryRegisterAttemptAsync("10.0.0.1", 3));
}
[Fact]
public async Task TheWindowSlides_SoAnAgedOutBudgetIsReleased()
{
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var node = NewNode(clock);
for (var i = 0; i < 3; i++)
{
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.1", 3));
}
Assert.False(await node.TryRegisterAttemptAsync("10.0.0.1", 3));
clock.Advance(TimeSpan.FromMinutes(61));
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.1", 3));
}
[Fact]
public async Task BucketsAreIsolatedPerClient()
{
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var node = NewNode(clock);
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.1", 1));
Assert.False(await node.TryRegisterAttemptAsync("10.0.0.1", 1));
// A different client — and the shared unattributed bucket — are unaffected.
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.2", 1));
Assert.True(await node.TryRegisterAttemptAsync("<unattributed>", 1));
}
[Fact]
public async Task StoreFailure_FailsOpen_AndDoesNotBlockTheUnlock()
{
// Availability of legitimate imports outranks a best-effort rate cap: a database
// error must permit the attempt, not take the import surface down with the DB.
var window = Substitute.For<IBundleUnlockAttemptWindow>();
window
.TryRegisterAttemptAsync(
Arg.Any<string>(), Arg.Any<int>(), Arg.Any<TimeSpan>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new InvalidOperationException("database unavailable"));
var limiter = new BundleUnlockRateLimiter(
TimeProvider.System, BundleUnlockRateLimiter.DefaultWindow, window, logger: null);
for (var i = 0; i < 20; i++)
{
Assert.True(await limiter.TryRegisterAttemptAsync("10.0.0.1", 1));
}
}
[Fact]
public async Task CallerCancellation_IsNotSwallowedByTheFailOpenPath()
{
// Fail-open must not hide the caller's own cancellation — that would turn an
// aborted request into a silently permitted attempt.
var window = Substitute.For<IBundleUnlockAttemptWindow>();
window
.TryRegisterAttemptAsync(
Arg.Any<string>(), Arg.Any<int>(), Arg.Any<TimeSpan>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException());
var limiter = new BundleUnlockRateLimiter(
TimeProvider.System, BundleUnlockRateLimiter.DefaultWindow, window, logger: null);
using var cts = new CancellationTokenSource();
await cts.CancelAsync();
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => limiter.TryRegisterAttemptAsync("10.0.0.1", 10, cts.Token));
}
[Fact]
public async Task WithoutASharedWindow_TheLimiterStillEnforcesAProcessLocalCap()
{
// Hosts that register Transport without a shared window keep the pre-row-52
// behaviour rather than losing the cap entirely.
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var limiter = new BundleUnlockRateLimiter(clock, BundleUnlockRateLimiter.DefaultWindow);
Assert.False(limiter.IsFleetWide);
Assert.True(await limiter.TryRegisterAttemptAsync("10.0.0.1", 1));
Assert.False(await limiter.TryRegisterAttemptAsync("10.0.0.1", 1));
}
[Fact]
public async Task AgedRowsArePrunedOpportunistically_OnceThePruneIntervalHasElapsed()
{
// No background service: the prune rides the unlock path, claimed at most once per
// PruneMinInterval. Rows are retained for PruneRetentionWindows x window so a prune
// racing a count can never delete a row the count still needs.
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var node = NewNode(clock);
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.1", 10));
// Move past both the retention horizon (2 hours) and the prune cadence.
clock.Advance(TimeSpan.FromHours(3));
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.2", 10));
using var scope = _provider.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
var remaining = await ctx.BundleUnlockAttempts.AsNoTracking().ToListAsync();
// The aged row is gone; the one just written by the pruning attempt survives.
Assert.Equal("10.0.0.2", Assert.Single(remaining).ClientKey);
}
[Fact]
public async Task RecentRowsSurviveAPrunePass()
{
// Negative control for the prune horizon: an attempt still inside the retention
// window must not be deleted, or the throttle would forget live budget.
var clock = new TestClock(new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero));
var node = NewNode(clock);
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.1", 10));
// Past the prune cadence (15 min) but inside the 2-hour retention horizon.
clock.Advance(TimeSpan.FromMinutes(30));
Assert.True(await node.TryRegisterAttemptAsync("10.0.0.2", 10));
using var scope = _provider.CreateScope();
var ctx = scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>();
Assert.Equal(2, await ctx.BundleUnlockAttempts.AsNoTracking().CountAsync());
}
/// <summary>Minimal mutable-clock <see cref="TimeProvider"/> for deterministic windows.</summary>
private sealed class TestClock : TimeProvider
{
private DateTimeOffset _now;
public TestClock(DateTimeOffset start) { _now = start; }
public override DateTimeOffset GetUtcNow() => _now;
public void Advance(TimeSpan by) => _now = _now.Add(by);
}
}