forked from mtusi2026/mtusi2026lab7
выполение на 5
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify the worker identity is restricted to its required database, S3, and broker actions."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
import pika
|
||||
import psycopg
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from src.jobs import connect
|
||||
from src.object_store import ObjectStore
|
||||
from src.store import database_url
|
||||
|
||||
|
||||
def check_database() -> dict:
|
||||
with psycopg.connect(database_url()) as connection:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("SELECT current_user")
|
||||
role = cursor.fetchone()[0]
|
||||
cursor.execute("SELECT id, status FROM attachments LIMIT 1")
|
||||
cursor.fetchall()
|
||||
try:
|
||||
cursor.execute("SELECT id FROM users LIMIT 1")
|
||||
except psycopg.errors.InsufficientPrivilege:
|
||||
connection.rollback()
|
||||
users_denied = True
|
||||
else:
|
||||
raise AssertionError("worker can read users")
|
||||
if role != "messenger_worker":
|
||||
raise AssertionError(f"unexpected worker database role: {role}")
|
||||
return {"role": role, "attachments_read": True, "users_read_denied": users_denied}
|
||||
|
||||
|
||||
def check_s3() -> dict:
|
||||
client = ObjectStore().client
|
||||
results = {}
|
||||
for bucket in ("messenger-staging", "messenger-attachments", "messenger-variants"):
|
||||
try:
|
||||
client.list_objects_v2(Bucket=bucket, MaxKeys=1)
|
||||
except ClientError as error:
|
||||
code = error.response.get("Error", {}).get("Code")
|
||||
if code not in ("AccessDenied", "403"):
|
||||
raise
|
||||
results[f"{bucket}_list_denied"] = True
|
||||
else:
|
||||
raise AssertionError(f"worker can list {bucket}")
|
||||
|
||||
for bucket, key in (
|
||||
("messenger-attachments", f"original/{uuid.uuid4()}"),
|
||||
("messenger-variants", f"variants/{uuid.uuid4()}"),
|
||||
):
|
||||
try:
|
||||
client.get_object(Bucket=bucket, Key=key)
|
||||
except ClientError as error:
|
||||
code = error.response.get("Error", {}).get("Code")
|
||||
if code not in ("AccessDenied", "403"):
|
||||
raise
|
||||
results[f"{bucket}_read_denied"] = True
|
||||
else:
|
||||
raise AssertionError(f"worker can read objects from {bucket}")
|
||||
|
||||
probe = str(uuid.uuid4())
|
||||
allowed_objects = (
|
||||
("messenger-staging", f"staging/isolation-probe/{probe}", True),
|
||||
("messenger-attachments", f"original/isolation-probe/{probe}", False),
|
||||
("messenger-variants", f"variants/isolation-probe/{probe}", False),
|
||||
)
|
||||
for bucket, key, verify_read in allowed_objects:
|
||||
try:
|
||||
client.put_object(Bucket=bucket, Key=key, Body=b"worker-scope-probe")
|
||||
if verify_read:
|
||||
response = client.get_object(Bucket=bucket, Key=key)
|
||||
try:
|
||||
if response["Body"].read() != b"worker-scope-probe":
|
||||
raise AssertionError("worker staging read returned unexpected data")
|
||||
finally:
|
||||
response["Body"].close()
|
||||
results[f"{bucket}_write"] = True
|
||||
finally:
|
||||
try:
|
||||
client.delete_object(Bucket=bucket, Key=key)
|
||||
except ClientError as error:
|
||||
if error.response.get("Error", {}).get("Code") not in ("NoSuchKey", "404"):
|
||||
raise
|
||||
results["messenger-staging_read"] = True
|
||||
results["temporary_objects_removed"] = True
|
||||
return results
|
||||
|
||||
|
||||
def check_broker() -> dict:
|
||||
connection = connect()
|
||||
channel = connection.channel()
|
||||
try:
|
||||
try:
|
||||
channel.basic_publish(
|
||||
exchange="",
|
||||
routing_key="attachments.process.v1",
|
||||
body=b"worker-isolation-probe",
|
||||
)
|
||||
channel.close()
|
||||
except pika.exceptions.ChannelClosedByBroker as error:
|
||||
if error.reply_code != 403:
|
||||
raise
|
||||
publish_denied = True
|
||||
else:
|
||||
publish_denied = False
|
||||
if not publish_denied:
|
||||
raise AssertionError("worker can publish to the processing queue")
|
||||
return {"broker_identity_connected": True, "publish_denied": publish_denied}
|
||||
finally:
|
||||
if channel.is_open:
|
||||
channel.close()
|
||||
if connection.is_open:
|
||||
connection.close()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
result = {
|
||||
"database": check_database(),
|
||||
"s3": check_s3(),
|
||||
"broker": check_broker(),
|
||||
}
|
||||
print(json.dumps({"result": "PASS", **result}, sort_keys=True))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user