#!/usr/bin/env python3 """Fixed native transport for the selected-return operator; observe by default. No mailbox read, SMTP send, credential fetch or Kubernetes resource mutation. Explicit apply/reconcile touches only the reviewed private audit and suppression. """ from __future__ import annotations import argparse import base64 import hashlib import json import re import shlex import subprocess import sys from datetime import UTC, datetime from pathlib import Path import mail_return_operation as operation ROOT = Path(__file__).resolve().parent IMAGE = ( "forgejo.coulomb.social/coulomb/email-connect@sha256:" "5d79fd1fec15d3a279383820f170d263de3a55f5308df01a0a34732ebaea6d3d" ) CLUSTER = "a553c742-0115-43d4-99a4-a5ca56fe0786" NAMESPACE = "5678c5d7-e4f5-4a62-8f24-1c6e1d906319" DEPLOYMENT = "2e98e52a-a2d1-4c52-9ac8-6efbc0a18e72" TRANSACTIONAL = "36fdfd8cb58f10148cd491a7e34d7aea798e4289fce760af70a48776bede204d" CALLERS = "06fe961b9030dba2c0b26b92f8097e8fbd24e205d5fa79642aa9af60382886dc" SCHEMA = "coulomb.native-mail-return.v1" FILES = ("mail_return_native.py", "mail_return_operation.py") require = operation.require BOOTSTRAP = r""" import hashlib,json,os,sys,tempfile from pathlib import Path sys.dont_write_bytecode=True raw=sys.stdin.buffer.read(4*1024**2+1) assert __debug__ and len(raw)<=4*1024**2 r=json.loads(raw) assert set(r['sources'])=={'mail_return_native.py','mail_return_operation.py'} assert sum(len(s.encode()) for s in r['sources'].values())<=128*1024 hashes={n:hashlib.sha256(v.encode()).hexdigest() for n,v in r['sources'].items()} assert hashes==r['source_sha256'] if r.get('reviewed_source_sha256') is not None:assert hashes==r['reviewed_source_sha256'] with tempfile.TemporaryDirectory(prefix='mail-return-source-') as directory: for name,source in r['sources'].items(): fd=os.open(Path(directory)/name,os.O_WRONLY|os.O_CREAT|os.O_EXCL,0o600) with os.fdopen(fd,'w') as stream:stream.write(source) sys.path.insert(0,directory) import mail_return_native as native try:print(json.dumps(native.run_node(r))) except Exception as error: print(json.dumps({'status':'native_return_requires_inspection','failure_type':type(error).__name__, 'retry_automatically':False})) sys.exit(1) """ POD_PROGRAM = r""" import base64,hashlib,json,os,sys,types from pathlib import Path from datetime import datetime sys.dont_write_bytecode=True from email_connect import transactional,callers r=json.load(sys.stdin) assert __debug__ and os.getuid()==10001 actual=hashlib.sha256(Path(transactional.__file__).read_bytes()).hexdigest() assert actual==r['transactional_sha256'] assert hashlib.sha256(Path(callers.__file__).read_bytes()).hexdigest()==r['callers_sha256'] assert os.environ.get('EMAIL_CONNECT_CALLERS_FILE')=='/etc/email-connect-callers/callers.json' database=os.environ.get('EMAIL_CONNECT_DATABASE_PATH','/data/email-connect.db') assert database=='/data/email-connect.db' p=Path('/etc/email-connect-callers/callers.json') assert p.stat().st_size<=64*1024 policy=json.loads(p.read_text()) m=types.ModuleType('selected_mail_return') exec(compile(r['core_source'],'','exec'),m.__dict__) context={'caller_policy_sha256':m.digest(policy)} if r.get('policy_sha256') is not None:assert context['caller_policy_sha256']==r['policy_sha256'] ledger=Path('/data/email-connect.db') audit=Path('/data/mail-returns') action=r['action'] if action=='observe': with m.closing(m.connect(ledger)) as db: db.execute('BEGIN') tables={row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='table'")} assert tables=={'deliveries','verifications','suppressions','send_attempts', 'verification_attempts','caller_buckets'} size=db.execute('PRAGMA page_count').fetchone()[0]*db.execute('PRAGMA page_size').fetchone()[0] assert 0