Recover a Windows maintenance setup interrupted before policy activation
This commit is contained in:
@@ -70,10 +70,10 @@ $journal=Uuid $Witness.journal_id;$helper=Uuid $Witness.helper_id;$decision=Uuid
|
||||
$checkpoint=[long]$Witness.helper_checkpoint
|
||||
Check ($checkpoint -gt 0) 'Missing closed helper checkpoint'
|
||||
$rows=@($Witness.attempts|Where-Object {$null -ne $_})
|
||||
$gate=$null;$db=$null;$transaction=$false;$stopped=$false
|
||||
$gate=$null;$db=$null;$transaction=$false
|
||||
try {
|
||||
$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')
|
||||
if((Get-Service $agent).Status -ne 'Stopped'){Stop-Service $agent;$stopped=$true}
|
||||
if((Get-Service $agent).Status -ne 'Stopped'){Stop-Service $agent}
|
||||
$apply=$Action -eq 'Apply'
|
||||
$db=[EnrollmentWindowDb]::new($dbPath,$apply)
|
||||
if($apply){
|
||||
@@ -84,9 +84,9 @@ try {
|
||||
if($apply){Check ($db.Scalar('PRAGMA local.journal_mode') -ceq 'delete') 'Local consumption journal mode differs';$db.Exec('PRAGMA local.synchronous=FULL')}
|
||||
$db.Exec($(if($apply){'BEGIN IMMEDIATE'}else{'BEGIN'}));$transaction=$true
|
||||
$done=$db.Scalar("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='retired_windows_update_windows'") -eq '1'
|
||||
if($done -and $db.Scalar("SELECT count(*) FROM retired_windows_update_windows WHERE case_id='$case' AND helper_id='$helper' AND journal_id='$journal'") -eq '1'){
|
||||
if($done -and $db.Scalar("SELECT count(*) FROM retired_windows_update_windows WHERE case_id='$case' AND helper_id='$helper' AND journal_id='$journal' AND decision_id='$decision' AND helper_checkpoint=$checkpoint") -eq '1'){
|
||||
$db.Exec('ROLLBACK');$transaction=$false
|
||||
[ordered]@{already_retired=$true;case_id=$case;attempts=$rows.Count}|ConvertTo-Json -Compress;return
|
||||
[ordered]@{already_retired=$true;verified=$true;applied=$true;case_id=$case;helper_id=$helper;decision_id=$decision;attempts=$rows.Count}|ConvertTo-Json -Compress;return
|
||||
}
|
||||
Check ($db.Scalar("SELECT count(*) FROM reconcile_meta WHERE id=1 AND journal_id='$journal' AND helper_id='$helper' AND helper_checkpoint=$checkpoint AND last_decision='$decision' AND pending_request IS NULL AND (fault IS NULL OR fault='HELPER_JOURNAL_DISAPPEARED')") -eq '1') 'Agent and signed central final checkpoint differ'
|
||||
Check ($db.Scalar("SELECT count(*) FROM proof.helper_sync_meta WHERE journal_id='$helper' AND agent_journal_id='$journal' AND checkpoint>=$checkpoint AND last_decision='$decision' AND fault IS NULL") -eq '1') 'Archived helper has not applied the same signed final decision'
|
||||
|
||||
@@ -88,6 +88,13 @@ $binding=[ordered]@{tenant_id=$clock.tenant_id;asset_id=$AssetId.ToString();agen
|
||||
$inputDto=[ordered]@{binding=$binding;release_id=$ReleaseId.ToString();agent_sha256=(GetHash $agentExe);helper_sha256=(GetHash $helperExe);max_runtime_seconds=$Seconds}
|
||||
$markerJson=([ordered]@{case_id=$CaseId.ToString();release_id=$ReleaseId.ToString();agent_id=$AgentId.ToString();asset_id=$AssetId.ToString()}|ConvertTo-Json -Compress)
|
||||
$archive=$root+'.closed-'+$CaseId.ToString()
|
||||
$staging=$root+'.preparing-'+$CaseId.ToString()
|
||||
function PublishMarker($destination,$readers) {
|
||||
if(Test-Path -LiteralPath $destination){VerifyMarker $destination;return}
|
||||
NewPrivateDir $staging $readers
|
||||
WriteFixed (Join-Path $staging 'windows-update-enrollment.json') $markerJson
|
||||
[IO.Directory]::Move($staging,$destination)
|
||||
}
|
||||
$backup=Join-Path $root 'original-agent-config.json'
|
||||
$written=Join-Path $root 'active-agent-config.json'
|
||||
$gate=$null
|
||||
@@ -111,7 +118,7 @@ function StartNormal {
|
||||
function RemoveWindow {
|
||||
if (!(Test-Path -LiteralPath $root)) {
|
||||
Check (!(Service $helper) -and !(JsonFile $cfg).windows_update_helper) 'Helper SCM or configuration present without its protected case marker.'
|
||||
if(Test-Path -LiteralPath $archive){VerifyMarker $archive}
|
||||
PublishMarker $archive @()
|
||||
if((Get-Service $actor).Status -ne 'Running'){Start-Service $actor}
|
||||
return
|
||||
}
|
||||
@@ -142,7 +149,14 @@ if(Test-Path -LiteralPath $root){VerifyMarker $root}
|
||||
if($Action -eq 'Plan'){[ordered]@{planned=$true;tenant_id=$binding.tenant_id;agent_id=$AgentId;asset_id=$AssetId;release_id=$ReleaseId;agent_sha256=$inputDto.agent_sha256;helper_sha256=$inputDto.helper_sha256;seconds=$Seconds;requires_helper_creation=(!$h);case_id=$CaseId}|ConvertTo-Json -Depth 6 -Compress;return}
|
||||
try {
|
||||
# The updater and agent hold the same file throughout a transition or execution.
|
||||
$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')
|
||||
$until=[DateTime]::UtcNow.AddSeconds(60)
|
||||
do {
|
||||
try {$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')}
|
||||
catch [IO.IOException] {
|
||||
if (($_.Exception.HResult -band 0xffff) -ne 32 -or $Action -ne 'Remove' -or [DateTime]::UtcNow -ge $until){throw}
|
||||
Start-Sleep -Milliseconds 400
|
||||
}
|
||||
} while (!$gate)
|
||||
Check ((JsonFile $active).release_id -ceq $ReleaseId.ToString()) 'Release switched during preparation.'
|
||||
if($Action -eq 'Resume'){
|
||||
Check (!(Service $helper) -and !(JsonFile $cfg).windows_update_helper -and !(Test-Path -LiteralPath $root)) 'Helper still active.'
|
||||
@@ -160,7 +174,7 @@ try {
|
||||
Check ((!$h) -or (Test-Path -LiteralPath $marker)) 'Existing helper service has no matching protected case.'
|
||||
$actorSid=GetSid $actor
|
||||
# The helper runs as SYSTEM; its service SID is optional on file DACLs.
|
||||
NewPrivateDir $root @($actorSid);WriteFixed $marker $markerJson
|
||||
PublishMarker $root @($actorSid)
|
||||
Check (!(Test-Path -LiteralPath (Join-Path $data 'release-state\release-transition.json'))) 'Release transition must finish before maintenance.'
|
||||
Set-Service 'RmmDiscoveryReleasePull' -StartupType Manual
|
||||
if((Get-Service 'RmmDiscoveryReleasePull').Status -ne 'Stopped'){Stop-Service 'RmmDiscoveryReleasePull'}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"date": "2026-11-17",
|
||||
"environment": "development",
|
||||
"test_command": "python3 tests/windows_update_early_abort.py --execute",
|
||||
"platform": "windows-x86_64-gnu",
|
||||
"agent_version": "0.1.4",
|
||||
"case_id": "ef29ea93-b730-4fdc-861e-46a4ba632c25",
|
||||
"enrollment": {
|
||||
"agent_id": "858a27db-8ba3-4a75-af51-66f6a0909a91",
|
||||
"agent_sha256": "f30ffac8b9f674c4933d50645bf93f6ee89442608ff73f597cedef3a42172292",
|
||||
"asset_id": "26d4cdef-dc84-47c3-8f49-49e22ef64ea8",
|
||||
"helper_binary_sha256": "eeb81736a3ddf297230d808b81f4fbf29f5ebe29723fc298455746f3e4b8f2b7",
|
||||
"release_id": "3dcb2484-fb2d-418d-84f0-aa54f5c4c209",
|
||||
"tenant_id": "11111111-1111-4111-8111-111111111111"
|
||||
},
|
||||
"prepared": {
|
||||
"at": "2026-11-17T16:29:13.662531+00:00",
|
||||
"signed_decision_id": "145ae67c-f6e4-46dc-85d6-fe8b24547e9e",
|
||||
"checkpoint": 453,
|
||||
"journal_id": "0c1ec1d3-c868-42f7-a783-70d99a4f9636",
|
||||
"helper_journal_id": null,
|
||||
"helper_checkpoint": 0
|
||||
},
|
||||
"injected_failure": {
|
||||
"actual_controller_exit_code": 85,
|
||||
"before_central_activation": true,
|
||||
"first_observed_authenticated_scoped_heartbeat": "2026-11-17T16:30:21.655709+00:00",
|
||||
"sync_ready": true,
|
||||
"helper_sync_ready": true,
|
||||
"signed_head": {
|
||||
"checkpoint": 476,
|
||||
"decision_id": "5eda9bd1-6cd8-4475-a317-25436e610312",
|
||||
"helper_checkpoint": 3,
|
||||
"helper_id": "aef1b629-f417-4dc3-9bd7-ef6be94ec7b8",
|
||||
"journal_id": "0c1ec1d3-c868-42f7-a783-70d99a4f9636"
|
||||
},
|
||||
"open_jobs": 0
|
||||
},
|
||||
"recovery": {
|
||||
"separate_process": "python3 tools/windows_update_scope.py close --case ef29ea93-b730-4fdc-861e-46a4ba632c25",
|
||||
"central_tombstone_at": "2026-11-17T16:30:25.544765+00:00",
|
||||
"window_candidate_persisted": false,
|
||||
"new_system_jobs_or_grants_since_preparation": 0,
|
||||
"native_verified_decision_id": "5eda9bd1-6cd8-4475-a317-25436e610312",
|
||||
"native_archived_attempts": 0,
|
||||
"central_helper_detach_committed": true,
|
||||
"native_helper_detach_committed": true,
|
||||
"helper_service_registered": false,
|
||||
"agent_service": "Running",
|
||||
"release_pull_service": "Running",
|
||||
"post_close_contact": "2026-11-17T16:30:43.878785+00:00",
|
||||
"sync_ready": true,
|
||||
"system_caps": false,
|
||||
"open_jobs": 0
|
||||
},
|
||||
"result": "passed"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
# Lieferplan für Skriptverwaltung, Installation und Agentupdates
|
||||
|
||||
**Stand 17. November 2026. Die Kernstrecke des Entwicklungs-MVP ist auf Linux und Windows nativ nachgewiesen.** Ziel ist ein Gerät, das sich per HTTPS-Installer anmeldet, im Leerlauf Inventar meldet, einen Bibliotheksauftrag abholt, sein Ergebnis liefert und den nächsten signierten Release selbst übernimmt. [Produktvertrag](../Rust_RMM_Implementierungsspezifikation.md)
|
||||
**Fortsetzung am 17. November 2026. Beauftragt sind die echte Windows-Updateinstallation einschließlich erforderlicher Wartungsneustarts und der Wiederanlauf nach frühem Abbruch der Wartungseinrichtung.** Ziel ist ein Gerät, das sich per HTTPS-Installer anmeldet, im Leerlauf Inventar meldet, einen Bibliotheksauftrag abholt, sein Ergebnis liefert und den nächsten signierten Release selbst übernimmt. [Produktvertrag](../Rust_RMM_Implementierungsspezifikation.md)
|
||||
|
||||
## Produkt und Betriebsstand
|
||||
|
||||
@@ -60,3 +60,7 @@ Aus einem sauberen Klon von `95aa707` wurden drei Buildpläne geprüft und der R
|
||||
Den aktuellen Kernablauf mit den vorhandenen Build-, Deployment- und Operatorbefehlen benutzen. Eine tatsächliche Windows-Updateinstallation mit Ergebnis je Update und festgestelltem Neustartbedarf kann als eigener Wartungsauftrag abgenommen werden. Dafür das dokumentierte Fenster öffnen, die gespeicherte Updatevorlage wählen und den festen Plan in der WebUI genehmigen. Den automatischen Agentversionswechsel und bereits erfolgreiche Bibliotheksaufträge nur bei einem betroffenen Fehler erneut abnehmen.
|
||||
|
||||
Der integrierte Quellstand liegt auf `codex/mvp-development-release`. Die auf dem dedizierten Server aktivierte API-/Gatewayrevision ist `a799272`, Datenbankschema 25. Linux betreibt Release `0.1.0-mvp.2`, Windows GNU `0.1.4`. Ältere, aufgabenfremde Arbeiten liegen weiterhin gesondert im Arbeitsbaum.
|
||||
|
||||
### Wiederanlauf nach frühem Abbruch — nativ abgenommen
|
||||
|
||||
Der Controller wurde nach registriertem Helperkontakt vor der zentralen Freigabe mit Exitcode 85 beendet. Ein unabhängiger erneuter Aufruf stellte den normalen Geräteabgleich wieder her. Geräteidentität, Archivnachweise und Release blieben gebunden, die Probe erzeugte keine Aufträge. [Abnahme vom 17. November](evidence/windows-update-early-abort-20261117.json)
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
Stand: **2026-11-17**, Spezifikation **1.34** (`Rust_RMM_Implementierungsspezifikation.md`). Die ausdrücklichen normativen Entscheidungen in Kapitel 0 haben Vorrang vor früheren Architekturannahmen. Version 1.6 ersetzt die lokale Servertopologie durch den dedizierten Internetserver; Version 1.7 präzisiert das Notebook als reinen Entwicklungsrechner; Version 1.8 erlaubt vorgezogene parallele Windows-Entwicklung.
|
||||
|
||||
## Native Windows-Updateabnahme am 17. November 2026
|
||||
|
||||
Der Betreiber hat die Installation echter Windows-Updates auf der Windows-Test-VM, erforderliche Wartungsneustarts und die Prüfung des Wiederanlaufs nach einem frühen Abbruch der Wartungseinrichtung ausdrücklich beauftragt. Die Updatevorlage wird als eigener fester Bibliotheksauftrag über die WebUI genehmigt. Ein benötigter administrativer Neustart der Entwicklungs-VM kann über den geprüften SSH-Zugang erfolgen und wird mit Bootwechsel und anschließendem Agentkontakt belegt.
|
||||
|
||||
## Entwicklungsdaten und MVP-Abnahme (Version 1.34)
|
||||
|
||||
Der Betreiber hat für das Entwicklungssystem die Löschung der benannten Anfrage und der blockierenden Testdaten freigegeben. Eine frische Geräteidentität ist für die Installationsabnahme zulässig. Der reguläre Updatepfad erhält die Identität. Der Windows-GNU-Entwicklungsbuild ist als eigener signierter Release adressierbar. Für die mitgelieferte unveränderte Windows-Update-Vorlage dürfen feste Freigabe, Server- und Helperpolicy bei passender Capability bis 3600 Sekunden erlauben. Der dedizierte Gateway stellt den Geräteauftrag über HTTPS-Polling zu. Der Entwicklungsoperator darf nach geschlossenem Wartungsscope, terminalem Ergebnis und geprüftem Geräte-/Releasebezug die abgeschlossenen lokalen Arbeitsprojektionen archivieren und die dazugehörige Helper-Zuordnung lösen. Das ermöglicht den Rückbau eines temporären Wartungshelpers. Serverergebnisse und native Ausführungsnachweise werden dabei erhalten. [Bereinigungsnachweis](evidence/dev-cleanup-pull-20261117.json)
|
||||
|
||||
@@ -15,7 +15,7 @@ Auf dem per HTTPS aufgenommenen Windows-Testgerät läuft der Agent als LocalSer
|
||||
python3 tools/windows_update_scope.py window --minutes 15
|
||||
```
|
||||
|
||||
Der Befehl gibt die Fall-ID aus. Der Dev-Operator setzt die aktuelle HTTPS-Erstaufnahme mit Enrollmentgeneration 1 und Zuweisungsversion 1 voraus. Beim Öffnen prüft er die signierte Paarversion, den laufenden Agenten und seine Gerätebindung. Der Server aktiviert die Richtlinie erst nach einem aktuellen, passenden Geräte-Heartbeat. Die einzige zugelassene Bedienperson im Entwicklungsprofil ist `simulated-testprincipal-a`. Das Release-Polling pausiert während des Fensters. Die Ablaufzeit des Fensters steht in UTC in der Ausgabe. Ein anderes noch aktives SYSTEM-Fenster bleibt dem bisherigen Betreiber zugeordnet.
|
||||
Der Befehl gibt die Fall-ID aus. Der Dev-Operator setzt die aktuelle HTTPS-Erstaufnahme mit Enrollmentgeneration 1 und Zuweisungsversion 1 voraus. Vor der ersten Geräteänderung speichert der Server die Fall-ID, den signierten Ausgangscheckpoint, beide Binärhashes und die ursprüngliche Richtlinie. Beim Öffnen prüft der Operator die signierte Paarversion, den laufenden Agenten und seine Gerätebindung. Nach einem passenden Geräte-Heartbeat und einer aktuellen signierten Helper-Uhr meldet der Operator `central_scope_open`. Ab dann steht die Richtlinie bereit. Die einzige zugelassene Bedienperson im Entwicklungsprofil ist `simulated-testprincipal-a`. Das Release-Polling pausiert während des Fensters. Die Ablaufzeit des Fensters steht in UTC in der Ausgabe. Ein anderes noch aktives SYSTEM-Fenster bleibt dem bisherigen Betreiber zugeordnet.
|
||||
|
||||
3. In der privaten WebUI das Windows-Gerät und die gewünschte gespeicherte Skriptversion wählen. Zur Prüfung der Identität eine Bibliotheksrevision für Windows PowerShell und Identität `system` verwenden. Der Skripttext ist die folgende Zeile mit genau einem abschließenden LF.
|
||||
|
||||
@@ -35,4 +35,12 @@ Auf dem per HTTPS aufgenommenen Windows-Testgerät läuft der Agent als LocalSer
|
||||
|
||||
Die native Identitätsabfrage und der vollständige Rückbau sind im [Entwicklungsnachweis vom 17. November](evidence/windows-update-window-20261117.json) dokumentiert.
|
||||
|
||||
Bei einem Abbruch der allerersten Einrichtung vor der zentralen Fensterakte kann bereits eine Helper-Journalzuordnung bestehen. Der Operator verlangt dann eine fallbezogene Prüfung der Geräte- und Journalbindung. Der automatische Rückbau dieses frühen Abbruchfalls ist noch nicht abgenommen.
|
||||
Derselbe `close --case` schließt eine abgebrochene Einrichtung, sobald die Fall-ID ausgegeben wurde. War der Helper schon synchronisiert, vergleicht der Controller den ursprünglichen und den aktuellen signierten Checkpoint mit den lokalen Journalen. Zum Abschluss müssen Agent und Release-Polling laufen und ein neuer synchroner Heartbeat ohne SYSTEM-Fähigkeiten vorliegen.
|
||||
|
||||
Die [native Abbruchabnahme](evidence/windows-update-early-abort-20261117.json) beendet den Controller nach dem ersten passenden authentisierten Helper-Heartbeat und vor dem Öffnen der Richtlinie. Auf einem freien Windows-Entwicklungsgerät lässt sich der Ablauf wiederholen.
|
||||
|
||||
```sh
|
||||
python3 tests/windows_update_early_abort.py --execute
|
||||
```
|
||||
|
||||
Auch nach einem abgebrochenen Test gibt der Befehl die Fall-ID aus, mit der sich der Rückbau fortsetzen lässt.
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Native development acceptance: kill a preparing maintenance controller before activation."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tools'))
|
||||
import windows_update_scope as scope
|
||||
|
||||
|
||||
def child(case):
|
||||
original = scope.remote_server
|
||||
def interrupt(action, data):
|
||||
if action == 'read' and data.get('helper_policy_sha256'):
|
||||
row = original(action, data)
|
||||
if scope.scoped(data, row) and row['sync_head']['helper_id']:
|
||||
evidence = {'case_id': case, 'killed_before_activation': True,
|
||||
'server_time': row['install']['contact'], 'sync_head': row['sync_head'],
|
||||
'sync_ready': row['install']['ready'], 'helper_sync_ready': row['install']['helper_ready'],
|
||||
'open_jobs': row['open_jobs']}
|
||||
path = ROOT / 'devstate/windows-update-windows' / (case + '-fault.json')
|
||||
scope.atomic(path, scope.encode(evidence))
|
||||
scope.emit(evidence)
|
||||
os._exit(85)
|
||||
return row
|
||||
scope.require(action != 'enable', 'Fault boundary missed')
|
||||
return original(action, data)
|
||||
scope.remote_server = interrupt
|
||||
sys.argv = [scope.__file__, 'window', '--case', case, '--minutes', '15']
|
||||
scope.local()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--execute', action='store_true', help='Run against the actual development Windows enrollment')
|
||||
parser.add_argument('--child', type=scope.identifier, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args()
|
||||
if args.child:
|
||||
child(args.child)
|
||||
return
|
||||
if not args.execute:
|
||||
parser.print_help()
|
||||
return
|
||||
case = str(uuid.uuid4())
|
||||
scope.emit({'native_abort_test':case})
|
||||
state = ROOT / 'devstate/windows-update-windows'
|
||||
try:
|
||||
stopped = subprocess.run([sys.executable, __file__, '--child', case])
|
||||
scope.require(stopped.returncode == 85, 'Controller did not stop at the expected fault boundary')
|
||||
finally:
|
||||
if (state / (case + '.json')).is_file():
|
||||
subprocess.run([sys.executable, scope.__file__, 'close', '--case', case], check=True)
|
||||
scope.emit({'verified_early_abort_recovery':case})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -65,31 +65,32 @@ def public_key():
|
||||
conn.close()
|
||||
|
||||
|
||||
def observation(ids, opened, cutoff):
|
||||
def observation(ids, opened, cutoff=None):
|
||||
"""Only canonical UUIDs and normalized datetimes reach the query."""
|
||||
tenant, asset, agent = (ids[k] for k in ('tenant_id', 'asset_id', 'agent_id'))
|
||||
horizon = f"BETWEEN '{opened}' AND '{cutoff}'" if cutoff else f">= '{opened}'"
|
||||
return f"""
|
||||
SELECT jsonb_build_object(
|
||||
'open_jobs',(SELECT count(*) FROM jobs j WHERE j.tenant_id=i.tenant_id AND j.agent_id=i.agent_id
|
||||
AND j.state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')),
|
||||
'journal_id',s.journal_id,'checkpoint',s.checkpoint,'decision_id',s.decision_id,
|
||||
'journal_id',s.journal_id,'checkpoint',s.checkpoint,'decision_id',s.decision_id,'head_created_at',c.created_at,
|
||||
'helper_id',s.helper_journal_id,'helper_checkpoint',s.helper_checkpoint,
|
||||
'signed',encode(c.payload,'hex'),'signature',encode(c.signature,'hex'),
|
||||
'jobs',COALESCE((SELECT jsonb_agg(jsonb_build_object(
|
||||
'job',j.job_id,'attempt',j.attempt_id,'grant',g.grant_id,'state',j.state,
|
||||
'payload_sha256',j.payload_hash,'bytes_hash',encode(sha256(j.payload),'hex'),
|
||||
'payload_binding',p-'action','action',p->'action',
|
||||
'principal',z.principal,'window_approval',z.created_at BETWEEN '{opened}' AND '{cutoff}',
|
||||
'principal',z.principal,'window_approval',z.created_at {horizon},
|
||||
'dev',z.testsimulation AND z.approval_origin='dev_test_browser_simulation',
|
||||
'plan_hash',z.plan_hash,'authorization_id',z.authorization_id,
|
||||
'in_window',g.created_at BETWEEN '{opened}' AND '{cutoff}',
|
||||
'in_window',g.created_at {horizon},
|
||||
'reserved',j.start_reserved,'result_grant',j.result->>'grant_id','revision',j.revision)
|
||||
ORDER BY j.job_id)
|
||||
FROM jobs j JOIN authorizations z USING(tenant_id,operation_id)
|
||||
CROSS JOIN LATERAL (SELECT convert_from(j.payload,'UTF8')::jsonb AS p) jp
|
||||
LEFT JOIN execution_permits g USING(tenant_id,job_id,attempt_id)
|
||||
WHERE j.tenant_id=i.tenant_id AND j.agent_id=i.agent_id AND p->>'execution_identity'='system'
|
||||
AND (z.created_at BETWEEN '{opened}' AND '{cutoff}' OR g.created_at BETWEEN '{opened}' AND '{cutoff}')),
|
||||
AND (z.created_at {horizon} OR g.created_at {horizon})),
|
||||
'[]'::jsonb)) AS observation
|
||||
FROM agent_installations i JOIN agent_sync_state s USING(tenant_id,agent_id)
|
||||
JOIN sync_checkpoints c USING(tenant_id,agent_id,decision_id)
|
||||
@@ -145,6 +146,63 @@ def verify(snapshot, ids):
|
||||
return {**ids, **{k:snapshot[k] for k in (*fields,'helper_id')}, 'attempts':attempts}
|
||||
|
||||
|
||||
def prepared_baseline(case, data, ids):
|
||||
path = case/'prepared.json'
|
||||
prepared = json.loads(private_read(path))
|
||||
require(prepared['schema_version'] == 1 and all(prepared[k] == value for k,value in ids.items())
|
||||
and prepared['release_id'] == scope.identifier(data['release_id']), 'Prepared device or release differs')
|
||||
for k in ('agent_sha256','helper_binary_sha256'):
|
||||
require(re.fullmatch('[a-f0-9]{64}', prepared[k]) and data.get(k,prepared[k]) == prepared[k],
|
||||
'Prepared component differs')
|
||||
journal = scope.identifier(prepared['journal_id'])
|
||||
decision = scope.identifier(prepared['decision_id'])
|
||||
checkpoint = prepared['checkpoint']
|
||||
require(type(checkpoint) is int and checkpoint >= 0, 'Invalid prepared checkpoint')
|
||||
opened = datetime.fromtimestamp(path.stat().st_mtime, timezone.utc)
|
||||
signed = sql(f"""SELECT json_build_object('signed',encode(payload,'hex'),'signature',encode(signature,'hex'))
|
||||
FROM sync_checkpoints WHERE tenant_id='{ids['tenant_id']}' AND agent_id='{ids['agent_id']}'
|
||||
AND journal_id='{journal}' AND checkpoint={checkpoint} AND decision_id='{decision}'
|
||||
AND created_at<='{opened.isoformat()}'""")
|
||||
raw = bytes.fromhex(signed['signed'])
|
||||
key_id, public = public_key()
|
||||
public.verify(bytes.fromhex(signed['signature']), raw)
|
||||
baseline = json.loads(raw)
|
||||
require(baseline['domain']=='rmm.reconciliation-decision' and baseline['schema_version']==1
|
||||
and baseline['key_id']==key_id and baseline['ready'] is True and baseline['reason']=='CONSISTENT'
|
||||
and baseline['helper_journal_id'] is None and baseline['helper_checkpoint']==0
|
||||
and baseline['enrollment_generation']==baseline['assignment_version']==1
|
||||
and all(baseline[k]==prepared[k] for k in ('tenant_id','agent_id','asset_id','journal_id','checkpoint','decision_id')),
|
||||
'Prepared baseline must be the signed decision of the normal device without a helper')
|
||||
return prepared, opened
|
||||
|
||||
|
||||
def require_native_verification(data, witness):
|
||||
proof = data.get('native_verification') or {}
|
||||
require(data.get('witness') == witness and proof.get('verified') is True and proof.get('applied') is False
|
||||
and all(proof.get(k)==witness[k] for k in ('case_id','helper_id','decision_id'))
|
||||
and type(proof.get('attempts')) is int and proof['attempts']==len(witness['attempts']),
|
||||
'Exact witness and positive native archive verification required before detach')
|
||||
|
||||
|
||||
def window_query(window, case, ids, data):
|
||||
policy = json.loads(private_read(window))
|
||||
only, = policy['scopes']
|
||||
hashes = ('helper_binary_sha256','helper_policy_sha256')
|
||||
expected_scope = {k:ids[k] for k in ('tenant_id','agent_id','asset_id')}
|
||||
expected_scope.update(acceptance_evidence_id=ids['case_id'], principals=[PRINCIPAL], allowed_services=[],
|
||||
enrollment_generation=1, assignment_version=1, max_runtime_seconds=30,
|
||||
windows_update_max_runtime_seconds=3600)
|
||||
for k in hashes:
|
||||
require(re.fullmatch('[a-f0-9]{64}',only[k]) and data.get(k,only[k]) == only[k], 'Helper pin differs')
|
||||
expected_scope[k] = only[k]
|
||||
require(policy['schema_version'] == 3 and policy['profile'] == 'development' and only == expected_scope,
|
||||
'Case identity, privileges or original policy differ')
|
||||
opened = datetime.fromtimestamp(window.stat().st_mtime, timezone.utc)
|
||||
cutoff = min(scope.stamp(policy['expires_at']), datetime.fromtimestamp((case/'closed').stat().st_mtime,timezone.utc))
|
||||
require(opened <= cutoff, 'Invalid maintenance horizon')
|
||||
return observation(ids,opened.isoformat(),cutoff.isoformat())
|
||||
|
||||
|
||||
def server(action, data):
|
||||
require(action in ('witness','detach'), 'Only witness and detach are supported')
|
||||
ids = {k:scope.identifier(data[k]) for k in ('case_id','tenant_id','asset_id','agent_id')}
|
||||
@@ -163,35 +221,29 @@ def server(action, data):
|
||||
mode = case.lstat()
|
||||
require(case.resolve() == case and stat.S_ISDIR(mode.st_mode) and mode.st_uid == 0
|
||||
and stat.S_IMODE(mode.st_mode) == 0o700, 'Unsafe development window directory')
|
||||
if not window.exists():
|
||||
# Even a failed opening can already have registered a real helper journal.
|
||||
if not window.exists() and not (case/'prepared.json').exists():
|
||||
# The operator persists its local case ID before prepare. A crash in
|
||||
# that gap has no authority to detach any helper or alter old jobs.
|
||||
require(action == 'witness' and data.get('native_window_absent') is True
|
||||
and private_read(case/'closed') == b'closed\n', 'Unprepared case needs a closed, empty native window')
|
||||
original = case/'original.json'
|
||||
require(not original.exists() or private_read(original)==current, 'Policy changed during incomplete preparation')
|
||||
row = sql(f"""SELECT json_build_object('empty',EXISTS (
|
||||
SELECT 1 FROM agent_installations i LEFT JOIN agent_sync_state s USING(tenant_id,agent_id)
|
||||
SELECT 1 FROM agent_installations i JOIN agent_sync_state s USING(tenant_id,agent_id)
|
||||
WHERE i.tenant_id='{ids['tenant_id']}' AND i.agent_id='{ids['agent_id']}' AND i.asset_id='{ids['asset_id']}'
|
||||
AND i.revoked_at IS NULL AND s.helper_journal_id IS NULL AND coalesce(s.helper_checkpoint,0)=0
|
||||
AND i.revoked_at IS NULL AND s.helper_journal_id IS NULL AND s.helper_checkpoint=0
|
||||
AND NOT EXISTS (SELECT 1 FROM jobs j WHERE j.tenant_id=i.tenant_id AND j.agent_id=i.agent_id AND
|
||||
(j.state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')
|
||||
OR convert_from(j.payload,'UTF8')::jsonb->>'execution_identity'='system'))))""")
|
||||
require(row['empty'] and action == 'witness', 'A helper or SYSTEM history exists without an opened scope')
|
||||
j.state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown'))))""")
|
||||
require(row['empty'], 'Unprepared case still has a helper or an unfinished job')
|
||||
return None
|
||||
policy = json.loads(private_read(window))
|
||||
require(private_read(case/'closed') == b'closed\n' and private_read(case/'original.json') == current,
|
||||
'Expected this closed scope and its original policy')
|
||||
only, = policy['scopes']
|
||||
hashes = ('helper_binary_sha256','helper_policy_sha256')
|
||||
expected_scope = {k:ids[k] for k in ('tenant_id','agent_id','asset_id')}
|
||||
expected_scope.update(acceptance_evidence_id=ids['case_id'], principals=[PRINCIPAL], allowed_services=[],
|
||||
enrollment_generation=1, assignment_version=1, max_runtime_seconds=30,
|
||||
windows_update_max_runtime_seconds=3600)
|
||||
for k in hashes:
|
||||
require(re.fullmatch('[a-f0-9]{64}',only[k]) and data.get(k,only[k]) == only[k], 'Helper pin differs')
|
||||
expected_scope[k] = only[k]
|
||||
require(policy['schema_version'] == 3 and policy['profile'] == 'development' and only == expected_scope,
|
||||
'Case identity, privileges or original policy differ')
|
||||
opened = datetime.fromtimestamp(window.stat().st_mtime, timezone.utc)
|
||||
cutoff = min(scope.stamp(policy['expires_at']), datetime.fromtimestamp((case/'closed').stat().st_mtime,timezone.utc))
|
||||
require(opened <= cutoff, 'Invalid maintenance horizon')
|
||||
query = observation(ids,opened.isoformat(),cutoff.isoformat())
|
||||
prepared = None
|
||||
if window.exists():
|
||||
query = window_query(window, case, ids, data)
|
||||
else:
|
||||
prepared, opened = prepared_baseline(case, data, ids)
|
||||
query = observation(ids, opened.isoformat())
|
||||
record = sql(query)
|
||||
require(record['open_jobs'] == 0, 'Open jobs require separate reconciliation')
|
||||
saved = case/'detach-intent.json'
|
||||
@@ -200,16 +252,28 @@ def server(action, data):
|
||||
if done.exists():
|
||||
require(receipt and json.loads(private_read(done)) == receipt, 'Completion receipt differs from detach intent')
|
||||
return receipt
|
||||
if prepared:
|
||||
require(record['journal_id']==prepared['journal_id'] and record['checkpoint']>=prepared['checkpoint']
|
||||
and not record['jobs'], 'Prepared journal changed or a SYSTEM job or grant was created after preparation')
|
||||
if record['helper_id'] is not None:
|
||||
require(record['decision_id'] != prepared['decision_id'] and scope.stamp(record['head_created_at'])>=opened,
|
||||
'The current helper was not bound after preparation')
|
||||
if record['helper_id'] is None:
|
||||
if prepared and receipt is None and action=='witness' and record['helper_checkpoint']==0:
|
||||
return None
|
||||
require(receipt and receipt['journal_id'] == record['journal_id'] and record['helper_checkpoint'] == 0,
|
||||
'Missing evidence of the original detach transaction')
|
||||
if action == 'detach':
|
||||
if prepared:
|
||||
require_native_verification(data, receipt)
|
||||
scope.atomic(done,scope.encode(receipt))
|
||||
return receipt
|
||||
witness = verify(record,ids)
|
||||
require(receipt in (None,witness) and data.get('witness',witness) == witness, 'Device or witness changed')
|
||||
if action == 'witness':
|
||||
return witness
|
||||
if prepared:
|
||||
require_native_verification(data, witness)
|
||||
if receipt is None:
|
||||
scope.atomic(saved,scope.encode(witness))
|
||||
# Fence a concurrent signed sync (same lock order as the gateway). The server-owned
|
||||
|
||||
@@ -74,6 +74,9 @@ def read_registry(data):
|
||||
AND certificate_sha256=last_authenticated_certificate_sha256 AND last_contact_at > now() - interval '2 minutes',
|
||||
'enrollment_generation',enrollment_generation,'assignment_version',assignment_version,'capabilities',capabilities)
|
||||
FROM agent_installations WHERE tenant_id='{tenant}' AND asset_id='{asset}' AND agent_id='{agent}' AND revoked_at IS NULL),
|
||||
'sync_head',(SELECT json_build_object('journal_id',s.journal_id,'checkpoint',s.checkpoint,'decision_id',s.decision_id,
|
||||
'helper_id',s.helper_journal_id,'helper_checkpoint',s.helper_checkpoint) FROM agent_sync_state s
|
||||
WHERE tenant_id='{tenant}' AND agent_id='{agent}'),
|
||||
'open_jobs',(SELECT count(*) FROM jobs WHERE tenant_id='{tenant}' AND agent_id='{agent}'
|
||||
AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')),
|
||||
'principal_authorized',EXISTS (SELECT 1 FROM memberships WHERE tenant_id='{tenant}' AND principal='simulated-testprincipal-a'
|
||||
@@ -156,31 +159,49 @@ def server(action, data):
|
||||
if not (history/'closed').exists():
|
||||
atomic(history/'closed',b'closed\n')
|
||||
return {'central_scope_closed':True}
|
||||
require(action=='enable', 'Unsupported central operation')
|
||||
for key in ('helper_binary_sha256','helper_policy_sha256'):
|
||||
require(re.fullmatch('[a-f0-9]{64}',data[key]),'Invalid helper hash')
|
||||
require(scoped(data, read_registry(data)), 'Device has no current authenticated narrow helper capabilities')
|
||||
require(action in ('prepare','enable'), 'Unsupported central operation')
|
||||
ids={k:identifier(data[k]) for k in ('case_id','tenant_id','asset_id','agent_id','release_id')}
|
||||
hashes={k:data[k] for k in ('agent_sha256','helper_binary_sha256')}
|
||||
require(all(re.fullmatch('[a-f0-9]{64}',v) for v in hashes.values()), 'Invalid signed image pair')
|
||||
expires=stamp(data['expires_at'])
|
||||
require(utc()<expires<=utc()+timedelta(minutes=120), 'Window expired or exceeds 120 minutes')
|
||||
if history.exists():
|
||||
require(not (history/'closed').exists() and (history/'original.json').is_file()
|
||||
and (history/'window.json').is_file(), 'Case is closed or incomplete')
|
||||
expected=(history/'window.json').read_bytes()
|
||||
require(current in (expected,(history/'original.json').read_bytes()), 'Central policy changed while opening')
|
||||
require(json.loads(expected)['expires_at']==data['expires_at'], 'Cannot extend an existing window')
|
||||
else:
|
||||
original,prepared,candidate=history/'original.json',history/'prepared.json',history/'window.json'
|
||||
require(not (history/'closed').exists(),'Case is already closed')
|
||||
observation=read_registry(data)
|
||||
if action=='prepare':
|
||||
require(usable(observation),'An idle, synchronized and authenticated enrollment is required')
|
||||
head=observation['sync_head']
|
||||
require(head and head['helper_id'] is None and head['helper_checkpoint']==0,'Existing helper journal must close first')
|
||||
old=json.loads(current)
|
||||
require(old['profile']=='development' and (stamp(old['expires_at'])<=utc() or not old['scopes']),
|
||||
'Another live maintenance window is active')
|
||||
scope={k:identifier(data[k]) for k in ('tenant_id','asset_id','agent_id')}
|
||||
scope.update(enrollment_generation=1,assignment_version=1,principals=['simulated-testprincipal-a'],
|
||||
acceptance_evidence_id=case,allowed_services=[],max_runtime_seconds=30,
|
||||
windows_update_max_runtime_seconds=3600,
|
||||
**{k:data[k] for k in ('helper_binary_sha256','helper_policy_sha256')})
|
||||
expected=encode(dict(schema_version=3,profile='development',expires_at=data['expires_at'],scopes=[scope]))
|
||||
history.mkdir(mode=0o700)
|
||||
atomic(history/'original.json',current)
|
||||
atomic(history/'window.json',expected)
|
||||
entry={'schema_version':1,**ids,**hashes,**{k:head[k] for k in ('journal_id','checkpoint','decision_id')}}
|
||||
history.mkdir(mode=0o700,exist_ok=True)
|
||||
require(not candidate.exists() and (not original.exists() or original.read_bytes()==current),
|
||||
'Another policy or opening changed this case')
|
||||
if prepared.exists():
|
||||
prior=json.loads(prepared.read_bytes())
|
||||
require(all(prior[k]==entry[k] for k in (*ids,*hashes,'journal_id')), 'Different prepared enrollment')
|
||||
else:
|
||||
atomic(original,current)
|
||||
atomic(prepared,encode(entry))
|
||||
return {'prepared':True,'case_id':case,'journal_id':head['journal_id']}
|
||||
saved=original.read_bytes()
|
||||
prior=json.loads(prepared.read_bytes())
|
||||
require(all(prior[k]==v for k,v in (ids|hashes).items()),'Installed pair differs from the prepared case')
|
||||
require(re.fullmatch('[a-f0-9]{64}',data['helper_policy_sha256']),'Invalid helper policy hash')
|
||||
require(scoped(data,observation),'Device has no current authenticated narrow helper capabilities')
|
||||
scope={k:ids[k] for k in ('tenant_id','asset_id','agent_id')}
|
||||
scope.update(enrollment_generation=1,assignment_version=1,principals=['simulated-testprincipal-a'],
|
||||
acceptance_evidence_id=case,allowed_services=[],max_runtime_seconds=30,
|
||||
windows_update_max_runtime_seconds=3600,helper_binary_sha256=hashes['helper_binary_sha256'],
|
||||
helper_policy_sha256=data['helper_policy_sha256'])
|
||||
expected=encode(dict(schema_version=3,profile='development',expires_at=data['expires_at'],scopes=[scope]))
|
||||
if candidate.exists():
|
||||
require(candidate.read_bytes()==expected and current in (expected,saved),'Changed window or central policy')
|
||||
else:
|
||||
require(current==saved,'Central policy changed after preparation')
|
||||
atomic(candidate,expected)
|
||||
atomic(path,expected,(st.st_uid,st.st_gid))
|
||||
return {'central_scope_open':True,'expires_at':data['expires_at']}
|
||||
|
||||
@@ -255,9 +276,11 @@ def local():
|
||||
emit(plan);return
|
||||
state_dir.mkdir(parents=True,mode=0o700,exist_ok=True)
|
||||
casefile=state_dir/(data['case_id']+'.json')
|
||||
with casefile.open('xb') as dst:
|
||||
data.update(agent_sha256=plan['agent_sha256'],helper_binary_sha256=plan['helper_sha256'])
|
||||
with os.fdopen(os.open(casefile,os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600),'wb') as dst:
|
||||
dst.write(encode(data));dst.flush();os.fsync(dst.fileno())
|
||||
emit({'preparing_window':data['case_id'],'expires_at':data['expires_at']})
|
||||
emit(remote_server('prepare',data))
|
||||
try:
|
||||
result=guest('Install',data)
|
||||
require(result['case_id']==data['case_id'] and result['agent_id']==data['agent_id']
|
||||
@@ -299,16 +322,19 @@ def close(data):
|
||||
wait_for(data,lambda row:row['install'] and row['open_jobs']==0,180,
|
||||
'Attempt still active. Central scope is revoked; retry close when the result is terminal.')
|
||||
observed=windows('Inspect-DevelopmentWindowsUpdate.ps1',{})
|
||||
if observed.get('case_id') is not None or observed.get('helper') is not None:
|
||||
absent=observed.get('case_id') is None and observed.get('helper') is None
|
||||
if not absent:
|
||||
require(observed.get('case_id')==data['case_id'],'Local helper belongs to another maintenance window')
|
||||
if observed.get('release_id')==data['release_id']:
|
||||
result=guest('Remove',data)
|
||||
require(result['closed'] and not result['helper_registered'] and result['actor_running'],
|
||||
'Could not withdraw the helper from this enrollment')
|
||||
emit(result)
|
||||
witness=detach_server('witness',data)
|
||||
witness=detach_server('witness',{**data,'native_window_absent':absent})
|
||||
if witness and witness.get('helper_id'):
|
||||
emit(retire_native('Verify',data,witness))
|
||||
emit(detach_server('detach',{**data,'witness':witness}))
|
||||
verified=retire_native('Verify',data,witness)
|
||||
emit(verified)
|
||||
emit(detach_server('detach',{**data,'witness':witness,'native_verification':verified}))
|
||||
emit(retire_native('Apply',data,witness))
|
||||
latest=windows('Inspect-DevelopmentWindowsUpdate.ps1',{})
|
||||
if latest['release_id']==data['release_id']:
|
||||
|
||||
Reference in New Issue
Block a user