feat: secure messenger ingress and rate-limit logins
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Probe each API replica directly from the Docker host on Linux."""
|
||||
import http.client
|
||||
import json
|
||||
import ssl
|
||||
import socket
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
TLS = ROOT / "secrets/tls"
|
||||
|
||||
|
||||
def check(address, port, identity, error):
|
||||
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, port), timeout=5) as tcp:
|
||||
try:
|
||||
with context.wrap_socket(tcp, server_hostname="api") as connection:
|
||||
connection.sendall(b"GET /health/live HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n")
|
||||
response = http.client.HTTPResponse(connection)
|
||||
response.begin()
|
||||
assert response.status == 200, response.status
|
||||
except ssl.SSLError as exc:
|
||||
if not error or error not in str(exc).lower():
|
||||
raise
|
||||
return
|
||||
if error:
|
||||
raise AssertionError(f"{identity or 'no certificate'} unexpectedly accepted")
|
||||
|
||||
|
||||
def main():
|
||||
config = subprocess.check_output(
|
||||
["docker", "compose", "config", "--format", "json"], cwd=ROOT, text=True
|
||||
)
|
||||
port = int(json.loads(config)["services"]["api"]["environment"]["APP_PORT"])
|
||||
replicas = subprocess.check_output(
|
||||
["docker", "compose", "ps", "-q", "api"], cwd=ROOT, text=True
|
||||
).split()
|
||||
assert len(replicas) >= 2, "Expected at least two running API replicas"
|
||||
for replica in replicas:
|
||||
address = subprocess.check_output(
|
||||
["docker", "inspect", "--format",
|
||||
'{{(index .NetworkSettings.Networks "highload-lab7_proxy").IPAddress}}', replica],
|
||||
text=True,
|
||||
).strip()
|
||||
check(address, port, "lb", None)
|
||||
check(address, port, None, "certificate required")
|
||||
check(address, port, "wrong-ca", "unknown ca")
|
||||
check(address, port, "wrong-role", "bad certificate")
|
||||
print(f"PASS {replica[:12]}: LB permitted; absent, foreign CA and disallowed SAN rejected")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user