feat(lab7): implement secure messenger infrastructure with TLS, OpenBao and observability
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Negative RabbitMQ ACL tests over authenticated TLS."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
import pika
|
||||
|
||||
from src.queue import RABBITMQ_QUEUE, connect
|
||||
|
||||
|
||||
def test_publisher_cannot_consume(channel):
|
||||
try:
|
||||
method, properties, body = channel.basic_get(
|
||||
queue=RABBITMQ_QUEUE,
|
||||
auto_ack=False,
|
||||
)
|
||||
|
||||
# Safety: if permissions are unexpectedly too broad,
|
||||
# immediately return any received message to the queue.
|
||||
if method is not None:
|
||||
channel.basic_nack(
|
||||
delivery_tag=method.delivery_tag,
|
||||
requeue=True,
|
||||
)
|
||||
|
||||
print("FAIL: publisher was allowed to read queue")
|
||||
return False
|
||||
|
||||
except pika.exceptions.ChannelClosedByBroker as exc:
|
||||
if exc.reply_code == 403 and "ACCESS_REFUSED" in exc.reply_text:
|
||||
print("PASS: publisher cannot consume image_jobs")
|
||||
return True
|
||||
|
||||
print(
|
||||
"FAIL: unexpected broker response:",
|
||||
exc.reply_code,
|
||||
exc.reply_text,
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def test_consumer_cannot_publish(channel):
|
||||
# A unique nonexistent routing key ensures that even if
|
||||
# publishing is unexpectedly permitted, no existing queue
|
||||
# receives a test message.
|
||||
routing_key = "lab7_acl_unroutable_" + uuid.uuid4().hex
|
||||
|
||||
try:
|
||||
channel.basic_publish(
|
||||
exchange="",
|
||||
routing_key=routing_key,
|
||||
body=b'{"acl_test":true}',
|
||||
mandatory=False,
|
||||
)
|
||||
|
||||
# Flush asynchronous broker responses.
|
||||
channel.queue_declare(
|
||||
queue=RABBITMQ_QUEUE,
|
||||
passive=True,
|
||||
)
|
||||
|
||||
print("FAIL: consumer was allowed to publish")
|
||||
return False
|
||||
|
||||
except pika.exceptions.ChannelClosedByBroker as exc:
|
||||
if exc.reply_code == 403 and "ACCESS_REFUSED" in exc.reply_text:
|
||||
print("PASS: consumer cannot publish")
|
||||
return True
|
||||
|
||||
print(
|
||||
"FAIL: unexpected broker response:",
|
||||
exc.reply_code,
|
||||
exc.reply_text,
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
role = os.environ.get("RABBITMQ_USER")
|
||||
|
||||
connection = connect()
|
||||
|
||||
try:
|
||||
print("RabbitMQ role:", role)
|
||||
print("TLS connection: authenticated")
|
||||
|
||||
channel = connection.channel()
|
||||
|
||||
if role == "messenger_publisher":
|
||||
passed = test_publisher_cannot_consume(channel)
|
||||
|
||||
elif role == "messenger_consumer":
|
||||
passed = test_consumer_cannot_publish(channel)
|
||||
|
||||
else:
|
||||
print("FAIL: unexpected RabbitMQ role")
|
||||
sys.exit(1)
|
||||
|
||||
if not passed:
|
||||
sys.exit(1)
|
||||
|
||||
print("PASS: RabbitMQ ACL tests for", role)
|
||||
|
||||
finally:
|
||||
if connection.is_open:
|
||||
connection.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user