66 lines
3.2 KiB
Python
66 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Run ordinary Node/Playwright acceptance under an OS-held dedicated-account lock.
|
|
No sudo, SQL access, secrets in arguments, or authentication bypasses.
|
|
"""
|
|
import argparse
|
|
import fcntl
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import uuid
|
|
|
|
|
|
def run(script, arguments, resume=None):
|
|
if os.environ.get('CYJ_CONTROLLER_WORKER', 'application') != 'application':
|
|
raise ValueError('Application browser identity is restricted to the application worker')
|
|
root = Path.home()/'.local/state/cyj/browser/application'
|
|
root.mkdir(parents=True, mode=0o700, exist_ok=True)
|
|
if root.is_symlink() or root.stat().st_uid != os.getuid():
|
|
raise ValueError('Invalid private browser state directory')
|
|
root.chmod(0o700)
|
|
fd = os.open(root/'session.lock', os.O_CREAT|os.O_RDWR|os.O_NOFOLLOW, 0o600)
|
|
try:
|
|
st = os.fstat(fd)
|
|
if not stat.S_ISREG(st.st_mode) or st.st_uid != os.getuid() or stat.S_IMODE(st.st_mode) != 0o600 or st.st_nlink != 1:
|
|
raise ValueError('Invalid browser lock')
|
|
try: fcntl.flock(fd, fcntl.LOCK_EX|fcntl.LOCK_NB)
|
|
except BlockingIOError: raise ValueError('Dedicated browser identity is busy; retry after the current session exits')
|
|
if resume and (len(resume)!=32 or any(c not in '0123456789abcdef' for c in resume)):
|
|
raise ValueError('Resume requires an existing full run ID')
|
|
run_id = resume or uuid.uuid4().hex
|
|
state = root/(run_id+'.json')
|
|
if resume and not state.is_file(): raise ValueError('Unknown acceptance run')
|
|
if not resume:
|
|
# Pending fixtures must be explicitly reconciled, never silently replaced.
|
|
for candidate in root.glob('*.json'):
|
|
if json.loads(candidate.read_text()).get('projects'):
|
|
raise ValueError('Unclean acceptance run exists; resume its cleanup before starting another session')
|
|
os.ftruncate(fd,0);os.write(fd,run_id.encode());os.lseek(fd,0,0)
|
|
env = {**os.environ, 'PD_DEV_BROWSER_RUN_ID':run_id, 'PD_DEV_BROWSER_STATE':str(state),
|
|
'PD_DEV_BROWSER_LOCK_FD':str(fd)}
|
|
# Playwright protocol/API debug output can include password fill values.
|
|
for key in ('DEBUG', 'PWDEBUG', 'PLAYWRIGHT_TRACE_DIR', 'CHROME_LOG_FILE'):
|
|
env.pop(key, None)
|
|
result = subprocess.run(['/usr/bin/node',str(Path(script).resolve()),*arguments],env=env,pass_fds=(fd,))
|
|
return result.returncode
|
|
finally:os.close(fd)
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--resume')
|
|
parser.add_argument('script',type=Path)
|
|
parser.add_argument('arguments',nargs=argparse.REMAINDER)
|
|
args=parser.parse_args()
|
|
if not args.script.is_file():raise ValueError('Acceptance script unavailable')
|
|
return run(args.script,args.arguments,args.resume)
|
|
|
|
if __name__=='__main__':
|
|
try:raise SystemExit(main())
|
|
except (ValueError,OSError):
|
|
print('Browser acceptance launcher refused: check the dedicated-account lock, pending run cleanup and private state permissions. No credentials logged.',file=sys.stderr)
|
|
raise SystemExit(1)
|