b7ca71f188
#916: Metadata.stable_source_id carried the internal acquisition key (<len>:<driverInstanceId><RawPath>) on every export, so a UNS-only peer learned driver ids and RawPaths. No consumer uses the field beyond byte accounting; it is optional on the wire and absent decodes as null. It is now never sent; the internal BackingSourceKey is unchanged. #917: alarm-bearing raw tags never get a value but were exported as Variables. They are now the existing explicit UnsupportedVariable kind (browsable, Unsupported on every value op). The node is kept so exports rooted at it still compile and A1 condition placements are unaffected. The real-origin v1 goldens are not re-recorded: GrpcDataV1RealOriginCorrections applies exactly these two edits to the parsed goldens, pinned by a test. Evidence: docs/plans/evidence/grpc-data-ext/V1-916-917.md
117 lines
6.4 KiB
C#
117 lines
6.4 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
/// <summary>
|
|
/// A1 (§5.2 "Dedupe key", N3, m-T6): mints the opaque <c>condition_key</c> an origin puts on every condition record.
|
|
/// <para>
|
|
/// <c>condition_key = base64url(HMAC-SHA256(K, upper(originClusterId) ‖ 0x00 ‖ realm ‖ ConditionId))</c>, with
|
|
/// <c>K = HKDF-SHA256(DataConnections:ConditionKeySecret, info = "otopcua/condition-key/v1")</c>. The realm byte
|
|
/// (1 = Raw, 2 = Uns — the node manager's condition realm key) is an A1 addition to the plan's formula: a native
|
|
/// ConditionId is a RawPath and a scripted one a ScriptedAlarmId, and nothing stops the two spaces from sharing a
|
|
/// string, so the realm keeps two distinct conditions from sharing a key.
|
|
/// </para>
|
|
/// <para>
|
|
/// The key depends only on the secret, the origin cluster and the condition: it is stable across restarts,
|
|
/// failover and re-deploys, identical on both pair members (the secret is a per-cluster <c>${secret:}</c>), and
|
|
/// equal across every listing and principal (m-T6), so a consumer dedupes a condition's Raw and UNS placements.
|
|
/// Being keyed, it cannot be dictionary-reversed from guessable RawPaths, and it carries no RawPath or driver id
|
|
/// (the v1 <c>Metadata.stable_source_id</c> did, and is no longer sent, #916).
|
|
/// </para>
|
|
/// <para>
|
|
/// Without a secret the origin advertises no <c>conditions</c> capability (fail closed): <see cref="IsAvailable"/>
|
|
/// is false and <see cref="UnavailableReason"/> is the diagnostic. A present but unusable secret is a configuration
|
|
/// error (<see cref="SecretProblem"/>, enforced by the host's DataConnections options validator).
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed class ConditionKeys
|
|
{
|
|
/// <summary>The configuration key, for diagnostics.</summary>
|
|
public const string ConfigurationKey = "DataConnections:ConditionKeySecret";
|
|
|
|
/// <summary>Minimum secret length in UTF-8 bytes (256 bits of input keying material).</summary>
|
|
public const int MinimumSecretBytes = 32;
|
|
|
|
/// <summary>HKDF <c>info</c>: versions the derivation, so a future scheme cannot collide with this one.</summary>
|
|
public const string DerivationInfo = "otopcua/condition-key/v1";
|
|
|
|
/// <summary>The fail-closed diagnostic when no secret is provisioned.</summary>
|
|
public const string NotProvisionedReason =
|
|
ConfigurationKey + " is not provisioned; this origin does not advertise the conditions capability.";
|
|
|
|
private readonly byte[]? _key;
|
|
|
|
private ConditionKeys(byte[]? key, string? unavailableReason)
|
|
{
|
|
_key = key;
|
|
UnavailableReason = unavailableReason;
|
|
}
|
|
|
|
/// <summary>An instance that never derives (no secret).</summary>
|
|
public static ConditionKeys Unavailable { get; } = new(null, NotProvisionedReason);
|
|
|
|
/// <summary>Whether a secret is provisioned and keys can be derived.</summary>
|
|
public bool IsAvailable => _key is not null;
|
|
|
|
/// <summary>Why <see cref="IsAvailable"/> is false; null when available.</summary>
|
|
public string? UnavailableReason { get; }
|
|
|
|
/// <summary>True when a secret value is configured at all (null/empty means "not provisioned", never an error).</summary>
|
|
public static bool IsProvisioned(string? secret) => !string.IsNullOrEmpty(secret);
|
|
|
|
/// <summary>
|
|
/// The configuration error for a PROVISIONED but unusable secret, or null (absent or valid). Never echoes the value.
|
|
/// </summary>
|
|
public static string? SecretProblem(string? secret)
|
|
{
|
|
if (!IsProvisioned(secret)) return null;
|
|
if (secret!.Contains("${secret:", StringComparison.OrdinalIgnoreCase))
|
|
return ConfigurationKey + " still holds an unexpanded ${secret:} reference.";
|
|
if (string.IsNullOrWhiteSpace(secret) || secret.Any(char.IsControl))
|
|
return ConfigurationKey + " must not be blank or contain control characters.";
|
|
if (Encoding.UTF8.GetByteCount(secret) < MinimumSecretBytes)
|
|
return $"{ConfigurationKey} must be at least {MinimumSecretBytes} bytes; generate a random per-cluster secret.";
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds the deriver from the configured secret: <see cref="Unavailable"/> when absent; throws
|
|
/// <see cref="ArgumentException"/> (without the value) when provisioned but unusable.
|
|
/// </summary>
|
|
public static ConditionKeys FromSecret(string? secret)
|
|
{
|
|
if (!IsProvisioned(secret)) return Unavailable;
|
|
if (SecretProblem(secret) is { } problem) throw new ArgumentException(problem, nameof(secret));
|
|
var material = Encoding.UTF8.GetBytes(secret!);
|
|
try
|
|
{
|
|
return new(HKDF.DeriveKey(HashAlgorithmName.SHA256, material, 32, salt: [],
|
|
info: Encoding.UTF8.GetBytes(DerivationInfo)), null);
|
|
}
|
|
finally { CryptographicOperations.ZeroMemory(material); }
|
|
}
|
|
|
|
/// <summary>The key for one origin condition; throws when <see cref="IsAvailable"/> is false.</summary>
|
|
public string Derive(string originClusterId, AddressSpaceRealm realm, string conditionId)
|
|
{
|
|
if (_key is null) throw new InvalidOperationException(UnavailableReason);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(originClusterId);
|
|
ArgumentException.ThrowIfNullOrEmpty(conditionId);
|
|
if (originClusterId.Contains('\0')) throw new ArgumentException("Cluster ids cannot contain NUL.", nameof(originClusterId));
|
|
if (realm is not (AddressSpaceRealm.Raw or AddressSpaceRealm.Uns)) throw new ArgumentOutOfRangeException(nameof(realm));
|
|
var cluster = Encoding.UTF8.GetBytes(originClusterId.ToUpperInvariant());
|
|
var condition = Encoding.UTF8.GetBytes(conditionId);
|
|
var message = new byte[cluster.Length + 2 + condition.Length];
|
|
cluster.CopyTo(message, 0);
|
|
message[cluster.Length] = 0;
|
|
message[cluster.Length + 1] = realm == AddressSpaceRealm.Raw ? (byte)1 : (byte)2;
|
|
condition.CopyTo(message, cluster.Length + 2);
|
|
var key = Convert.ToBase64String(HMACSHA256.HashData(_key, message)).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
|
System.Diagnostics.Debug.Assert(ImportedConditionIds.IsWellFormedConditionKey(key));
|
|
return key;
|
|
}
|
|
}
|