docs(grpc-data): plan §5.2 condition_key formula matches ConditionKeys.Derive (realm byte, upper-cased cluster) + golden-vector test

This commit is contained in:
Joseph Doherty
2026-11-24 10:37:02 -05:00
parent f08ca7d9e3
commit 3b242f5839
2 changed files with 39 additions and 3 deletions
@@ -570,9 +570,13 @@ addressed in that realm:
it is `"<len>:<driverInstanceId><RawPath>"` (`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.
@@ -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;
/// <summary>
/// Golden vectors for the plan's §5.2 <c>condition_key</c> formula:
/// <c>base64url(HMAC-SHA256(K, UTF8(upper(cluster)) ‖ 0x00 ‖ realm ‖ UTF8(ConditionId)))</c> with
/// <c>K = HKDF-SHA256(UTF8(secret), salt = empty, info = "otopcua/condition-key/v1", 32)</c>. The expected keys were
/// computed independently of <see cref="ConditionKeys"/> (Python <c>hmac</c>/<c>hashlib</c>), 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.
/// </summary>
[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);
}
}