#!/bin/sh # Installs only this new sampler. Does not alter agent/Xray/env/network rules. set -eu umask 077 if [ "$#" -ne 2 ] || [ "$1" != "--install" ]; then echo 'Usage: deploy_host_traffic_sampler.sh --install EXPECTED_NODE_SLUG' >&2 exit 2 fi test "$(id -u)" = 0 BUNDLE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) export PYTHONDONTWRITEBYTECODE=1 python3 - "$BUNDLE" "$2" <<'PY' import hashlib,json,os,pathlib,re,shutil,stat,subprocess,sys from datetime import datetime,timezone bundle=pathlib.Path(sys.argv[1]);expected=sys.argv[2] assert re.fullmatch(r'[A-Za-z0-9_-]{1,120}',expected),'Invalid expected node slug' destination=pathlib.Path('/opt/toplc-vpn-host-traffic') unit=pathlib.Path('/etc/systemd/system/toplc-vpn-host-traffic.service') for path in (destination,unit): assert not path.exists() and not path.is_symlink(),'Sampler already exists; review prior attempt before changing it' for ancestor in path.parents: assert not ancestor.is_symlink(),'Linked installation parent refused' manifest=json.loads((bundle/'manifest.json').read_text()) assert manifest['kind']=='toplc-host-traffic-sampler-bundle' and manifest['version']=='1.0.0' expected_files={'host_traffic_sampler.py','test_host_traffic_sampler.py','toplc-vpn-host-traffic.service','README.md','host-traffic-contract-fixture.json','deploy_host_traffic_sampler.sh'} assert set(manifest['files'])==expected_files,'Bundle inventory differs' assert {p.name for p in bundle.iterdir()}==expected_files|{'manifest.json'},'Unmanifested bundle files' def digest(path):return hashlib.sha256(path.read_bytes()).hexdigest() for name,value in manifest['files'].items(): path=bundle/name assert path.is_file() and not path.is_symlink() and digest(path)==value,'Bundle hash mismatch' env=pathlib.Path('/opt/toplc-vpn-node/node.env') assert env.is_file() and not env.is_symlink() and env.stat().st_uid==0 and stat.S_IMODE(env.stat().st_mode)==0o600,'Expected root0600 node.env' agent=pathlib.Path('/opt/toplc-vpn-node/agent.py') compose=pathlib.Path('/opt/toplc-vpn-node/docker-compose.yml') agent_unit=pathlib.Path('/etc/systemd/system/toplc-vpn-node-agent.service') protected=[env,agent,compose,agent_unit] before={str(path):digest(path) for path in protected} # Import from verified bundle; reads only slug/token and never executes node.env. sys.path.insert(0,str(bundle)) import host_traffic_sampler as sampler credentials=sampler.load_credentials(env) assert credentials.node_slug==expected,'Existing node slug differs' configuration=subprocess.run(['/usr/bin/python3','-B',str(bundle/'host_traffic_sampler.py'),'--check-config'],capture_output=True,text=True,timeout=10) assert configuration.returncode==0,'Sampler configuration or physical WAN detection failed' safe=json.loads(configuration.stdout) assert safe['node_slug']==expected and safe['configuration_valid'] is True and safe['network_requested'] is False subprocess.run(['systemd-analyze','verify',str(bundle/'toplc-vpn-host-traffic.service')],check=True,capture_output=True,timeout=15) destination.mkdir(mode=0o750) for name in ('host_traffic_sampler.py','README.md'): shutil.copyfile(bundle/name,destination/name);os.chmod(destination/name,0o600) assert digest(destination/name)==manifest['files'][name] shutil.copyfile(bundle/'toplc-vpn-host-traffic.service',unit);os.chmod(unit,0o644) assert digest(unit)==manifest['files']['toplc-vpn-host-traffic.service'] subprocess.run(['systemctl','daemon-reload'],check=True,timeout=20) subprocess.run(['systemctl','enable','--now','toplc-vpn-host-traffic.service'],check=True,capture_output=True,timeout=20) subprocess.run(['systemctl','is-active','--quiet','toplc-vpn-host-traffic.service'],check=True,timeout=10) after={str(path):digest(path) for path in protected} assert before==after,'Protected existing deployment files changed; inspect external activity' receipt={'kind':'toplc-host-traffic-sampler-install','version':'1.0.0','node_slug':expected,'interface':safe['interface'],'kind_semantics':'server_host_interface','counter_semantics':'linux_proc_net_dev_interface_bytes','sample_seconds':1,'upload_seconds':5,'queue_capacity':60,'protected_existing_files_unchanged':True,'network_rules_modified':False,'existing_agent_restarted':False,'xray_restarted':False,'new_sampler_active':True,'installed_at':datetime.now(timezone.utc).isoformat(),'bundle_files':manifest['files']} with (destination/'install-receipt.json').open('x') as handle:json.dump(receipt,handle,indent=2);handle.write('\n') print(json.dumps(receipt)) PY