Separate SYSTEM policy and verify thirteen policy guards on cloud Linux
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Exact pure server policy units: local release build, then parent-run Linux VM.
|
||||
|
||||
No server daemon, DB, networking, approval/job, package or device operation.
|
||||
Native execution requires an explicit flag. Lost replies permit readback only.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
import remote_client_bootstrap as linux
|
||||
import vm_mvp_acceptance as contract
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
STATE=ROOT/'devstate/server-windows-policy'
|
||||
IMAGE='rust-discovery-durable-final-build:local'
|
||||
REMOTE='/var/lib/rmm-server-policy-acceptance'
|
||||
TESTS=(
|
||||
'windows_policy::tests::system_permission_and_helper_requirement_are_distinct_from_root',
|
||||
'windows_policy::tests::exact_system_scope_never_inherits_root_or_other_device_authority',
|
||||
'windows_policy::tests::narrow_scm_ceiling_denies_unreviewed_effects_and_provider_inputs',
|
||||
'windows_policy::tests::policy_file_refuses_symlinks_public_mode_wrong_owner_and_writable_parent',
|
||||
'root_policy::tests::no_capability_or_relabel_shortcut',
|
||||
'root_policy::root_log_tests::exact_root_log_plan_shape_and_selected_scope',
|
||||
'root_policy::root_log_tests::default_empty_and_service_mutation_list_never_authorize_logs',
|
||||
'root_policy::root_log_tests::scoped_allowlist_and_global_ceiling_are_exact',
|
||||
'root_policy::root_log_tests::exact_log_bounds_not_rounded_hours',
|
||||
'root_policy::root_log_tests::env_lists_are_bounded_data_not_paths_wildcards_or_bools',
|
||||
'root_policy::root_log_tests::classification_cannot_be_bypassed_by_another_scope_or_empty_env',
|
||||
'root_policy::root_log_tests::permissions_are_separate_root_read_and_sensitive_requirements',
|
||||
'root_policy::root_log_tests::unknown_log_profile_version_denies',
|
||||
)
|
||||
require=contract.require
|
||||
|
||||
def sha(path):return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
def store(folder,name,value):
|
||||
fd=os.open(folder/name,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as out:json.dump(value,out,indent=2);out.write('\n');out.flush();os.fsync(out.fileno())
|
||||
def sources():
|
||||
paths=[ROOT/p for p in ('Cargo.toml','Cargo.lock','rust-toolchain.toml','tests/backend_root_logs.rs')]
|
||||
for crate in ('server','domain','protocol','persistence','oidc-auth'):
|
||||
base=ROOT/'crates'/crate
|
||||
paths.extend(p for p in base.rglob('*') if p.is_file() and (p.suffix in ('.rs','.sql') or p.name=='Cargo.toml'))
|
||||
paths.extend((ROOT/'proto').glob('*.proto'))
|
||||
return {str(p.relative_to(ROOT)):sha(p) for p in sorted(set(paths))}
|
||||
def command(args,timeout=300):
|
||||
p=subprocess.run(args,capture_output=True,timeout=timeout)
|
||||
if p.returncode:raise RuntimeError(p.stderr.decode(errors='replace')[-4000:])
|
||||
return p.stdout
|
||||
|
||||
def build():
|
||||
os.umask(0o077);STATE.mkdir(parents=True,exist_ok=True,mode=0o700)
|
||||
folder=STATE/('build-'+str(uuid.uuid4()));folder.mkdir(mode=0o700)
|
||||
before=sources();runner_before=sha(__file__);store(folder,'source-before.json',before)
|
||||
target=STATE/'target';target.mkdir(mode=0o700,exist_ok=True)
|
||||
args=['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m',
|
||||
'-v',str(ROOT)+':/src:ro','-v',str(target)+':/target','-e','CARGO_TARGET_DIR=/target','--entrypoint','cargo',IMAGE]
|
||||
command(args+['clippy','--manifest-path','/src/Cargo.toml','--locked','--offline','-p','rmm-server','--all-targets','--','-D','warnings'])
|
||||
raw=command(args+['test','--manifest-path','/src/Cargo.toml','--locked','--offline','-p','rmm-server','--release','--no-run','--message-format=json'])
|
||||
paths=[]
|
||||
for line in raw.splitlines():
|
||||
try:item=json.loads(line)
|
||||
except ValueError:continue
|
||||
if item.get('reason')=='compiler-artifact' and item.get('profile',{}).get('test') and item.get('target',{}).get('name')=='rmm-server' and item.get('executable'):
|
||||
paths.append(target/Path(item['executable']).relative_to('/target'))
|
||||
require(len(paths)==1,'one actual release server UNIT harness, not server daemon')
|
||||
artifact=folder/'server-policy-tests';shutil.copyfile(paths[0],artifact);artifact.chmod(0o600)
|
||||
elf=artifact.read_bytes();require(elf[:6]==b'\x7fELF\x02\x01' and elf[18:20]==b'\x3e\x00','x64 ELF unit harness')
|
||||
# Exact-list inspection executes no tests; then only the thirteen named pure
|
||||
# units run in an isolated local container without network/DB configuration.
|
||||
run=['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=64m','-v',str(artifact.resolve())+':/server-policy-tests:ro','--entrypoint','/bin/sh',IMAGE,'-c']
|
||||
listed=command(run+['exec /lib64/ld-linux-x86-64.so.2 /server-policy-tests --list --format terse'])
|
||||
names=set(re.findall(rb'^(.+): test$',listed,re.M));require(all(name.encode() in names for name in TESTS),'all exact selected test names present')
|
||||
local=[]
|
||||
for name in TESTS:
|
||||
output=command(run+['exec /lib64/ld-linux-x86-64.so.2 /server-policy-tests --exact '+name+' --test-threads=1'],timeout=15)
|
||||
require(b'1 passed; 0 failed; 0 ignored;' in output,'one selected local policy guard passed')
|
||||
local.append(dict(name=name,stdout_sha256=hashlib.sha256(output).hexdigest()))
|
||||
require(sources()==before and sha(__file__)==runner_before,'source drift during qualification; never qualify stale binary')
|
||||
manifest=dict(schema_version=1,target='x86_64-unknown-linux-gnu',profile='release-unit-harness',
|
||||
source_files=before,source_before_after_equal=True,artifact=dict(path=str(artifact.resolve()),sha256=sha(artifact),bytes=artifact.stat().st_size),
|
||||
selected_tests=list(TESTS),local_tests=local,runner_sha256=runner_before,clippy_all_targets_D_warnings=True,native_vm_executed=False,
|
||||
database_tests_executed=False,server_daemon_started=False,rmm_jobs_created=False)
|
||||
store(folder,'manifest.json',manifest);print(json.dumps(dict(manifest=str((folder/'manifest.json').resolve()),binary_sha256=sha(artifact),selected_tests=len(TESTS),native_vm_executed=False)))
|
||||
|
||||
def verified(path):
|
||||
m=contract.protected_json(path)
|
||||
require(m['runner_sha256']==sha(__file__),'frozen exact native test orchestrator')
|
||||
require(m['source_files']==sources() and m['selected_tests']==list(TESTS) and m['source_before_after_equal'] is True,'frozen exact policy source/test selection')
|
||||
p=Path(m['artifact']['path']);require(sha(p)==m['artifact']['sha256'] and p.stat().st_size==m['artifact']['bytes'],'actual unchanged unit harness')
|
||||
require(m['profile']=='release-unit-harness' and m['native_vm_executed'] is False,'test harness only')
|
||||
return m
|
||||
|
||||
# Sent only by the parent's explicitly invoked native phase through the existing
|
||||
# pinned SSH bootstrap. It never reads credentials, product state or live logs.
|
||||
def native_case(payload):
|
||||
import resource
|
||||
import stat
|
||||
import time
|
||||
root=Path('/var/lib/rmm-server-policy-acceptance');ident=str(uuid.UUID(payload['id']))
|
||||
require(ident==payload['id'] and payload['action'] in ('prepare','run','read'),'fixed native case')
|
||||
sha=payload['binary_sha256'];require(len(sha)==64 and all(c in '0123456789abcdef' for c in sha),'fixed unit artifact hash')
|
||||
# Duplicate the frozen names in the remote source: payload cannot select an
|
||||
# arbitrary test from the executable (in particular no DB/async/job tests).
|
||||
names=[
|
||||
'windows_policy::tests::system_permission_and_helper_requirement_are_distinct_from_root',
|
||||
'windows_policy::tests::exact_system_scope_never_inherits_root_or_other_device_authority',
|
||||
'windows_policy::tests::narrow_scm_ceiling_denies_unreviewed_effects_and_provider_inputs',
|
||||
'windows_policy::tests::policy_file_refuses_symlinks_public_mode_wrong_owner_and_writable_parent',
|
||||
'root_policy::tests::no_capability_or_relabel_shortcut',
|
||||
'root_policy::root_log_tests::exact_root_log_plan_shape_and_selected_scope',
|
||||
'root_policy::root_log_tests::default_empty_and_service_mutation_list_never_authorize_logs',
|
||||
'root_policy::root_log_tests::scoped_allowlist_and_global_ceiling_are_exact',
|
||||
'root_policy::root_log_tests::exact_log_bounds_not_rounded_hours',
|
||||
'root_policy::root_log_tests::env_lists_are_bounded_data_not_paths_wildcards_or_bools',
|
||||
'root_policy::root_log_tests::classification_cannot_be_bypassed_by_another_scope_or_empty_env',
|
||||
'root_policy::root_log_tests::permissions_are_separate_root_read_and_sensitive_requirements',
|
||||
'root_policy::root_log_tests::unknown_log_profile_version_denies']
|
||||
require(payload['tests']==names and os.geteuid()==0,'explicit exact thirteen bootstrap unit tests')
|
||||
def safe(path,directory):
|
||||
m=path.lstat();require(m.st_uid==0 and not m.st_mode&0o077 and not stat.S_ISLNK(m.st_mode) and (stat.S_ISDIR(m.st_mode) if directory else stat.S_ISREG(m.st_mode)),'private fixed native test path')
|
||||
def save(path,value):
|
||||
fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as f:json.dump(value,f);f.flush();os.fsync(f.fileno())
|
||||
if not root.exists():root.mkdir(mode=0o700)
|
||||
safe(root,True);case=root/ident
|
||||
if payload['action']=='prepare':
|
||||
case.mkdir(mode=0o700);save(case/'intent.json',payload);return dict(prepared=True)
|
||||
safe(case,True);safe(case/'intent.json',False);intent=json.loads((case/'intent.json').read_bytes())
|
||||
require(all(intent[k]==payload[k] for k in ('id','binary_sha256','manifest_sha256','tests')),'original native intent')
|
||||
if payload['action']=='read':safe(case/'result.json',False);return json.loads((case/'result.json').read_bytes())
|
||||
binary=case/'server-policy-tests';safe(binary,False)
|
||||
require(hashlib.sha256(binary.read_bytes()).hexdigest()==sha,'native staged test bytes')
|
||||
save(case/'started.json',dict(binary_sha256=sha,tests=names,no_rerun_after_lost_reply=True))
|
||||
binary.chmod(0o500);temporary=case/'tmp';temporary.mkdir(mode=0o700)
|
||||
env={'PATH':'/usr/bin:/bin','LANG':'C.UTF-8','TMPDIR':str(temporary),'RUST_BACKTRACE':'0'}
|
||||
check=subprocess.run(['/usr/bin/unshare','--net','--','/usr/bin/true'],env=env,capture_output=True,timeout=3)
|
||||
require(check.returncode==0,'network namespace required; do not weaken isolation')
|
||||
def limits():resource.setrlimit(resource.RLIMIT_CORE,(0,0));resource.setrlimit(resource.RLIMIT_FSIZE,(1048576,1048576));resource.setrlimit(resource.RLIMIT_CPU,(10,10))
|
||||
results=[];deadline=time.monotonic()+20
|
||||
for number,name in enumerate(names):
|
||||
out=case/(str(number)+'-stdout.bin');err=case/(str(number)+'-stderr.bin')
|
||||
timed_out=False
|
||||
with out.open('xb') as stdout,err.open('xb') as stderr:
|
||||
out.chmod(0o600);err.chmod(0o600)
|
||||
try:
|
||||
remaining=deadline-time.monotonic();require(remaining>0,'aggregate pure test deadline')
|
||||
result=subprocess.run(['/usr/bin/unshare','--net','--',str(binary),'--exact',name,'--test-threads=1'],cwd=case,env=env,stdin=subprocess.DEVNULL,stdout=stdout,stderr=stderr,timeout=min(5,remaining),preexec_fn=limits)
|
||||
code=result.returncode
|
||||
except subprocess.TimeoutExpired:timed_out=True;code=None
|
||||
stdout.flush();stderr.flush();os.fsync(stdout.fileno());os.fsync(stderr.fileno())
|
||||
raw=out.read_bytes();errors=err.read_bytes();require(len(raw)<=1048576 and len(errors)<=1048576,'bounded unit output')
|
||||
results.append(dict(name=name,exit_code=code,timed_out=timed_out,stdout_b64=base64.b64encode(raw).decode(),stderr_b64=base64.b64encode(errors).decode()))
|
||||
if code!=0:break
|
||||
report=dict(id=ident,binary_sha256=sha,manifest_sha256=payload['manifest_sha256'],results=results,native_platform='linux',network_namespace=True,environment_cleared=True,server_daemon_started=False,database_tests_executed=False,rmm_jobs_created=False)
|
||||
save(case/'result.json',report);return report
|
||||
|
||||
def native(manifest_path,ident=None):
|
||||
m=verified(manifest_path);os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True)
|
||||
if ident is None:
|
||||
ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700)
|
||||
payload=dict(id=ident,binary_sha256=m['artifact']['sha256'],manifest_sha256=sha(manifest_path),tests=list(TESTS))
|
||||
store(folder,'intent.json',dict(payload=payload,manifest_path=str(manifest_path.resolve())))
|
||||
linux.rpc('linux-vm.conf',native_case,dict(payload,action='prepare'))
|
||||
cfg=linux.access.load_config(ROOT/'credentials/linux-vm.conf')
|
||||
args=['sftp','-F','/dev/null','-P',cfg.get('port','22'),'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','-i',str(Path(cfg['ssh_key_path']).expanduser()),'-b','-',cfg['username']+'@'+cfg['address']]
|
||||
result=subprocess.run(args,input=('put '+json.dumps(m['artifact']['path'])+' '+json.dumps(REMOTE+'/'+ident+'/server-policy-tests')+'\n').encode(),capture_output=True,timeout=120)
|
||||
store(folder,'upload.json',dict(returncode=result.returncode));require(result.returncode==0,'fixed native test transfer')
|
||||
store(folder,'run-intent.json',dict(execute_only_exact_pure_tests=True))
|
||||
value=linux.rpc('linux-vm.conf',native_case,dict(payload,action='run'))
|
||||
else:
|
||||
ident=str(uuid.UUID(ident));folder=STATE/ident;intent=contract.protected_json(folder/'intent.json');payload=intent['payload']
|
||||
require(intent['manifest_path']==str(manifest_path.resolve()) and payload['manifest_sha256']==sha(manifest_path),'original manifest only')
|
||||
value=linux.rpc('linux-vm.conf',native_case,dict(payload,action='read'))
|
||||
store(folder,'readback-'+str(uuid.uuid4())+'.json',value)
|
||||
require(value['binary_sha256']==m['artifact']['sha256'] and len(value['results'])==len(TESTS),'all exact native pure tests observed')
|
||||
for result,name in zip(value['results'],TESTS,strict=True):
|
||||
raw=base64.b64decode(result['stdout_b64'],validate=True)
|
||||
require(result['name']==name and result['exit_code']==0 and result['timed_out'] is False and b'1 passed; 0 failed; 0 ignored;' in raw,'exact native test passed, no ignored tests')
|
||||
proof=dict(verified=True,case_id=ident,native_platform='linux',selected_tests=len(TESTS),manifest_sha256=sha(manifest_path),report=value)
|
||||
if (folder/'passed.json').exists():require(contract.protected_json(folder/'passed.json')==proof,'retain identical original native proof')
|
||||
else:store(folder,'passed.json',proof)
|
||||
print(json.dumps(dict(verified=True,case_id=ident,selected_native_tests=len(TESTS))))
|
||||
|
||||
def main():
|
||||
p=argparse.ArgumentParser(description=__doc__);p.add_argument('action',choices=('build-local','native','readback'));p.add_argument('--manifest',type=Path);p.add_argument('--case');p.add_argument('--execute-native-tests',action='store_true');a=p.parse_args()
|
||||
if a.action=='build-local':require(not(a.manifest or a.case or a.execute_native_tests),'local build only');build()
|
||||
elif a.action=='native':require(a.manifest and a.execute_native_tests and not a.case,'parent explicitly starts one native case');native(a.manifest)
|
||||
else:require(a.manifest and a.case and not a.execute_native_tests,'readback only, no repeated native run');native(a.manifest,a.case)
|
||||
if __name__=='__main__':main()
|
||||
Reference in New Issue
Block a user