lab7: Argon2id из env (замер bench/argon2_bench.py), лимит неудачных входов в Valkey (429 + Retry-After), cap_drop ALL и no-new-privileges, X-Instance-Id = случайный alias
This commit is contained in:
+11
-1
@@ -13,6 +13,7 @@ from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
@@ -33,6 +34,13 @@ def run(*args: str, timeout: float = 60) -> str:
|
||||
return result.stdout
|
||||
|
||||
|
||||
def instance_alias(container_id: str) -> str | None:
|
||||
"""X-Instance-Id of the process running in the container (lab 7: random alias, not the hostname)."""
|
||||
result = subprocess.run(["docker", "exec", container_id, "cat", "/tmp/instance_id"], capture_output=True,
|
||||
text=True, timeout=30, env={**os.environ, "MSYS_NO_PATHCONV": "1"})
|
||||
return (result.stdout.strip() or None) if result.returncode == 0 else None
|
||||
|
||||
|
||||
def replicas(all_states: bool = True) -> list[dict]:
|
||||
"""API containers of this compose project (running and stopped)."""
|
||||
ids = run("docker", "compose", "ps", "-q", *(["-a"] if all_states else []), "api").split()
|
||||
@@ -46,6 +54,8 @@ def replicas(all_states: bool = True) -> list[dict]:
|
||||
"id": item["Id"][:12],
|
||||
"name": item["Name"].lstrip("/"),
|
||||
"hostname": item["Config"]["Hostname"],
|
||||
# Lab 7: X-Instance-Id is a random alias the process writes to /tmp/instance_id.
|
||||
"instance": instance_alias(item["Id"][:12]) if item["State"]["Status"] == "running" else None,
|
||||
"state": item["State"]["Status"],
|
||||
"health": (item["State"].get("Health") or {}).get("Status", "-"),
|
||||
"exit_code": item["State"]["ExitCode"],
|
||||
@@ -75,7 +85,7 @@ def lb_servers() -> list[dict]:
|
||||
container = by_ip.get(addr)
|
||||
servers.append({"slot": name, "addr": addr, "state": state,
|
||||
"container": container["name"] if container else None,
|
||||
"instance_id": container["hostname"] if container else None})
|
||||
"instance_id": container["instance"] if container else None})
|
||||
return servers
|
||||
|
||||
|
||||
|
||||
+8
-5
@@ -1,14 +1,17 @@
|
||||
#!/bin/sh
|
||||
# Failover hook for `make test-failover ACTION_SCRIPT=scripts/stop-one.sh`.
|
||||
# Stops exactly ONE API replica: the container whose X-Instance-Id (= container hostname,
|
||||
# see src/messenger/config.py) is in TARGET_INSTANCE_ID. DB, Valkey, LB and volumes are
|
||||
# not touched. The replica is LEFT STOPPED (`docker stop` = no restart by the policy);
|
||||
# restore it explicitly afterwards: scripts/restore-api.sh.
|
||||
# Stops exactly ONE API replica: the container whose X-Instance-Id is TARGET_INSTANCE_ID.
|
||||
# Lab 7: X-Instance-Id is a random alias (no hostname/IP, src/messenger/config.py); each replica
|
||||
# writes its alias to /tmp/instance_id inside its own container, which is how the operator maps
|
||||
# alias -> container. DB, Valkey, LB, OpenBao and volumes are not touched. The replica is LEFT
|
||||
# STOPPED (`docker stop` = no restart by the policy); restore it explicitly afterwards:
|
||||
# scripts/restore-api.sh (make restore).
|
||||
# STOP_MODE=stop (default) SIGTERM: readiness 503, 3 s drain, graceful exit (~4 s)
|
||||
# STOP_MODE=kill SIGKILL at once (`docker stop -t 0`): crash of the process
|
||||
# Returns within 10 s. The test hides the output; run by hand to see it (no secrets printed).
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
export MSYS_NO_PATHCONV=1 # Git Bash: keep /tmp/instance_id a container path
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
*[!A-Za-z0-9._-]*) echo "stop-one: invalid TARGET_INSTANCE_ID" >&2; exit 2 ;;
|
||||
@@ -18,7 +21,7 @@ mode="${STOP_MODE:-stop}"
|
||||
target=""
|
||||
# Only RUNNING containers of THIS compose project's service `api` are candidates.
|
||||
for id in $(docker compose ps -q api); do
|
||||
if [ "$(docker inspect -f '{{.Config.Hostname}}' "$id")" = "$TARGET_INSTANCE_ID" ]; then
|
||||
if [ "$(docker exec "$id" cat /tmp/instance_id 2>/dev/null | tr -d '\r\n')" = "$TARGET_INSTANCE_ID" ]; then
|
||||
target="$id"
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user