Files
rust-discovery/tools/windows_update_scope.py
T

337 lines
17 KiB
Python

#!/usr/bin/env python3
"""Open and revoke a device-bound development window for the built-in Windows Update template."""
import argparse
import base64
from datetime import datetime, timedelta, timezone
import fcntl
import gzip
import json
import os
from pathlib import Path
import re
import shlex
import stat
import subprocess
import sys
import tempfile
import time
import uuid
def require(ok, message):
if not ok:
raise ValueError(message)
def identifier(value):
raw = str(value)
parsed = uuid.UUID(raw)
require(parsed.int and str(parsed) == raw, 'A canonical, nonempty UUID is required')
return raw
def utc():
return datetime.now(timezone.utc)
def stamp(value):
result = datetime.fromisoformat(value.replace('Z', '+00:00'))
require(result.tzinfo is not None, 'Timestamp lacks timezone')
return result
def encode(value):
return json.dumps(value, separators=(',', ':'), sort_keys=True).encode()
def atomic(path, data, owner=None):
with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as handle:
temp = Path(handle.name)
try:
if owner is not None:
os.fchown(handle.fileno(), *owner)
os.fchmod(handle.fileno(), 0o600)
handle.write(data)
handle.flush()
os.fsync(handle.fileno())
os.replace(temp, path)
directory = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY)
try:
os.fsync(directory)
finally:
os.close(directory)
finally:
temp.unlink(missing_ok=True)
def read_registry(data):
tenant, asset, agent = [identifier(data[k]) for k in ('tenant_id', 'asset_id', 'agent_id')]
# Each interpolated value is a canonical UUID; this invocation cannot mutate Postgres.
sql = f"""BEGIN READ ONLY;
SELECT json_build_object('install',(SELECT json_build_object(
'contact',last_contact_at,'ready',sync_ready AND NOT restore_blocked,'helper_ready',helper_sync_ready,
'fresh',last_authenticated_at > now() - interval '2 minutes'
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),
'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'
AND revoked_at IS NULL AND kind='test_human' AND 'scripts:execute'=ANY(permissions) AND 'system:execute'=ANY(permissions)));
COMMIT;"""
raw = subprocess.check_output(['docker', 'exec', '-i', 'rust-discovery-server-postgres-1',
'psql', '-XqAt', '-v', 'ON_ERROR_STOP=1', '-U', 'postgres', '-d', 'rmm'], input=sql.encode())
return json.loads(raw)
def policy_location():
base = Path('/opt/rust-discovery/devstate/windows-system-policy')
mounts = []
for role in ('api', 'gateway'):
container = f'rust-discovery-server-{role}-1'
settings = subprocess.check_output(['docker','exec', container, 'printenv','RMM_PROFILE',
'RMM_DEV_SYSTEM_ENABLED','RMM_DEV_SYSTEM_POLICY_FILE'], text=True).splitlines()
require(settings == ['development','true','/system-policy/policy.json'], 'Development policy unavailable on '+role)
bind = json.loads(subprocess.check_output(['docker','inspect','--format','{{json .Mounts}}', container]))
dirs = [Path(m['Source']) for m in bind if m['Destination']=='/system-policy' and m['Type']=='bind' and not m['RW']]
require(len(dirs)==1 and dirs[0].parent.parent==base and dirs[0].name=='mounted'
and dirs[0].resolve()==dirs[0], 'Unexpected or writable SYSTEM policy mount')
mounts.append(dirs[0])
require(mounts[0]==mounts[1], 'API and gateway must use the same directory')
return mounts[0]/'policy.json'
def usable(observation):
install = observation.get('install') or {}
return (observation['open_jobs']==0 and observation['principal_authorized']
and install.get('fresh') and install['ready']
and install['enrollment_generation']==install['assignment_version']==1)
def scoped(data, observation):
if not usable(observation) or not observation['install']['helper_ready']:
return False
caps=set(observation['install']['capabilities'])
needed={'windows.base/1','rmm.sync.v1','helper.system.v1','system:run_script','script.windows_update.long/1',
'helper.policy.sha256:'+data['helper_policy_sha256'], 'helper.binary.sha256:'+data['helper_binary_sha256'],
'run_script','script.powershell/1','script.powershell.process_remote_signed/1'}
return needed<=caps and not any(c in caps for c in
('script.full_maintenance/1','system:set_service_state','system:ensure_package','system:reboot','linux.base/1'))
def server(action, data):
if action=='read':
return read_registry(data)
case = identifier(data['case_id'])
path = policy_location()
if action=='preflight':
current=json.loads(path.read_bytes())
require(current['profile']=='development' and (stamp(current['expires_at'])<=utc() or not current['scopes']),
'Another live maintenance window is active')
return {'dev_scope_available':True}
history = path.parent.parent.parent/('update-window-'+case)
lockpath = Path('/opt/rust-discovery/devstate/.windows-system-scope.lock')
fd = os.open(lockpath, os.O_RDWR|os.O_CREAT|os.O_NOFOLLOW, 0o600)
with os.fdopen(fd,'rb') as mutex:
fcntl.flock(mutex, fcntl.LOCK_EX)
st=path.lstat()
require(stat.S_ISREG(st.st_mode) and st.st_uid==st.st_gid==10001 and stat.S_IMODE(st.st_mode)==0o600
and st.st_size<=65536 and st.st_nlink==1, 'Unexpected policy file or ownership')
current=path.read_bytes()
if action=='revoke':
history.mkdir(mode=0o700,exist_ok=True)
if history.exists():
original, window=history/'original.json', history/'window.json'
# The candidate is recorded durably before replacing the live policy.
if not window.exists():
require(not original.exists() or original.read_bytes()==current,'Partial opening has a different live policy')
if not (history/'closed').exists():
atomic(history/'closed',b'closed\n')
return {'central_scope_closed':True}
saved=original.read_bytes()
candidate=window.read_bytes()
require(current in (candidate,saved), 'Central policy changed since opening. Reconcile before replacing it.')
if current!=saved:
atomic(path,saved,(st.st_uid,st.st_gid))
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')
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:
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)
atomic(path,expected,(st.st_uid,st.st_gid))
return {'central_scope_open':True,'expires_at':data['expires_at']}
def remote_server(action, data):
from dev_stage import ssh_prefix
# Only public device bindings and case data travel as arguments.
cmd=shlex.join(['python3','-I','-','--on-server',action,base64.b64encode(encode(data)).decode()])
return json.loads(subprocess.check_output([*ssh_prefix('server'),cmd],input=Path(__file__).read_bytes()))
def windows(file, args):
from dev_stage import ssh_prefix
directory=Path(__file__).resolve().parents[1]/'deploy/windows'
packet={'args':args,'program':(directory/file).read_text()}
payload=base64.b64encode(gzip.compress(encode(packet))).decode()
# Windows OpenSSH does not reliably signal EOF on Console.In; keep the fixed
# local operator and its typed arguments in a compressed, printable packet.
command=("$ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue';try{$m=[IO.MemoryStream]::new([Convert]::FromBase64String('"+payload+
"'));$z=[IO.Compression.GzipStream]::new($m,[IO.Compression.CompressionMode]::Decompress);"
"$j=[IO.StreamReader]::new($z).ReadToEnd()|ConvertFrom-Json;$a=@{};foreach($v in $j.args.psobject.Properties)"
"{$a[$v.Name]=$v.Value};& ([ScriptBlock]::Create([string]$j.program)) @a}"
"catch{[Console]::Error.WriteLine($_.Exception.Message);exit 1}")
cmd='powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(command.encode('utf-16-le')).decode()
require(len(cmd)<31000,'Windows operator exceeds native command-line limit')
return json.loads(subprocess.check_output([*ssh_prefix('windows'),cmd],timeout=120))
def guest(action, data):
args={'Action':action, 'CaseId':data['case_id'],'AgentId':data['agent_id'],'AssetId':data['asset_id'],'ReleaseId':data['release_id']}
return windows('Set-DevelopmentWindowsUpdate.ps1',args)
def emit(value):
print(json.dumps(value,ensure_ascii=False),flush=True)
def wait_for(data, predicate, timeout, message):
end=time.monotonic()+timeout
while True:
result=remote_server('read',data)
if predicate(result):
return result
require(time.monotonic()<end,message)
time.sleep(4)
def local():
cli=argparse.ArgumentParser(description=__doc__)
cli.add_argument('action',choices=('inspect','plan','window','close'))
cli.add_argument('--case',type=identifier)
cli.add_argument('--minutes',type=int,default=15,choices=range(1,121),metavar='1..120')
args=cli.parse_args()
state_dir=Path(__file__).resolve().parents[1]/'devstate/windows-update-windows'
if args.action=='close':
require(args.case, 'Pass --case from the opening command')
data=json.loads((state_dir/(args.case+'.json')).read_bytes())
close(data)
return
observed=windows('Inspect-DevelopmentWindowsUpdate.ps1',{})
data={k:identifier(observed[k]) for k in ('tenant_id','agent_id','asset_id','release_id')}
baseline=remote_server('read',data)
if args.action=='inspect':
emit({'device':observed,'server':baseline});return
require(usable(baseline), 'Device must be idle, authenticated, synchronized and bound to an enabled operator')
data.update(case_id=args.case or str(uuid.uuid4()),expires_at=(utc()+timedelta(minutes=args.minutes)).isoformat())
remote_server('preflight',data)
plan=guest('Plan',data)
require(plan['requires_helper_creation'] and not observed['case_id'], 'Another helper or local window is active')
if args.action=='plan':
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:
dst.write(encode(data));dst.flush();os.fsync(dst.fileno())
emit({'preparing_window':data['case_id'],'expires_at':data['expires_at']})
try:
result=guest('Install',data)
require(result['case_id']==data['case_id'] and result['agent_id']==data['agent_id']
and result['helper_binary_sha256']==plan['helper_sha256'],'Device binding changed')
data.update({k:result[k] for k in ('helper_binary_sha256','helper_policy_sha256')})
atomic(casefile,encode(data))
wait_for(data,lambda row:scoped(data,row),min(180,max(1,(stamp(data['expires_at'])-utc()).total_seconds())),
'No authenticated narrow SYSTEM capabilities received during the preparation window')
# The static capability refresh can precede the first signed helper-role
# clock exchange. Check the actual current probe before exposing this scope.
while True:
probe=windows('Inspect-DevelopmentWindowsUpdate.ps1',{'ClockOnly':True})
fresh=probe.get('observed_at') and timedelta(0)<=utc()-stamp(probe['observed_at'])<timedelta(seconds=46)
if (fresh and probe.get('clock_qualified') and probe.get('helper_ready') and probe.get('authenticated')
and probe.get('agent_id')==data['agent_id'] and probe.get('agent_pid')==probe.get('current_agent_pid')
and probe.get('policy_sha256')==data['helper_policy_sha256'] and probe.get('helper_sha256')==data['helper_binary_sha256']):
break
require(utc()<stamp(data['expires_at']), 'Window expired while waiting for the native signed helper clock')
time.sleep(5)
emit(remote_server('enable',data))
while utc()<stamp(data['expires_at']):
time.sleep(min(4,max(0,(stamp(data['expires_at'])-utc()).total_seconds())))
except KeyboardInterrupt:
pass
finally:
close(data)
def retire_native(action,data,witness):
args={'CaseId':data['case_id'],'AgentId':data['agent_id'],'AssetId':data['asset_id'],
'ReleaseId':data['release_id'],'Witness':witness,'Action':action}
return windows('Retire-DevelopmentWindowsUpdate.ps1',args)
def close(data):
from windows_update_detach import remote_server as detach_server
started=utc()
emit(remote_server('revoke',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:
require(observed.get('case_id')==data['case_id'],'Local helper belongs to another maintenance window')
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)
if witness and witness.get('helper_id'):
emit(retire_native('Verify',data,witness))
emit(detach_server('detach',{**data,'witness':witness}))
emit(retire_native('Apply',data,witness))
latest=windows('Inspect-DevelopmentWindowsUpdate.ps1',{})
if latest['release_id']==data['release_id']:
resumed=guest('Resume',data)
require(resumed['actor_running'] and resumed['release_pull_running'],'Normal services did not resume')
else:
require(latest.get('agent_id')==data['agent_id'] and latest.get('helper') is None
and latest['release_pull']['state']=='Running' and latest['agent']['state']=='Running',
'Post-close release selection needs review')
ended=wait_for(data,lambda row:usable(row) and stamp(row['install']['contact'])>=started
and not {'helper.system.v1','system:run_script'} & set(row['install']['capabilities']),
180,'Still awaiting a new synchronized non-SYSTEM heartbeat after closing')
emit({'closed_case':data['case_id'],'normal_agent_contact':ended['install']['contact']})
if __name__=='__main__':
try:
if len(sys.argv)==4 and sys.argv[1]=='--on-server':
emit(server(sys.argv[2],json.loads(base64.b64decode(sys.argv[3],validate=True))))
else:
local()
except subprocess.SubprocessError as exc:
sys.exit('Remote command failed: '+type(exc).__name__+' (see target diagnostic above)')
except (ValueError,OSError,KeyError) as exc:
sys.exit(str(exc))