forked from mtusi2026/mtusi2026lab7
Implement lab 7 transport security and worker containment
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import asyncio
|
||||
import json
|
||||
import sys
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
|
||||
import aio_pika
|
||||
from security_probe import RESULTS, context, record, signed_s3
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
|
||||
async def compare(data):
|
||||
engine = create_async_engine(
|
||||
data["DATABASE_URL"].replace("postgresql://", "postgresql+asyncpg://"),
|
||||
connect_args={"ssl": context()},
|
||||
hide_parameters=True,
|
||||
)
|
||||
async with engine.connect() as connection:
|
||||
await connection.execute(text("SELECT count(*) FROM users"))
|
||||
record("broad-db:users-readable", True)
|
||||
await connection.execute(text("CREATE TABLE security_comparison (id integer)"))
|
||||
await connection.rollback()
|
||||
record("broad-db:ddl-allowed", True)
|
||||
await engine.dispose()
|
||||
record(
|
||||
"broad-s3:private-write",
|
||||
signed_s3(data, "PUT", "private/security-comparison", b"probe", role="operator") == 200,
|
||||
)
|
||||
record(
|
||||
"broad-s3:private-delete",
|
||||
signed_s3(data, "DELETE", "private/security-comparison", role="operator") == 204,
|
||||
)
|
||||
parsed = urlsplit(data["BROKER_URL"])
|
||||
url = urlunsplit(
|
||||
("amqps", parsed.netloc.replace("127.0.0.1:5672", "broker:5671"), parsed.path, "", "")
|
||||
)
|
||||
async with await aio_pika.connect(
|
||||
url, ssl_context=context("operator"), timeout=5
|
||||
) as connection:
|
||||
channel = await connection.channel()
|
||||
queue = await channel.declare_queue("security-comparison", exclusive=True)
|
||||
await channel.default_exchange.publish(aio_pika.Message(b"probe"), routing_key=queue.name)
|
||||
message = await queue.get(timeout=5)
|
||||
await message.ack()
|
||||
await queue.delete()
|
||||
record("broad-queue:configure-publish-consume-delete", True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(compare(json.load(sys.stdin)))
|
||||
print(
|
||||
json.dumps({"checks": RESULTS, "passed": all(item["passed"] for item in RESULTS)}, indent=2)
|
||||
)
|
||||
raise SystemExit(0 if all(item["passed"] for item in RESULTS) else 1)
|
||||
Reference in New Issue
Block a user