using System.Text.Json; using Microsoft.Extensions.Configuration; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Host.Configuration; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections; /// /// A1 (§5.2, §6): DataConnections:ConditionKeySecret. Absent ⇒ fail closed (no error, conditions never advertised); /// provisioned but weak ⇒ a startup validation error that never echoes the value; disabled data connections ⇒ not /// checked at all. Never a committed literal. /// [Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "A1")] public sealed class GrpcDataConditionKeySecretTests { private static DataConnectionOptions Enabled(string? secret) => new() { Enabled = true, ListenPort = 9443, CertificatePath = "server.pem", AllowedHosts = ["source.local"], ConditionKeySecret = secret }; [Fact] public void Absent_secret_is_not_an_error_and_a_valid_one_passes() { DataConnectionOptionsValidator.Validate(Enabled(null), development: false).ShouldBeEmpty(); DataConnectionOptionsValidator.Validate(Enabled(""), development: false).ShouldBeEmpty(); DataConnectionOptionsValidator.Validate(Enabled(new string('k', 32)), development: false).ShouldBeEmpty(); } [Theory] [InlineData("too-short-secret")] [InlineData(" ")] [InlineData("${secret:otopcua/dataconnections/condition-key}")] public void A_weak_or_unexpanded_secret_fails_startup_validation_without_echoing_it(string secret) { var errors = DataConnectionOptionsValidator.Validate(Enabled(secret), development: false); var error = errors.ShouldHaveSingleItem(); error.ShouldContain("DataConnections:ConditionKeySecret"); if (secret.Trim().Length > 0) error.ShouldNotContain(secret); } [Fact] public void Disabled_data_connections_do_not_validate_the_secret() { var options = Enabled("short"); options.Enabled = false; DataConnectionOptionsValidator.Validate(options, development: false).ShouldBeEmpty(); } [Fact] public void Secret_binds_from_the_DataConnections_section_like_an_env_var_override() { // DataConnections__ConditionKeySecret (env) and ${secret:} both land on this key before binding. var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["DataConnections:ConditionKeySecret"] = new string('s', 40) }).Build(); var options = configuration.GetSection(DataConnectionOptions.SectionName).Get()!; options.ConditionKeySecret.ShouldBe(new string('s', 40)); ConditionKeys.FromSecret(options.ConditionKeySecret).IsAvailable.ShouldBeTrue(); } [Fact] public void No_committed_appsettings_carries_a_condition_key_secret_literal() { var root = FindRepoRoot(); var files = Directory.EnumerateFiles(Path.Combine(root, "src"), "appsettings*.json", SearchOption.AllDirectories) .Where(f => !f.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}") && !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}")) .ToArray(); files.ShouldNotBeEmpty(); foreach (var file in files) { using var document = JsonDocument.Parse(File.ReadAllText(file), new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true }); if (document.RootElement.TryGetProperty("DataConnections", out var section) && section.TryGetProperty("ConditionKeySecret", out var value) && value.ValueKind == JsonValueKind.String) (value.GetString() ?? string.Empty).ShouldStartWith("${secret:", customMessage: file); } } private static string FindRepoRoot() { var directory = new DirectoryInfo(AppContext.BaseDirectory); while (directory is not null && !Directory.Exists(Path.Combine(directory.FullName, "src", "Server"))) directory = directory.Parent; return directory?.FullName ?? throw new DirectoryNotFoundException("Repository root not found."); } }