ЛР7 - БВТ2302 - Зюлина - на 3
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
"""Measure the selected Argon2id cost using only synthetic input."""
|
||||
from statistics import median
|
||||
from time import perf_counter
|
||||
|
||||
from argon2 import PasswordHasher, Type
|
||||
|
||||
memory_kib, iterations, parallelism = 19456, 2, 1
|
||||
hasher = PasswordHasher(time_cost=iterations, memory_cost=memory_kib,
|
||||
parallelism=parallelism, hash_len=32, salt_len=16,
|
||||
type=Type.ID)
|
||||
samples = []
|
||||
for _ in range(7):
|
||||
started = perf_counter()
|
||||
hasher.hash("synthetic-lab7-password")
|
||||
samples.append((perf_counter() - started) * 1000)
|
||||
print(f"algorithm=Argon2id memory_kib={memory_kib} iterations={iterations} parallelism={parallelism}")
|
||||
print(f"samples_ms={','.join(f'{sample:.1f}' for sample in samples)} median_ms={median(samples):.1f}")
|
||||
@@ -0,0 +1,12 @@
|
||||
"""Apply worker's narrow table grant after migrations have created attachments."""
|
||||
import os
|
||||
import psycopg2
|
||||
|
||||
conn = psycopg2.connect(os.environ["DATABASE_URL"], sslmode="verify-full",
|
||||
sslrootcert=os.getenv("DB_CA_FILE", "/run/tls/service-ca.crt"))
|
||||
try:
|
||||
with conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE attachments TO messenger_worker")
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Exercise the per-replica mTLS sidecar from its isolated network namespace."""
|
||||
import ssl
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import os
|
||||
|
||||
BASE = os.getenv("MTLS_PROBE_URL", "https://api1:8443/health/live")
|
||||
|
||||
|
||||
def probe(cert=None, key=None):
|
||||
context = ssl.create_default_context(cafile="/run/tls/service-ca.crt")
|
||||
context.check_hostname = True
|
||||
if cert:
|
||||
context.load_cert_chain(cert, key)
|
||||
try:
|
||||
with urllib.request.urlopen(BASE, context=context, timeout=4) as response:
|
||||
return response.status
|
||||
except urllib.error.HTTPError as exc:
|
||||
return exc.code
|
||||
|
||||
|
||||
def rejected_at_client_auth(cert=None, key=None):
|
||||
try:
|
||||
status = probe(cert, key)
|
||||
except urllib.error.URLError as exc:
|
||||
if not isinstance(exc.reason, ssl.SSLError):
|
||||
raise SystemExit("FAIL: client-auth probe failed for a non-TLS reason") from exc
|
||||
return True
|
||||
# Nginx may complete the TLS handshake and answer 400 before proxying when
|
||||
# ssl_verify_client is on and the peer sends no acceptable certificate.
|
||||
return status == 400
|
||||
|
||||
if not rejected_at_client_auth():
|
||||
raise SystemExit("FAIL: API sidecar accepted a connection without a client certificate")
|
||||
print("PASS: no client certificate rejected")
|
||||
|
||||
if not rejected_at_client_auth("/test/foreign-client.crt", "/test/foreign-client.key"):
|
||||
raise SystemExit("FAIL: API sidecar accepted a foreign-CA client certificate")
|
||||
print("PASS: foreign CA rejected")
|
||||
|
||||
if probe("/test/wrong-lb-client.crt", "/test/wrong-lb-client.key") != 403:
|
||||
raise SystemExit("FAIL: trusted but wrong client identity was not denied with 403")
|
||||
print("PASS: wrong identity from the trusted LB CA denied")
|
||||
|
||||
if probe("/test/lb-client.crt", "/test/lb-client.key") != 200:
|
||||
raise SystemExit("FAIL: allowlisted LB identity did not reach API")
|
||||
print("PASS: allowlisted LB identity accepted")
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
DOCKER_COMMAND=${DOCKER_COMMAND:-docker}
|
||||
DOCKER_ROOT=$ROOT
|
||||
case "$DOCKER_COMMAND" in *.exe) DOCKER_ROOT=$(wslpath -w "$ROOT") ;; esac
|
||||
compose() { "$DOCKER_COMMAND" compose --env-file "$DOCKER_ROOT/.env" -f "$DOCKER_ROOT/compose.yaml" "$@"; }
|
||||
|
||||
# Stop ingress/apps before stores so no new requests are admitted while state
|
||||
# services restart. Persistent Compose volumes are retained; OpenBao stays up
|
||||
# so its operator-only unseal shares are never needed by this hook.
|
||||
compose stop lb api1-mtls api2-mtls api1 api2 worker db redis redis-broker minio
|
||||
compose start db redis redis-broker minio
|
||||
|
||||
wait_healthy() {
|
||||
service=$1
|
||||
container=$(compose ps -q "$service")
|
||||
[ -n "$container" ] || { echo "Missing container: $service" >&2; exit 1; }
|
||||
tries=0
|
||||
while [ "$("$DOCKER_COMMAND" inspect --format '{{.State.Health.Status}}' "$container" 2>/dev/null || true)" != healthy ]; do
|
||||
tries=$((tries + 1))
|
||||
[ "$tries" -lt 90 ] || { echo "Timed out waiting for $service" >&2; exit 1; }
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
wait_healthy db
|
||||
wait_healthy minio
|
||||
wait_healthy redis-broker
|
||||
sleep 2
|
||||
compose start api1 api2 worker api1-mtls api2-mtls lb
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT"
|
||||
DOCKER_COMMAND=${DOCKER_COMMAND:-docker}
|
||||
DOCKER_ROOT=$ROOT
|
||||
case "$DOCKER_COMMAND" in *.exe) DOCKER_ROOT=$(wslpath -w "$ROOT") ;; esac
|
||||
compose() { "$DOCKER_COMMAND" compose --env-file "$DOCKER_ROOT/.env" -f "$DOCKER_ROOT/compose.yaml" "$@"; }
|
||||
|
||||
case "${TARGET_INSTANCE_ID:?test provides an API replica alias}" in
|
||||
replica-a) compose stop api1-mtls api1 ;;
|
||||
replica-b) compose stop api2-mtls api2 ;;
|
||||
*) echo 'Unknown API replica alias' >&2; exit 2 ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user