92 lines
3.6 KiB
Python
92 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise AMQP EXTERNAL authentication and role ACLs on new connections."""
|
|
import socket
|
|
import ssl
|
|
import struct
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
TLS = Path(__file__).resolve().parents[1] / "secrets/tls"
|
|
|
|
|
|
def read_exact(conn, size):
|
|
result = bytearray()
|
|
while len(result) < size:
|
|
chunk = conn.recv(size - len(result))
|
|
if not chunk:
|
|
raise EOFError("RabbitMQ closed the connection")
|
|
result.extend(chunk)
|
|
return bytes(result)
|
|
|
|
|
|
def read_method(conn):
|
|
kind, _, size = struct.unpack(">BHI", read_exact(conn, 7))
|
|
assert kind == 1 and size >= 4
|
|
body = read_exact(conn, size)
|
|
assert read_exact(conn, 1) == b"\xce"
|
|
return struct.unpack(">HH", body[:4]), body[4:]
|
|
|
|
|
|
def send_method(conn, channel, klass, method, args):
|
|
body = struct.pack(">HH", klass, method) + args
|
|
conn.sendall(struct.pack(">BHI", 1, channel, len(body)) + body + b"\xce")
|
|
|
|
|
|
def short(value):
|
|
return bytes([len(value)]) + value
|
|
|
|
|
|
def authenticate(conn):
|
|
conn.sendall(b"AMQP\x00\x00\x09\x01")
|
|
method, data = read_method(conn)
|
|
assert method == (10, 10) and b"EXTERNAL" in data
|
|
send_method(conn, 0, 10, 11, b"\x00" * 4 + short(b"EXTERNAL") + b"\x00" * 4 + short(b"en_US"))
|
|
try:
|
|
method, data = read_method(conn)
|
|
except EOFError:
|
|
return False
|
|
if method == (10, 50):
|
|
assert struct.unpack_from(">H", data)[0] == 403
|
|
return False
|
|
assert method == (10, 30)
|
|
send_method(conn, 0, 10, 31, data)
|
|
send_method(conn, 0, 10, 40, short(b"/") + short(b"") + b"\x00")
|
|
assert read_method(conn)[0] == (10, 41)
|
|
return True
|
|
|
|
|
|
def main():
|
|
container = subprocess.check_output(["docker", "compose", "ps", "-q", "broker"], text=True).strip()
|
|
address = subprocess.check_output(["docker", "inspect", "-f",
|
|
'{{(index .NetworkSettings.Networks "highload-lab7_data").IPAddress}}', container], text=True).strip()
|
|
for identity in (None, "wrong-role", "messenger_unregistered", "messenger_worker", "messenger_dispatcher"):
|
|
context = ssl.create_default_context(cafile=str(TLS / "lab7-ca.crt"))
|
|
if identity:
|
|
context.load_cert_chain(TLS / f"{identity}.crt", TLS / f"{identity}.key")
|
|
with socket.create_connection((address, 5671), timeout=5) as tcp:
|
|
try:
|
|
with context.wrap_socket(tcp, server_hostname="broker") as conn:
|
|
admitted = authenticate(conn)
|
|
assert admitted == (identity in ("messenger_worker", "messenger_dispatcher"))
|
|
if admitted:
|
|
send_method(conn, 1, 20, 10, short(b""))
|
|
assert read_method(conn)[0] == (20, 11)
|
|
# Worker cannot publish. Dispatcher cannot read using basic.get.
|
|
if identity == "messenger_worker":
|
|
send_method(conn, 1, 60, 40, b"\x00\x00" + short(b"") + short(b"attachments.v1") + b"\x00")
|
|
conn.sendall(struct.pack(">BHIHHQH", 2, 1, 14, 60, 0, 0, 0) + b"\xce")
|
|
else:
|
|
send_method(conn, 1, 60, 70, b"\x00\x00" + short(b"attachments.v1") + b"\x01")
|
|
method, data = read_method(conn)
|
|
assert method == (20, 40) and struct.unpack_from(">H", data)[0] == 403
|
|
except ssl.SSLError:
|
|
if identity not in (None, "wrong-role"):
|
|
raise
|
|
else:
|
|
assert identity not in (None, "wrong-role"), "TLS accepted an untrusted peer"
|
|
print(f"PASS: {identity or 'no certificate'}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|