247 lines
5.3 KiB
Python
247 lines
5.3 KiB
Python
|
|
"""Lab 7: RabbitMQ mTLS + ACL.
|
|
|
|
Проверяет:
|
|
1. Relay certificate + relay user -> connection succeeds.
|
|
2. No client certificate -> TLS handshake fails.
|
|
3. Worker cannot publish -> 403 access_refused.
|
|
4. Relay cannot consume -> 403 access_refused.
|
|
5. Foreign trusted-CA certificate (api-1) with worker user
|
|
cannot publish -> 403 access_refused.
|
|
|
|
Note: RabbitMQ currently authenticates users using PLAIN credentials.
|
|
The certificate is verified at TLS level, but is not mapped to
|
|
the RabbitMQ username.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ssl
|
|
import sys
|
|
|
|
import pika
|
|
import pika.exceptions as pe
|
|
|
|
|
|
CERTS = "infra/certs"
|
|
HOST = "localhost"
|
|
PORT = 5671
|
|
QUEUE = "attachment.process.v1"
|
|
|
|
|
|
def connect(
|
|
cert=None,
|
|
key=None,
|
|
user="relay",
|
|
password="relay-secret",
|
|
):
|
|
ctx = ssl.create_default_context(
|
|
cafile=f"{CERTS}/ca.crt"
|
|
)
|
|
ctx.check_hostname = True
|
|
ctx.verify_mode = ssl.CERT_REQUIRED
|
|
|
|
if cert and key:
|
|
ctx.load_cert_chain(cert, key)
|
|
|
|
params = pika.ConnectionParameters(
|
|
host=HOST,
|
|
port=PORT,
|
|
virtual_host="/",
|
|
credentials=pika.PlainCredentials(user, password),
|
|
ssl_options=pika.SSLOptions(
|
|
ctx,
|
|
server_hostname="broker",
|
|
),
|
|
heartbeat=10,
|
|
blocked_connection_timeout=5,
|
|
)
|
|
|
|
return pika.BlockingConnection(params)
|
|
|
|
|
|
def expect_publish_denied(cert, key, user, password, label):
|
|
conn = None
|
|
|
|
try:
|
|
conn = connect(
|
|
cert,
|
|
key,
|
|
user=user,
|
|
password=password,
|
|
)
|
|
|
|
ch = conn.channel()
|
|
ch.confirm_delivery()
|
|
|
|
ch.basic_publish(
|
|
exchange="",
|
|
routing_key=QUEUE,
|
|
body=b"{}",
|
|
properties=pika.BasicProperties(
|
|
delivery_mode=2
|
|
),
|
|
)
|
|
|
|
print(f"FAIL: {label}: publish unexpectedly succeeded")
|
|
return False
|
|
|
|
except pe.ChannelClosedByBroker as exc:
|
|
if exc.reply_code == 403:
|
|
print(f"PASS: {label}: 403 access_refused")
|
|
return True
|
|
|
|
print(
|
|
f"FAIL: {label}: "
|
|
f"{exc.reply_code} {exc.reply_text}"
|
|
)
|
|
return False
|
|
|
|
except Exception as exc:
|
|
print(
|
|
f"FAIL: {label}: "
|
|
f"{type(exc).__name__}: {exc}"
|
|
)
|
|
return False
|
|
|
|
finally:
|
|
if conn is not None and conn.is_open:
|
|
conn.close()
|
|
|
|
|
|
def expect_consume_denied():
|
|
conn = None
|
|
|
|
try:
|
|
conn = connect(
|
|
f"{CERTS}/broker-relay.crt",
|
|
f"{CERTS}/broker-relay.key",
|
|
user="relay",
|
|
password="relay-secret",
|
|
)
|
|
|
|
ch = conn.channel()
|
|
|
|
ch.basic_consume(
|
|
queue=QUEUE,
|
|
on_message_callback=lambda *args: None,
|
|
auto_ack=False,
|
|
)
|
|
|
|
print("FAIL: relay consume unexpectedly succeeded")
|
|
return False
|
|
|
|
except pe.ChannelClosedByBroker as exc:
|
|
if exc.reply_code == 403:
|
|
print("PASS: relay consume: 403 access_refused")
|
|
return True
|
|
|
|
print(
|
|
f"FAIL: relay consume: "
|
|
f"{exc.reply_code} {exc.reply_text}"
|
|
)
|
|
return False
|
|
|
|
except Exception as exc:
|
|
print(
|
|
f"FAIL: relay consume: "
|
|
f"{type(exc).__name__}: {exc}"
|
|
)
|
|
return False
|
|
|
|
finally:
|
|
if conn is not None and conn.is_open:
|
|
conn.close()
|
|
|
|
|
|
def main():
|
|
failures = 0
|
|
|
|
# 0. Sanity check: valid relay identity.
|
|
conn = None
|
|
|
|
try:
|
|
conn = connect(
|
|
f"{CERTS}/broker-relay.crt",
|
|
f"{CERTS}/broker-relay.key",
|
|
user="relay",
|
|
password="relay-secret",
|
|
)
|
|
|
|
print("PASS: relay cert + relay user -> connected")
|
|
|
|
except Exception as exc:
|
|
print(
|
|
"FAIL: relay connection: "
|
|
f"{type(exc).__name__}: {exc}"
|
|
)
|
|
sys.exit(1)
|
|
|
|
finally:
|
|
if conn is not None and conn.is_open:
|
|
conn.close()
|
|
|
|
# 1. No client certificate.
|
|
conn = None
|
|
|
|
try:
|
|
conn = connect(
|
|
cert=None,
|
|
key=None,
|
|
)
|
|
|
|
print("FAIL: connection without client certificate")
|
|
failures += 1
|
|
|
|
except (
|
|
pe.AMQPConnectionError,
|
|
ssl.SSLError,
|
|
OSError,
|
|
) as exc:
|
|
print(
|
|
"PASS: no client certificate -> "
|
|
f"{type(exc).__name__}"
|
|
)
|
|
|
|
finally:
|
|
if conn is not None and conn.is_open:
|
|
conn.close()
|
|
|
|
# 2. Worker cannot publish.
|
|
if not expect_publish_denied(
|
|
f"{CERTS}/broker-worker.crt",
|
|
f"{CERTS}/broker-worker.key",
|
|
"worker",
|
|
"worker-secret",
|
|
"worker publish",
|
|
):
|
|
failures += 1
|
|
|
|
# 3. Relay cannot consume.
|
|
if not expect_consume_denied():
|
|
failures += 1
|
|
|
|
# 4. Foreign trusted-CA certificate cannot publish
|
|
# using worker's restricted account.
|
|
if not expect_publish_denied(
|
|
f"{CERTS}/api-1.crt",
|
|
f"{CERTS}/api-1.key",
|
|
"worker",
|
|
"worker-secret",
|
|
"api-1 certificate publish",
|
|
):
|
|
failures += 1
|
|
|
|
print()
|
|
|
|
if failures:
|
|
print(f"FAIL: {failures} test(s)")
|
|
sys.exit(1)
|
|
|
|
print("PASS: all broker mTLS/ACL tests")
|
|
sys.exit(0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|