174 lines
11 KiB
C#
174 lines
11 KiB
C#
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text.Json;
|
|
using Shouldly;
|
|
using ZB.MOM.WW.Secrets.Abstractions;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections;
|
|
|
|
[Trait("Feature", "GrpcData"), Trait("Phase", "P11")]
|
|
public sealed class GrpcDataBundleCommitObserverTests
|
|
{
|
|
[Theory]
|
|
[InlineData("credential-notice")]
|
|
[InlineData("credential-delete")]
|
|
[InlineData("trust-notice")]
|
|
public async Task Scope_only_observation_waits_for_pending_retirement_completion(string failure)
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var store = new GrpcDataSecretProvisioningTests.MemoryStore();
|
|
var cipher = new GrpcDataSecretProvisioningTests.TestCipher();
|
|
var changes = new DataConnectionSecretChangeHandler();
|
|
var credentials = new DataConnectionSecretProvisioner(store, cipher,
|
|
new GrpcDataSecretProvisioningTests.NoopInvalidator(), changes, "cluster", "node");
|
|
var trust = new DataConnectionTrustPolicyStore(store, cipher, changes, "cluster", "node");
|
|
var observer = new DataConnectionBundleCommitObserver(credentials, trust);
|
|
await credentials.SetScopeAsync(9, new Dictionary<string, string> { ["link"] = "secret:data/key" }, ct);
|
|
await trust.ApplyScopeAsync(9, ["link"], ct);
|
|
await credentials.ApplyAsync(new("cluster", "node", 12, [new("link", "secret:data/key", "test-value", false)], 9), ct);
|
|
Action<string> failNotice = _ => throw new InvalidOperationException("controlled notification failure");
|
|
if (failure == "trust-notice")
|
|
{
|
|
await credentials.SetScopeAsync(10, new Dictionary<string, string>(), ct);
|
|
changes.ConnectionChanged += failNotice;
|
|
await Should.ThrowAsync<AggregateException>(() => trust.ApplyScopeAsync(10, [], ct));
|
|
changes.ConnectionChanged -= failNotice;
|
|
}
|
|
else
|
|
{
|
|
await trust.ApplyScopeAsync(10, [], ct);
|
|
if (failure == "credential-notice") changes.ConnectionChanged += failNotice;
|
|
else store.FailDelete = true;
|
|
if (failure == "credential-notice")
|
|
await Should.ThrowAsync<AggregateException>(() => credentials.SetScopeAsync(10, new Dictionary<string, string>(), ct));
|
|
else
|
|
await Should.ThrowAsync<InvalidOperationException>(() => credentials.SetScopeAsync(10, new Dictionary<string, string>(), ct));
|
|
changes.ConnectionChanged -= failNotice;
|
|
store.FailDelete = false;
|
|
}
|
|
var pending = await observer.ObserveAsync("fixture-scope-only", ct);
|
|
if (failure == "trust-notice")
|
|
{ pending.TrustScopeVersion.ShouldBeNull(); pending.CredentialScopeVersion.ShouldBe(10); }
|
|
else
|
|
{ pending.CredentialScopeVersion.ShouldBeNull(); pending.TrustScopeVersion.ShouldBe(10); }
|
|
await credentials.SetScopeAsync(10, new Dictionary<string, string>(), ct);
|
|
await trust.ApplyScopeAsync(10, [], ct);
|
|
var committed = await observer.ObserveAsync("fixture-scope-only", ct);
|
|
committed.CredentialScopeVersion.ShouldBe(10); committed.TrustScopeVersion.ShouldBe(10);
|
|
committed.CredentialVersion.ShouldBeNull(); committed.TrustVersion.ShouldBeNull();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true)]
|
|
[InlineData(false)]
|
|
public async Task Scope_observation_waits_for_interrupted_bundle_notice_and_import_retry(bool credential)
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var store = new GrpcDataSecretProvisioningTests.MemoryStore();
|
|
var cipher = new GrpcDataSecretProvisioningTests.TestCipher();
|
|
var changes = new DataConnectionSecretChangeHandler();
|
|
var credentials = new DataConnectionSecretProvisioner(store, cipher,
|
|
new GrpcDataSecretProvisioningTests.NoopInvalidator(), changes, "cluster", "node");
|
|
var trust = new DataConnectionTrustPolicyStore(store, cipher, changes, "cluster", "node");
|
|
var observer = new DataConnectionBundleCommitObserver(credentials, trust);
|
|
await credentials.SetScopeAsync(9, new Dictionary<string, string> { ["link"] = "secret:data/key" }, ct);
|
|
await trust.ApplyScopeAsync(9, ["link"], ct);
|
|
await credentials.ApplyAsync(new("cluster", "node", 12, [new("link", "secret:data/key", "first", false)], 9), ct);
|
|
using var rsa = RSA.Create(2048);
|
|
var request = new CertificateRequest("CN=fixture", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
|
using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddHours(1));
|
|
await trust.SetAsync("link", 12, [certificate], 9, ct);
|
|
Action<string> failNotice = _ => throw new InvalidOperationException("controlled notification failure");
|
|
changes.ConnectionChanged += failNotice;
|
|
if (credential)
|
|
await Should.ThrowAsync<AggregateException>(() => credentials.ApplyAsync(new("cluster", "node", 13, [new("link", "secret:data/key", "second", false)], 9), ct));
|
|
else
|
|
await Should.ThrowAsync<AggregateException>(() => trust.SetAsync("link", 13, [certificate], 9, ct));
|
|
changes.ConnectionChanged -= failNotice;
|
|
var pending = await observer.ObserveAsync("fixture-scope-only", ct);
|
|
if (credential) { pending.CredentialScopeVersion.ShouldBeNull(); pending.TrustScopeVersion.ShouldBe(9); }
|
|
else { pending.TrustScopeVersion.ShouldBeNull(); pending.CredentialScopeVersion.ShouldBe(9); }
|
|
if (credential)
|
|
await credentials.ApplyAsync(new("cluster", "node", 13, [new("link", "secret:data/key", "second", false)], 9), ct);
|
|
else
|
|
await trust.SetAsync("link", 13, [certificate], 9, ct);
|
|
var committed = await observer.ObserveAsync("fixture-scope-only", ct);
|
|
committed.CredentialScopeVersion.ShouldBe(9); committed.TrustScopeVersion.ShouldBe(9);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Observer_requires_both_committed_versions_and_never_reads_the_secret_payload()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
var store = new GrpcDataSecretProvisioningTests.MemoryStore();
|
|
var cipher = new PayloadGuardCipher();
|
|
var changes = new DataConnectionSecretChangeHandler();
|
|
var credentials = new DataConnectionSecretProvisioner(store, cipher,
|
|
new GrpcDataSecretProvisioningTests.NoopInvalidator(), changes, "cluster", "node");
|
|
var trust = new DataConnectionTrustPolicyStore(store, cipher, changes, "cluster", "node");
|
|
var observer = new DataConnectionBundleCommitObserver(credentials, trust);
|
|
await credentials.SetScopeAsync(9, new Dictionary<string, string> { ["link"] = "secret:data/key" }, ct);
|
|
await trust.ApplyScopeAsync(9, ["link"], ct);
|
|
var empty = await observer.ObserveAsync("link", ct);
|
|
empty.CredentialScopeVersion.ShouldBe(9); empty.TrustScopeVersion.ShouldBe(9);
|
|
empty.CredentialVersion.ShouldBeNull(); empty.CredentialDeleted.ShouldBeNull();
|
|
empty.TrustVersion.ShouldBeNull(); empty.TrustRetired.ShouldBeNull();
|
|
await credentials.ApplyAsync(new("cluster", "node", 12, [new("link", "secret:data/key", "unit-bearer-marker", false)], 9), ct);
|
|
cipher.DenyPayloadReads = true;
|
|
var pendingTrust = await observer.ObserveAsync("link", ct);
|
|
pendingTrust.CredentialVersion.ShouldBe(12); pendingTrust.CredentialDeleted.ShouldBe(false);
|
|
pendingTrust.TrustVersion.ShouldBeNull(); pendingTrust.TrustRetired.ShouldBeNull();
|
|
using var rsa = RSA.Create(2048);
|
|
var request = new CertificateRequest("CN=fixture", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
|
using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddHours(1));
|
|
await trust.SetAsync("link", 12, [certificate], 9, ct);
|
|
var committed = await observer.ObserveAsync("link", ct);
|
|
committed.CredentialScopeVersion.ShouldBe(9); committed.CredentialVersion.ShouldBe(12);
|
|
committed.TrustScopeVersion.ShouldBe(9); committed.TrustVersion.ShouldBe(12);
|
|
committed.CredentialDeleted.ShouldBe(false); committed.TrustRetired.ShouldBe(false);
|
|
JsonSerializer.Serialize(committed).ShouldNotContain("unit-bearer-marker");
|
|
cipher.DenyPayloadReads = false;
|
|
await credentials.ApplyAsync(new("cluster", "node", 13, [new("link", "secret:data/key", "new-bearer-marker", false)], 9), ct);
|
|
cipher.DenyPayloadReads = true;
|
|
var staggered = await observer.ObserveAsync("link", ct);
|
|
staggered.CredentialVersion.ShouldBe(13); staggered.TrustVersion.ShouldBe(12);
|
|
cipher.DenyPayloadReads = false;
|
|
await credentials.ApplyAsync(new("cluster", "node", 14, [new("link", "secret:data/key", null, true)], 9), ct);
|
|
await trust.SetAsync("link", 14, [], 9, ct);
|
|
cipher.DenyPayloadReads = true;
|
|
var tombstones = await observer.ObserveAsync("link", ct);
|
|
tombstones.CredentialVersion.ShouldBe(14); tombstones.CredentialDeleted.ShouldBe(true);
|
|
tombstones.TrustVersion.ShouldBe(14); tombstones.TrustRetired.ShouldBe(true);
|
|
await credentials.SetScopeAsync(10, new Dictionary<string, string>(), ct);
|
|
await trust.ApplyScopeAsync(10, [], ct);
|
|
var retired = await observer.ObserveAsync("link", ct);
|
|
retired.CredentialScopeVersion.ShouldBe(10); retired.TrustScopeVersion.ShouldBe(10);
|
|
retired.CredentialVersion.ShouldBeNull(); retired.CredentialDeleted.ShouldBeNull();
|
|
retired.TrustVersion.ShouldBeNull(); retired.TrustRetired.ShouldBeNull();
|
|
}
|
|
|
|
private sealed class PayloadGuardCipher : ISecretCipher
|
|
{
|
|
private readonly GrpcDataSecretProvisioningTests.TestCipher _inner = new();
|
|
public bool DenyPayloadReads { get; set; }
|
|
public StoredSecret Encrypt(SecretName name, string plaintext, SecretContentType contentType) => _inner.Encrypt(name, plaintext, contentType);
|
|
public string Decrypt(StoredSecret secret)
|
|
{
|
|
if (DenyPayloadReads && secret.Name.Value == "data/key") throw new InvalidOperationException("Observer read secret payload");
|
|
return _inner.Decrypt(secret);
|
|
}
|
|
public StoredSecret Rewrap(StoredSecret secret, IMasterKeyProvider oldKek, IMasterKeyProvider newKek) => _inner.Rewrap(secret, oldKek, newKek);
|
|
}
|
|
|
|
[Fact]
|
|
public void Host_exposes_a_typed_bounded_committed_bundle_observer()
|
|
{
|
|
var type = typeof(DataConnectionSecretProvisioner).Assembly.GetType(
|
|
"ZB.MOM.WW.OtOpcUa.Host.Configuration.DataConnectionBundleCommitObserver");
|
|
type.ShouldNotBeNull("the fixture needs committed credential and trust versions before declaring the deployed scope ready");
|
|
type.GetMethod("ObserveAsync").ShouldNotBeNull();
|
|
}
|
|
}
|