Implement lab 7 security and mTLS

This commit is contained in:
2026-11-26 16:55:53 +03:00
parent 20afc2c790
commit ab7b467957
54 changed files with 3240 additions and 25 deletions
+91
View File
@@ -0,0 +1,91 @@
{
"title": "Messenger overview",
"uid": "messenger-overview",
"timezone": "utc",
"schemaVersion": 39,
"version": 1,
"time": {"from": "now-1h", "to": "now"},
"refresh": "10s",
"panels": [
{
"id": 1, "title": "HTTP requests/s by status class", "type": "timeseries",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "sum(rate(http_requests_total{route!~\"/health.*\"}[1m])) by (status_class)", "legendFormat": "{{status_class}}"}],
"fieldConfig": {"defaults": {"unit": "reqps"}, "overrides": []}
},
{
"id": 2, "title": "HTTP error ratio (5xx / all business requests)", "type": "stat",
"gridPos": {"h": 8, "w": 6, "x": 12, "y": 0},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "sum(rate(http_requests_total{status_class=\"5xx\", route!~\"/health.*\"}[5m])) / sum(rate(http_requests_total{route!~\"/health.*\"}[5m]))"}],
"fieldConfig": {"defaults": {"unit": "percentunit", "decimals": 2}, "overrides": []}
},
{
"id": 3, "title": "HTTP duration p50/p95/p99", "type": "timeseries",
"gridPos": {"h": 8, "w": 6, "x": 18, "y": 0},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [
{"expr": "histogram_quantile(0.50, sum(rate(http_request_duration_seconds_bucket{route!~\"/health.*\"}[5m])) by (le))", "legendFormat": "p50"},
{"expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{route!~\"/health.*\"}[5m])) by (le))", "legendFormat": "p95"},
{"expr": "histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket{route!~\"/health.*\"}[5m])) by (le))", "legendFormat": "p99"}
],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 4, "title": "Jobs processed/s by outcome", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 0, "y": 8},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "sum(rate(jobs_processed_total[1m])) by (outcome)", "legendFormat": "{{outcome}}"}],
"fieldConfig": {"defaults": {"unit": "ops"}, "overrides": []}
},
{
"id": 5, "title": "Job processing duration p95", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 8, "y": 8},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "histogram_quantile(0.95, sum(rate(job_processing_duration_seconds_bucket[5m])) by (le))", "legendFormat": "p95 processing"}],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 6, "title": "Time to ready (upload -> ready) p95", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 16, "y": 8},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "histogram_quantile(0.95, sum(rate(time_to_ready_seconds_bucket[5m])) by (le))", "legendFormat": "p95 time-to-ready"}],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 7, "title": "Queue depth (pending jobs)", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 0, "y": 16},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "queue_backlog{metric=\"depth\"}", "legendFormat": "pending"}],
"fieldConfig": {"defaults": {"unit": "short"}, "overrides": []}
},
{
"id": 8, "title": "Oldest pending job age", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 8, "y": 16},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "queue_backlog{metric=\"oldest_age_seconds\"}", "legendFormat": "oldest age"}],
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}
},
{
"id": 9, "title": "DB pool used/idle", "type": "timeseries",
"gridPos": {"h": 8, "w": 8, "x": 16, "y": 16},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "db_pool_connections", "legendFormat": "{{service_instance_id}} {{state}}"}],
"fieldConfig": {"defaults": {"unit": "short"}, "overrides": []}
},
{
"id": 10, "title": "Worker busy/capacity", "type": "timeseries",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 24},
"datasource": {"type": "prometheus", "uid": "Prometheus"},
"targets": [{"expr": "worker_concurrency_slots", "legendFormat": "{{state}}"}],
"fieldConfig": {"defaults": {"unit": "short"}, "overrides": []}
},
{
"id": 11, "title": "Recent API logs", "type": "logs",
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 24},
"datasource": {"type": "loki", "uid": "Loki"},
"targets": [{"expr": "{service_name=\"messenger-api\"}"}]
}
]
}
@@ -0,0 +1,68 @@
apiVersion: 1
groups:
- orgId: 1
name: messenger-slo
folder: Messenger
interval: 30s
rules:
- uid: messenger-high-error-rate
title: High HTTP error rate
condition: C
for: 2m
data:
- refId: A
relativeTimeRange: {from: 300, to: 0}
datasourceUid: Prometheus
model:
expr: sum(rate(http_requests_total{status_class="5xx", route!~"/health.*"}[5m])) / sum(rate(http_requests_total{route!~"/health.*"}[5m]))
instant: true
refId: A
- refId: C
datasourceUid: "__expr__"
model:
type: threshold
expression: A
conditions:
- evaluator: {type: gt, params: [0.05]}
refId: C
labels:
severity: critical
annotations:
summary: "5xx ratio on business requests above 5% for 2 minutes"
description: >
SLI: 5xx / all business requests (excluding /health*), 5m window.
SLO: error ratio < 5%. Runbook: check `db_pool_connections` (DB
saturation?) and recent error-level entries in {service_name="messenger-api"}
in Loki; correlate via trace_id with Tempo to find the failing dependency.
- uid: messenger-growing-queue-lag
title: Growing attachment queue lag
condition: C
for: 2m
data:
- refId: A
relativeTimeRange: {from: 60, to: 0}
datasourceUid: Prometheus
model:
expr: max(queue_backlog{metric="oldest_age_seconds"})
instant: true
refId: A
- refId: C
datasourceUid: "__expr__"
model:
type: threshold
expression: A
conditions:
- evaluator: {type: gt, params: [120]}
refId: C
labels:
severity: warning
annotations:
summary: "Oldest pending attachment job older than 2 minutes"
description: >
SLI: oldest_age_seconds from queue_backlog. SLO: < 120s under
normal load. Runbook: check `worker_concurrency_slots` (busy==capacity
sustained => worker saturated) and whether the worker container is
running (`docker compose ps worker`); check messenger-worker logs
in Loki for repeated processing_failed entries (S3/DB dependency down).
@@ -0,0 +1,11 @@
apiVersion: 1
providers:
- name: messenger
orgId: 1
folder: Messenger
type: file
disableDeletion: false
updateIntervalSeconds: 30
options:
path: /etc/grafana/dashboards
@@ -0,0 +1,29 @@
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
uid: Prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
jsonData:
timeInterval: 5s
- name: Loki
type: loki
uid: Loki
access: proxy
url: http://loki:3100
jsonData:
derivedFields:
- name: TraceID
matcherRegex: "trace_id\":\"(\\w+)\""
url: "$${__value.raw}"
datasourceUid: tempo
- name: Tempo
type: tempo
access: proxy
url: http://tempo:3200
uid: tempo
+29
View File
@@ -0,0 +1,29 @@
auth_enabled: false
server:
http_listen_port: 3100
common:
path_prefix: /loki
storage:
filesystem:
chunks_directory: /loki/chunks
rules_directory: /loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2024-01-01
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
limits_config:
allow_structured_metadata: true
retention_period: 168h
+44
View File
@@ -0,0 +1,44 @@
worker_processes 1;
# A single worker keeps the round-robin counter shared and deterministic for
# the lab's small replica count; a production LB would instead rely on a
# shared/consistent balancing policy across workers.
events { worker_connections 1024; }
http {
upstream messenger_api {
# Passive health check: a backend failing 1 probe is removed for 3s,
# which keeps failover detection well under the 10s budget.
server api1:8080 max_fails=1 fail_timeout=3s;
server api2:8080 max_fails=1 fail_timeout=3s;
}
server {
listen 8443 ssl;
ssl_certificate /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_timeout 10m;
ssl_verify_client off;
# Slightly above the app's own 10 MiB attachment limit so the API --
# not Nginx's default HTML error page -- produces the contract's
# JSON 413 envelope. The API still enforces the exact byte budget.
client_max_body_size 11M;
location / {
proxy_pass https://messenger_api;
proxy_ssl_verify on;
proxy_ssl_trusted_certificate /etc/nginx/certs/ca.crt;
proxy_ssl_verify_depth 2;
proxy_ssl_certificate /etc/nginx/certs/lb-client.crt;
proxy_ssl_certificate_key /etc/nginx/certs/lb-client.key;
proxy_ssl_server_name on;
proxy_ssl_name messenger_api;
proxy_connect_timeout 1s;
proxy_read_timeout 5s;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
+17
View File
@@ -0,0 +1,17 @@
ui = false
api_addr = "http://openbao:8200"
cluster_addr = "http://openbao:8201"
storage "file" {
path = "/bao/data"
}
listener "tcp" {
address = "0.0.0.0:8200"
cluster_address = "0.0.0.0:8201"
tls_disable = true
}
telemetry {
disable_hostname = true
}
+3
View File
@@ -0,0 +1,3 @@
path "secret/data/messenger/*" {
capabilities = ["read"]
}
+3
View File
@@ -0,0 +1,3 @@
path "secret/data/messenger/*" {
capabilities = ["read"]
}
+55
View File
@@ -0,0 +1,55 @@
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
tls:
cert_file: /etc/otel/certs/collector.crt
key_file: /etc/otel/certs/collector.key
client_ca_file: /etc/otel/certs/ca.crt
http:
endpoint: 0.0.0.0:4318
tls:
cert_file: /etc/otel/certs/collector.crt
key_file: /etc/otel/certs/collector.key
client_ca_file: /etc/otel/certs/ca.crt
processors:
# Bounds the Collector's own memory so an unavailable backend cannot grow
# RAM without limit; it sheds/refuses data under pressure instead.
memory_limiter:
check_interval: 1s
limit_mib: 150
spike_limit_mib: 40
batch:
send_batch_size: 512
timeout: 5s
exporters:
otlphttp/tempo:
endpoint: http://tempo:4318
tls:
insecure: true
otlphttp/loki:
endpoint: http://loki:3100/otlp
tls:
insecure: true
prometheus:
endpoint: 0.0.0.0:8889
resource_to_telemetry_conversion:
enabled: true
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/tempo]
metrics:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [prometheus]
logs:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlphttp/loki]
+8
View File
@@ -0,0 +1,8 @@
global:
scrape_interval: 5s
evaluation_interval: 5s
scrape_configs:
- job_name: otel-collector
static_configs:
- targets: ["otel-collector:8889"]
+18
View File
@@ -0,0 +1,18 @@
{
"identities": [
{
"name": "messenger-api",
"credentials": [
{
"accessKey": "__S3_ACCESS_KEY_ID__",
"secretKey": "__S3_SECRET_ACCESS_KEY__"
}
],
"actions": [
"Read:messenger-attachments",
"Write:messenger-attachments",
"List:messenger-attachments"
]
}
]
}
+24
View File
@@ -0,0 +1,24 @@
server:
http_listen_port: 3200
distributor:
receivers:
otlp:
protocols:
http:
grpc:
ingester:
max_block_duration: 5m
compactor:
compaction:
block_retention: 168h
storage:
trace:
backend: local
local:
path: /var/tempo/traces
wal:
path: /var/tempo/wal