feat(deploy): #971 follow-up deploy-time script compile gate
The deploy gate never compiled scripts, so a stored script the #971 allow-list now refuses only failed after the deploy (VirtualTag/Calculation Bad "sandbox violation", scripted-alarm load failing for every alarm on the node). - Core.Scripting.Abstractions: IDeployScriptChecker (Roslyn-free) + request/result records; ScriptContextKind moved here (same namespace). - Core.Scripting: ScriptEvaluator.Verify shares a private BuildCheckedCompilation factory with CompileToImage (wrap, injection guard, sandbox compile, ForbiddenTypeAnalyzer; no stack-guard rewrite, no emit). RoslynDeployScriptChecker checks per seam (VT/Calc as VirtualTagContext->object?, passthrough/blank skipped; predicates DependencyExtractor first then AlarmPredicateContext->bool), caches by (compile identity, SHA-256), parallel, bounded. - ControlPlane: DeployScriptGate collects enabled VTs / enabled alarm predicates / Calculation tags, resolves {{equip}} through EquipmentReferenceMap exactly as the compose seams do (template compiled once when SubstitutionPreservesCompilation, else the resolved source), groups findings per script naming its users. AdminOperationsActor warms the cache before the snapshot transaction and runs the authoritative pass on the same snapshot; Deployment:ScriptCompileGateMode = Error (default) | Warn | Off; an absent checker or an internal failure never blocks. - Host binds RoslynDeployScriptChecker on admin nodes. - Docs: VirtualTags.md (gate + upgrade note), ScriptEditor.md, security.md, CLAUDE.md. Found while testing: {{equip}} substitution pastes RawPaths into C# literals unescaped, so a segment containing \ or " breaks (or silently alters) the script; the gate now reports it at deploy.
This commit is contained in:
@@ -28,6 +28,11 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
private readonly IReadOnlyDictionary<string, IDriverProbe> _probesByType;
|
||||
private readonly TagConfigValidationMode _tagConfigValidationMode;
|
||||
|
||||
/// <summary>lmxopcua#971 follow-up: the deploy-time script compile gate; null = not wired, or mode Off.</summary>
|
||||
private readonly IDeployScriptChecker? _scriptChecker;
|
||||
|
||||
private readonly ScriptCompileGateMode _scriptCompileGateMode;
|
||||
|
||||
/// <summary>
|
||||
/// The coordinator's apply deadline. A <c>Dispatching</c> row with no expected-ack rows that is older
|
||||
/// than this is a lost dispatch, closed by the in-flight check (fix round 1 / I7).
|
||||
@@ -59,6 +64,8 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
/// </param>
|
||||
/// <param name="applyDeadline">The coordinator's apply deadline; defaults to <see cref="DeployTimings.ApplyDeadline"/> (the coordinator's default source).</param>
|
||||
/// <param name="alarmVerdictWindow">#950: how long an <c>/alerts</c> ack/shelve waits for the nodes' verdicts; defaults to <see cref="AlarmCommandVerdictCollector.DefaultWindow"/>.</param>
|
||||
/// <param name="scriptChecker">lmxopcua#971 follow-up: the deploy-time script compile gate, or <see langword="null"/> to skip it.</param>
|
||||
/// <param name="scriptCompileGateMode">How a refused script is treated (Error default | Warn | Off).</param>
|
||||
/// <returns>Props configured to create an AdminOperationsActor.</returns>
|
||||
public static Props Props(
|
||||
IDbContextFactory<OtOpcUaConfigDbContext> dbFactory,
|
||||
@@ -67,9 +74,12 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
TagConfigValidationMode tagConfigValidationMode = TagConfigValidationMode.Warn,
|
||||
IActorRef? meshRouter = null,
|
||||
TimeSpan? applyDeadline = null,
|
||||
TimeSpan? alarmVerdictWindow = null) =>
|
||||
TimeSpan? alarmVerdictWindow = null,
|
||||
IDeployScriptChecker? scriptChecker = null,
|
||||
ScriptCompileGateMode scriptCompileGateMode = ScriptCompileGateMode.Error) =>
|
||||
Akka.Actor.Props.Create(() =>
|
||||
new AdminOperationsActor(dbFactory, coordinator, probes, tagConfigValidationMode, meshRouter, applyDeadline, alarmVerdictWindow));
|
||||
new AdminOperationsActor(dbFactory, coordinator, probes, tagConfigValidationMode, meshRouter, applyDeadline,
|
||||
alarmVerdictWindow, scriptChecker, scriptCompileGateMode));
|
||||
|
||||
/// <summary>Initializes a new instance of the AdminOperationsActor.</summary>
|
||||
/// <param name="dbFactory">Factory for creating config database contexts.</param>
|
||||
@@ -79,6 +89,8 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
/// <param name="meshRouter">Central comm actor, or <see langword="null"/> for the mediator.</param>
|
||||
/// <param name="applyDeadline">The coordinator's apply deadline; defaults to <see cref="DeployTimings.ApplyDeadline"/> (the coordinator's default source).</param>
|
||||
/// <param name="alarmVerdictWindow">#950: the <c>/alerts</c> verdict collection window.</param>
|
||||
/// <param name="scriptChecker">lmxopcua#971 follow-up: the deploy-time script compile gate, or <see langword="null"/> to skip it.</param>
|
||||
/// <param name="scriptCompileGateMode">How a refused script is treated (Error default | Warn | Off).</param>
|
||||
public AdminOperationsActor(
|
||||
IDbContextFactory<OtOpcUaConfigDbContext> dbFactory,
|
||||
IActorRef coordinator,
|
||||
@@ -86,8 +98,12 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
TagConfigValidationMode tagConfigValidationMode = TagConfigValidationMode.Warn,
|
||||
IActorRef? meshRouter = null,
|
||||
TimeSpan? applyDeadline = null,
|
||||
TimeSpan? alarmVerdictWindow = null)
|
||||
TimeSpan? alarmVerdictWindow = null,
|
||||
IDeployScriptChecker? scriptChecker = null,
|
||||
ScriptCompileGateMode scriptCompileGateMode = ScriptCompileGateMode.Error)
|
||||
{
|
||||
_scriptChecker = scriptCompileGateMode == ScriptCompileGateMode.Off ? null : scriptChecker;
|
||||
_scriptCompileGateMode = scriptCompileGateMode;
|
||||
_alarmVerdictWindow = alarmVerdictWindow ?? AlarmCommandVerdictCollector.DefaultWindow;
|
||||
// The same source the coordinator's default reads (ConfigPublishCoordinator.DefaultApplyDeadline IS
|
||||
// DeployTimings.ApplyDeadline), so the lost-dispatch closure and the coordinator's own deadline agree.
|
||||
@@ -466,6 +482,15 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
/// </summary>
|
||||
private async Task<ComposeOutcome> ComposeDeploymentAsync(StartDeployment msg, string sourceNode, ComposePhaseTimer phases)
|
||||
{
|
||||
// lmxopcua#971 follow-up: warm the script compile gate's cache BEFORE the snapshot transaction + compose app
|
||||
// lock, so the Roslyn work does not run while they are held. The authoritative check runs on the snapshot
|
||||
// below; with the cache warm it is (almost always) all hits.
|
||||
if (_scriptChecker is not null)
|
||||
{
|
||||
await WarmScriptCompileGateAsync(_scriptChecker).ConfigureAwait(false);
|
||||
phases.Mark("script-gate-warm");
|
||||
}
|
||||
|
||||
// lmxopcua#949: phase timings, logged only when the deploy is slow (see ComposePhaseTimer).
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
// ONE transaction holds the snapshot read, the scope-revision advance and the Deployment row, committed
|
||||
@@ -585,6 +610,35 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
_log.Warning("StartDeployment: data-connection warning (non-blocking): {Warning}", w);
|
||||
|
||||
phases.Mark("validate");
|
||||
|
||||
// lmxopcua#971 follow-up — the deploy-time script compile gate: every script the runtime will compile
|
||||
// (enabled VirtualTags, enabled scripted-alarm predicates, Calculation tags), {{equip}} resolved exactly as the
|
||||
// compose seams resolve it, through the same compile + allow-list analyzer the runtime runs. On the SAME
|
||||
// snapshot as everything else (R03:3.2); the warm-up above means this is normally all cache hits.
|
||||
var scriptWarnings = new List<string>();
|
||||
if (_scriptChecker is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var f in DeployScriptGate.Run(_scriptChecker, draft))
|
||||
{
|
||||
if (_scriptCompileGateMode == ScriptCompileGateMode.Error)
|
||||
errors.Add(new ValidationError(DeployScriptGate.ErrorCode, f.Text, f.ScriptId));
|
||||
else
|
||||
{
|
||||
scriptWarnings.Add(f.Text);
|
||||
_log.Warning("StartDeployment: script compile warning (Warn mode, non-blocking): {Warning}", f.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// An internal checker failure must not block every deploy; the gate is skipped and says so.
|
||||
_log.Error(ex, "StartDeployment: script compile gate failed internally; skipped for this deploy (correlation {CorrelationId})", msg.CorrelationId);
|
||||
scriptWarnings.Add("script compile gate skipped (internal error; see the central log)");
|
||||
}
|
||||
phases.Mark("script-gate");
|
||||
}
|
||||
if (errors.Count > 0)
|
||||
{
|
||||
phases.LogIfSlow(_log, msg, outcome: "rejected");
|
||||
@@ -677,6 +731,7 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
if (advisory is not null) messageParts.Add(advisory);
|
||||
messageParts.AddRange(tagConfigWarnings.Select(x => x.Text));
|
||||
messageParts.AddRange(writableWarnings);
|
||||
messageParts.AddRange(scriptWarnings);
|
||||
var acceptedMessage = messageParts.Count > 0 ? string.Join("; ", messageParts) : null;
|
||||
|
||||
return new ComposeOutcome(
|
||||
@@ -689,6 +744,34 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
new DispatchDeployment(deploymentId, revHash, msg.CorrelationId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#971 follow-up: warms the script compile gate's cache from the three small script tables (no
|
||||
/// Tags read), outside any transaction. Best-effort — a failure here is logged and the authoritative
|
||||
/// in-transaction pass does the work instead.
|
||||
/// </summary>
|
||||
/// <param name="checker">The compile checker.</param>
|
||||
private async Task WarmScriptCompileGateAsync(IDeployScriptChecker checker)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var db = await _dbFactory.CreateDbContextAsync().ConfigureAwait(false);
|
||||
var scripts = await db.Scripts.AsNoTracking()
|
||||
.Select(s => new { s.ScriptId, s.SourceCode })
|
||||
.ToListAsync().ConfigureAwait(false);
|
||||
if (scripts.Count == 0) return;
|
||||
var vtScriptIds = await db.VirtualTags.AsNoTracking().Where(v => v.Enabled)
|
||||
.Select(v => v.ScriptId).Distinct().ToListAsync().ConfigureAwait(false);
|
||||
var predicateScriptIds = await db.ScriptedAlarms.AsNoTracking().Where(a => a.Enabled)
|
||||
.Select(a => a.PredicateScriptId).Distinct().ToListAsync().ConfigureAwait(false);
|
||||
checker.Check(DeployScriptGate.WarmRequests(
|
||||
scripts.Select(s => (s.ScriptId, s.SourceCode)), vtScriptIds, predicateScriptIds));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Warning(ex, "StartDeployment: script compile gate warm-up failed; the in-snapshot check runs cold");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test Connect runs on the thread pool and pipes its reply straight to the requester: a probe
|
||||
/// against a black-holed PLC (60 s clamp) must not suspend the singleton's mailbox (R03:3.1). Every
|
||||
|
||||
Reference in New Issue
Block a user