feat(galaxy): #1074 secured writes over gRPC data connections are a separate opt-in, default off

- WriteCaller gains an additive Origin (WriteCallerOrigin: Unspecified (default) | OpcUaSession | DataConnection),
  stamped by the runtime at the receiving seam: OtOpcUaNodeManager -> OpcUaSession, the rich (gRPC data-connection)
  write path -> DataConnection. Never parsed from the principal/key.
- New driver-config option MxAccess.SecuredWritesOverDataConnections (default false; factory key
  mxAccess.securedWritesOverDataConnections; AdminUI "Also for gRPC data-connection writes", owned path). The
  Galaxy driver gives a caller an identity source only in ApiKeyIdentity mode AND for an allowed origin:
  OpcUaSession always, DataConnection only with the option, Unspecified never (fail closed). Off => a secured write
  over a data connection is refused exactly as before #1074 (local BadUserAccessDenied, no identity lookup; the gRPC
  caller sees the same OutcomeUnknown as before).
- Tests: option off refuses a gRPC write end to end into the real GalaxyDriver (with an OPC UA positive control on
  the same driver); option-on end-to-end kept; driver-level origin matrix incl. Unspecified; factory binding; AdminUI
  round-trip; node-manager callers now carry OpcUaSession.
- Docs: Galaxy.md (origin table, opt-in, forwarded-write delegation caveat, live-gate leg), security.md,
  DataConnections.md, Configuration.md, rules/drivers.md.
This commit is contained in:
Joseph Doherty
2026-12-03 16:47:23 -05:00
parent be58573822
commit 29ad36c028
17 changed files with 295 additions and 56 deletions
@@ -7,7 +7,31 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions;
/// </summary>
/// <param name="Principal">The ACL principal (e.g. the key's principal, or <c>anonymous</c>); null when unknown.</param>
/// <param name="ApiKeyId">The authenticating API key's public id; null for anonymous.</param>
public sealed record WriteCaller(string? Principal, string? ApiKeyId);
/// <param name="Origin">
/// #1074 — which data plane the write arrived on. Set by the runtime from where the write was RECEIVED (the OPC UA
/// node manager or the gRPC data-connection admission), never parsed out of the principal or key id. The default,
/// <see cref="WriteCallerOrigin.Unspecified" />, must be treated as untrusted by a driver that grants anything on the
/// caller's identity (fail closed): a call site that forgets to say where a write came from gets nothing.
/// </param>
public sealed record WriteCaller(string? Principal, string? ApiKeyId,
WriteCallerOrigin Origin = WriteCallerOrigin.Unspecified);
/// <summary>#1074 — the data plane a <see cref="WriteCaller" />'s write arrived on.</summary>
public enum WriteCallerOrigin
{
/// <summary>Not stated. A driver acting on the caller's identity treats it as no identity (fail closed).</summary>
Unspecified = 0,
/// <summary>An OPC UA client session on this server (the session's API key, or anonymous).</summary>
OpcUaSession = 1,
/// <summary>
/// A gRPC data-connection client: the key the CONNECTION authenticated with. That can be a direct client
/// (e.g. ScadaBridge) or another OtOpcUa cluster forwarding its own OPC UA clients' writes (a projected node's
/// write-through) — in which case the key is the link's service key, not the end user.
/// </summary>
DataConnection = 2,
}
/// <summary>
/// Optional companion to <see cref="IWritable" /> for a driver whose device enforces a per-USER write
@@ -184,6 +184,17 @@ public sealed record GalaxyMxAccessOptions(
/// <c>VerifiedWrite</c> attribute is refused in every mode.
/// </summary>
public GalaxySecuredWriteMode SecuredWrites { get; init; } = GalaxySecuredWriteMode.Refuse;
/// <summary>
/// #1074 — whether <see cref="GalaxySecuredWriteMode.ApiKeyIdentity"/> also applies to writes arriving over a
/// gRPC data connection. Default <c>false</c>: such a SecuredWrite is refused <c>BadUserAccessDenied</c> locally,
/// exactly as in <see cref="GalaxySecuredWriteMode.Refuse"/>, and only OPC UA sessions get the caller's identity.
/// ⚠️ When on, the identity is the CONNECTION's key: a consumer OtOpcUa cluster forwarding its OPC UA clients'
/// writes (projected-node write-through) presents its link's service key, so every client allowed to write the
/// projected node writes secured attributes as that key's Galaxy identity. Ignored unless
/// <see cref="SecuredWrites"/> is <see cref="GalaxySecuredWriteMode.ApiKeyIdentity"/>.
/// </summary>
public bool SecuredWritesOverDataConnections { get; init; }
}
/// <summary>How the Galaxy driver handles a write to a <c>SecuredWrite</c> attribute (#1017).</summary>
@@ -2385,8 +2385,9 @@ public sealed class GalaxyDriver
/// <summary>
/// #1017 — <see cref="WriteAsync(IReadOnlyList{WriteRequest}, CancellationToken)"/> on behalf of
/// <paramref name="caller"/>. With <c>MxAccess.SecuredWrites = ApiKeyIdentity</c> a SecuredWrite attribute is
/// written as the caller's API key's own Galaxy identity (no fallback account); otherwise — and for every
/// VerifiedWrite attribute — it is refused <c>BadUserAccessDenied</c> as before. Non-secured tags are
/// written as the caller's API key's own Galaxy identity (no fallback account) — for an OPC UA session, and for a
/// gRPC data connection only with <c>MxAccess.SecuredWritesOverDataConnections</c> (#1074); otherwise — and for
/// every VerifiedWrite attribute — it is refused <c>BadUserAccessDenied</c> as before. Non-secured tags are
/// unaffected by the caller.
/// </summary>
/// <param name="writes">Pairs of full reference (RawPath) + value.</param>
@@ -2424,8 +2425,10 @@ public sealed class GalaxyDriver
// #1017: only an opted-in driver with a known caller carries an identity source; the writer resolves it
// lazily (at most once per batch, only for a SecuredWrite tag). Key-only — never the ack fallback account.
// No source ⇒ the writer refuses a SecuredWrite BadUserAccessDenied locally (the #991 behaviour).
Func<CancellationToken, Task<GalaxyAckCredentialResolution>>? securedCredential = null;
if (caller is not null && _options.MxAccess.SecuredWrites == GalaxySecuredWriteMode.ApiKeyIdentity)
if (caller is not null && _options.MxAccess.SecuredWrites == GalaxySecuredWriteMode.ApiKeyIdentity
&& IsSecuredWriteOriginAllowed(caller.Origin))
{
var apiKeyId = caller.ApiKeyId;
securedCredential = ct => _ackCredentialResolver.ResolveKeyIdentityAsync(apiKeyId, ct);
@@ -2435,6 +2438,18 @@ public sealed class GalaxyDriver
dialledWrites, attributeRef => ResolveSecurity(ToRawPath(attributeRef)), securedCredential, cancellationToken);
}
/// <summary>
/// #1074 — which data planes a SecuredWrite may run under the caller's identity on. An OPC UA session always
/// (the #1017 opt-in alone); a gRPC data connection only with <c>MxAccess.SecuredWritesOverDataConnections</c>
/// (its key may be a link forwarding other clients' writes); an unstated origin never (fail closed).
/// </summary>
private bool IsSecuredWriteOriginAllowed(WriteCallerOrigin origin) => origin switch
{
WriteCallerOrigin.OpcUaSession => true,
WriteCallerOrigin.DataConnection => _options.MxAccess.SecuredWritesOverDataConnections,
_ => false,
};
// ===== ISubscribable (PR 4.4) =====
/// <inheritdoc />
@@ -118,6 +118,7 @@ public static class GalaxyDriverFactoryExtensions
EventPumpChannelCapacity: dto.MxAccess.EventPumpChannelCapacity ?? 50_000)
{
SecuredWrites = ParseSecuredWrites(dto.MxAccess.SecuredWrites, driverInstanceId),
SecuredWritesOverDataConnections = dto.MxAccess.SecuredWritesOverDataConnections ?? false,
},
Repository: new GalaxyRepositoryOptions(
DiscoverPageSize: dto.Repository?.DiscoverPageSize ?? 5000,
@@ -241,6 +242,8 @@ public static class GalaxyDriverFactoryExtensions
public int? EventPumpChannelCapacity { get; init; }
/// <summary>Gets or sets the SecuredWrite handling (#1017): <c>Refuse</c> (default) or <c>ApiKeyIdentity</c>.</summary>
public string? SecuredWrites { get; init; }
/// <summary>#1074: whether <c>ApiKeyIdentity</c> also applies to gRPC data-connection writes (default false).</summary>
public bool? SecuredWritesOverDataConnections { get; init; }
}
/// <summary>Repository configuration section.</summary>
@@ -112,6 +112,18 @@
<code>VerifiedWrite</code> attributes are always refused.
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch mt-4">
<InputCheckbox aria-describedby="@FieldIds("field-11b-hint")" @bind-Value="_form.Galaxy.MxSecuredWritesOverDataConnections" @bind-Value:after="EmitAsync" class="form-check-input" id="@FieldId("field-11b")" disabled="@(_form.Galaxy.MxSecuredWrites != GalaxySecuredWriteMode.ApiKeyIdentity)" />
<label class="form-check-label" for="@FieldId("field-11b")">Also for gRPC data-connection writes</label>
</div>
<div id="@FieldId("field-11b-hint")" class="form-text">
Off (default): a secured write arriving over a data connection is refused, and only OPC UA sessions write
as their key's identity (#1074). On: it runs as the <em>connection's</em> key. A consumer cluster forwarding
its own clients' writes presents its link's service key, so every client allowed to write the projected
node would write secured attributes as that key's Galaxy identity.
</div>
</div>
</div>
</div>
</section>
@@ -254,6 +266,7 @@
"gateway.keepAlivePingDelaySeconds", "gateway.keepAlivePingTimeoutSeconds",
"mxAccess.clientName", "mxAccess.publishingIntervalMs", "mxAccess.writeUserId", "mxAccess.eventPumpChannelCapacity",
"mxAccess.securedWrites",
"mxAccess.securedWritesOverDataConnections",
"repository.discoverPageSize", "repository.watchDeployEvents",
"reconnect.initialBackoffMs", "reconnect.maxBackoffMs", "reconnect.replayOnSessionLost",
"probeTimeoutSeconds",
@@ -334,6 +347,7 @@
public int MxWriteUserId { get; set; } = 0;
public int MxEventPumpChannelCapacity { get; set; } = 50_000;
public GalaxySecuredWriteMode MxSecuredWrites { get; set; } = GalaxySecuredWriteMode.Refuse;
public bool MxSecuredWritesOverDataConnections { get; set; }
// GalaxyRepositoryOptions
public int RepositoryDiscoverPageSize { get; set; } = 5000;
@@ -376,6 +390,7 @@
MxWriteUserId = mx.WriteUserId,
MxEventPumpChannelCapacity = mx.EventPumpChannelCapacity,
MxSecuredWrites = mx.SecuredWrites,
MxSecuredWritesOverDataConnections = mx.SecuredWritesOverDataConnections,
RepositoryDiscoverPageSize = repo.DiscoverPageSize,
RepositoryWatchDeployEvents = repo.WatchDeployEvents,
ReconnectInitialBackoffMs = rc.InitialBackoffMs,
@@ -407,6 +422,7 @@
EventPumpChannelCapacity: MxEventPumpChannelCapacity)
{
SecuredWrites = MxSecuredWrites,
SecuredWritesOverDataConnections = MxSecuredWritesOverDataConnections,
},
Repository: new GalaxyRepositoryOptions(
DiscoverPageSize: RepositoryDiscoverPageSize,
@@ -3559,7 +3559,8 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
return DispatchDeviceWrite(gateway, nodeKey, nodeBareId, nodeRealm, optimisticValue, priorValue, priorStatus, clientUserId,
(node as EquipmentVariableState)?.ActiveWriteStamp,
// #1017: WHO wrote rides to the driver, so a per-user device permission (Galaxy SecuredWrite) can act on it.
identity is null ? null : new WriteCaller(identity.Principal.Length > 0 ? identity.Principal : null, identity.ApiKeyId));
identity is null ? null : new WriteCaller(identity.Principal.Length > 0 ? identity.Principal : null, identity.ApiKeyId,
WriteCallerOrigin.OpcUaSession));
}
/// <summary>
@@ -1580,11 +1580,14 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers
/// <summary>
/// #1074 — who issued a gRPC data-connection write, in the same shape the OPC UA node manager builds for a
/// session (#1017): the admitted principal and the public id of the API key the connection authenticated with.
/// Both are mandatory on an admission (<see cref="DataWriteAdmission.Validate"/>) and carry no secret.
/// Both are mandatory on an admission (<see cref="DataWriteAdmission.Validate"/>) and carry no secret. Marked
/// <see cref="WriteCallerOrigin.DataConnection"/> so a driver can hold this plane to its own opt-in (Galaxy
/// <c>MxAccess.SecuredWritesOverDataConnections</c>, default off).
/// </summary>
internal static WriteCaller RichCaller(DataWriteAdmission admission) =>
new(string.IsNullOrWhiteSpace(admission.PrincipalId) ? null : admission.PrincipalId,
string.IsNullOrWhiteSpace(admission.ApiKeyId) ? null : admission.ApiKeyId);
string.IsNullOrWhiteSpace(admission.ApiKeyId) ? null : admission.ApiKeyId,
WriteCallerOrigin.DataConnection);
private static async Task ObserveRichDriver(Task<IReadOnlyList<DataWriteOutcome>> task,
TaskCompletionSource<DataWriteOutcome> physical)