385 lines
26 KiB
Python
385 lines
26 KiB
Python
#!/usr/bin/env python3
|
|
"""Owned, explicitly opted-in VM lifecycle. Nothing runs at import/default smoke.
|
|
|
|
No package inventory, baseline resolver, private key export, policy expansion,
|
|
journal edits or reboot syscall. A reboot is solely a separately approved job.
|
|
"""
|
|
from contextlib import ExitStack
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import uuid
|
|
|
|
import vm_mvp_acceptance as m
|
|
sys.path.insert(0,str(m.ROOT/'deploy'))
|
|
import vm_job_bootstrap as bootstrap
|
|
|
|
|
|
class OwnedRuntime:
|
|
def __init__(self,settings,backend,verdict):
|
|
for field in ('agent_sha256','helper_sha256'):
|
|
m.require(isinstance(verdict.get(field),str) and __import__('re').fullmatch('[a-f0-9]{64}',verdict[field]),'not_configured:current_wire_build_hashes_required')
|
|
m.require(verdict.get('agent_id')==settings['agent_id'],'wire_verdict_fixed_identity_required')
|
|
self.settings,self.backend,self.verdict=settings,backend,verdict
|
|
self.stack=ExitStack();self.tunnel=None;self.started=False;self.owned_pid=None
|
|
self.directory=m.STATE/('mvp-owned-'+uuid.uuid4().hex)
|
|
self.directory.mkdir(mode=0o700)
|
|
self.transport_file='/etc/systemd/system/rmm-agent.service.d/90-mvp-owned-transport.conf'
|
|
self.original_enabled=None
|
|
self.helpers_started=[]
|
|
|
|
def remote(self,body):
|
|
code=('import os,json,hashlib,subprocess,sqlite3,socket,struct,ssl,http.client,time,uuid\nfrom pathlib import Path\n'
|
|
'from contextlib import closing\n'+body)
|
|
return json.loads(bootstrap.remote_python(self.ssh,self.target,self.env,code))
|
|
|
|
def server_state(self):
|
|
agent=str(uuid.UUID(self.settings['agent_id']))
|
|
value=self.backend.sql(f"SELECT json_build_object('agent_id',agent_id,'generation',enrollment_generation,'assignment',assignment_version,'connection',connection_generation,'boot_id',connection_boot_id,'transport',transport,'fingerprint',certificate_sha256,'sync_required',sync_required,'sync_ready',sync_ready,'helper_ready',helper_sync_ready,'restore_blocked',restore_blocked,'recovery',to_jsonb(a)->'sync_recovery_required') FROM agent_installations a WHERE agent_id='{agent}';")
|
|
m.require(value,'not_configured:server_identity_missing')
|
|
return json.loads(value)
|
|
|
|
def inspect(self):
|
|
return self.remote('''account=__import__('pwd').getpwnam('rmm-agent')
|
|
identity=json.loads(Path('/var/lib/rmm-agent/identity.json').read_text())
|
|
pid=int(subprocess.run(['systemctl','show','--property=MainPID','--value','rmm-agent.service'],capture_output=True,check=True).stdout)
|
|
ports=subprocess.run(['ss','-H','-ltn','sport = :18443'],capture_output=True,text=True,check=True).stdout
|
|
result=dict(agent_id=identity['agent_id'],asset_id=identity['asset_id'],uid=account.pw_uid,pid=pid,
|
|
boot_id=Path('/proc/sys/kernel/random/boot_id').read_text().strip(),listener=bool(ports.strip()),
|
|
agent_sha256=hashlib.sha256(Path('/usr/local/libexec/rmm-agent').read_bytes()).hexdigest(),
|
|
helper_sha256=hashlib.sha256(Path('/usr/local/libexec/rmm-privileged-helper').read_bytes()).hexdigest(),
|
|
state_uid=Path('/var/lib/rmm-agent').stat().st_uid)
|
|
if pid:
|
|
result['process_uid']=Path('/proc/'+str(pid)).stat().st_uid;result['exe']=os.readlink('/proc/'+str(pid)+'/exe')
|
|
status=dict(line.split(':',1) for line in Path('/proc/'+str(pid)+'/status').read_text().splitlines() if ':' in line)
|
|
result['cap_eff']=status['CapEff'].strip();result['cap_amb']=status['CapAmb'].strip();result['nnp']=status['NoNewPrivs'].strip()
|
|
print(json.dumps(result))
|
|
''')
|
|
|
|
def verify_installation(self,facts):
|
|
m.require(facts['agent_id']==self.settings['agent_id'] and facts['asset_id']==self.settings['asset_id']
|
|
and facts['uid']==self.settings['agent_uid']==facts['state_uid'] and facts['uid']>0
|
|
and facts['agent_sha256']==self.verdict['agent_sha256'] and facts['helper_sha256']==self.verdict['helper_sha256'],
|
|
'not_configured:actual_nonroot_identity_or_wire_artifact_mismatch')
|
|
|
|
def acquire(self,transport):
|
|
self.backend.outstanding_guard(self.settings)
|
|
self.ssh,self.target,self.env=self.stack.enter_context(bootstrap.connection())
|
|
facts=self.inspect();self.verify_installation(facts)
|
|
old_connection=self.server_state()['connection']
|
|
m.require(facts['pid']==0 and not facts['listener'],'not_configured:idle_lifecycle_transfer_required_no_foreign_process_stopped')
|
|
self.transport=transport
|
|
content='[Service]\nEnvironment=RMM_TRANSPORT='+transport+'\n'
|
|
self.remote('''path=Path(%r)
|
|
assert not path.exists() and not path.is_symlink()
|
|
parent=path.parent
|
|
assert parent.is_dir() and not parent.is_symlink() and parent.stat().st_uid==0 and parent.stat().st_mode&0o022==0
|
|
fd=os.open(path,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o644)
|
|
with os.fdopen(fd,'w') as stream:stream.write(%r)
|
|
subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True)
|
|
print('{}')
|
|
'''%(self.transport_file,content))
|
|
self.start_helpers();self.start_tunnel();self.start_agent();state=self.wait_sync(old_connection)
|
|
m.require(state['transport']==transport,'not_configured:requested_native_transport_not_wired')
|
|
m.write_private(self.directory/'acquired.json',dict(agent_id=self.settings['agent_id'],agent_pid=self.owned_pid,
|
|
tunnel_pid=self.tunnel.pid,transport=transport,server_state=state))
|
|
|
|
def start_tunnel(self):
|
|
facts=self.inspect();m.require(not facts['listener'],'not_configured:foreign_reverse_listener')
|
|
control=str(self.directory/'ssh-control')
|
|
m.require(not Path(control).exists(),'owned_control_socket_requires_review')
|
|
self.log=self.stack.enter_context((self.directory/'tunnel.log').open('ab'))
|
|
self.tunnel=subprocess.Popen(self.ssh+['-M','-S',control,'-N','-T','-o','ExitOnForwardFailure=yes',
|
|
'-o','ServerAliveInterval=30','-o','ServerAliveCountMax=3','-R','127.0.0.1:18443:127.0.0.1:18443',self.target],
|
|
env=self.env,stdin=subprocess.DEVNULL,stdout=self.log,stderr=self.log,start_new_session=True)
|
|
end=time.monotonic()+15
|
|
while not Path(control).exists():
|
|
m.require(self.tunnel.poll() is None and time.monotonic()<end,'owned_reverse_tunnel_failed')
|
|
time.sleep(.1)
|
|
check=self.remote("lines=subprocess.run(['ss','-H','-ltn','sport = :18443'],capture_output=True,text=True,check=True).stdout.splitlines();print(json.dumps([line.split()[3] for line in lines]))")
|
|
m.require(check==['127.0.0.1:18443'],'reverse_listener_must_be_only_ipv4_loopback')
|
|
|
|
def start_helpers(self):
|
|
units=self.verdict.get('helper_units',{})
|
|
m.require(isinstance(units,dict) and set(units)<= {'rmm-helper-dev-test.service','rmm-apt-helper.service'},'only_owned_installed_helper_units_may_be_bootstrapped')
|
|
for unit,expected_hash in units.items():
|
|
state=self.remote("p=Path('/etc/systemd/system')/%r;assert p.is_file() and not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o022==0;assert hashlib.sha256(p.read_bytes()).hexdigest()==%r;r=subprocess.run(['systemctl','is-active',%r],capture_output=True,text=True);print(json.dumps(r.stdout.strip()=='active'))"%(unit,expected_hash,unit))
|
|
if state:continue # Existing worker-owned daemon is not ours to stop.
|
|
self.helpers_started.append(unit)
|
|
self.remote("subprocess.run(['systemctl','start',%r],capture_output=True,check=True);print('{}')"%unit)
|
|
|
|
def start_agent(self):
|
|
facts=self.inspect();self.verify_installation(facts)
|
|
m.require(facts['pid']==0,'no_foreign_agent_start_or_restart')
|
|
self.remote("subprocess.run(['systemctl','start','rmm-agent.service'],capture_output=True,check=True);print('{}')")
|
|
for _ in range(50):
|
|
facts=self.inspect()
|
|
if facts['pid'] and facts.get('exe')=='/usr/local/libexec/rmm-agent':break
|
|
time.sleep(.1)
|
|
self.verify_installation(facts)
|
|
if facts['pid'] and facts.get('exe')=='/usr/local/libexec/rmm-agent':
|
|
self.started=True;self.owned_pid=facts['pid']
|
|
m.require(facts.get('process_uid')==self.settings['agent_uid'],'actual_network_agent_is_not_expected_nonroot_uid')
|
|
m.require(int(facts['cap_eff'],16)==0 and int(facts['cap_amb'],16)==0 and facts['nnp']=='1','network_agent_must_have_no_effective_or_ambient_capabilities')
|
|
|
|
def wait_sync(self,minimum_generation=None,timeout=100):
|
|
end=time.monotonic()+timeout
|
|
while True:
|
|
state=self.server_state()
|
|
if (state['sync_required'] and state['sync_ready'] and not state['restore_blocked']
|
|
and state.get('recovery') is False and (minimum_generation is None or state['connection']>minimum_generation)):
|
|
device=self.backend.call('inventory.get_device',dict(tenant_id=self.settings['tenant_id'],asset_id=self.settings['asset_id']))
|
|
if device['presence']=='online' and 0 <= (m.now()-m.timestamp(device['last_contact_at'])).total_seconds()<90:return state
|
|
m.require(time.monotonic()<end,'not_configured:actual_signed_sync_gate_or_fresh_heartbeat_blocked')
|
|
time.sleep(2)
|
|
|
|
def pause_idle(self):
|
|
self.backend.outstanding_guard(self.settings)
|
|
facts=self.inspect();self.verify_installation(facts)
|
|
m.require(self.started and facts['pid']==self.owned_pid,'only_owned_agent_may_be_stopped')
|
|
self.remote("subprocess.run(['systemctl','stop','rmm-agent.service'],capture_output=True,check=True);print('{}')")
|
|
self.started=False
|
|
|
|
def resume_after_offline(self,job_id):
|
|
# Resume ONLY this fixed approved queued/expired/cancelled attempt. No
|
|
# reset, regrant, changed deadline or fresh execution attempt is created.
|
|
agent=str(uuid.UUID(self.settings['agent_id']));job=str(uuid.UUID(job_id))
|
|
other=self.backend.sql(f"SELECT count(*) FROM jobs WHERE agent_id='{agent}' AND job_id<>'{job}' AND state NOT IN ('succeeded','failed','expired','cancelled');")
|
|
m.require(other=='0' and self.backend.counts(self.settings['tenant_id'],job)[2]==0,'offline_resume_ambiguous_grant_or_other_active_job')
|
|
self.start_agent();self.wait_sync()
|
|
|
|
def require_action(self,request):
|
|
state=self.wait_sync()
|
|
device=self.backend.call('inventory.get_device',dict(tenant_id=self.settings['tenant_id'],asset_id=self.settings['asset_id']))
|
|
required={request['action'],'rmm.sync.v1'}
|
|
if request['parameters']['execution_identity']=='root':
|
|
required|={'helper.root.v1','root:'+request['action']}
|
|
m.require(state['helper_ready'],'not_configured:independent_helper_sync_not_applied')
|
|
policy=self.control_probe('general')
|
|
m.require('helper.policy.sha256:'+policy['policy_sha256'] in device['capabilities'],'not_configured:actual_root_peer_policy_and_negotiated_digest_differ')
|
|
rules=policy['policy']
|
|
if request['action']=='reboot':m.require(rules['allow_reboot'],'not_configured:root_allow_reboot_false')
|
|
if request['action']=='run_script':
|
|
m.require(rules['allow_root_scripts'],'not_configured:narrow_test_root_script_policy_or_ReadWritePaths_missing')
|
|
if request['parameters'].get('variants',[{}])[0].get('script_utf8')==m.SERVICE_SETUP:
|
|
paths=self.remote("p=subprocess.run(['systemctl','show','--property=ReadWritePaths','--value','rmm-helper-dev-test.service'],capture_output=True,text=True,check=True);print(json.dumps(p.stdout.split()))")
|
|
m.require('/etc/systemd/system/rmm-mvp-test.service' in paths,
|
|
'not_configured:fixed_test_service_inode_ReadWritePaths_missing_no_broad_etc_write')
|
|
if request['action']=='set_service_state':m.require(request['parameters']['unit'] in rules['allowed_services'],'not_configured:fixed_test_service_not_in_helper_policy')
|
|
m.require(required<=set(device['capabilities']),'not_configured:actual_negotiated_action_capabilities_missing')
|
|
|
|
def control_probe(self,profile):
|
|
apt=profile=='apt_only'
|
|
pin=self.verdict.get('apt_pin_file' if apt else 'general_pin_file')
|
|
m.require(isinstance(pin,str) and pin.startswith('/etc/rmm/') and '..' not in Path(pin).parts,'not_configured:independently_root_provisioned_public_pin_file_required')
|
|
socket_path='/run/rmm-apt-helper/helper.sock' if apt else '/run/rmm-helper/control.sock'
|
|
# Execute as the real unprivileged peer. Only public metadata and nonce-
|
|
# bound read-only readiness/policy replies leave the VM, never a key.
|
|
script='''import json,os,socket,struct,uuid
|
|
from pathlib import Path
|
|
pin_path=Path(%r);sock_path=Path(%r)
|
|
for path in list(pin_path.parents)+[pin_path]+list(sock_path.parents):
|
|
meta=path.lstat();assert not path.is_symlink() and meta.st_uid==0 and meta.st_mode&0o022==0
|
|
meta=sock_path.lstat();assert meta.st_uid==0 and meta.st_mode&0o007==0
|
|
pin=json.loads(pin_path.read_text());challenge=str(uuid.uuid4())
|
|
apt=%r
|
|
request=dict(operation='apt_readiness_v1' if apt else 'general_policy_v1',challenge=challenge,identity=pin['identity'])
|
|
if apt:request['expected_stack_sha256']=pin['provider_stack_sha256']
|
|
s=socket.socket(socket.AF_UNIX);s.settimeout(10);s.connect(str(sock_path))
|
|
assert struct.unpack('3i',s.getsockopt(socket.SOL_SOCKET,socket.SO_PEERCRED,12))[1]==0
|
|
raw=json.dumps(request,separators=(',',':')).encode();s.sendall(struct.pack('!I',len(raw))+raw)
|
|
def receive(n):
|
|
b=b''
|
|
while len(b)<n:
|
|
c=s.recv(n-len(b));assert c;b+=c
|
|
return b
|
|
n=struct.unpack('!I',receive(4))[0];assert n<=1024*1024
|
|
response=json.loads(receive(n));s.close()
|
|
assert response['schema_version']==1 and response['challenge']==challenge and response['identity']==pin['identity'] and response['profile']==pin['profile']
|
|
field='provider_stack_sha256' if apt else 'policy_sha256';assert response[field]==pin[field]
|
|
assert response['ledger_inode'] and response['lock_inode']
|
|
print(json.dumps(response))
|
|
'''%(pin,socket_path,apt)
|
|
result=self.remote("p=subprocess.run(['/usr/sbin/runuser','-u','rmm-agent','--','/usr/bin/python3','-I','-c',%r],capture_output=True,text=True,check=True);print(p.stdout)"%script)
|
|
m.require(all(result['identity'][key]==self.settings[key] for key in ('tenant_id','asset_id','agent_id')),'root_peer_identity_mismatch')
|
|
server=self.server_state()
|
|
status,_,key=self.backend.request('/api/v1/job-signing-key',token=self.backend.token)
|
|
m.require(status==200 and result['identity']['signing_key_id']==key['key_id']
|
|
and result['identity']['enrollment_generation']==server['generation']
|
|
and result['identity']['assignment_version']==server['assignment'],'root_peer_current_authenticated_key_or_generation_mismatch')
|
|
if not apt:
|
|
policy=result['policy']
|
|
raw=json.dumps(policy,separators=(',',':')).encode()
|
|
m.require(hashlib.sha256(raw).hexdigest()==result['policy_sha256'],'independent_general_policy_bytes_hash_mismatch')
|
|
return result
|
|
|
|
def apt_probe(self):
|
|
general=self.control_probe('general')
|
|
apt=self.control_probe('apt_only')
|
|
m.require(all(apt[key]==general[key] for key in ('ledger_device','ledger_inode','lock_device','lock_inode')),
|
|
'not_configured:APT_and_general_helper_must_share_exact_ledger_and_lock_inodes')
|
|
return apt
|
|
|
|
def apt_metadata(self):
|
|
# Administrative installed-code/config pins only. No package/status/
|
|
# baseline read, resolver call, native probe or acceptance marker write.
|
|
return self.remote("p=Path('/etc/rmm/apt-helper.json');assert not p.is_symlink() and p.stat().st_uid==0 and p.stat().st_mode&0o777==0o600;c=json.loads(p.read_text())['apt_provider'];print(json.dumps(dict(runtime_sha256=c['runtime_sha256'],apt_config_sha256=c['apt_config_sha256'],provider_sha256=hashlib.sha256(Path('/usr/lib/rmm-apt-transaction/apt_transaction.py').read_bytes()).hexdigest(),hook_sha256=hashlib.sha256(Path('/usr/lib/rmm-apt-transaction/hook').read_bytes()).hexdigest())))")
|
|
|
|
def prepare_reboot(self):
|
|
m.require(self.tunnel is not None and self.tunnel.poll() is None and self.started,'reboot_requires_owned_agent_and_tunnel')
|
|
self.original_enabled=self.remote("p=subprocess.run(['systemctl','is-enabled','rmm-agent.service'],capture_output=True,text=True);print(json.dumps(p.stdout.strip()=='enabled'))")
|
|
if not self.original_enabled:self.remote("subprocess.run(['systemctl','enable','rmm-agent.service'],capture_output=True,check=True);print('{}')")
|
|
|
|
def recover_reboot(self,old_boot):
|
|
end=time.monotonic()+180;facts=None
|
|
while time.monotonic()<end:
|
|
try:
|
|
candidate=self.inspect()
|
|
if candidate['boot_id']!=old_boot:facts=candidate;break
|
|
except (RuntimeError,OSError,subprocess.TimeoutExpired):pass
|
|
time.sleep(2)
|
|
m.require(facts is not None,'unknown:reboot_no_authenticated_new_boot_no_retry')
|
|
self.verify_installation(facts)
|
|
if self.tunnel.poll() is None:self.tunnel.terminate();self.tunnel.wait(timeout=15)
|
|
# The old VM listener disappeared with its boot. Never unlink a foreign
|
|
# socket/listener or stop another worker's tunnel to force recovery.
|
|
self.start_tunnel()
|
|
m.require(facts['pid']>0 and facts.get('process_uid')==self.settings['agent_uid'],'unknown:agent_did_not_autostart_after_real_reboot')
|
|
self.owned_pid=facts['pid'];self.started=True
|
|
state=self.wait_sync(timeout=100)
|
|
m.require(state['boot_id']==facts['boot_id'] and state['helper_ready'],'unknown:new_boot_helper_sync_not_confirmed')
|
|
|
|
def restart_server(self):
|
|
self.backend.outstanding_guard(self.settings);before=self.server_state()
|
|
command=['docker','compose','--env-file',str(m.STATE/'.env'),'-f',str(m.ROOT/'deploy/compose.yaml'),'-p','rust-discovery-dev','restart','server']
|
|
subprocess.run(command,capture_output=True,check=True,timeout=60)
|
|
# Same deployed env/profile/volumes: no OIDC/config reset or DB mutation.
|
|
end=time.monotonic()+60
|
|
while True:
|
|
try:self.backend.__init__();break
|
|
except (OSError,m.GuardError):
|
|
m.require(time.monotonic()<end,'server_restart_verified_TLS_MCP_recovery_timeout')
|
|
time.sleep(2)
|
|
self.wait_sync(before['connection'])
|
|
|
|
def native_renewal(self):
|
|
arguments=dict(tenant_id=self.settings['tenant_id'],asset_id=self.settings['asset_id'])
|
|
before_api=self.backend.call('inventory.get_device',arguments)
|
|
m.require(isinstance(before_api.get('certificate_sha256'),str),'not_configured:device_API_certificate_sha256_not_exposed')
|
|
before=self.server_state();end=time.monotonic()+90
|
|
m.require(before_api['certificate_sha256']==before['fingerprint'],'certificate_API_and_server_ledger_disagree')
|
|
while time.monotonic()<end:
|
|
after=self.server_state()
|
|
if after['fingerprint']!=before['fingerprint']:
|
|
m.require(after['agent_id']==before['agent_id'] and after['generation']==before['generation'],'native_rotation_changed_device_identity')
|
|
fingerprint=self.remote('''root=Path('/var/lib/rmm-agent');agent=%r
|
|
selectors=[]
|
|
for path in root.rglob('current.json'):
|
|
value=json.loads(path.read_text())
|
|
if value.get('schema_version')==1 and value.get('agent_id')==agent and 'version' in value:selectors.append(path.parent/'versions'/str(uuid.UUID(value['version'])))
|
|
assert len(selectors)<=1
|
|
certificate=(selectors[0] if selectors else root)/'device.pem'
|
|
der=subprocess.run(['openssl','x509','-in',str(certificate),'-outform','DER'],capture_output=True,check=True).stdout
|
|
print(json.dumps(hashlib.sha256(der).hexdigest()))
|
|
'''%self.settings['agent_id'])
|
|
m.require(fingerprint==after['fingerprint'],'native_rotated_certificate_and_server_fingerprint_mismatch')
|
|
after_api=self.backend.call('inventory.get_device',arguments)
|
|
m.require(after_api['agent_id']==before_api['agent_id'] and after_api['enrollment_generation']==before_api['enrollment_generation']
|
|
and after_api.get('certificate_sha256')==fingerprint,'native_rotation_API_identity_generation_fingerprint_binding')
|
|
self.wait_sync();return
|
|
time.sleep(2)
|
|
raise m.GuardError('not_configured:native_renewal_not_due_or_no_exposed_native_trigger_no_private_key_export')
|
|
|
|
def replay_original_result(self,checkpoint):
|
|
tenant=self.settings['tenant_id'];job=str(uuid.UUID(checkpoint['job_id']));attempt=str(uuid.UUID(checkpoint['attempt_id']))
|
|
before=self.backend.call('jobs.get',dict(tenant_id=tenant,job_id=job))
|
|
m.require(before['state']=='succeeded' and self.backend.counts(tenant,job)==[1,1,1], 'completed_original_attempt_required_for_replay')
|
|
fingerprint=self.server_state()['fingerprint']
|
|
script='''job=%r;attempt=%r;agent=%r;expected_fingerprint=%r
|
|
root=Path('/var/lib/rmm-agent')
|
|
with closing(sqlite3.connect((root/'journal.sqlite').as_uri()+'?mode=ro',uri=True)) as db:
|
|
row=db.execute('SELECT envelope FROM reconcile_results WHERE job=? AND attempt=? ORDER BY revision DESC LIMIT 1',(job,attempt)).fetchone()
|
|
assert row is not None, 'ORIGINAL_RESULT_NOT_ARCHIVED'
|
|
original=bytes(row[0]);assert 0<len(original)<=1048576
|
|
def varint(number):
|
|
out=bytearray()
|
|
while number>127:out.append((number&127)|128);number>>=7
|
|
out.append(number);return bytes(out)
|
|
def fields(data):
|
|
index=0;result={}
|
|
def number():
|
|
nonlocal index
|
|
value=0
|
|
for shift in range(0,70,7):
|
|
b=data[index];index+=1;value|=(b&127)<<shift
|
|
if b<128:return value
|
|
raise ValueError('WIRE')
|
|
while index<len(data):
|
|
tag=number();kind=tag&7;tag>>=3
|
|
if kind==0:value=number()
|
|
elif kind==2:
|
|
size=number();value=data[index:index+size];index+=size;assert len(value)==size
|
|
else:raise ValueError('WIRE')
|
|
result.setdefault(tag,[]).append(value)
|
|
return result
|
|
outer=fields(original);result=fields(outer[25][0])
|
|
assert outer[4]==[agent.encode()] and result[1]==[job.encode()] and result[2]==[attempt.encode()]
|
|
assert result[4]==[b'succeeded'];message=outer[3][0].decode();uuid.UUID(message)
|
|
selectors=[]
|
|
for path in root.rglob('current.json'):
|
|
value=json.loads(path.read_text())
|
|
if value.get('schema_version')==1 and value.get('agent_id')==agent and 'version' in value:
|
|
version=str(uuid.UUID(value['version']));selectors.append(path.parent/'versions'/version)
|
|
assert len(selectors)<=1
|
|
identity=selectors[0] if selectors else root
|
|
certificate=identity/'device.pem';key=identity/'device.key'
|
|
der=subprocess.run(['openssl','x509','-in',str(certificate),'-outform','DER'],capture_output=True,check=True).stdout
|
|
assert hashlib.sha256(der).hexdigest()==expected_fingerprint
|
|
context=ssl.create_default_context(cafile='/etc/rmm/ca.pem');context.load_cert_chain(certificate,key)
|
|
class Connection(http.client.HTTPSConnection):
|
|
def connect(self):
|
|
raw=socket.create_connection(('127.0.0.1',18443),timeout=10)
|
|
self.sock=self._context.wrap_socket(raw,server_hostname='rmm-dev.internal')
|
|
batch=b'\\x08\\x01'+varint((2<<3)|2)+varint(len(original))+original
|
|
def consumed():
|
|
with closing(sqlite3.connect((root/'consumption.sqlite').as_uri()+'?mode=ro',uri=True)) as db:
|
|
return sum(job in row and attempt in row for row in db.execute('SELECT * FROM consumed'))
|
|
before=consumed();assert before==1
|
|
statuses=[]
|
|
for _ in range(2):
|
|
c=Connection('rmm-dev.internal',18443,context=context,timeout=10)
|
|
c.request('POST','/agent/v1/messages',body=batch,headers={'Content-Type':'application/x-protobuf'})
|
|
response=c.getresponse();statuses.append(response.status);response.read(1048577);c.close()
|
|
assert statuses==[200,200] and consumed()==before
|
|
print(json.dumps(dict(message_id=message,envelope_sha256=hashlib.sha256(original).hexdigest(),
|
|
statuses=statuses,local_attempt_consumptions=before,original_metadata_preserved=True)))
|
|
'''%(job,attempt,self.settings['agent_id'],fingerprint)
|
|
proof=self.remote(script)
|
|
after=self.backend.call('jobs.get',dict(tenant_id=tenant,job_id=job))
|
|
m.require(after==before and self.backend.counts(tenant,job)==[1,1,1],'replay_changed_result_attempt_or_grant')
|
|
message=str(uuid.UUID(proof['message_id']))
|
|
receipts=self.backend.sql(f"SELECT count(*) FROM job_receipts WHERE tenant_id='{tenant}' AND agent_id='{self.settings['agent_id']}' AND message_id='{message}';")
|
|
m.require(receipts=='1','original_result_receipt_not_deduplicated')
|
|
return dict(regression_verified=True,job_id=job,attempt_id=attempt,proof=proof,server_result_unchanged=True,server_receipts=1)
|
|
|
|
def release_if_idle(self):
|
|
try:self.backend.outstanding_guard(self.settings)
|
|
except Exception:return # Preserve process, tunnel, protected logs and journals.
|
|
try:
|
|
if self.started:self.pause_idle()
|
|
if self.original_enabled is False:self.remote("subprocess.run(['systemctl','disable','rmm-agent.service'],capture_output=True,check=True);print('{}')")
|
|
if self.tunnel and self.tunnel.poll() is None:self.tunnel.terminate();self.tunnel.wait(timeout=15)
|
|
for unit in reversed(self.helpers_started):
|
|
self.remote("with closing(sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True)) as db:\n assert db.execute('SELECT count(*) FROM consumed WHERE result IS NULL').fetchone()[0]==0\nsubprocess.run(['systemctl','stop',%r],capture_output=True,check=True);print('{}')"%unit)
|
|
if hasattr(self,'transport'):
|
|
content='[Service]\nEnvironment=RMM_TRANSPORT='+self.transport+'\n'
|
|
self.remote("p=Path(%r);assert p.is_file() and not p.is_symlink() and p.read_text()==%r;p.unlink();subprocess.run(['systemctl','daemon-reload'],capture_output=True,check=True);print('{}')"%(self.transport_file,content))
|
|
self.stack.close()
|
|
except Exception:
|
|
# Unknown cleanup is not permission for broad kill/rm/reset.
|
|
raise m.GuardError('owned_lifecycle_cleanup_unconfirmed_preserve_private_state')
|