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
+110 -119
View File
@@ -1,32 +1,100 @@
PYTHON ?= python3
GO ?= go
VERSION ?= $(shell git describe --always --dirty 2>/dev/null || echo dev)
export VERSION
# Lab1–lab6 base still serves plain HTTP on 8080; the lab7 implementation moves
# the stand to the contract default https://localhost:8443.
BASE_URL ?= http://localhost:8080
CA_FILE ?=
BASE_URL ?= https://localhost:8443
LAB7_SECRETS_DIR ?= secrets/lab7
CA_FILE ?= $(LAB7_SECRETS_DIR)/pki/operator/current/ca.crt
ACTION_SCRIPT ?=
PIPELINE ?= 2
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
ROLE ?= api
CERT_TTL ?= 1h
TLS_ARGS = --ca-file "$(CA_FILE)"
COMPOSE_BIN ?= docker compose
COMPOSE_FILE ?= compose.yaml
ENV_FILE ?= .env
COMPOSE = $(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE)
PYTHON_LOCAL = env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1 $(PYTHON)
LOCAL_BIN := $(CURDIR)/bin
GOLANGCI_LINT_VERSION ?= v2.15.0
GOLANGCI_LINT := $(LOCAL_BIN)/golangci-lint
OPENAPI_SPEC := contracts/openapi.json
OGEN_CONFIG := .ogen.yml
OAS_DIR := internal/transport/rest/internal/oas
.PHONY: help check validate test test-attachments test-presign load test-persistence test-failover test-security smoke-local load-local test-idempotency test-lb-readiness test-observability test-slo up up-base init-secrets down ps logs
-include .env
export
ENV_KEYS := $(shell sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' .env 2>/dev/null)
CLEAN_ENV = env $(addprefix -u ,$(ENV_KEYS))
.DEFAULT_GOAL := help
.PHONY: help check validate prepare build bootstrap up down ps logs test test-security test-lab7 test-pki auth-budget pki-issue pki-renew renew-watch bao-unseal test-attachments test-presign test-queue test-faults test-track2 test-observability test-slo test-idempotency test-lb-readiness load test-persistence test-failover jobs-failed replay reprocess unit-test integration-test test-db-up test-db-down dev-up dev-down dev-reload migrate generate generate-api generate-mocks sqlc cover lint
help:
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
@echo "up: lab6 stand with telemetry profile; up-base: lab5 regression without telemetry"
@echo "down/ps/logs: same profile; test-observability/test-slo: lab6 checks"
@echo "prepare/build/up: local PKI, image, operator Bao init/unseal and full verified-TLS stand"
@echo "test/test-security/test-lab7: public HTTPS, external trust, complete internal security probes"
@echo "test-pki: 3 isolated lifecycle experiments; auth-budget: measured Argon2/login budget"
@echo "pki-issue/pki-renew/renew-watch ROLE=api CERT_TTL=1h; bao-unseal: operator recovery"
check:
$(PYTHON) scripts/check_contract.py
$(PYTHON) scripts/check_s3_creds.py
validate:
$(PYTHON) scripts/validate_openapi.py
prepare:
$(PYTHON_LOCAL) scripts/securityctl.py prepare
build: prepare
$(COMPOSE) build api
bootstrap:
$(PYTHON_LOCAL) scripts/securityctl.py bootstrap
up: build
$(PYTHON_LOCAL) scripts/securityctl.py up
down:
$(COMPOSE) down
ps:
$(COMPOSE) ps
logs:
$(COMPOSE) logs -f --tail 100
bao-unseal:
$(PYTHON_LOCAL) scripts/securityctl.py unseal
pki-issue:
$(PYTHON_LOCAL) scripts/securityctl.py issue "$(ROLE)" --ttl "$(CERT_TTL)"
pki-renew:
$(PYTHON_LOCAL) scripts/securityctl.py renew "$(ROLE)" --ttl "$(CERT_TTL)"
renew-watch:
$(PYTHON_LOCAL) scripts/securityctl.py renew "$(ROLE)" --ttl "$(CERT_TTL)" --watch --interval 1
renewer:
$(PYTHON_LOCAL) scripts/lab7_renewer.py --interval 1800 --ttl "$(CERT_TTL)"
renew-once:
$(PYTHON_LOCAL) scripts/lab7_renewer.py --once --ttl "$(CERT_TTL)" --output evidence/lab7/renewer.json
test:
$(PYTHON_LOCAL) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-security:
$(PYTHON_LOCAL) tests/security.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-lab7:
$(PYTHON_LOCAL) student-tests/lab7_security.py --base-url "$(BASE_URL)" $(TLS_ARGS) --output evidence/lab7/security.json
test-pki:
$(PYTHON_LOCAL) scripts/lab7_track2.py --reps 3 --duration 30 --output evidence/lab7/track2.json
auth-budget:
$(PYTHON_LOCAL) scripts/lab7_auth_benchmark.py --api $(TLS_ARGS) --output evidence/lab7/auth.json
test-attachments:
$(PYTHON_LOCAL) student-tests/attachments_negative.py --base-url "$(BASE_URL)" $(TLS_ARGS)
@@ -42,7 +110,6 @@ test-faults:
test-track2:
$(PYTHON_LOCAL) scripts/lab5_track2.py --base-url "$(BASE_URL)" $(TLS_ARGS)
# Lab6 observability and SLO checks (stand must be running with the telemetry profile).
test-observability:
$(PYTHON_LOCAL) scripts/check_evidence.py
$(PYTHON_LOCAL) scripts/lab6_verify.py --base-url "$(BASE_URL)" $(TLS_ARGS)
@@ -50,154 +117,78 @@ test-observability:
test-slo:
$(PYTHON_LOCAL) scripts/lab6_track2.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-idempotency:
$(PYTHON_LOCAL) student-tests/idempotency_parallel.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-lb-readiness:
scripts/lb_readiness_check.sh
load:
$(PYTHON_LOCAL) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-persistence:
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/restart.sh"; exit 2)
$(PYTHON_LOCAL) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
test-failover:
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/stop-one.sh"; exit 2)
$(PYTHON_LOCAL) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
# Lab7: external endpoint must reject an untrusted CA; internal mTLS is checked
# by the student's own tests per contracts/trust-matrix.md.
test-security:
$(PYTHON_LOCAL) tests/security.py --base-url "$(BASE_URL)" $(TLS_ARGS)
# Локальный запуск без прокси: в окружении автора HTTP(S)_PROXY ломает localhost.
smoke-local:
$(PYTHON_LOCAL) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
load-local:
$(PYTHON_LOCAL) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
# Собственная проверка уровня 4: параллельные повторы одного Idempotency-Key
# через LB обязаны создать ровно одно сообщение.
test-idempotency:
$(PYTHON_LOCAL) student-tests/idempotency_parallel.py --base-url "$(BASE_URL)" $(TLS_ARGS)
# Собственная проверка уровня 4: LB обязан убрать из ротации реплику, которая
# осталась доступной, но потеряла БД (readiness 503).
test-lb-readiness:
scripts/lb_readiness_check.sh
COMPOSE_BIN ?= docker compose
COMPOSE_FILE ?= compose.yaml
ENV_FILE ?= .env
TELEMETRY_PROFILE ?= telemetry
up: init-secrets
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) --profile $(TELEMETRY_PROFILE) up -d --build
# Lab5 regression without the telemetry profile.
up-base: init-secrets
TELEMETRY_ENABLED=0 $(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) up -d --build
init-secrets:
./scripts/init-secrets.sh
down:
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) --profile $(TELEMETRY_PROFILE) down
ps:
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) --profile $(TELEMETRY_PROFILE) ps
logs:
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) --profile $(TELEMETRY_PROFILE) logs -f --tail 100
# Operator views: the failed registry is PostgreSQL, the DLQ is a backstop.
jobs-failed:
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) run --rm --entrypoint /usr/local/bin/jobctl worker failed
$(PYTHON_LOCAL) scripts/securityctl.py jobctl failed
replay:
@test -n "$(JOB)" || (echo "Set JOB=<job_id>"; exit 2)
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) run --rm --entrypoint /usr/local/bin/jobctl worker replay -job "$(JOB)"
$(PYTHON_LOCAL) scripts/securityctl.py jobctl replay -job "$(JOB)"
reprocess:
@test -n "$(ATTACHMENT)" || (echo "Set ATTACHMENT=<attachment_id>"; exit 2)
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f $(COMPOSE_FILE) run --rm --entrypoint /usr/local/bin/jobctl worker reprocess -attachment "$(ATTACHMENT)" -pipeline "$(PIPELINE)"
$(PYTHON_LOCAL) scripts/securityctl.py jobctl reprocess -attachment "$(ATTACHMENT)" -pipeline "$(PIPELINE)"
.DEFAULT_GOAL := help
GO ?= go
APP_NAME ?= messenger
CMD_PKG := ./cmd/$(APP_NAME)
LOCAL_BIN := $(CURDIR)/bin
GOLANGCI_LINT_VERSION ?= v2.13.2
GOLANGCI_LINT := $(LOCAL_BIN)/golangci-lint
OPENAPI_SPEC := contracts/openapi.json
OGEN_CONFIG := .ogen.yml
OAS_DIR := internal/transport/rest/internal/oas
-include .env
export
# .env feeds local tooling, but its application variables must not leak into the
# Go test process: the config tests assert defaults and the dev YAML, so an
# ambient REDIS_ENABLED/CACHE_ENABLED would fail them.
ENV_KEYS := $(shell sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' .env 2>/dev/null)
CLEAN_ENV = env $(addprefix -u ,$(ENV_KEYS))
.PHONY: unit-test
unit-test:
$(CLEAN_ENV) $(GO) test -race -shuffle=on -count=1 -timeout=5m ./...
.PHONY: integration-test
integration-test:
$(GO) test -tags=integration -p 1 -count=1 -timeout=15m ./internal/storage/postgres ./internal/storage/redis
$(PYTHON_LOCAL) scripts/lab7_integration.py
.PHONY: test-db-up test-db-down
test-db-up:
@docker-compose -f docker-compose.local.yaml up -d --wait postgres redis
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f docker-compose.local.yaml up -d --no-build postgres redis
test-db-down:
@docker-compose -f docker-compose.local.yaml down
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f docker-compose.local.yaml down
# не учитываем генеренные файлы, также cmd в подсчете покрытия
COVER_EXCLUDE ?= /(mocks|oas)/|/cmd/
dev-up: build
COMPOSE_FILE=docker-compose.local.yaml $(PYTHON_LOCAL) scripts/securityctl.py up
.PHONY: cover
cover:
$(CLEAN_ENV) $(GO) test -race -shuffle=on -count=1 -timeout=5m \
-covermode=atomic -coverprofile=coverage.out.tmp ./...
@grep -Ev '$(COVER_EXCLUDE)' coverage.out.tmp > coverage.out
@rm coverage.out.tmp
$(GO) tool cover -func=coverage.out | tail -n 1
$(GO) tool cover -html=coverage.out
dev-down:
$(COMPOSE_BIN) --env-file $(ENV_FILE) -f docker-compose.local.yaml down
dev-reload:
COMPOSE_FILE=docker-compose.local.yaml $(PYTHON_LOCAL) scripts/securityctl.py restart
migrate:
$(COMPOSE) run --rm --no-deps migrate
.PHONY: generate
generate: generate-api generate-mocks sqlc
.PHONY: generate-api
generate-api:
$(GO) run ./tools/specgen
$(GO) tool ogen --config $(OGEN_CONFIG) --target $(OAS_DIR) --package oas --clean .cache/openapi.codegen.json
.PHONY: generate-mocks
generate-mocks:
$(GO) tool mockery
.PHONY: sqlc
sqlc:
$(GO) tool sqlc generate
.PHONY: dev-up dev-down dev-reload
dev-up:
@docker-compose -f docker-compose.local.yaml up -d --build
COVER_EXCLUDE ?= /(mocks|oas)/|/cmd/
cover:
$(CLEAN_ENV) $(GO) test -race -shuffle=on -count=1 -timeout=5m -covermode=atomic -coverprofile=coverage.out.tmp ./...
@grep -Ev '$(COVER_EXCLUDE)' coverage.out.tmp > coverage.out
@rm coverage.out.tmp
$(GO) tool cover -func=coverage.out
dev-down:
@docker-compose -f docker-compose.local.yaml down
dev-reload:
@docker-compose -f docker-compose.local.yaml up -d --build --force-recreate
.PHONY: migrate
migrate:
$(GO) run ./cmd/migrate up
$(GOLANGCI_LINT):
$(GOLANGCI_LINT): go.mod Makefile
GOBIN=$(LOCAL_BIN) $(GO) install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
lint: $(GOLANGCI_LINT)