lab7: безопасность, mTLS, OpenBao и PKI lifecycle

This commit is contained in:
Vladislav Kunetskiy
2026-11-10 10:58:07 +03:00
parent 3d216fbd50
commit 3441f2269a
183 changed files with 216667 additions and 3165 deletions
+34 -31
View File
@@ -12,7 +12,6 @@ set -eu
ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT_DIR"
COMPOSE_BIN=${COMPOSE_BIN:-docker compose}
PYTHON=${PYTHON:-python3}
REPLICAS=${REPLICAS:-"1 2 4"}
TOTAL_CPU=${TOTAL_CPU:-2.0}
@@ -20,7 +19,8 @@ TOTAL_MEM_MB=${TOTAL_MEM_MB:-1024}
DURATION=${DURATION:-60}
CONCURRENCY=${CONCURRENCY:-16}
WARMUP=${WARMUP:-15}
BASE_URL=${BASE_URL:-http://localhost:8080}
BASE_URL=$("$PYTHON" -c 'import sys; sys.path.insert(0, "scripts"); from lab7_http import BASE_URL; print(BASE_URL)')
CA_FILE=$("$PYTHON" -c 'import sys; sys.path.insert(0, "scripts"); from lab7_http import CA_FILE; print(CA_FILE)')
LABEL=${LABEL:-}
OUT=${OUT:-evidence/track1}
@@ -74,14 +74,13 @@ NO_PROXY_ENV="$NO_PROXY_ENV NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,
mkdir -p "$OUT"
compose() {
# shellcheck disable=SC2086
$COMPOSE_BIN --env-file .env -f compose.yaml "$@"
"$PYTHON" student-tests/tests_helpers.py compose "$@"
}
wait_ready() {
attempt=0
while [ "$attempt" -lt 90 ]; do
if curl -fsS --noproxy '*' -o /dev/null "$BASE_URL/health/ready"; then
if curl -fsS --noproxy '*' --cacert "$CA_FILE" -o /dev/null "$BASE_URL/health/ready"; then
return 0
fi
@@ -106,42 +105,47 @@ container_of() {
compose ps -q "$1"
}
# Сеть стенда: клиент внутри VM обращается к балансировщику по ней, без проброса
# порта и без сетевого стека хоста.
stand_network() {
docker inspect "$(container_of api | head -1)" \
--format '{{range $name, $network := .NetworkSettings.Networks}}{{$name}}{{end}}'
# The VM client shares the LB namespace but mounts only public tests and CA.
lb_container() {
container_of lb
}
# $1 — длительность, $2 — путь к JSON-метрикам (необязательно).
load_client() {
if [ "$CLIENT" = vm ]; then
network=$(stand_network)
lb=$(lb_container)
ca=$("$PYTHON" -c 'import pathlib,sys; print(pathlib.Path(sys.argv[1]).resolve(strict=True))' "$CA_FILE")
out=$("$PYTHON" -c 'import pathlib,sys; print(pathlib.Path(sys.argv[1]).resolve())' "$OUT")
set -- "$1" "$2"
if [ -n "$2" ]; then
# shellcheck disable=SC2086
docker run --rm -i --network "$network" -v "$ROOT_DIR:/repo:ro" -v "$ROOT_DIR/$OUT:/out" \
-e NO_PROXY='lb,localhost,127.0.0.1' -e no_proxy='lb,localhost,127.0.0.1' \
"$CLIENT_IMAGE" python /repo/tests/load.py --base-url http://lb \
--duration "$1" --concurrency "$CONCURRENCY" --output "/out/$(basename "$2")"
destination="/out/$(basename "$2")"
set -- "$1" --output "$destination"
else
# shellcheck disable=SC2086
docker run --rm -i --network "$network" -v "$ROOT_DIR:/repo:ro" \
-e NO_PROXY='lb,localhost,127.0.0.1' -e no_proxy='lb,localhost,127.0.0.1' \
"$CLIENT_IMAGE" python /repo/tests/load.py --base-url http://lb \
--duration "$1" --concurrency "$CONCURRENCY"
set -- "$1"
fi
seconds=$1
shift
docker run --rm --network "container:$lb" \
--cap-drop ALL --security-opt no-new-privileges:true --read-only --tmpfs /tmp \
--mount "type=bind,source=$ROOT_DIR/tests,target=/repo/tests,readonly" \
--mount "type=bind,source=$ROOT_DIR/contracts,target=/repo/contracts,readonly" \
--mount "type=bind,source=$ROOT_DIR/lab.json,target=/repo/lab.json,readonly" \
--mount "type=bind,source=$ca,target=/ca.crt,readonly" \
--mount "type=bind,source=$out,target=/out" \
-e NO_PROXY=localhost,127.0.0.1 -e PYTHONDONTWRITEBYTECODE=1 \
"$CLIENT_IMAGE" python /repo/tests/load.py --base-url https://localhost:8443 \
--ca-file /ca.crt --duration "$seconds" --concurrency "$CONCURRENCY" "$@"
return
fi
if [ -n "$2" ]; then
# shellcheck disable=SC2086
$NO_PROXY_ENV $PYTHON tests/load.py --base-url "$BASE_URL" \
$NO_PROXY_ENV $PYTHON tests/load.py --base-url "$BASE_URL" --ca-file "$CA_FILE" \
--duration "$1" --concurrency "$CONCURRENCY" --output "$2"
else
# shellcheck disable=SC2086
$NO_PROXY_ENV $PYTHON tests/load.py --base-url "$BASE_URL" \
$NO_PROXY_ENV $PYTHON tests/load.py --base-url "$BASE_URL" --ca-file "$CA_FILE" \
--duration "$1" --concurrency "$CONCURRENCY"
fi
}
@@ -161,8 +165,7 @@ container_mem() {
wait_postgres_ready() {
attempt=0
while [ "$attempt" -lt 90 ]; do
if compose exec -T postgres pg_isready -U "${POSTGRES_USER:-messenger}" \
-d "${POSTGRES_DB:-messenger}" >/dev/null 2>&1; then
if printf 'SELECT 1;' | "$PYTHON" student-tests/tests_helpers.py psql >/dev/null 2>&1; then
return 0
fi
@@ -216,7 +219,7 @@ if [ ! -f "$SUMMARY" ]; then
echo "# each profile line below records the budget it was run with"
docker version --format 'docker engine {{.Server.Version}}'
# shellcheck disable=SC2086
$COMPOSE_BIN version
compose version
echo
} > "$SUMMARY"
fi
@@ -236,7 +239,7 @@ for replicas in $REPLICAS; do
apply_store_limits
API_REPLICAS="$replicas" API_CPU_LIMIT="$cpu" API_MEM_LIMIT="$mem" compose up -d --no-deps api
API_REPLICAS="$replicas" API_CPU_LIMIT="$cpu" API_MEM_LIMIT="$mem" compose up -d --no-deps --scale "api=$replicas" api
wait_ready
# Warm up caches, connection pools and the JIT-free query plans; the warmup
@@ -289,11 +292,11 @@ for replicas in $REPLICAS; do
# Shared-dependency view right after the run: connections, pool pressure and
# how much history the profile wrote into.
compose exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -Atc \
"SELECT 'postgres_connections=' || count(*) FROM pg_stat_activity;
printf '%s\n' "SELECT 'postgres_connections=' || count(*) FROM pg_stat_activity;
SELECT 'postgres_max_connections=' || current_setting('max_connections');
SELECT 'messages_rows=' || count(*) FROM messages;
SELECT 'idempotency_rows=' || count(*) FROM message_idempotency;" | tee -a "$SUMMARY"
SELECT 'idempotency_rows=' || count(*) FROM message_idempotency;" \
| "$PYTHON" student-tests/tests_helpers.py psql | tee -a "$SUMMARY"
printf '\n' >> "$SUMMARY"
done