From 3b242f5839f9f4e0a438b48dedbf7211b26b1038 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 10:37:02 -0500 Subject: [PATCH] =?UTF-8?q?docs(grpc-data):=20plan=20=C2=A75.2=20condition?= =?UTF-8?q?=5Fkey=20formula=20matches=20ConditionKeys.Derive=20(realm=20by?= =?UTF-8?q?te,=20upper-cased=20cluster)=20+=20golden-vector=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...pc-data-connections-history-alarms-plan.md | 10 ++++-- .../DataConnections/ConditionKeysTests.cs | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/ConditionKeysTests.cs diff --git a/docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md b/docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md index 582faa617..286706c39 100644 --- a/docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md +++ b/docs/plans/2026-11-23-grpc-data-connections-history-alarms-plan.md @@ -570,9 +570,13 @@ addressed in that realm: it is `":"` (`SourceSamplePublisher.cs:32-33`, emitted at `LocalExportCatalog.Compilation.cs:92,123`), so it would disclose the RawPath. -`condition_key = base64url(HMAC-SHA256(K, originClusterId ‖ 0x00 ‖ ConditionId))`, where -K = HKDF(`DataConnections:ConditionKeySecret`, info = `"otopcua/condition-key/v1"`). The secret is a per-origin-cluster -`${secret:}`, identical on both pair members. +`condition_key = base64url(HMAC-SHA256(K, UTF8(upper(originClusterId)) ‖ 0x00 ‖ realm ‖ UTF8(ConditionId)))` (unpadded +base64url, 43 characters), where `upper` is the invariant upper-casing (cluster ids compare OrdinalIgnoreCase), `realm` is one +byte — `0x01` = Raw (a native condition, ConditionId = RawPath), `0x02` = Uns (a scripted condition, ConditionId = +ScriptedAlarmId) — so a native and a scripted condition with equal id strings never share a key, and +K = HKDF-SHA256(IKM = UTF8(`DataConnections:ConditionKeySecret`), salt = empty, info = `"otopcua/condition-key/v1"`, L = 32). +This is exactly `ConditionKeys.Derive` and is pinned by a golden-vector test (`ConditionKeysTests`). The secret is a +per-origin-cluster `${secret:}` of at least 32 UTF-8 bytes, identical on both pair members. - It is stable across restarts, failover and re-deploys. - It is identical across all of the origin's listings and principals, so the consumer dedupes Raw and UNS placements. diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/ConditionKeysTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/ConditionKeysTests.cs new file mode 100644 index 000000000..e9dded73f --- /dev/null +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/ConditionKeysTests.cs @@ -0,0 +1,32 @@ +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; +using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; + +namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; + +/// +/// Golden vectors for the plan's §5.2 condition_key formula: +/// base64url(HMAC-SHA256(K, UTF8(upper(cluster)) ‖ 0x00 ‖ realm ‖ UTF8(ConditionId))) with +/// K = HKDF-SHA256(UTF8(secret), salt = empty, info = "otopcua/condition-key/v1", 32). The expected keys were +/// computed independently of (Python hmac/hashlib), so a change to the +/// derivation — the realm byte, the upper-casing, the separator, the HKDF parameters — turns this red. A changed key +/// re-mints every imported ConditionId on every consumer, so it must never change silently. +/// +[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "A1")] +public sealed class ConditionKeysTests +{ + private const string Secret = "otopcua-golden-vector-secret-0123456789abcdef"; + private const string ConditionId = "Plant/Modbus/dev1/TankHi"; + + [Theory] + [InlineData(AddressSpaceRealm.Raw, "vSnDDtNB8ECednVRpnb1uqlfJ3T3RTOyUwjHmnZH2G4")] + [InlineData(AddressSpaceRealm.Uns, "pfQgj3iDae6TsV9SvYlsBdcNivACEqdHBRP5RNNPAbU")] + public void A_fixed_secret_cluster_realm_and_id_give_a_fixed_key(AddressSpaceRealm realm, string expected) + { + var keys = ConditionKeys.FromSecret(Secret); + keys.Derive("Site-a", realm, ConditionId).ShouldBe(expected); + // The cluster id is upper-cased before hashing: any casing of it gives the same key. + keys.Derive("SITE-A", realm, ConditionId).ShouldBe(expected); + } +}