45 lines
2.6 KiB
Python
45 lines
2.6 KiB
Python
"""Read-only nonce-bound helper readiness as the actual unprivileged VM peer."""
|
|
|
|
|
|
def inspect_helpers(payload):
|
|
import sqlite3
|
|
program=r'''import json,os,socket,struct,uuid
|
|
from pathlib import Path
|
|
result={}
|
|
for kind,pin_path,socket_path in (
|
|
('general','/etc/rmm-remote/general-helper-proof.json','/run/rmm-helper-cloud/control.sock'),
|
|
('apt','/etc/rmm-remote/apt-helper-proof.json','/run/rmm-apt-helper/helper.sock')):
|
|
pin_path=Path(pin_path);socket_path=Path(socket_path)
|
|
for path in list(pin_path.parents)+[pin_path]+list(socket_path.parents):
|
|
meta=path.lstat();assert not path.is_symlink() and meta.st_uid==0 and meta.st_mode&0o022==0
|
|
meta=socket_path.lstat();assert meta.st_uid==0 and meta.st_mode&0o007==0
|
|
pin=json.loads(pin_path.read_text());challenge=str(uuid.uuid4())
|
|
request=dict(operation='apt_readiness_v1' if kind=='apt' else 'general_policy_v1',challenge=challenge,identity=pin['identity'])
|
|
field='provider_stack_sha256' if kind=='apt' else 'policy_sha256'
|
|
if kind=='apt':request['expected_stack_sha256']=pin[field]
|
|
s=socket.socket(socket.AF_UNIX);s.settimeout(10);s.connect(str(socket_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 0<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']
|
|
assert response[field]==pin[field] and response['ledger_inode'] and response['lock_inode']
|
|
result[kind]=response
|
|
assert all(result['general'][k]==result['apt'][k] for k in ('ledger_device','ledger_inode','lock_device','lock_inode'))
|
|
print(json.dumps(result))
|
|
'''
|
|
process=subprocess.run(['/usr/sbin/runuser','-u','rmm-agent','--','/usr/bin/python3','-I','-c',program],capture_output=True,check=True,timeout=25)
|
|
result=json.loads(process.stdout)
|
|
for peer in result.values():
|
|
require(all(peer['identity'][key]==payload[key] for key in ('tenant_id','asset_id','agent_id')),'helper target identity drift')
|
|
with sqlite3.connect('file:/var/lib/rmm-helper/consumption.sqlite?mode=ro',uri=True) as db:
|
|
result['ledger']=dict(integrity=db.execute('PRAGMA integrity_check').fetchone()[0],
|
|
consumed=db.execute('SELECT count(*) FROM consumed').fetchone()[0],
|
|
faults=db.execute('SELECT count(*) FROM helper_sync_meta WHERE fault IS NOT NULL').fetchone()[0])
|
|
return result
|