diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 156dc71024..31054e4140 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6293,12 +6293,8 @@ jobs: # Each arm installs the service under the DEFAULT virtual account (NT SERVICE\MessageFoundry), # with sign-in required, on its own fresh data directory, and uninstalls it afterwards. The helper # asserts the SCM run-as account and the owner of each sampled service process, so an arm that - # silently ran as some other account goes red instead of measuring the wrong thing. Every red - # names the identity and the trio file(s) granting it no read+write, and is tagged [STORE ACCESS] - # only when a log shows an open failure -- [NOT SHOWN TO BE STORE ACCESS] otherwise, so a refused - # start gate or a held port is not read as the lockout. After every open each trio file may grant only - # SYSTEM, Administrators, the service account and the operator, or the arm goes red. A restriction - # the engine logged as failed prints GREEN (CONDITIONAL). The helper's header says how each arm reds. + # silently ran as some other account goes red instead of measuring the wrong thing. + # What a red means: the helper's header. # # `!cancelled()` rather than the default `success()`: both readings are wanted on every run, so # the start-first arm still runs when the provision-first arm or the service smoke went red. diff --git a/changelog.d/5658.fixed.md b/changelog.d/5658.fixed.md new file mode 100644 index 0000000000..84559b4e63 --- /dev/null +++ b/changelog.d/5658.fixed.md @@ -0,0 +1,6 @@ +- **`measure-store-access.ps1` counts a start as refused only on a line that carries both + "refusing to start" and "no enabled Administrator".** It used to count any line naming "no + enabled Administrator", and the gate's skipped-gate and warn-mode warnings carry that too while + the start goes on. When a step that waits for the refusal sees one of the gate's let-start lines + (`$GateLetStart`), the run logs a reading quoting it, and that step's red carries no store-access + attribution. (`BACKLOG #5658`) diff --git a/scripts/service/measure-store-access.ps1 b/scripts/service/measure-store-access.ps1 index ce42685976..1aaca00992 100644 --- a/scripts/service/measure-store-access.ps1 +++ b/scripts/service/measure-store-access.ps1 @@ -32,10 +32,10 @@ provisions. Either outcome is reached only after the store opened, so it is the proof that the operator opened it. 3. The service is started again and must reach the same refusal, which proves it can still - open the store that the operator's open re-secured. If step 2 instead PROVISIONED (no - bootstrap account existed), the gate would pass, so the proof is /health and sign-in. If - step 2 FAILED, the service is restarted anyway as a control: reopening the store it - created itself tells "restricted to the service" apart from "restricted to nobody usable". + open the store after the operator's open. If step 2 instead PROVISIONED (no bootstrap + account existed), the gate would pass, so the proof is /health and sign-in. If step 2 + FAILED, the service is restarted anyway as a control, and the run reports whether that + restart logged the refusal. -Order StartFirstEndToEnd (ADR 0183 Amendment A, Wave 2: AC-11 and AC-13 across two identities) The start-first flow an operator now meets, with no outcome left open. @@ -53,17 +53,19 @@ NT SERVICE\, and every process sampled under the service must run as it. Where the engine is up (provision first), the sample must include the engine process itself. - WHAT A RED MEANS. Every red names the identity that could not open the store, and lists for each - file of the trio the entries it carries and whether any grants that identity read and write. That - list is a READING of the DACL, not an access check: it names the file, and the behaviour above - says the open failed. A red is tagged [STORE ACCESS] only when the service log or the probe - output shows an open failure; otherwise it is tagged [NOT SHOWN TO BE STORE ACCESS], because a - refused start gate or a held port also stops the engine from serving. - - CONFINEMENT IS CHECKED AFTER EVERY OPEN. Opening is half of the question; the other half is the - property ADR 0163's restriction exists for. After each identity's open, every trio file may grant - only SYSTEM, BUILTIN\Administrators, the service account and the operator, block inheritance, and - be owned by one of them, or the arm goes RED. + WHAT A RED MEANS. A red that carries the store-access attribution (Get-Attribution) names an + identity and lists, for each file of the trio, its entries and whether any grants that identity + read and write. That list is a READING of the DACL, not an access check. It is tagged + [STORE ACCESS] when the service log or the probe output has a line matching $OpenErrorPattern, + and [NOT SHOWN TO BE STORE ACCESS] otherwise. Which reds carry it is in the script; at least a + red for a start the ADR 0167 gate let go on, where a refusal was expected, does not. + + CONFINEMENT. Opening is half of the question; the other half is the property ADR 0163's + restriction exists for. Test-Resecured reads each trio file present and goes RED on one that + inherits, is owned outside SYSTEM, BUILTIN\Administrators, the service account and the + operator, or grants anyone outside them. It runs only where the script calls it, which is not + after every open: at least the control reopen, and an open whose step logged no proof, are not + checked. A GREEN CAN BE CONDITIONAL, and says so: store.py only logs when it cannot restrict a file, so a restriction warning in the service log or the CLI output prints GREEN (CONDITIONAL) with a warning. @@ -116,15 +118,23 @@ $Email = "w0-admin@example.invalid" # The ADR 0167 gate's own words (messagefoundry/api/app.py, _assert_security_notice_is_deliverable), # and the words provision_first_administrator declines with (messagefoundry/auth/service.py). If # either is reworded, the arm goes RED on a missing proof rather than passing on a stale one. -# $GateRefusal is the clause BOTH of the gate's branches open with, so the file-access arms prove the -# service read the user table whichever branch fired. $NoAdminRefusal and $ProvisionHint are the -# no-Administrator branch alone, which StartFirstEndToEnd requires (ADR 0183 Wave 2, AC-11). +# $GateRefusal is the clause the gate's no-Administrator and no-address branches both open with, so +# the file-access arms prove the service read the user table whichever of the two fired. The gate's +# Vault-transit refusal carries none of $GateRefusal, $NoAdminRefusal and $ProvisionHint (see +# Wait-ForGateRefusal). $NoAdminRefusal and $ProvisionHint are the no-Administrator branch alone, +# which StartFirstEndToEnd requires (ADR 0183 Wave 2, AC-11); $NoAdminRefusal is in it only when no account record failed its integrity seal. $GateRefusal = "no enabled Administrator" $NoAdminRefusal = "no enabled Administrator exists" $ProvisionHint = "provision-admin --username" -# The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry the two -# phrases above too, so without this an engine that started and served would pass step 1. +# The words that make it a REFUSAL. The skipped-gate WARNING and the warn-mode line carry +# $GateRefusal too, and the engine goes on to start after either. Test-GateRefusalLine requires +# $StartRefused and the clause on one line. $StartRefused = "refusing to start" +# The openings of the gate's lines that let a start go on, at least these three in app.py: the +# skipped-gate warning, the warn-mode line, and the warning that the gate could not run (a Vault +# transit outage while the gate is not enforcing). The third does not carry $GateRefusal, so this is +# searched for on its own. A regex, matched as is. +$GateLetStart = "the engine is starting with the notice gate skipped: |PHI instance with no deliverable security-notice recipient: |the security-notice gate could not run: " $ProvisionDecline = "already has an enabled Administrator" $OpenErrorPattern = "unable to open database file|Access is denied|PermissionError|OperationalError" # store.py logs these and carries on; each means that open did not restrict the trio as it tried to @@ -313,9 +323,8 @@ function Get-LogMatches { function Get-Attribution { <# - One sentence for a red: which trio files carry no read+write grant for $Identity, whether the - log shows an open failure, and the trio itself. Stated as readings so a red caused by something - other than file access (a start gate, a port) is not dressed up as one. + The attribution text a red appends: which trio files carry no read+write grant for $Identity, + whether the log or $Output has an open-failure line, and the trio itself. #> param([string]$Identity, [string[]]$IdentitySids, [string]$Output) $trio = Get-TrioReport -Identity $Identity -IdentitySids $IdentitySids @@ -323,8 +332,8 @@ function Get-Attribution { else { "Every present trio file grants $Identity read+write, so the cause is probably NOT file access; read the output below." } $hits = @(Get-LogMatches $OpenErrorPattern | Select-Object -First 3 | ForEach-Object { Get-Excerpt $_.Line 240 }) if ($Output -and $Output -match $OpenErrorPattern) { $hits += "probe output: " + (Get-Excerpt $Output 240) } - # The KIND leads the message, so a red that is not shown to be file access cannot be read as the - # lockout Wave 0 is looking for: a refused start gate or a held port also fails to serve. + # The KIND opens this attribution text, which the caller may put after its own words. Without + # an open-failure line the tag says so: a refused start gate or a held port also fails to serve. $kind = if ($hits.Count -gt 0) { "[STORE ACCESS]" } else { "[NOT SHOWN TO BE STORE ACCESS]" } $log = if ($hits.Count -gt 0) { "Open-failure evidence: $($hits -join ' || ')." } else { "No open-failure line was found in the service log or the probe output." } @@ -343,10 +352,11 @@ function Show-Trio([string]$Label) { function Test-Resecured { <# After an identity's open, is the store still CONFINED -- the property ADR 0163's restriction - exists for? Every present trio file must block inheritance, be OWNED by, and grant only, SYSTEM, - BUILTIN\Administrators, the service account or the operator. Anything else is a RED: an - outside entry makes the store readable by that principal; an outside owner keeps WRITE_DAC and - can grant itself access; an inheriting file takes whatever the directory is later given. + exists for? A present trio file whose DACL it reads is a RED if it inherits, is OWNED outside, + or grants an Allow entry outside, SYSTEM, BUILTIN\Administrators, the service account and the + operator: an outside entry makes the store readable by that principal; an outside owner keeps + WRITE_DAC and can grant itself access; an inheriting file takes whatever the directory is later + given. A DACL it cannot read is a CONDITION. This replaced a check on the MECHANISM ("the .db still inherits", "the DACL did not change"), which encoded the pre-0b rewrite-to-the-opener design and would have flagged Wave 0b's @@ -630,21 +640,66 @@ function Start-TheService([string]$Phase) { } } +function Test-GateRefusalLine([string]$Line, [string]$Clause = $GateRefusal) { + <# + Whether one log line is the ADR 0167 REFUSAL: it carries the gate's clause ($Clause, or a + narrower one such as $NoAdminRefusal) and the refusal's own words. The skipped-gate warning and + the warn-mode line carry the clause alone, and an engine that logs either goes on to start and + serve, so neither may count as a refusal. + #> + return ($Line -match [regex]::Escape($Clause)) -and ($Line -match [regex]::Escape($StartRefused)) +} + function Wait-ForGateRefusal([string]$Phase) { <# - Wait until the service logs the ADR 0167 refusal, which it reaches only after opening the store - and reading its users. Samples process owners throughout, since NSSM restarts the refused engine - and each restart is a new process. Returns $true on the refusal, $false on timeout. + Wait until the service logs the ADR 0167 refusal (Test-GateRefusalLine), which it reaches only + after opening the store and reading its users. Samples process owners throughout, since NSSM + restarts the refused engine and each restart is a new process. Returns $true on the refusal, + $false on timeout, and $false at once, with a reading quoting it, when one of the gate's + let-it-start lines ($GateLetStart) is logged. Any other line is waited past. #> + # Read by callers after a $false, so a failure text does not name a lockout when the gate logged + # a let-start line. Not every gate outcome is recognised. At least the Vault-transit + # integrity-seal refusal ("refusing to start a PHI instance: the accounts could not be checked + # against their integrity seal ...") carries $StartRefused but not $GateRefusal, and contains no + # $GateLetStart prefix. On such a path the wait times out, and the caller prints its timeout text and the store-access + # attribution (Get-Attribution) rather than naming the gate. + $script:GateLetStartSeen = $false $deadline = (Get-Date).AddSeconds($WaitSeconds) while ((Get-Date) -lt $deadline) { Update-OwnerSample "during $Phase" - if ((Get-LogMatches ([regex]::Escape($GateRefusal))).Count -gt 0) { return $true } + $gate = @(Get-LogMatches ([regex]::Escape($GateRefusal))) + if (@($gate | Where-Object { Test-GateRefusalLine $_.Line }).Count -gt 0) { return $true } + # Only the gate's own let-it-start lines end the wait: other engine lines carry the clause + # too, and say nothing about this gate. Searched on their own, since the could-not-run + # warning does not carry the clause. + $letStart = @(Get-LogMatches $GateLetStart) + if ($letStart.Count -gt 0) { + Add-Reading ("during $Phase the service reached the ADR 0167 gate, which did not refuse " + + "the start: $(Get-Excerpt $letStart[0].Line 300)") + $script:GateLetStartSeen = $true + return $false + } Start-Sleep -Seconds 1 } return $false } +function Add-FirstStartFailure { + <# + The failure for a first start on a fresh store that Wait-ForGateRefusal did not see refused. + As in the control and restart branches, it gives the store-access attribution unless the wait + saw one of the gate's let-start lines ($GateLetStart). + #> + $what = if ($script:GateLetStartSeen) { "logged a line of the ADR 0167 gate letting the start go on (the READING line quotes it), where a refusal was expected," } + elseif (Test-Path -LiteralPath $DbPath) { "created $DbPath but did not log the expected refusal line" } + else { "never created $DbPath" } + $cause = if ($script:GateLetStartSeen) { "No store-access attribution is given for a start the gate let go on." } + else { Get-Attribution $ServiceIdentity (Get-ServiceSids) "" } + Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + $cause) + Show-LogTail +} + function Test-ServiceServes([string]$Phase, [string]$StoreOrigin) { <# The engine is up on this store and a session can be had: /health over https pinned to the @@ -963,20 +1018,17 @@ try { "Wave 2 no path writes a bootstrap credential file (AC-13)") } $noAdmin = @(Get-LogMatches ([regex]::Escape($NoAdminRefusal)) | - Where-Object { $_.Line -match [regex]::Escape($StartRefused) }) + Where-Object { Test-GateRefusalLine $_.Line $NoAdminRefusal }) if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but never reached the account gate, so its open is unproven" } - else { "never created $DbPath" } - Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + - (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) - Show-LogTail + Add-FirstStartFailure } elseif ($noAdmin.Count -eq 0) { - $line = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | Select-Object -First 1 | + $line = @(Get-LogMatches ([regex]::Escape($GateRefusal)) | + Where-Object { Test-GateRefusalLine $_.Line } | Select-Object -First 1 | ForEach-Object { Get-Excerpt $_.Line 300 }) - Add-Failure ("$ServiceIdentity reached the ADR 0167 gate on a fresh store, but no line said " + - "both '$StartRefused' and '$NoAdminRefusal'. Either the start was not refused, or it " + - "was refused for a missing address, which means the first start created an account no " + - "operator named. Gate line: $($line -join ' ')") + Add-Failure ("$ServiceIdentity was refused by the ADR 0167 gate on a fresh store, but not " + + "in the words '$NoAdminRefusal'. A refusal for a missing address would mean the first " + + "start created an account no operator named; the refusal line says which. " + + "Refusal line: $($line -join ' ')") Show-LogTail } elseif (@($noAdmin | Where-Object { $_.Line -match [regex]::Escape($ProvisionHint) }).Count -eq 0) { Add-Failure ("$ServiceIdentity was refused for want of an Administrator, but the refusal " + @@ -1018,11 +1070,7 @@ try { Stop-TheService Show-Trio "service first start" if (-not $opened) { - $what = if (Test-Path -LiteralPath $DbPath) { "created $DbPath but never reached the account gate, so its open is unproven" } - else { "never created $DbPath" } - Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. " + - (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) - Show-LogTail + Add-FirstStartFailure } else { Add-Reading "$ServiceIdentity created and opened $DbPath, then the ADR 0167 gate refused it (expected)" Test-Resecured -Identity $ServiceIdentity -FromServiceLog @@ -1033,21 +1081,26 @@ try { $provisioned = ($r.Code -eq 0) $declined = ($r.Text -match [regex]::Escape($ProvisionDecline)) if (-not ($provisioned -or $declined)) { - Add-Failure ("$Operator could not open $DbPath, which $ServiceIdentity created and " + - "secured, through provision-admin's open path (exit $($r.Code)). " + + Add-Failure ("$Operator ran provision-admin on $DbPath, which $ServiceIdentity created, " + + "and it exited $($r.Code) without provisioning or declining, so the operator's " + + "open is unproven. " + (Get-Attribution $Operator (Get-OperatorSids) $r.Text)) - # CONTROL: nothing re-secured the store, so a service that cannot reopen even its own - # store means the first open locked out EVERY identity, not only the operator. That - # separates "restricted to the service" from "restricted to nobody usable". + # CONTROL: restart the service on the store it created and report whether it logs the + # refusal again. The trio listing above shows what the operator's failed run left. Start-TheService "reopen of its own store (control)" $own = Wait-ForGateRefusal "the control reopen" Stop-TheService if ($own) { - Add-Reading "control: $ServiceIdentity reopened the store it created, so the store is restricted to the service and not to nobody" + Add-Reading "control: $ServiceIdentity, restarted on the store it created, logged the expected refusal, so it reopened that store" + } elseif ($script:GateLetStartSeen) { + Add-Failure ("control: $ServiceIdentity, restarted on the store it created, logged a " + + "line of the ADR 0167 gate letting the start go on (the reading above), where " + + "this control expected a refusal. No store-access attribution is given for it.") + Show-LogTail } else { - Add-Failure ("control: $ServiceIdentity could not reopen even the store it created " + - "itself, which nobody else touched, so its own first open locked out every " + - "identity. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) + Add-Failure ("control: $ServiceIdentity, restarted on the store it created itself, " + + "did not log the expected refusal line in $WaitSeconds s. " + + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } } else { @@ -1055,12 +1108,12 @@ try { else { "and provision-admin declined on an existing Administrator (the pre-Wave 2 bootstrap shape)" } Add-Reading "$Operator opened $DbPath $how" Test-Resecured -Identity $Operator -Output $r.Text - # 3. The service starts again on the store the operator's open re-secured. + # 3. The service starts again on the store after the operator's open. Start-TheService "restart" if ($provisioned) { # An addressed Administrator now exists, so the gate passes: prove the open by serving. $failed = $Failures.Count - Test-ServiceServes "the restart" "$Operator's open re-secured" + Test-ServiceServes "the restart" "$Operator provisioned" Stop-TheService if ($Failures.Count -eq $failed) { Test-Resecured -Identity $ServiceIdentity -FromServiceLog @@ -1068,9 +1121,15 @@ try { } else { $reopened = Wait-ForGateRefusal "the restart" Stop-TheService - if (-not $reopened) { - Add-Failure ("$ServiceIdentity could not open $DbPath after $Operator's open " + - "re-secured it: the restart never reached the account gate in $WaitSeconds s. " + + if (-not $reopened -and $script:GateLetStartSeen) { + Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open, " + + "logged a line of the ADR 0167 gate letting the start go on (the reading " + + "above), where the restart expected a refusal. No store-access attribution " + + "is given for it.") + Show-LogTail + } elseif (-not $reopened) { + Add-Failure ("$ServiceIdentity, restarted on $DbPath after $Operator's open, " + + "did not log the expected refusal line in $WaitSeconds s. " + (Get-Attribution $ServiceIdentity (Get-ServiceSids) "")) Show-LogTail } else { @@ -1114,7 +1173,7 @@ if ($Conditions.Count -gt 0) { Write-Host "::warning title=ADR 0183 Wave 0 ($Order) GREEN (CONDITIONAL)::$(Format-Annotation $why)" Write-Host "GREEN (CONDITIONAL) [$Order]: $why" } else { - Write-Host "GREEN [$Order]: both identities opened the store in this order, and it stayed confined after each open." + Write-Host "GREEN [$Order]: both identities opened the store in this order, and no confinement check went red." } # The Actions wrapper exits with $LASTEXITCODE, which the last native command (nssm, icacls) set. exit 0 diff --git a/tests/test_measure_store_access_gate_refusal.py b/tests/test_measure_store_access_gate_refusal.py new file mode 100644 index 0000000000..9ef05be433 --- /dev/null +++ b/tests/test_measure_store_access_gate_refusal.py @@ -0,0 +1,223 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# Copyright (C) 2026 MessageFoundry Foundation, LLC and contributors +"""``measure-store-access.ps1`` counts only the ADR 0167 REFUSAL as a refused start. + +``Wait-ForGateRefusal`` used to return on any log line carrying the gate's clause, "no enabled +Administrator". The skipped-gate warning and the warn-mode line carry that clause too, and an engine +that logs either goes on to start and serve, so the StartFirst arm could report a refusal for an +engine that served. Now a line must also carry the refusal's own words, through +``Test-GateRefusalLine``. + +NOTHING HERE RUNS THE SCRIPT: it installs a service and must run elevated, which only the hosted +windows-service-smoke leg does. The tests lift the phrase assignments and functions they need out +of the script's AST: they call ``Test-GateRefusalLine`` on lines worded as the engine words them, +and run ``Wait-ForGateRefusal`` against a stand-in log reader. A substring check finds each line's +prefix, and source fragments of the no-Administrator and no-address detail texts, somewhere in +``app.py``; it does not render the lines, so it does not prove the refusal still puts the clause and +"refusing to start" on one line. Nothing here pins at least ``$ProvisionHint`` or ``$ProvisionDecline``. Each +case that runs PowerShell needs its own host, ``pwsh`` or ``powershell``, and skips where that host +is missing; the ``app.py`` pin and the source-text test need neither. +""" + +from __future__ import annotations + +import json +import shutil +import uuid +from pathlib import Path + +import pytest + +from tests._spawn_lock import run_single + +_ROOT = Path(__file__).resolve().parent.parent +_SCRIPT = _ROOT / "scripts" / "service" / "measure-store-access.ps1" +_APP = _ROOT / "messagefoundry" / "api" / "app.py" + +#: The prefix each of the gate's lines opens with in ``app.py``, and a line worded so. +_REFUSAL = ( + "refusing to start a PHI instance: ", + "RuntimeError: refusing to start a PHI instance: no enabled Administrator exists in this store," + " so no enabled account holds the role that manages users and roles", +) +_SKIPPED = ( + "the engine is starting with the notice gate skipped: ", + "WARNING messagefoundry.api.app: the engine is starting with the notice gate skipped: no enabled" + " Administrator exists in this store, so no enabled account holds the role", +) +_WARN_MODE = ( + "PHI instance with no deliverable security-notice recipient: ", + "WARNING messagefoundry.api.app: PHI instance with no deliverable security-notice recipient: no" + " enabled Administrator has a notification address, so every out-of-band security notice", +) +#: The gate's could-not-run warning (a Vault transit outage while the gate is not enforcing). It lets +#: the start go on, and it does not carry the clause the lines above share. +_COULD_NOT_RUN = ( + "the security-notice gate could not run: ", + "WARNING messagefoundry.api.app: the security-notice gate could not run: the accounts could not" + " be checked against their integrity seal because Vault transit did not answer", +) + + +def _psq(value: str) -> str: + return "'" + value.replace("'", "''") + "'" + + +def _run(host: str, tmp_path: Path, script: str) -> dict[str, object]: + if shutil.which(host) is None: + pytest.skip(f"SKIP (nothing run): {host} not on PATH") + f = tmp_path / f"gate-{uuid.uuid4().hex}.ps1" + # With a BOM, which Windows PowerShell 5.1 needs to read a non-ASCII path as UTF-8. + f.write_text(script, encoding="utf-8-sig") + # Windows PowerShell's default policy can refuse an unsigned -File; pwsh's does not. + policy = ["-ExecutionPolicy", "Bypass"] if host == "powershell" else [] + proc = run_single( + [host, "-NoProfile", "-NonInteractive", *policy, "-File", str(f)], + capture_output=True, + text=True, + timeout=120, + check=False, + ) + assert proc.returncode == 0, f"harness failed: {proc.stdout}\n{proc.stderr}" + result: dict[str, object] = json.loads(proc.stdout.strip().splitlines()[-1]) + return result + + +def _lift(variables: list[str], functions: list[str]) -> str: + """PowerShell that dot-sources the named assignments and functions out of the script's AST.""" + names = ", ".join(_psq(v) for v in variables) + funcs = ", ".join(_psq(f) for f in functions) + return f""" +$ErrorActionPreference = 'Stop' +$errs = $null +$ast = [System.Management.Automation.Language.Parser]::ParseFile({_psq(str(_SCRIPT))}, [ref]$null, [ref]$errs) +if (@($errs).Count) {{ throw "the script does not parse: $($errs[0])" }} +foreach ($var in @({names})) {{ + $set = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.AssignmentStatementAst] -and + $args[0].Left.Extent.Text -eq ('$' + $var) }}, $false) + if (-not $set) {{ throw "missing assignment `${{var}}" }} + . ([scriptblock]::Create($set.Extent.Text)) +}} +foreach ($name in @({funcs})) {{ + $def = $ast.Find({{ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + $args[0].Name -eq $name }}, $true) + if (-not $def) {{ throw "missing function $name" }} + . ([scriptblock]::Create($def.Extent.Text)) +}} +""" + + +def _judge(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: + script = _lift( + ["GateRefusal", "NoAdminRefusal", "StartRefused", "GateLetStart"], ["Test-GateRefusalLine"] + ) + ( + f""" +$lines = {_psq(json.dumps(lines))} | ConvertFrom-Json +ConvertTo-Json -Compress -InputObject @{{ + refusal = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_) }}) + no_admin = @($lines | ForEach-Object {{ [bool](Test-GateRefusalLine $_ $NoAdminRefusal) }}) + let_start = @($lines | ForEach-Object {{ $_ -match $GateLetStart }}) + clause = @($lines | ForEach-Object {{ $_ -match [regex]::Escape($GateRefusal) }}) }} +""" + ) + return _run(host, tmp_path, script) + + +def _wait(host: str, tmp_path: Path, lines: list[str]) -> dict[str, object]: + """Run the script's own ``Wait-ForGateRefusal`` with ``lines`` as the whole service log.""" + script = _lift( + ["GateRefusal", "StartRefused", "GateLetStart"], + ["Test-GateRefusalLine", "Wait-ForGateRefusal", "Get-Excerpt"], + ) + ( + f""" +$script:LogLines = @({_psq(json.dumps(lines))} | ConvertFrom-Json) +$script:ReadingsSeen = New-Object System.Collections.ArrayList +function Update-OwnerSample([string]$Phase) {{ }} +function Get-LogMatches {{ param([string]$Pattern) @($script:LogLines | Select-String -Pattern $Pattern) }} +function Add-Reading([string]$Text) {{ [void]$script:ReadingsSeen.Add($Text) }} +$WaitSeconds = 2 +$refused = [bool](Wait-ForGateRefusal 'the first start') +ConvertTo-Json -Compress -InputObject @{{ + refused = $refused; let_start_seen = [bool]$script:GateLetStartSeen + readings = @($script:ReadingsSeen).Count }} +""" + ) + return _run(host, tmp_path, script) + + +def test_the_engine_still_words_the_gate_lines_so() -> None: + app = _APP.read_text(encoding="utf-8") + for prefix, line in (_REFUSAL, _SKIPPED, _WARN_MODE, _COULD_NOT_RUN): + assert prefix in app, prefix + assert prefix in line + # The clauses the script matches on, as app.py spells the detail text after each prefix. The + # no-Administrator detail is split across an f-string, so its two halves are pinned apart. + assert '"no enabled Administrator{' in app + assert '"exists in this store, so no enabled account holds the role "' in app + assert ( + '"no enabled Administrator has a notification address, so every out-of-band security "' + in app + ) + + +@pytest.mark.parametrize("host", ["pwsh", "powershell"]) +def test_only_the_refusal_counts_as_a_refused_start(tmp_path: Path, host: str) -> None: + # Another engine line that carries the clause and is not the gate's. + census = ( + "WARNING messagefoundry.auth.service: no enabled Administrator can sign in without an" + " outside identity service" + ) + got = _judge( + host, + tmp_path, + [_REFUSAL[1], _SKIPPED[1], _WARN_MODE[1], _COULD_NOT_RUN[1], census, "unrelated line"], + ) + assert got["refusal"] == [True, False, False, False, False, False], got + # The narrower clause the StartFirstEndToEnd arm passes: the no-Administrator refusal only. + assert got["no_admin"] == [True, False, False, False, False, False], got + # Only the gate's let-it-start lines end the wait early, not the census line. + assert got["let_start"] == [False, True, True, True, False, False], got + # The control: the clause the wait used to match alone is in the first two warning lines, so the + # old criterion took an engine that went on to serve for a refused one. The could-not-run line + # does not carry it. + assert got["clause"] == [True, True, True, False, True, False], got + + +@pytest.mark.parametrize("host", ["pwsh", "powershell"]) +@pytest.mark.parametrize( + ("line", "refused", "let_start"), + [ + (_REFUSAL[1], True, False), + (_SKIPPED[1], False, True), + (_WARN_MODE[1], False, True), + # Without the clause, a let-start search confined to clause lines never sees this one: the + # wait ran to its timeout and the caller's failure carried the store-access attribution. + (_COULD_NOT_RUN[1], False, True), + ("unrelated line", False, False), + ], +) +def test_the_wait_ends_on_each_gate_line( + tmp_path: Path, host: str, line: str, refused: bool, let_start: bool +) -> None: + got = _wait(host, tmp_path, [line]) + assert got["refused"] is refused, got + assert got["let_start_seen"] is let_start, got + # A let-start line is quoted in a reading, which the failure text points at. + assert got["readings"] == (1 if let_start else 0), got + + +def test_a_first_start_the_gate_let_go_on_is_not_attributed_to_store_access() -> None: + # Both first-start branches go through Add-FirstStartFailure, which names the cause only as the + # control and restart branches do: the store-access attribution only when the gate did not let + # the start go on. + text = _SCRIPT.read_text(encoding="utf-8") + calls = [ln.strip() for ln in text.splitlines()] + assert calls.count("Add-FirstStartFailure") == 2 + failure = 'Add-Failure ("$ServiceIdentity $what within $WaitSeconds s on a fresh store. "' + assert text.count(failure) == 1 + at = text.index(failure) + assert text.rindex("function Add-FirstStartFailure", 0, at) > 0 + before = text[text.rindex("$cause = if", 0, at) : at] + assert before.startswith("$cause = if ($script:GateLetStartSeen) {"), before + assert "Get-Attribution" in before.split("else", 1)[1], before + assert "Get-Attribution" not in text[at : text.index("\n", at)] diff --git a/tests/test_tooling_partition.py b/tests/test_tooling_partition.py index 289e68bdf6..24c05e457c 100644 --- a/tests/test_tooling_partition.py +++ b/tests/test_tooling_partition.py @@ -76,6 +76,10 @@ # the change that breaks it. Its other half reads docs/SECURITY.md, which is what makes it a # doc-drift guard, but the doc half is not the reason it stays. "test_adaptive_attributes_doc_drift.py", + # Reads messagefoundry/api/app.py to pin the ADR 0167 gate's log-line prefixes that + # scripts/service/measure-store-access.ps1 tells apart. An engine diff that rewords them must + # meet this test, and the tooling job's path gate would not run it then. + "test_measure_store_access_gate_refusal.py", # Reads tests/conftest.py and packaging/messagefoundry-webconsole/tests/{conftest, # _ui_clients}.py off disk, the two copies of the in-process browser stand-in that every # /ui write test in both suites runs through (vault BACKLOG #3316). No messagefoundry/**