Files
dpl/scripts/fuc-xb-launch/horizon-php.py
T

41 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Give only this FUC CLI worker the process functions Horizon requires."""
import hashlib,json,os,pathlib,pwd,subprocess
BASE=pathlib.Path('/var/www/wwwroot/fuc-xb-test')
UNIT=pathlib.Path('/etc/systemd/system/fuc-xb-test-horizon.service')
DIR=BASE/'private/launch-ready-20261105/horizon-php'
SCAN=BASE/'shared/horizon-php.d'
EXPECTED='b3d82cb30ef7e42fcdfbc97c5b2d6eb2a70477eda4ee5dbb26f3a261b17ed67a'
REQUIRED={'proc_open','proc_get_status','proc_close','proc_terminate'}
def run(args,env=None):
p=subprocess.run(args,capture_output=True,text=True,env=env,timeout=100)
assert p.returncode==0,'FUC CLI worker operation failed'
return p.stdout.strip()
def main():
os.umask(0o077);assert hashlib.sha256(UNIT.read_bytes()).hexdigest()==EXPECTED
assert not DIR.exists()and not SCAN.exists(),'Existing FUC CLI override; inspect'
settings=json.loads(run(['php','-r',"echo json_encode(['scan'=>PHP_CONFIG_FILE_SCAN_DIR,'disabled'=>ini_get('disable_functions'),'ini'=>php_ini_loaded_file()]);"]))
source=pathlib.Path(settings['scan']);assert source.is_dir() and source.is_absolute()and ':'not in str(source)
disabled={x.strip()for x in settings['disabled'].split(',')if x.strip()};restored=sorted(disabled&REQUIRED)
assert 'proc_open'in restored,'Unexpected CLI hardening; inspect rather than changing'
before=UNIT.read_bytes();DIR.mkdir(mode=0o700);(DIR/'unit-before').write_bytes(before)
SCAN.mkdir(mode=0o750);group=pwd.getpwnam('fucxbtest').pw_gid;os.chown(SCAN,0,group)
sources={}
for f in sorted(source.glob('*.ini')):
assert f.is_file();(SCAN/f.name).symlink_to(f);sources[str(f.resolve())]=hashlib.sha256(f.read_bytes()).hexdigest()
override=SCAN/'zz-fuc-horizon-process.ini';assert not override.exists();override.write_text('disable_functions='+','.join(sorted(disabled-REQUIRED))+'\n');override.chmod(0o640);os.chown(override,0,group)
env={**os.environ,'PHP_INI_SCAN_DIR':str(SCAN),'XB_INSTANCE':'fuc-xb-test'}
probe=json.loads(run(['runuser','-u','fucxbtest','--','env','PHP_INI_SCAN_DIR='+str(SCAN),'php','-r',"echo json_encode(array_combine(['proc_open','proc_get_status','proc_close','proc_terminate'],array_map('function_exists',['proc_open','proc_get_status','proc_close','proc_terminate'])));"],env))
assert all(probe.values()),'Required FUC worker process functions unavailable'
updated=before.decode().replace('Environment=XB_INSTANCE=fuc-xb-test\n','Environment=XB_INSTANCE=fuc-xb-test\nEnvironment=PHP_INI_SCAN_DIR='+str(SCAN)+'\n')
assert updated!=before.decode()
try:
UNIT.write_text(updated);UNIT.chmod(0o644);run(['systemctl','daemon-reload']);run(['systemctl','restart','fuc-xb-test-horizon'])
assert run(['systemctl','is-active','fuc-xb-test-horizon'])=='active'
assert all(hashlib.sha256(pathlib.Path(p).read_bytes()).hexdigest()==sha for p,sha in sources.items()),'Global PHP sources changed'
except BaseException:
UNIT.write_bytes(before);run(['systemctl','daemon-reload']);raise
receipt={'configured':True,'scope':'FUC-XB CLI Horizon and its children only','restored_process_functions':restored,'global_php_and_fpm_unchanged':True,'other_disabled_functions_retained':True,'unit_sha256':hashlib.sha256(UNIT.read_bytes()).hexdigest(),'scan_directory':str(SCAN),'source_ini_sha256':sources}
(DIR/'receipt.json').write_text(json.dumps(receipt,indent=2));print(json.dumps({k:v for k,v in receipt.items()if k!='source_ini_sha256'}))
if __name__=='__main__':main()