forked from mtusi2026/mtusi2026lab7
Выполнить лабораторную 7 по безопасности мессенджера
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Start a new, explicitly versioned processing job for a ready attachment."""
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
import psycopg
|
||||
from jobs import s3_client
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
DB=os.environ["DATABASE_URL"]
|
||||
BUCKET=os.getenv("S3_BUCKET","messenger-attachments")
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) not in (3,4) or (len(sys.argv)==4 and sys.argv[3]!="--apply"):
|
||||
raise SystemExit("usage: python scripts/reprocess_pipeline.py ATTACHMENT_UUID NEW_VERSION [--apply]")
|
||||
attachment_id=uuid.UUID(sys.argv[1]); new_version=int(sys.argv[2]); apply=len(sys.argv)==4
|
||||
if new_version<1: raise SystemExit("NEW_VERSION must be positive")
|
||||
s3=s3_client()
|
||||
staged=None
|
||||
with psycopg.connect(DB,row_factory=dict_row) as conn:
|
||||
with conn.transaction():
|
||||
row=conn.execute("SELECT * FROM attachments WHERE id=%s FOR UPDATE",(attachment_id,)).fetchone()
|
||||
if not row: raise SystemExit("attachment not found")
|
||||
if row["status"]!="ready": raise SystemExit("only ready attachments can start a new pipeline version")
|
||||
if new_version<=row["pipeline_version"]: raise SystemExit("NEW_VERSION must be greater than the current version")
|
||||
old_key=row["object_key"]
|
||||
if not apply:
|
||||
print(f"dry-run attachment={attachment_id} current_version={row['pipeline_version']} new_version={new_version} source={old_key}")
|
||||
return
|
||||
obj=s3.get_object(Bucket=BUCKET,Key=old_key)
|
||||
body=obj["Body"].read(int(row["size_bytes"])+1)
|
||||
if len(body)!=row["size_bytes"] or __import__('hashlib').sha256(body).hexdigest()!=row["sha256"]:
|
||||
raise SystemExit("stored original failed checksum verification")
|
||||
job_id=uuid.uuid4(); staged=f"staging/{attachment_id}/{job_id}/original"
|
||||
s3.put_object(Bucket=BUCKET,Key=staged,Body=body,ContentType=row["content_type"],Metadata={"sha256":row["sha256"],"attachment-id":str(attachment_id),"job-id":str(job_id)})
|
||||
conn.execute("""UPDATE attachments SET object_key=NULL,staging_key=%s,job_id=%s,pipeline_version=%s,status='queued',attempts=0,
|
||||
lease_until=NULL,next_attempt_at=NULL,variants='[]'::jsonb,error_code=NULL,updated_at=now() WHERE id=%s""",
|
||||
(staged,job_id,new_version,attachment_id))
|
||||
conn.execute("INSERT INTO job_outbox(job_id,attempt,attachment_id,pipeline_version) VALUES (%s,1,%s,%s)",(job_id,attachment_id,new_version))
|
||||
conn.execute("INSERT INTO pipeline_audit(attachment_id,job_id,pipeline_version,event,detail) VALUES (%s,%s,%s,'reprocess_requested',jsonb_build_object('previous_version',%s::integer))",
|
||||
(attachment_id,job_id,new_version,row["pipeline_version"]))
|
||||
print(f"queued attachment={attachment_id} version={new_version}")
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user