From 627b7ee42f87d6f1694c28f5b6f9211d19c9e863 Mon Sep 17 00:00:00 2001 From: CharaWein Date: Thu, 12 Nov 2026 16:51:45 +0300 Subject: [PATCH] =?UTF-8?q?lab7:=20=D1=82=D0=BE=D0=BF=D0=BE=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D1=8F=20RabbitMQ=20=D1=83=20=D0=BE=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B0=20(import=5Fdefinitions),?= =?UTF-8?q?=20relay=20=E2=80=94=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20p?= =?UTF-8?q?ublish;=20=D0=B4=D0=B8=D0=B0=D0=B3=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BA=D0=B0=20=D1=80=D0=BE=D1=82=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?,=20=D0=BB=D0=BE=D0=B3=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=20=D0=B4=D0=BE=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D0=B0=D1=82=D0=B5=D0=BB=D1=8C=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/lab7-stand.yml | 1 + compose.yaml | 3 ++- infra/rabbitmq/definitions.json | 17 +++++++++++++++++ scripts/bootstrap.py | 9 +++++---- student-tests/observability_ops.py | 11 +++++++---- student-tests/security_ops.py | 15 ++++++++++++++- 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 infra/rabbitmq/definitions.json diff --git a/.github/workflows/lab7-stand.yml b/.github/workflows/lab7-stand.yml index ed3d082..7c3994d 100644 --- a/.github/workflows/lab7-stand.yml +++ b/.github/workflows/lab7-stand.yml @@ -99,6 +99,7 @@ jobs: docker compose ps -a > evidence/docker_ps_final_${{ matrix.part }}.txt || true for c in $(docker compose ps -aq); do docker inspect -f '{{.Name}} restarts={{.RestartCount}} oom_killed={{.State.OOMKilled}} exit={{.State.ExitCode}}' "$c"; done > evidence/containers_state_${{ matrix.part }}.txt || true docker compose logs --no-color --tail 300 > evidence/compose_logs_${{ matrix.part }}.log 2>&1 || true + docker compose logs --no-color api worker relay 2>&1 | grep -v '"msg":"http request"' | tail -3000 > evidence/app_logs_${{ matrix.part }}.log || true - uses: actions/upload-artifact@v4 if: always() with: diff --git a/compose.yaml b/compose.yaml index 80a9cd4..09738aa 100644 --- a/compose.yaml +++ b/compose.yaml @@ -326,6 +326,7 @@ services: - rabbitmq_data:/var/lib/rabbitmq - ./infra/rabbitmq/rabbitmq.conf:/etc/rabbitmq/conf.d/30-lab7.conf:ro - ./infra/rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins:ro + - ./infra/rabbitmq/definitions.json:/etc/rabbitmq/lab7-definitions.json:ro secrets: - { source: ca_crt, target: ca.crt } - { source: rabbitmq_crt, target: rabbitmq.crt } @@ -354,7 +355,7 @@ services: OTEL_SERVICE_NAME: messenger-relay BAO_ROLE: relay BAO_KV_PATH: messenger/relay - BROKER_DECLARE: "true" + BROKER_DECLARE: "false" # топология — у оператора (definitions.json) FAULT_POINTS: ${RELAY_FAULT_POINTS:-} secrets: - { source: ca_crt, target: ca.crt } diff --git a/infra/rabbitmq/definitions.json b/infra/rabbitmq/definitions.json new file mode 100644 index 0000000..2797919 --- /dev/null +++ b/infra/rabbitmq/definitions.json @@ -0,0 +1,17 @@ +{ + "rabbit_version": "4.0.9", + "vhosts": [{"name": "/"}], + "exchanges": [ + {"name": "attachments", "vhost": "/", "type": "direct", "durable": true, "auto_delete": false, "internal": false, "arguments": {}}, + {"name": "attachments.dlx", "vhost": "/", "type": "fanout", "durable": true, "auto_delete": false, "internal": false, "arguments": {}} + ], + "queues": [ + {"name": "attachment.dead", "vhost": "/", "durable": true, "auto_delete": false, "arguments": {"x-queue-type": "quorum"}}, + {"name": "attachment.process", "vhost": "/", "durable": true, "auto_delete": false, + "arguments": {"x-queue-type": "quorum", "x-dead-letter-exchange": "attachments.dlx", "x-delivery-limit": 10}} + ], + "bindings": [ + {"source": "attachments", "vhost": "/", "destination": "attachment.process", "destination_type": "queue", "routing_key": "process", "arguments": {}}, + {"source": "attachments.dlx", "vhost": "/", "destination": "attachment.dead", "destination_type": "queue", "routing_key": "", "arguments": {}} + ] +} diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py index a5a9894..a360fca 100644 --- a/scripts/bootstrap.py +++ b/scripts/bootstrap.py @@ -191,10 +191,11 @@ def rabbit_users(worker_cn='worker-g1', relay_cn='relay-g1', drop=()): names = {u['user'] for u in json.loads(users or '[]')} if 'guest' in names: ctl('delete_user', 'guest') - perms = {relay_cn: (r'^(attachments|attachments\.dlx|attachment\.process|attachment\.dead)$', - r'^(attachments|attachments\.dlx|attachment\.process|attachment\.dead)$', - r'^(attachments|attachments\.dlx)$'), - worker_cn: ('^$', '^$', r'^attachment\.process$')} + # Топологию (exchange, quorum-очереди, DLX, bindings) создаёт оператор импортом + # infra/rabbitmq/definitions.json; сервисам configure не нужен вовсе. + ctl('import_definitions', '/etc/rabbitmq/lab7-definitions.json') + perms = {relay_cn: ('^$', r'^attachments$', '^$'), # только publish в exchange attachments + worker_cn: ('^$', '^$', r'^attachment\.process$')} # только чтение своей очереди for u, (conf, write, read) in perms.items(): if u not in names: # Пароль случайный и сразу удаляется: вход только SASL EXTERNAL (сертификат). diff --git a/student-tests/observability_ops.py b/student-tests/observability_ops.py index 70b7503..f15052b 100644 --- a/student-tests/observability_ops.py +++ b/student-tests/observability_ops.py @@ -298,11 +298,14 @@ def breakdown(spans): srv = next((s for s in spans if s['parent'] == '' or s['name'].startswith(('POST /api', 'GET /api'))), None) if not srv: return None - db = sum(s['ms'] for s in spans if s['service'] == srv['service'] and is_db(s)) - s3 = sum(s['ms'] for s in spans if s['name'].startswith('S3')) + # DB-spans вложены (BEGIN/batch/query): сумма перекрывается, поэтому берём самый + # длинный отдельный span БД относительно server span. + dbs = sorted(((s['name'], s['ms']) for s in spans if s['service'] == srv['service'] and is_db(s)), key=lambda x: -x[1]) + db = dbs[0][1] if dbs else 0.0 + s3 = max([s['ms'] for s in spans if s['name'].startswith('S3')] or [0.0]) return {'server_span': srv['name'], 'server_ms': srv['ms'], 'status': srv['attrs'].get('http.response.status_code'), - 'db_ms': round(db, 1), 's3_ms': round(s3, 1), 'db_share': round(db / srv['ms'], 3) if srv['ms'] else None, - 'db_spans': [(s['name'], s['ms']) for s in spans if is_db(s)][:6]} + 'longest_db_span_ms': round(db, 1), 'longest_s3_span_ms': round(s3, 1), + 'db_share': round(db / srv['ms'], 3) if srv['ms'] else None, 'db_spans_longest': dbs[:4]} def scenario_incident_db(c): diff --git a/student-tests/security_ops.py b/student-tests/security_ops.py index 951ac4b..477d934 100644 --- a/student-tests/security_ops.py +++ b/student-tests/security_ops.py @@ -353,7 +353,20 @@ def scenario_rotation(c): time.sleep(35) # pgx health check (30 s) выбрасывает разорванные соединения res['db_old_password'] = probe('db', '-user', 'app_api', '-stdin', stdin=old_pw).get('connect') check(denied(res['db_old_password']), 'old db password still works') - res['api_after_db_rotation'] = c.call('GET', '/api/v1/chats', actor=alice)['status'] + tries = [] + for _ in range(12): + try: + tries.append(c.call('GET', '/api/v1/chats', (200, 503), actor=alice)['status']) + except Failure as e: + tries.append(str(e)[-40:]) + if tries[-1] == 200: + break + time.sleep(5) + res['api_after_db_rotation_attempts'] = tries + if tries[-1] != 200: + res['api_secret_events'] = [l[-220:] for l in compose('logs', '--no-color', '--since', '5m', 'api').stdout.splitlines() + if 'secrets.' in l or 'postgres' in l.lower()][-15:] + check(tries[-1] == 200, 'API did not recover after DB password rotation: %s' % res) # 2) ключ S3 worker: загрузка после ротации обрабатывается bootstrap('rotate-s3', 'worker') time.sleep(35)