fix(secrets): SqlServer/Akka registration is order-independent; bump 0.6.6 (scadaproj#30)

AddZbSecretsSqlServerStore / AddZbSecretsSqlServerReplication (and the Akka
extension, same defect class) TryAdd-ed their ISecretStore, ISecretsStoreMigrator
and ISecretReplicator ahead of their internal AddZbSecrets call. An application
that had already called AddZbSecrets therefore silently kept the SQLite store
(shared-store mode) or NoOpSecretReplicator (hub/Akka modes).

They now call AddZbSecrets first and RemoveAll + AddSingleton their own
registrations; a later AddZbSecrets only TryAdds, so either order yields the
same graph with exactly one ISecretStore registration.

- tests: RegistrationOrderTests (SqlServer + Akka) build and resolve the real
  container in both orders; core-first fails without the fix
- Directory.Build.props 0.6.5 -> 0.6.6 (NOT packed or published)
- code-reviews: Secrets-001 Resolved; README regenerated

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 14:47:01 -05:00
parent 1786bab0a0
commit 015c556ced
12 changed files with 263 additions and 31 deletions
@@ -0,0 +1,61 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.Secrets.Abstractions;
using ZB.MOM.WW.Secrets.DependencyInjection;
using ZB.MOM.WW.Secrets.Replication;
using ZB.MOM.WW.Secrets.Replicator.AkkaDotNet.DependencyInjection;
namespace ZB.MOM.WW.Secrets.Replicator.AkkaDotNet.Tests.DependencyInjection;
/// <summary>
/// scadaproj#30 (Secrets-001), Akka half: the package must bind its own replicator and decorator
/// whether the application calls <c>AddZbSecrets</c> before or after it. Until 0.6.6 the package
/// TryAdd-ed its replicator ahead of its internal <c>AddZbSecrets</c>, which fixed the 0.2.0 order
/// bug only for the package's own call — an application that called <c>AddZbSecrets</c> FIRST still
/// got <c>NoOpSecretReplicator</c>. Builds the real container against a joined cluster and resolves.
/// </summary>
public sealed class RegistrationOrderTests(SingleNodeClusterFixture cluster)
: IClassFixture<SingleNodeClusterFixture>
{
public enum Order
{
CoreFirst,
ExtensionFirst,
}
[Theory]
[InlineData(Order.CoreFirst)]
[InlineData(Order.ExtensionFirst)]
public void Replication_is_bound_in_either_order(Order order)
{
string dbPath = Path.Combine(Path.GetTempPath(), $"zb-akka-order-{Guid.NewGuid():N}.db");
IConfigurationRoot config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Secrets:SqlitePath"] = dbPath,
["Secrets:RunMigrationsOnStartup"] = "false",
["Secrets:Replication:AnnounceInterval"] = "00:00:30",
["Secrets:Replication:ActorName"] = $"zb-secret-replication-{order}",
})
.Build();
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton(cluster.System);
if (order == Order.CoreFirst)
{
services.AddZbSecrets(config, "Secrets");
services.AddZbSecretsAkkaReplication(config, "Secrets", "Secrets:Replication");
}
else
{
services.AddZbSecretsAkkaReplication(config, "Secrets", "Secrets:Replication");
services.AddZbSecrets(config, "Secrets");
}
using ServiceProvider provider = services.BuildServiceProvider();
Assert.IsType<AkkaSecretReplicator>(provider.GetRequiredService<ISecretReplicator>());
Assert.IsType<ReplicatingSecretStore>(Assert.Single(provider.GetServices<ISecretStore>()));
}
}
@@ -0,0 +1,49 @@
using Akka.Actor;
using Akka.Cluster;
using Akka.Configuration;
namespace ZB.MOM.WW.Secrets.Replicator.AkkaDotNet.Tests.DependencyInjection;
/// <summary>
/// One self-joined single-node cluster shared by a test class — the miniature app node the
/// replication actor's DistributedPubSub mediator needs. Tests that spawn the actor must give each
/// container its own <c>Secrets:Replication:ActorName</c>, since actor names are unique per system.
/// </summary>
public sealed class SingleNodeClusterFixture : IAsyncLifetime
{
/// <summary>The joined, Up actor system.</summary>
public ActorSystem System { get; private set; } = null!;
/// <inheritdoc />
public async Task InitializeAsync()
{
System = ActorSystem.Create("zb-secrets-order", ConfigurationFactory.ParseString("""
akka {
loglevel = WARNING
actor.provider = cluster
remote.dot-netty.tcp {
hostname = "127.0.0.1"
public-hostname = "127.0.0.1"
port = 0
}
}
""").WithFallback(AkkaSecretsReplication.SerializationConfig));
Cluster cluster = Cluster.Get(System);
cluster.Join(cluster.SelfAddress);
DateTime deadline = DateTime.UtcNow + TimeSpan.FromSeconds(20);
while (cluster.State.Members.All(m => m.Status != MemberStatus.Up))
{
if (DateTime.UtcNow > deadline)
{
throw new TimeoutException("Single-node cluster never reached Up.");
}
await Task.Delay(100);
}
}
/// <inheritdoc />
public async Task DisposeAsync() => await System.Terminate();
}