Files
compras/scripts/verify-authorized-mail-delivery.py
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

39 lines
2.4 KiB
Python

"""Read only log records belonging to the owner's authorised Compras test."""
from pathlib import Path
import json
import re
import subprocess
import sys
directory = Path('/var/backups/compras-presentation-20261129/mail-configuration')
receipt = json.loads((directory/'submission.json').read_text())
assert receipt['state'] == 'submitted' and receipt['recipient'] == 'nala@tempo.co.mz'
log = Path('/var/log/mail.log')
if log.exists():
with log.open('rb') as stream:
stream.seek(max(0, log.stat().st_size-4*1024*1024))
lines = stream.read().decode('utf-8', errors='replace').splitlines()
else:
lines = subprocess.check_output(['journalctl', '--since', '-15 minutes', '--no-pager', '-o', 'cat', 'SYSLOG_FACILITY=2']).decode('utf-8', errors='replace').splitlines()
identifiers = {receipt['message_id']}
matching = []
for _ in range(5):
for line in lines:
if any(identifier+':' in line or 'message-id=<'+identifier+'>' in line for identifier in identifiers):
if line not in matching:
matching.append(line)
identifiers.update(re.findall(r'queued as ([A-Za-z0-9]+)', line))
delivered = [line for line in matching if 'to=<nala@tempo.co.mz>' in line and 'status=sent' in line and re.search(r'relay=(?:dovecot|virtual|local|[^,]*lmtp)', line)]
mailbox = subprocess.run(['doveadm', 'search', '-u', 'nala@tempo.co.mz', 'HEADER', 'Subject',
'Compras - verificacao do envio de email', 'SINCE', '29-Nov-2026'], capture_output=True, text=True)
mailbox_matches = [line for line in mailbox.stdout.splitlines() if re.fullmatch(r'[a-f0-9]{32} [0-9]+', line)] if mailbox.returncode == 0 else []
proof = {'state': 'delivered_to_mailbox' if delivered or len(mailbox_matches) == 1 else 'submission_accepted_delivery_not_yet_verified',
'sender': receipt['sender'], 'recipient': receipt['recipient'], 'transport': receipt['transport'],
'queue_identifiers': sorted(identifiers), 'matching_mail_log_records': len(matching),
'mailbox_delivery_records': len(delivered), 'targeted_test_message_found_in_authorized_mailbox': len(mailbox_matches) == 1,
'mailbox_other_messages_or_message_bodies_read': False, 'test_messages_sent': 1, 'business_or_credential_data_sent': False}
(directory/'delivery.json').write_text(json.dumps(proof, indent=2)+'\n')
print(json.dumps(proof))
if '--diagnose' in sys.argv:
print('\n'.join(matching))