import os import ssl import subprocess import sys import time from pathlib import Path from urllib.request import urlopen from urllib.error import URLError from cryptography import x509 from cryptography.hazmat.primitives import serialization import pki root = Path(__file__).resolve().parents[1] os.chdir(root) sys.path.insert(0,str(root/"tests")) from client import Client base = os.environ.get("BASE_URL","https://127.0.0.1:8443") try: urlopen(base+"/health/live",timeout=5) except URLError as exc: if not isinstance(exc.reason,ssl.SSLCertVerificationError): raise else: raise RuntimeError("Unexpected trust") print("PASS: public HTTPS rejected untrusted CA") ca=x509.load_pem_x509_certificate((pki.local/"ca.crt").read_bytes()) issuer=serialization.load_pem_private_key((pki.local/"ca.key").read_bytes(),password=None) pki.issue("other-client",ca,issuer) rogue,key=pki.issue("outside-ca") pki.issue("outside-client",rogue,key) probe = """ import socket, ssl for target in ("api","api2"): for name in ("none","other-client","outside-client","balancer"): ctx=ssl.create_default_context(cafile="/certs/ca.crt") if name!="none": ctx.load_cert_chain("/probe/"+name+".pem") ok=False try: with socket.create_connection((target+"-gw",8443),timeout=5) as conn: with ctx.wrap_socket(conn,server_hostname=target) as sock: sock.sendall(b"GET /health/live HTTP/1.0\\r\\n\\r\\n") ok=b" 200 " in sock.recv(256) except (ssl.SSLError,ConnectionError): pass if ok!=(name=="balancer"): raise RuntimeError(target+" "+name) print(target,name,"allowed" if ok else "denied") """ args=["docker","compose","run","--rm","-T","--no-deps","--entrypoint","python"] for name in ("balancer","outside-client","other-client"): args+=["-v",str(pki.local/(name+".pem"))+":/probe/"+name+".pem:ro"] subprocess.run(args+["worker","-c",probe],check=True) bench = "from time import perf_counter;from argon2 import PasswordHasher;k=PasswordHasher(time_cost=2,memory_cost=19456,parallelism=1);t=perf_counter();h=k.hash('synthetic-benchmark');k.verify(h,'synthetic-benchmark');print('Argon2id hash+verify ms',round((perf_counter()-t)*1000,1))" subprocess.run(["docker","compose","exec","-T","api","python","-c",bench],check=True) client=Client(base,str(pki.local/"ca.crt")) for i in range(65): result=client.call("POST","/api/v1/auth/sessions",[401,429]) if result["status"]==429: assert int(result["headers"]["Retry-After"])>0 print("PASS: shared login limit with Retry-After") break else: raise RuntimeError("Missing rate limit")