77 lines
4.1 KiB
Python
77 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Local authenticated synthetic fixture + isolated browser trust; never approve/run a job.
|
|
|
|
Private login/operator/enrollment values are consumed by existing helpers/browser code,
|
|
never printed, passed in argv or placed in browser artifacts. No VM access.
|
|
"""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import ssl
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
from backend_contract import request # noqa: E402
|
|
from local_integration import STATE, CA, A, sql, token, WebSocket, envelope, field # noqa: E402
|
|
|
|
|
|
def quiet_run(args, **kwargs):
|
|
result = subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, **kwargs)
|
|
if result.returncode:
|
|
raise RuntimeError('private subprocess failed')
|
|
|
|
|
|
def main():
|
|
cache = ROOT / 'web/node_modules/.cache'
|
|
home = cache / 'browser-home'
|
|
nss = home / '.pki/nssdb'
|
|
nss.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
home.chmod(0o700)
|
|
certutil = shutil.which('certutil') or str(cache / 'nss-tools/root/usr/bin/certutil')
|
|
if not (nss / 'cert9.db').exists():
|
|
quiet_run([certutil, '-N', '-d', 'sql:' + str(nss), '--empty-password'])
|
|
quiet_run([certutil, '-A', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-t', 'C,,', '-i', str(CA)])
|
|
quiet_run([certutil, '-V', '-d', 'sql:' + str(nss), '-n', 'RMM isolated development CA', '-u', 'L'])
|
|
baseline = sql('SELECT (SELECT count(*) FROM jobs)||\':\'||(SELECT count(*) FROM outbox)||\':\'||(SELECT count(*) FROM execution_permits);')
|
|
agent = None
|
|
stage = 'authenticated fixture enrollment'
|
|
try:
|
|
with tempfile.TemporaryDirectory(prefix='browser-fixture-', dir=STATE) as folder:
|
|
folder = Path(folder)
|
|
quiet_run(['openssl', 'req', '-new', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:prime256v1', '-nodes', '-keyout', str(folder/'device.key'), '-out', str(folder/'device.csr'), '-subj', '/CN=synthetic-local-browser-contract'])
|
|
status, _, enrolled = request('/agent/v1/enroll', 'POST', {'token': token(A), 'csr_pem': (folder/'device.csr').read_text()})
|
|
if status != 200: raise RuntimeError('fixture enrollment failed')
|
|
agent = enrolled['agent_id']
|
|
(folder/'device.pem').write_text(enrolled['certificate_pem'])
|
|
context = ssl.create_default_context(cafile=str(CA)); context.load_cert_chain(folder/'device.pem', folder/'device.key')
|
|
ws = WebSocket(context)
|
|
try:
|
|
ws.send(envelope(agent, 1, 0, 10, field(1, 'synthetic-local-browser-contract') + field(2, 'run_script')))
|
|
if ws.receive()[0] != 2: raise RuntimeError('fixture mTLS hello failed')
|
|
stage = 'browser TLS and pending-only UI checks'
|
|
env = {k: v for k, v in os.environ.items() if k not in ('DEBUG', 'PWDEBUG', 'NODE_DEBUG')}
|
|
env.update(RMM_E2E_PROFILE='dev/test', RMM_E2E_MODE='pending-only', RMM_E2E_LOGIN_FILE=str(STATE/'browser-login.json'), RMM_E2E_ASSET_ID=enrolled['asset_id'], PLAYWRIGHT_BROWSERS_PATH=str(cache/'playwright-browsers'))
|
|
result = subprocess.run(['npm', 'run', 'test:browser', '--', 'pending.spec.ts'], cwd=ROOT/'web', env=env)
|
|
if result.returncode: raise RuntimeError('browser checks failed')
|
|
stage = 'preapproval execution artifact guard'
|
|
after = sql('SELECT (SELECT count(*) FROM jobs)||\':\'||(SELECT count(*) FROM outbox)||\':\'||(SELECT count(*) FROM execution_permits);')
|
|
if baseline != after: raise RuntimeError('execution artifacts changed; review concurrent activity')
|
|
finally:
|
|
ws.close()
|
|
except Exception:
|
|
print('FAIL: ' + stage + '; private details suppressed.', file=sys.stderr)
|
|
return 1
|
|
finally:
|
|
if agent:
|
|
sql(f"UPDATE agent_installations SET revoked_at=now() WHERE agent_id='{agent}';")
|
|
print('PASS: isolated browser trust, authenticated local fixture, pending-only UI, no new jobs/outbox/grants. No VM or approval/execution evidence.')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|