forked from mtusi2026/mtusi2026lab7
ЛР 7: реализация мессенджера и сценарии проверки
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Prove CA and SAN checks on a loopback socket using the generated PKI."""
|
||||
|
||||
import socket
|
||||
import ssl
|
||||
import sys
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
from tls_context import allowed, listener
|
||||
|
||||
|
||||
def main():
|
||||
keys = ROOT / "certs" / "private"
|
||||
server_context = listener(keys / "api.crt", keys / "api.key", keys / "ca.crt", True)
|
||||
cases = [(None, False), ("foreign-client", False), ("untrusted-role", False), ("lb", True)]
|
||||
with socket.create_server(("127.0.0.1", 0)) as connection:
|
||||
connection.settimeout(4)
|
||||
port = connection.getsockname()[1]
|
||||
|
||||
def serve():
|
||||
for _ in cases:
|
||||
accepted, _ = connection.accept()
|
||||
with accepted:
|
||||
accepted.settimeout(3)
|
||||
try:
|
||||
with server_context.wrap_socket(accepted, server_side=True) as transport:
|
||||
if allowed(transport, {"lb"}):
|
||||
transport.sendall(b"allow")
|
||||
except ssl.SSLError:
|
||||
pass
|
||||
|
||||
with ThreadPoolExecutor(1) as thread:
|
||||
worker = thread.submit(serve)
|
||||
for role, expected in cases:
|
||||
client = ssl.create_default_context(cafile=keys / "ca.crt")
|
||||
if role:
|
||||
client.load_cert_chain(keys / f"{role}.crt", keys / f"{role}.key")
|
||||
try:
|
||||
with socket.create_connection(("127.0.0.1", port), timeout=3) as raw:
|
||||
with client.wrap_socket(raw, server_hostname="api") as transport:
|
||||
actual = transport.recv(16) == b"allow"
|
||||
except (ssl.SSLError, ConnectionResetError):
|
||||
actual = False
|
||||
assert actual == expected, role
|
||||
print(f"PASS {role or 'without client certificate'}: {'accepted' if actual else 'rejected'}")
|
||||
worker.result()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Verify repeatable login limiting over the HTTPS ingress."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, check
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="https://localhost:8443")
|
||||
parser.add_argument("--ca-file", default=ROOT / "certs/private/ca.crt")
|
||||
args = parser.parse_args()
|
||||
c = Client(args.base_url, str(args.ca_file))
|
||||
user = c.user("ratelimit")
|
||||
name = user["registration"]["username"]
|
||||
authorization = base64.b64encode((name + ":synthetic-wrong-password").encode()).decode()
|
||||
outcomes = []
|
||||
for _ in range(9):
|
||||
reply = c.call("POST", "/api/v1/auth/sessions", (401, 429),
|
||||
headers={"Authorization": "Basic " + authorization})
|
||||
outcomes.append(reply["status"])
|
||||
if reply["status"] == 429:
|
||||
check(int(reply["headers"]["Retry-After"]) > 0, "missing retry interval")
|
||||
check(outcomes[:7] == [401] * 7 and outcomes[7:] == [429, 429],
|
||||
f"unexpected shared login limit: {outcomes}")
|
||||
print("PASS: 401 for wrong passwords, then shared 429/Retry-After over HTTPS")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user