fix(host): follow an in-place site PSK rotation without a central restart (ScadaBridge#96)

SitePskProvider cached each site's gRPC preshared key forever; only site removal
invalidated it, so rotating SB-GRPC-PSK-{site} broke the central<->site link on
both central nodes until they restarted. The cached key is now dropped when:

- the secret is written through /admin/secrets: central replaces the secrets
  library's ISecretCacheInvalidator with SitePskRotationInvalidator, which also
  publishes SitePskInvalidated on a new DistributedPubSub topic
  (site-psk-invalidation) so the peer central node drops its copy and its
  resolver cache entry too (SitePskInvalidationSubscriber);
- a site rejects a central->site call with PermissionDenied/Unauthenticated:
  WithSiteCredentials wraps every per-site channel's handler in
  PskRejectionRefreshHandler, so the next call re-reads the key (covers
  out-of-process writes such as the secret CLI);
- a site->central call presents a token that does not match the cached key:
  CentralControlAuthInterceptor invalidates and re-reads once before refusing;
- ScadaBridge:Communication:SitePsks reloads.

Invalidation is case-insensitive because SecretName lowercases names. Rotation
runbook (topology-guide), Component-Communication/Host and the #85 cache audit
(D2) updated.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 14:27:14 -05:00
parent 7f479f3a5c
commit 3a31dd2de7
15 changed files with 666 additions and 26 deletions
@@ -204,4 +204,61 @@ public class SitePskProviderTests
Assert.Equal("key-a", await provider.GetAsync("site-a", CancellationToken.None));
Assert.Equal("key-b", await provider.GetAsync("site-b", CancellationToken.None));
}
/// <summary>
/// Verifies that a reload of the configured <c>SitePsks</c> map drops cached keys, so a key
/// rotated in configuration is used without a restart (Gitea #96).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ReloadedSitePsks_DropsTheCache_SoAKeyRotatedInConfigIsUsed()
{
var resolver = Substitute.For<ISecretResolver>();
var options = new CommunicationOptions();
options.SitePsks["site-a"] = "old";
Action<CommunicationOptions, string?>? listener = null;
var monitor = Substitute.For<IOptionsMonitor<CommunicationOptions>>();
monitor.CurrentValue.Returns(_ => options);
monitor.OnChange(Arg.Do<Action<CommunicationOptions, string?>>(l => listener = l));
using var provider = new SitePskProvider(resolver, monitor, NullLogger<SitePskProvider>.Instance);
Assert.Equal("old", await provider.GetAsync("site-a", CancellationToken.None));
options = new CommunicationOptions();
options.SitePsks["site-a"] = "rotated";
Assert.NotNull(listener);
listener!(options, null);
Assert.Equal("rotated", await provider.GetAsync("site-a", CancellationToken.None));
}
/// <summary>Verifies the secret-name to site-id mapping the rotation hook relies on.</summary>
/// <param name="name">A secret name.</param>
/// <param name="expected">The expected site id, or null.</param>
[Theory]
[InlineData("SB-GRPC-PSK-site-a", "site-a")]
[InlineData("SB-GRPC-PSK-", null)]
[InlineData("OTHER-SECRET", null)]
[InlineData("sb-grpc-psk-site-a", "site-a")]
public void SiteIdFromSecretName_MapsOnlyPskNames(string name, string? expected)
{
Assert.Equal(expected, SitePskProvider.SiteIdFromSecretName(name));
}
/// <summary>
/// Verifies that invalidation ignores case: the rotation hook derives the site id from a
/// lowercased secret name, but the site may be cached under its mixed-case identifier.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Invalidate_IgnoresCase_BecauseSecretNamesAreLowercased()
{
var resolver = Substitute.For<ISecretResolver>();
resolver.GetAsync(Arg.Any<SecretName>(), Arg.Any<CancellationToken>())
.Returns("old", "rotated");
var provider = Create(resolver);
Assert.Equal("old", await provider.GetAsync("Site-A", CancellationToken.None));
provider.Invalidate("site-a");
Assert.Equal("rotated", await provider.GetAsync("Site-A", CancellationToken.None));
}
}