513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
221 lines
11 KiB
C#
221 lines
11 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Artifacts;
|
|
using ZB.MOM.WW.Secrets.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager.Tests;
|
|
|
|
/// <summary>
|
|
/// <see cref="SiteSecretsCollector"/> is the central half of design §3.1/§3.3: derive which
|
|
/// secrets a site's data connections reference, resolve each against the central
|
|
/// <see cref="ISecretStore"/>, and report the ones that could not be resolved. It never sees
|
|
/// or produces plaintext — everything here is ciphertext bytes.
|
|
/// </summary>
|
|
public class SiteSecretsCollectorTests
|
|
{
|
|
private static DataConnectionArtifact Dc(string name, string? primary, string? backup = null)
|
|
=> new(name, "MxGateway", primary, backup, 3);
|
|
|
|
private static StoredSecret Row(string name, long revision, bool deleted = false, string? description = null) => new()
|
|
{
|
|
Name = new SecretName(name),
|
|
Description = description,
|
|
ContentType = SecretContentType.Text,
|
|
Ciphertext = [1, 2, 3],
|
|
Nonce = [4, 5],
|
|
Tag = [6, 7],
|
|
WrappedDek = [8, 9],
|
|
WrapNonce = [10, 11],
|
|
WrapTag = [12, 13],
|
|
KekId = "kek-1",
|
|
Revision = revision,
|
|
IsDeleted = deleted,
|
|
CreatedUtc = DateTimeOffset.Parse("2026-01-01T00:00:00Z"),
|
|
UpdatedUtc = DateTimeOffset.Parse("2026-01-02T00:00:00Z"),
|
|
};
|
|
|
|
/// <summary>Verifies that collect returns only referenced rows and names missing.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_ReturnsOnlyReferencedRows_AndNamesMissing()
|
|
{
|
|
var store = new FakeSecretStore(Row("a", 3), Row("b", 1, deleted: true), Row("other", 1));
|
|
|
|
var result = await new SiteSecretsCollector(store).CollectAsync(
|
|
[Dc("GW1", """{"ApiKey":"secret:a"}"""), Dc("GW2", """{"ApiKey":"secret:b"}""")], default);
|
|
|
|
// Only the referenced names come back — "other" is in the store but never referenced.
|
|
Assert.Equal(["a"], result.Secrets.Select(s => s.Name));
|
|
Assert.Equal(3, result.Secrets[0].Revision);
|
|
// "b" is tombstoned (IsDeleted) — treated the same as absent.
|
|
Assert.Equal(["b"], result.Missing.Select(m => m.Name));
|
|
Assert.Equal(["GW2"], result.Missing[0].ReferencedBy);
|
|
}
|
|
|
|
/// <summary>Verifies that collect resolved row maps every field from the stored secret verbatim.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_ResolvedRow_MapsEveryFieldFromTheStoredSecretVerbatim()
|
|
{
|
|
var row = Row("mxgw-key", revision: 7, description: "MxGateway API key");
|
|
var store = new FakeSecretStore(row);
|
|
|
|
var result = await new SiteSecretsCollector(store).CollectAsync(
|
|
[Dc("GW1", """{"ApiKey":"secret:mxgw-key"}""")], default);
|
|
|
|
var artifact = Assert.Single(result.Secrets);
|
|
Assert.Empty(result.Missing);
|
|
Assert.Equal(row.Name.Value, artifact.Name);
|
|
Assert.Equal(row.ContentType.ToString(), artifact.ContentType);
|
|
Assert.Equal(row.Ciphertext, artifact.Ciphertext);
|
|
Assert.Equal(row.Nonce, artifact.Nonce);
|
|
Assert.Equal(row.Tag, artifact.Tag);
|
|
Assert.Equal(row.WrappedDek, artifact.WrappedDek);
|
|
Assert.Equal(row.WrapNonce, artifact.WrapNonce);
|
|
Assert.Equal(row.WrapTag, artifact.WrapTag);
|
|
Assert.Equal(row.KekId, artifact.KekId);
|
|
Assert.Equal(row.Revision, artifact.Revision);
|
|
Assert.Equal(row.CreatedUtc, artifact.CreatedUtc);
|
|
Assert.Equal(row.UpdatedUtc, artifact.UpdatedUtc);
|
|
Assert.Equal(row.Description, artifact.Description);
|
|
}
|
|
|
|
/// <summary>Verifies that collect absent name is reported missing not thrown.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_AbsentName_IsReportedMissing_NotThrown()
|
|
{
|
|
var store = new FakeSecretStore();
|
|
|
|
var result = await new SiteSecretsCollector(store).CollectAsync(
|
|
[Dc("GW1", """{"ApiKey":"secret:never-created"}""")], default);
|
|
|
|
Assert.Empty(result.Secrets);
|
|
var missing = Assert.Single(result.Missing);
|
|
Assert.Equal("never-created", missing.Name);
|
|
Assert.Equal(["GW1"], missing.ReferencedBy);
|
|
}
|
|
|
|
/// <summary>Verifies that collect no secret references returns empty without querying the store.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_NoSecretReferences_ReturnsEmptyWithoutQueryingTheStore()
|
|
{
|
|
var store = new FakeSecretStore(Row("unused", 1));
|
|
|
|
var result = await new SiteSecretsCollector(store).CollectAsync(
|
|
[Dc("GW1", """{"Endpoint":"http://gw:5000"}""")], default);
|
|
|
|
Assert.Empty(result.Secrets);
|
|
Assert.Empty(result.Missing);
|
|
}
|
|
|
|
/// <summary>Verifies that collect store throws returns store error instead of throwing.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_StoreThrows_ReturnsStoreError_InsteadOfThrowing()
|
|
{
|
|
// One site's store read failing must not abort a whole sweep.
|
|
var store = new FakeSecretStore(Row("a", 1)) { Fault = new InvalidOperationException("db down") };
|
|
|
|
var result = await new SiteSecretsCollector(store).CollectAsync(
|
|
[Dc("GW1", """{"ApiKey":"secret:a"}""")], default);
|
|
|
|
Assert.NotNull(result.StoreError);
|
|
Assert.Contains("'a'", result.StoreError);
|
|
Assert.Contains("InvalidOperationException: db down", result.StoreError);
|
|
}
|
|
|
|
/// <summary>Verifies that collect caller cancellation still propagates.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_CallerCancellation_StillPropagates()
|
|
{
|
|
using var cts = new CancellationTokenSource();
|
|
cts.Cancel();
|
|
var store = new FakeSecretStore(Row("a", 1)) { Fault = new OperationCanceledException(cts.Token) };
|
|
|
|
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => new SiteSecretsCollector(store).CollectAsync(
|
|
[Dc("GW1", """{"ApiKey":"secret:a"}""")], cts.Token));
|
|
}
|
|
|
|
/// <summary>Verifies that collect success has no store error.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task Collect_Success_HasNoStoreError()
|
|
{
|
|
var result = await new SiteSecretsCollector(new FakeSecretStore(Row("a", 1))).CollectAsync(
|
|
[Dc("GW1", """{"ApiKey":"secret:a"}""")], default);
|
|
|
|
Assert.Null(result.StoreError);
|
|
}
|
|
|
|
/// <summary>
|
|
/// In-memory <see cref="ISecretStore"/> stand-in. Implements only <see cref="GetAsync"/> —
|
|
/// the sole member <see cref="SiteSecretsCollector"/> calls; every other member throws so an
|
|
/// accidental call (a write, a list, a replication apply) fails the test loudly instead of
|
|
/// silently no-op'ing.
|
|
/// </summary>
|
|
private sealed class FakeSecretStore : ISecretStore
|
|
{
|
|
private readonly Dictionary<SecretName, StoredSecret> _rows;
|
|
|
|
/// <summary>Initializes a new instance of <c>FakeSecretStore</c>.</summary>
|
|
/// <param name="rows">The rows.</param>
|
|
public FakeSecretStore(params StoredSecret[] rows) => _rows = rows.ToDictionary(r => r.Name);
|
|
|
|
/// <summary>When set, <see cref="GetAsync"/> throws this instead of reading.</summary>
|
|
public Exception? Fault { get; init; }
|
|
|
|
/// <summary>Returns the canned secret row, or faults when a fault is configured.</summary>
|
|
/// <param name="name">The secret name to look up.</param>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that yields the stored secret, or null.</returns>
|
|
public Task<StoredSecret?> GetAsync(SecretName name, CancellationToken ct) =>
|
|
Fault is not null
|
|
? Task.FromException<StoredSecret?>(Fault)
|
|
: Task.FromResult(_rows.TryGetValue(name, out var row) ? row : null);
|
|
|
|
/// <summary>Not supported by this fake.</summary>
|
|
/// <param name="row">The secret row, unused.</param>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public Task UpsertAsync(StoredSecret row, CancellationToken ct) => throw new NotSupportedException();
|
|
|
|
/// <summary>Not supported by this fake.</summary>
|
|
/// <param name="name">The secret name, unused.</param>
|
|
/// <param name="actor">The acting user, unused.</param>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that yields whether a row was deleted.</returns>
|
|
public Task<bool> DeleteAsync(SecretName name, string? actor, CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
|
|
/// <summary>Not supported by this fake.</summary>
|
|
/// <param name="includeDeleted">Whether tombstoned secrets are included.</param>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that yields the secret metadata.</returns>
|
|
public Task<IReadOnlyList<SecretMetadata>> ListAsync(bool includeDeleted, CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
|
|
/// <summary>Not supported by this fake.</summary>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that yields the secret manifest.</returns>
|
|
public Task<IReadOnlyList<SecretManifestEntry>> GetManifestAsync(CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
|
|
/// <summary>Not supported by this fake.</summary>
|
|
/// <param name="row">The replicated secret row, unused.</param>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public Task ApplyReplicatedAsync(StoredSecret row, CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
|
|
/// <summary>Not supported by this fake.</summary>
|
|
/// <param name="rewrappedRow">The rewrapped secret row, unused.</param>
|
|
/// <param name="expectedCurrentWrappedDek">The wrapped data key expected to be current, unused.</param>
|
|
/// <param name="ct">A token to cancel the operation.</param>
|
|
/// <returns>A task that yields whether the rewrap was applied.</returns>
|
|
public Task<bool> ApplyRewrapAsync(
|
|
StoredSecret rewrappedRow, byte[] expectedCurrentWrappedDek, CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|