feat: secure messenger ingress and rate-limit logins
This commit is contained in:
Executable
+45
@@ -0,0 +1,45 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
umask 077
|
||||
dir=secrets/tls
|
||||
mkdir -p "$dir"
|
||||
chmod 700 "$dir"
|
||||
|
||||
make_ca() {
|
||||
name=$1
|
||||
if [ ! -e "$dir/$name.crt" ]; then
|
||||
openssl req -x509 -newkey ed25519 -nodes -days 365 \
|
||||
-subj "/CN=$name" -addext "basicConstraints=critical,CA:TRUE,pathlen:0" \
|
||||
-addext "keyUsage=critical,keyCertSign,cRLSign" \
|
||||
-keyout "$dir/$name.key" -out "$dir/$name.crt"
|
||||
fi
|
||||
}
|
||||
|
||||
make_leaf() {
|
||||
name=$1 ca=$2 usage=$3 san=$4
|
||||
if [ ! -e "$dir/$name.crt" ]; then
|
||||
openssl req -new -newkey ed25519 -nodes -subj "/CN=$name" \
|
||||
-keyout "$dir/$name.key" -out "$dir/$name.csr"
|
||||
printf 'basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature\nextendedKeyUsage=%s\nsubjectAltName=%s\nsubjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid:always\n' \
|
||||
"$usage" "$san" > "$dir/$name.ext"
|
||||
openssl x509 -req -in "$dir/$name.csr" -CA "$dir/$ca.crt" -CAkey "$dir/$ca.key" \
|
||||
-CAcreateserial -days 30 -extfile "$dir/$name.ext" -out "$dir/$name.crt"
|
||||
rm "$dir/$name.ext" "$dir/$name.csr"
|
||||
fi
|
||||
# Containers can only read files individually mounted into them.
|
||||
chmod 644 "$dir/$name.crt" "$dir/$name.key"
|
||||
}
|
||||
|
||||
make_ca lab7-ca
|
||||
make_ca untrusted-ca
|
||||
chmod 644 "$dir/lab7-ca.crt"
|
||||
make_leaf ingress lab7-ca serverAuth DNS:localhost,IP:127.0.0.1
|
||||
make_leaf api lab7-ca serverAuth DNS:api
|
||||
make_leaf lb lab7-ca clientAuth URI:urn:lab7:load-balancer
|
||||
make_leaf wrong-role lab7-ca clientAuth URI:urn:lab7:probe
|
||||
make_leaf wrong-ca untrusted-ca clientAuth URI:urn:lab7:load-balancer
|
||||
for name in ingress lb; do
|
||||
cat "$dir/$name.crt" "$dir/$name.key" > "$dir/$name.pem"
|
||||
chmod 644 "$dir/$name.pem"
|
||||
done
|
||||
@@ -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()
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run against a quiet test ingress after at least 60 seconds without logins."""
|
||||
import argparse
|
||||
import ssl
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="https://localhost:8443")
|
||||
parser.add_argument("--ca-file", default="secrets/tls/lab7-ca.crt")
|
||||
args = parser.parse_args()
|
||||
context = ssl.create_default_context(cafile=args.ca_file)
|
||||
instances = set()
|
||||
for attempt in range(1, 14):
|
||||
request = Request(
|
||||
args.base_url.rstrip("/") + "/api/v1/auth/sessions",
|
||||
data=b"",
|
||||
headers={"X-Forwarded-For": f"192.0.2.{attempt}"},
|
||||
)
|
||||
try:
|
||||
with urlopen(request, context=context, timeout=5):
|
||||
raise AssertionError("Missing credentials unexpectedly accepted")
|
||||
except HTTPError as error:
|
||||
with error:
|
||||
instance = error.headers["X-Instance-Id"]
|
||||
assert instance, "Missing API instance identity"
|
||||
instances.add(instance)
|
||||
if attempt <= 12:
|
||||
assert error.code == 401, f"Attempt {attempt}: expected 401, got {error.code}"
|
||||
else:
|
||||
assert error.code == 429, f"Expected 429, got {error.code}"
|
||||
assert 0 < int(error.headers["Retry-After"]) <= 60
|
||||
assert len(instances) >= 2, "Expected requests to reach multiple API replicas"
|
||||
print(f"PASS: request 13 returned 429 with Retry-After across {len(instances)} API replicas")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+1
-1
@@ -26,7 +26,7 @@ def main():
|
||||
POSTGRES_USER="messenger",
|
||||
POSTGRES_PASSWORD=secrets.token_hex(24),
|
||||
)
|
||||
defaults.update(API_HTTP_PORT="8080", S3_ENDPOINT="http://s3:9000",
|
||||
defaults.update(API_HTTPS_PORT="8443", S3_ENDPOINT="http://s3:9000",
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT="http://otel-collector:4318", UPLOAD_CLEANUP_GRACE_SECONDS="600", UPLOAD_TMP_DIR="/tmp/uploads",
|
||||
S3_PUBLIC_ENDPOINT="http://127.0.0.1:9000",
|
||||
S3_CORS_ORIGINS="http://127.0.0.1:5173,http://localhost:5173")
|
||||
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
port=$(docker compose port haproxy 8443)
|
||||
for attempt in $(seq 1 30); do
|
||||
if curl --silent --fail --max-time 2 --cacert secrets/tls/lab7-ca.crt \
|
||||
"https://localhost:${port##*:}/health/ready" >/dev/null; then
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
printf 'HTTPS ingress did not become ready\n' >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user