"""Actual isolated review copy / validation / cleanup. Only disposable random fixture IDs.""" import json import os import pathlib import subprocess import time import urllib.error import urllib.request import uuid if os.environ.get('WSL_DISTRO_NAME') != 'WebUp-Local' or os.geteuid() != 0: raise SystemExit('Dedicated local environment required.') tokens = dict(line.strip().split('=', 1) for line in open('/etc/webup-local/gateway.env')) original, review = str(uuid.uuid4()), str(uuid.uuid4()) report = {'passed': False, 'checks': [], 'fixtureProject': original, 'reviewWorkspace': review} def api(path, payload=None, method=None): request = urllib.request.Request('http://127.0.0.1:8090' + path, headers={'Authorization': 'Bearer ' + tokens['SANDBOX_TOKEN'], 'Content-Type': 'application/json'}, data=None if payload is None else json.dumps(payload).encode(), method=method) with urllib.request.urlopen(request, timeout=150) as response: return json.load(response) def tool(identity, name, arguments, reviewing=False): return api(('/reviews/' if reviewing else '/workspaces/') + identity + '/tools', {'name': name, 'arguments': arguments}) def check(value, label): if not value: raise RuntimeError('FAIL ' + label) report['checks'].append(label) print('PASS ' + label, flush=True) try: check(tool(original, 'write_file', {'path': 'index.html', 'content': 'Review fixture'}).get('written'), 'real development fixture writes through stdin') tool(original, 'write_file', {'path': 'styles.css', 'content': 'body { color: navy; }'}) tool(original, 'write_file', {'path': '.env', 'content': 'PRIVATE_TEST_VALUE=must-not-copy'}) check(tool(original, 'write_file', {'path': 'large.txt', 'content': 'δΈ­' * 64000}).get('written') == 192000, 'large UTF-8 payload is not passed through process arguments') snapshot = tool(original, 'review_export', {}) check(not any(entry['path'] == '.env' for entry in snapshot['files']), 'snapshot excludes credential files') imported = tool(review, 'review_import', snapshot, True) check(imported.get('sourceHash') == snapshot['sourceHash'], 'isolated copy matches exported source fingerprint') checked = tool(review, 'review_validate', {}, True) check(checked.get('sourceHash') == snapshot['sourceHash'] and checked['steps'][0]['status'] == 'PASSED', 'HTML and stylesheet verified in actual review Pod') check(checked['browser']['status'] == 'NOT_RUN', 'unexecuted browser checks are explicitly reported') check(tool(original, 'review_export', {})['sourceHash'] == snapshot['sourceHash'], 'development source unchanged by review execution') pod_name = 'dev-' + uuid.UUID(review).hex pod = json.loads(subprocess.check_output(['k3s', 'kubectl', '-n', 'platform-reviews', 'get', 'pod', pod_name, '-o', 'json'], text=True)) check(pod['spec']['runtimeClassName'] == 'gvisor' and not pod['spec']['automountServiceAccountToken'], 'separate review namespace uses gVisor without service account token') container = pod['status']['containerStatuses'][0]['containerID'].split('://', 1)[1] runtime = json.loads(subprocess.check_output(['k3s', 'ctr', '-n', 'k8s.io', 'containers', 'info', container], text=True)) check(runtime.get('Runtime', runtime.get('runtime', {})).get('Name') == 'io.containerd.runsc.v1', 'review container actually runs through runsc') try: tool(review, 'run_command', {'command': 'echo not allowed'}, True) raise RuntimeError('Review API accepted arbitrary command') except urllib.error.HTTPError as error: check(error.code == 400, 'private review API rejects arbitrary shell tools') api('/reviews/' + review, method='DELETE') review = str(uuid.uuid4()) package = {'name':'webup-review-fixture','version':'1.0.0','private':True, 'scripts':{'build':'node build.cjs','test':'node --test'}} tool(original,'write_file',{'path':'package.json','content':json.dumps(package)}) tool(original,'write_file',{'path':'build.cjs','content':"const fs=require('node:fs');fs.mkdirSync('dist',{recursive:true});fs.copyFileSync('index.html','dist/index.html');console.log('REAL_BUILD_OK');"}) tool(original,'write_file',{'path':'site.test.cjs','content':"const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs');test('actual HTML artifact exists',()=>{assert.ok(fs.readFileSync('dist/index.html','utf8').includes('Review fixture'));});"}) node_snapshot=tool(original,'review_export',{}) tool(review,'review_import',node_snapshot,True) node_checked=tool(review,'review_validate',{},True) statuses={step['name']:step['status'] for step in node_checked['steps']} check(statuses.get('dependencies')=='PASSED' and statuses.get('build')=='PASSED' and statuses.get('test')=='PASSED','actual npm installation, build and Node test execute successfully inside copied review Pod') check(tool(original,'review_export',{})['sourceHash']==node_snapshot['sourceHash'],'npm artifacts and dependency files do not modify development workspace') api('/reviews/'+review,method='DELETE') review=str(uuid.uuid4()) tool(original,'write_file',{'path':'build.cjs','content':'process.exit(7);'}) failed_snapshot=tool(original,'review_export',{}) tool(review,'review_import',failed_snapshot,True) failed=tool(review,'review_validate',{},True) check(any(step['name']=='build' and step['status']=='FAILED' and step['exitCode']!=0 for step in failed['steps']),'real failing build is recorded as failure') report['passed'] = True finally: try: api('/reviews/' + review, method='DELETE') except Exception: report['cleanupError'] = True # These random IDs were generated above, never read from user data. own_name = 'dev-' + uuid.UUID(original).hex subprocess.run(['k3s', 'kubectl', '-n', 'platform-sandboxes', 'delete', 'pod,pvc,networkpolicy', own_name, '--ignore-not-found', '--wait=false'], stdout=subprocess.DEVNULL, timeout=20) report['checkedAtUnix'] = time.time() target = pathlib.Path(__file__).resolve().parents[2] / '.runtime/local-cluster/code-review-isolation.json' target.write_text(json.dumps(report, indent=2))