forked from mtusi2026/mtusi2026lab7
feat: completed tasks for grade 4
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
"""Проверки TLS внутри сети Compose на закреплённых версиях клиентов."""
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import ssl
|
||||
import time
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
import aio_pika
|
||||
import asyncpg
|
||||
from aiormq.exceptions import ChannelAccessRefused
|
||||
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import ExportLogsServiceRequest
|
||||
|
||||
from app.core.jobs import JobSettings
|
||||
from app.storage.broker import connect_broker
|
||||
|
||||
CERTS = Path("/certificates")
|
||||
|
||||
|
||||
def context(identity=None):
|
||||
tls = ssl.create_default_context(cafile=str(CERTS / "ca.crt"))
|
||||
if identity:
|
||||
tls.load_cert_chain(CERTS / f"{identity}.crt", CERTS / f"{identity}.key")
|
||||
return tls
|
||||
|
||||
|
||||
def http(host, port, path, identity=None, *, body=None):
|
||||
request = Request(f"https://{host}:{port}{path}", data=body, headers={
|
||||
"Content-Type": "application/x-protobuf",
|
||||
})
|
||||
try:
|
||||
with urlopen(request, context=context(identity), timeout=4) as response:
|
||||
return response.status
|
||||
except HTTPError as error:
|
||||
return error.code
|
||||
except (ssl.SSLError, ConnectionError):
|
||||
return 0
|
||||
except URLError as error:
|
||||
if isinstance(error.reason, (ssl.SSLError, ConnectionError)):
|
||||
return 0
|
||||
raise
|
||||
|
||||
|
||||
def check_http():
|
||||
for role in ("otel-api", "otel-worker", "otel-publisher"):
|
||||
assert http("otel-collector", 4318, "/v1/traces", role, body=b"") == 200
|
||||
assert http("otel-collector", 4318, "/v1/traces", "otel-wrong-role", body=b"") == 403
|
||||
for role in (None, "foreign-client"):
|
||||
assert http("otel-collector", 4318, "/v1/traces", role, body=b"") in (0, 400)
|
||||
for port in (8888, 8889):
|
||||
assert http("otel-collector", port, "/metrics", "otel-scraper") == 200
|
||||
assert http("otel-collector", port, "/metrics", "otel-api") in (0, 400)
|
||||
status = http("loki", 3100, "/loki/api/v1/labels", "otel-reader")
|
||||
assert status == 200, ("loki", status)
|
||||
status = http("tempo", 3200, "/api/search?limit=1", "otel-reader")
|
||||
assert status == 200, ("tempo", status)
|
||||
assert http("prometheus", 9090, "/-/ready", "otel-reader") == 200
|
||||
assert http("tempo", 4318, "/v1/traces", "otel-writer", body=b"") == 200
|
||||
assert http("tempo", 4318, "/v1/traces", "otel-reader", body=b"") == 0
|
||||
logs = ExportLogsServiceRequest()
|
||||
resource = logs.resource_logs.add()
|
||||
resource.resource.attributes.add(key="service.name").value.string_value = "tls-audit"
|
||||
event = resource.scope_logs.add().log_records.add(time_unix_nano=time.time_ns())
|
||||
event.body.string_value = "synthetic TLS audit"
|
||||
status = http("loki", 3100, "/otlp/v1/logs", "otel-writer", body=logs.SerializeToString())
|
||||
assert status in (200, 204), ("loki ingest", status)
|
||||
assert http("loki", 3100, "/ready", "otel-api") == 0
|
||||
assert http("grafana", 3000, "/api/datasources") == 401
|
||||
assert http("minio", 9000, "/minio/health/ready") == 200
|
||||
assert http("openbao", 8200, "/v1/sys/health") == 200
|
||||
|
||||
for port in (14318, 8887, 13133):
|
||||
try:
|
||||
with socket.create_connection(("otel-collector", port), 2):
|
||||
pass
|
||||
except ConnectionRefusedError:
|
||||
continue
|
||||
raise AssertionError(f"Loopback-порт Collector {port} доступен извне")
|
||||
|
||||
|
||||
async def check_broker():
|
||||
async def connect(identity):
|
||||
return await connect_broker(JobSettings(
|
||||
broker_port=5671, broker_ca_file=str(CERTS / "ca.crt"),
|
||||
broker_cert_file=str(CERTS / f"{identity}.crt"),
|
||||
broker_key_file=str(CERTS / f"{identity}.key"),
|
||||
))
|
||||
|
||||
for role in ("api-broker", "attachment-worker", "attachment-publisher"):
|
||||
async with await connect(role) as connection:
|
||||
assert not connection.is_closed
|
||||
for role in ("broker-wrong-role", "foreign-client"):
|
||||
try:
|
||||
async with await connect(role):
|
||||
pass
|
||||
except (aio_pika.exceptions.AMQPException, OSError):
|
||||
continue
|
||||
raise AssertionError(f"RabbitMQ принял запрещённую identity: {role}")
|
||||
|
||||
try:
|
||||
async with await aio_pika.connect(host="broker", port=5671, ssl=True,
|
||||
ssl_context=context(), auth="external", timeout=4):
|
||||
pass
|
||||
except (aio_pika.exceptions.AMQPException, OSError):
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("RabbitMQ принял соединение без сертификата")
|
||||
|
||||
async with await connect("api-broker") as connection:
|
||||
channel = await connection.channel()
|
||||
try:
|
||||
await channel.declare_queue("scope-audit-probe", auto_delete=True)
|
||||
except ChannelAccessRefused:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("Роль API смогла объявить очередь")
|
||||
|
||||
|
||||
async def check_database():
|
||||
try:
|
||||
await asyncpg.connect(host="postgres", user="worker", database="postgres",
|
||||
ssl=False, timeout=3)
|
||||
except asyncpg.InvalidAuthorizationSpecificationError as error:
|
||||
assert "rejects connection" in str(error)
|
||||
else:
|
||||
raise AssertionError("PostgreSQL принял plaintext")
|
||||
|
||||
with socket.create_connection(("redis", 6379), 3) as raw:
|
||||
with context().wrap_socket(raw, server_hostname="redis") as tls:
|
||||
tls.sendall(b"*1\r\n$4\r\nPING\r\n")
|
||||
assert tls.recv(128).startswith(b"-NOAUTH")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_http()
|
||||
asyncio.run(check_broker())
|
||||
asyncio.run(check_database())
|
||||
print("PASS: TLS-зависимости, mTLS identity и ACL RabbitMQ/Collector")
|
||||
@@ -15,20 +15,32 @@ TLS = ROOT / "secrets/tls"
|
||||
ROLES = ("api", "worker", "publisher", "cleanup")
|
||||
|
||||
|
||||
def bootstrap(port: int, *, unseal_only: bool = False) -> None:
|
||||
address = f"https://localhost:{port}"
|
||||
context = ssl.create_default_context(cafile=str(TLS / "ca.crt"))
|
||||
opener = build_opener(NoRedirect(), HTTPSHandler(context=context))
|
||||
class Bao:
|
||||
def __init__(self, port: int, certificate: Path | None = None, key: Path | None = None):
|
||||
self.address = f"https://localhost:{port}/v1/"
|
||||
context = ssl.create_default_context(cafile=str(TLS / "ca.crt"))
|
||||
if certificate:
|
||||
context.load_cert_chain(certificate, key)
|
||||
self.opener = build_opener(NoRedirect(), HTTPSHandler(context=context))
|
||||
|
||||
def call(path, payload=None, *, token=None, method=None):
|
||||
def call(self, path, payload=None, *, token=None, method=None):
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if token:
|
||||
headers["X-Vault-Token"] = token
|
||||
request = Request(address + "/v1/" + path, headers=headers, method=method,
|
||||
request = Request(self.address + path, headers=headers, method=method,
|
||||
data=None if payload is None else json.dumps(payload).encode())
|
||||
with opener.open(request, timeout=3) as response:
|
||||
with self.opener.open(request, timeout=5) as response:
|
||||
body = response.read()
|
||||
return json.loads(body) if body else None
|
||||
return json.loads(body) if body else None
|
||||
|
||||
|
||||
def operator_token(port: int) -> str:
|
||||
config = ROOT / f"secrets/bao-operator/operator-{port}.json"
|
||||
return json.loads(config.read_text())["root_token"]
|
||||
|
||||
|
||||
def bootstrap(port: int, *, unseal_only: bool = False) -> None:
|
||||
call = Bao(port).call
|
||||
|
||||
deadline = time.monotonic() + 30
|
||||
while True:
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Испытания TTL, распечатывания и отзыва cert identity OpenBao."""
|
||||
|
||||
import argparse
|
||||
import time
|
||||
from urllib.error import HTTPError
|
||||
|
||||
from scripts.bootstrap_bao import TLS, Bao, bootstrap, operator_token
|
||||
from scripts.deployment import ComposeProject
|
||||
from scripts.rotate_worker import cert_role, rotate
|
||||
from tests.client import Client
|
||||
|
||||
|
||||
def deny(call, codes=(400, 403)):
|
||||
try:
|
||||
call()
|
||||
except HTTPError as error:
|
||||
assert error.code in codes, error.code
|
||||
return
|
||||
raise AssertionError("OpenBao разрешил запрещённую операцию")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", required=True)
|
||||
parser.add_argument("--bao-port", required=True, type=int)
|
||||
args = parser.parse_args()
|
||||
project = ComposeProject()
|
||||
port = args.bao_port
|
||||
client = Client(args.base_url, str(TLS / "ca.crt"))
|
||||
cert, key = TLS / "worker-bao.crt", TLS / "worker-bao.key"
|
||||
operator, old_identity = Bao(port), Bao(port, cert, key)
|
||||
root = operator_token(port)
|
||||
probe = "auth/cert/certs/worker-expiry-probe"
|
||||
secret = "messenger/data/worker"
|
||||
|
||||
def login(bao, role="worker"):
|
||||
return bao.call("auth/cert/login", {"name": role})["auth"]
|
||||
|
||||
operator.call(probe, cert_role(cert, "worker", "3s"), token=root)
|
||||
try:
|
||||
temporary = login(old_identity, "worker-expiry-probe")
|
||||
assert 0 < temporary["lease_duration"] <= 3
|
||||
old_identity.call(secret, token=temporary["client_token"])
|
||||
time.sleep(temporary["lease_duration"] + 1)
|
||||
deny(lambda: old_identity.call(secret, token=temporary["client_token"]))
|
||||
finally:
|
||||
operator.call(probe, token=root, method="DELETE")
|
||||
print("PASS: короткоживущий токен теряет доступ после TTL")
|
||||
|
||||
operator.call("sys/seal", {}, token=root)
|
||||
try:
|
||||
client.call("GET", "/health/ready", schema="Health")
|
||||
deny(lambda: login(old_identity), (503,))
|
||||
try:
|
||||
project.run("exec", "-T", "worker", "python", "-c",
|
||||
"from app.core.bao import database_secret; database_secret()")
|
||||
except RuntimeError as error:
|
||||
assert "Не удалось получить секрет DB" in str(error)
|
||||
else:
|
||||
raise AssertionError("Новый процесс получил секрет из sealed OpenBao")
|
||||
finally:
|
||||
bootstrap(port, unseal_only=True)
|
||||
print("PASS: при seal текущий API готов, новая выдача запрещена")
|
||||
|
||||
old_token = login(old_identity)
|
||||
original = old_identity.call(secret, token=old_token["client_token"])["data"]["data"]["db_password"]
|
||||
actor, _, _, chat = client.bootstrap()
|
||||
image = project.run("images", "-q", "worker")
|
||||
rotate(project, port)
|
||||
operator.call("auth/token/revoke-accessor", {"accessor": old_token["accessor"]}, token=root)
|
||||
deny(lambda: login(old_identity))
|
||||
deny(lambda: old_identity.call(secret, token=old_token["client_token"]))
|
||||
new_identity = Bao(port, cert, key)
|
||||
new_token = login(new_identity)["client_token"]
|
||||
assert new_identity.call(secret, token=new_token)["data"]["data"]["db_password"] != original
|
||||
assert project.run("images", "-q", "worker") == image
|
||||
attachment, _ = client.attachment(actor, chat)
|
||||
client.wait_ready(actor, attachment, wait_seconds=60)
|
||||
print("PASS: старый leaf и токен отозваны, worker обработал вложение с новым паролем")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+72
-10
@@ -1,7 +1,8 @@
|
||||
"""Сквозные проверки уровня 3 для запущенного HTTPS-стенда."""
|
||||
"""Сквозные проверки защиты для запущенного HTTPS-стенда."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
@@ -64,15 +65,42 @@ asyncio.run(probe())
|
||||
"""
|
||||
|
||||
|
||||
ROOTFS_PROBE = """
|
||||
from os import ST_RDONLY, statvfs
|
||||
from tempfile import TemporaryFile
|
||||
|
||||
assert statvfs("/app").f_flag & ST_RDONLY
|
||||
with TemporaryFile(dir="/tmp") as temporary:
|
||||
temporary.write(b"tmpfs probe")
|
||||
"""
|
||||
|
||||
API_BROKER_PROBE = """
|
||||
import asyncio
|
||||
from app.core.jobs import JobSettings
|
||||
from app.storage.broker import connect_broker
|
||||
|
||||
|
||||
async def probe():
|
||||
async with await connect_broker(JobSettings()) as connection:
|
||||
assert not connection.is_closed
|
||||
|
||||
|
||||
asyncio.run(probe())
|
||||
"""
|
||||
|
||||
|
||||
def probe_deployment(project: ComposeProject) -> None:
|
||||
services = project.config["services"]
|
||||
for name in ("api", "worker"):
|
||||
service = services[name]
|
||||
assert not service.get("ports")
|
||||
assert "ALL" in service["cap_drop"]
|
||||
assert "ALL" in service["cap_drop"] and service["read_only"]
|
||||
assert "no-new-privileges:true" in service["security_opt"]
|
||||
assert service["tmpfs"]
|
||||
assert service["deploy"]["resources"]["limits"]["memory"]
|
||||
uid = project.run("exec", "-T", name, "id", "-u")
|
||||
assert int(uid) > 0
|
||||
project.run("exec", "-T", name, "python", "-c", ROOTFS_PROBE)
|
||||
for service in services.values():
|
||||
assert not service.get("privileged") and service.get("network_mode") != "host"
|
||||
assert all(mount["target"] != "/var/run/docker.sock"
|
||||
@@ -104,6 +132,22 @@ def probe_mtls(project: ComposeProject) -> None:
|
||||
print(output)
|
||||
|
||||
|
||||
def audit_dependencies(project: ComposeProject) -> None:
|
||||
command = ["run", "--rm", "--network", project.name + "_default",
|
||||
"--network", project.name + "_objects",
|
||||
"-v", f"{ROOT / 'scripts/audit_tls.py'}:/app/audit_tls.py:ro",
|
||||
"-v", f"{TLS / 'ca.crt'}:/certificates/ca.crt:ro"]
|
||||
for role in ("otel-api", "otel-worker", "otel-publisher", "otel-wrong-role",
|
||||
"otel-reader", "otel-writer", "otel-scraper", "api-broker",
|
||||
"attachment-worker", "attachment-publisher", "broker-wrong-role", "foreign-client"):
|
||||
for extension in ("crt", "key"):
|
||||
name = f"{role}.{extension}"
|
||||
command.extend(("-v", f"{TLS / name}:/certificates/{name}:ro"))
|
||||
project.run("exec", "-T", "api", "python", "-c", API_BROKER_PROBE)
|
||||
image = project.config["services"]["api"]["image"]
|
||||
print(project.docker(*command, image, "python", "-m", "audit_tls"))
|
||||
|
||||
|
||||
def probe_bao(port: int) -> None:
|
||||
def call(context, path, *, token=None, data=None):
|
||||
headers = {"Content-Type": "application/json"}
|
||||
@@ -178,19 +222,28 @@ def probe_http(base: str, project: ComposeProject) -> None:
|
||||
time.sleep(0.1)
|
||||
client.call("GET", "/api/v1/users/me", 401, actor=outsider)
|
||||
|
||||
basic = "Basic " + base64.b64encode(b"unknown:wrong").decode()
|
||||
basic = "Basic " + base64.b64encode(b"unknown:synthetic-password").decode()
|
||||
login = base + "/api/v1/auth/sessions"
|
||||
for _ in range(25):
|
||||
|
||||
def login_attempt(_):
|
||||
request = Request(login, data=b"", headers={"Authorization": basic})
|
||||
try:
|
||||
urlopen(request, context=tls, timeout=5)
|
||||
except HTTPError as error:
|
||||
if error.code == 429:
|
||||
assert int(error.headers["Retry-After"]) > 0
|
||||
break
|
||||
assert error.code == 401
|
||||
else:
|
||||
raise AssertionError("Лимит входа не сработал")
|
||||
return error.code, error.headers.get("X-Instance-Id")
|
||||
raise AssertionError("Неизвестный пользователь получил сессию")
|
||||
|
||||
started = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=12) as pool:
|
||||
results = list(pool.map(login_attempt, range(25)))
|
||||
statuses = [status for status, _ in results]
|
||||
replicas = {instance for status, instance in results if status == 401 and instance}
|
||||
assert set(statuses) == {401, 429} and len(replicas) >= 2, (statuses, len(replicas))
|
||||
print(f"PASS: login burst 25 за {time.monotonic() - started:.2f} с, "
|
||||
f"{statuses.count(401)} проверок на {len(replicas)} репликах, "
|
||||
f"{statuses.count(429)} ограничений")
|
||||
|
||||
try:
|
||||
urlopen(Request(login, data=b"x" * 17000), context=tls, timeout=5)
|
||||
@@ -203,8 +256,10 @@ def probe_http(base: str, project: ComposeProject) -> None:
|
||||
|
||||
def measure_argon2(project: ComposeProject) -> None:
|
||||
probe = """
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from resource import RUSAGE_SELF, getrusage
|
||||
from statistics import median
|
||||
from time import perf_counter
|
||||
from time import perf_counter, process_time
|
||||
|
||||
from app.core.security import _hash_password_sync, _verify_password_sync
|
||||
|
||||
@@ -216,7 +271,13 @@ for _ in range(15):
|
||||
started = perf_counter()
|
||||
assert _verify_password_sync(password, encoded)
|
||||
timings.append((perf_counter() - started) * 1000)
|
||||
print(f"PASS: Argon2id, медиана проверки {median(timings):.1f} мс (15 попыток)")
|
||||
wall, cpu = perf_counter(), process_time()
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
assert all(pool.map(lambda _: _verify_password_sync(password, encoded), range(24)))
|
||||
wall, cpu = perf_counter() - wall, process_time() - cpu
|
||||
print(f"PASS: Argon2id, медиана {median(timings):.1f} мс, 24 проверки на 2 потоках "
|
||||
f"за {wall:.2f} с, CPU {cpu / 24 * 1000:.1f} мс/проверку, "
|
||||
f"пиковый RSS {getrusage(RUSAGE_SELF).ru_maxrss / 1024:.1f} MiB")
|
||||
"""
|
||||
print(project.run("exec", "-T", "api", "python", "-c", probe))
|
||||
|
||||
@@ -229,6 +290,7 @@ def main() -> None:
|
||||
project = ComposeProject()
|
||||
probe_deployment(project)
|
||||
probe_mtls(project)
|
||||
audit_dependencies(project)
|
||||
probe_bao(args.bao_port)
|
||||
probe_http(args.base_url, project)
|
||||
measure_argon2(project)
|
||||
|
||||
@@ -28,12 +28,13 @@ class ComposeProject:
|
||||
self.config = json.loads(self.run("config", "--format", "json"))
|
||||
self.name = self.config["name"]
|
||||
|
||||
def docker(self, *args: str, timeout: int = 90, env=None) -> str:
|
||||
def docker(self, *args: str, timeout: int = 90, env=None, input_text=None) -> str:
|
||||
"""Выполняет Docker-команду с ограничением времени."""
|
||||
result = subprocess.run(
|
||||
["docker", *args],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
input=input_text,
|
||||
capture_output=True,
|
||||
timeout=timeout,
|
||||
env={**os.environ, **(env or {})},
|
||||
@@ -42,9 +43,9 @@ class ComposeProject:
|
||||
raise RuntimeError(result.stderr.strip() or result.stdout.strip())
|
||||
return result.stdout.strip()
|
||||
|
||||
def run(self, *args: str, timeout: int = 90, env=None) -> str:
|
||||
def run(self, *args: str, timeout: int = 90, env=None, input_text=None) -> str:
|
||||
"""Выполняет команду Compose с текущими overrides."""
|
||||
return self.docker(*self.command[1:], *args, timeout=timeout, env=env)
|
||||
return self.docker(*self.command[1:], *args, timeout=timeout, env=env, input_text=input_text)
|
||||
|
||||
def replicas(self) -> list[Replica]:
|
||||
"""Читает состояние API без окружения контейнеров."""
|
||||
@@ -170,7 +171,7 @@ def verify(*, keep_stand=False):
|
||||
summary = re.search(r'\d+ passed[^\n]*', result.stdout)
|
||||
if summary:
|
||||
entry['summary'] = summary.group(0)
|
||||
if name == 'security' and not result.returncode:
|
||||
if name in ('security', 'lifecycle') and not result.returncode:
|
||||
entry['summary'] = result.stdout.strip().splitlines()
|
||||
print(result.stdout.strip(), flush=True)
|
||||
if result.returncode:
|
||||
@@ -200,6 +201,8 @@ def verify(*, keep_stand=False):
|
||||
run('external-tls', [sys.executable, 'tests/security.py', '--base-url', 'https://localhost:28080'])
|
||||
run('security', [sys.executable, '-m', 'scripts.check_security',
|
||||
'--base-url', 'https://localhost:28080', '--bao-port', bao_port])
|
||||
run('lifecycle', [sys.executable, '-m', 'scripts.check_lifecycle',
|
||||
'--base-url', 'https://localhost:28080', '--bao-port', bao_port])
|
||||
run('failover', [sys.executable, 'tests/resilience.py', '--mode', 'failover',
|
||||
'--action-script', 'scripts/stop-one.sh', '--base-url', 'https://localhost:28080'])
|
||||
report['result'] = 'PASS'
|
||||
|
||||
+25
-27
@@ -1,34 +1,32 @@
|
||||
"""Готовит RabbitMQ definitions с хешами паролей и правами очереди."""
|
||||
"""Готовит cert-пользователей RabbitMQ и их права очереди."""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
|
||||
def initialize(directory: Path, queue: str, vhost: str, admin: str) -> None:
|
||||
users, permissions = [], []
|
||||
for name, filename, read, write in (
|
||||
(admin, "broker_password", ".*", ".*"),
|
||||
("attachment-publisher", "publisher_broker_password", "^$", "^amq\\.default$"),
|
||||
("attachment-worker", "worker_broker_password", f"^{re.escape(queue)}$", "^$a"),
|
||||
):
|
||||
salt = os.urandom(4)
|
||||
password = (directory / filename).read_bytes().strip()
|
||||
digest = hashlib.sha256(salt + password).digest()
|
||||
users.append({
|
||||
"name": name, "password_hash": base64.b64encode(salt + digest).decode(),
|
||||
"hashing_algorithm": "rabbit_password_hashing_sha256",
|
||||
"tags": ["administrator"] if name == admin else [],
|
||||
})
|
||||
permissions.append({
|
||||
"user": name, "vhost": vhost, "read": read, "write": write,
|
||||
"configure": ".*" if name == admin else f"^{re.escape(queue)}$",
|
||||
})
|
||||
def initialize(directory: Path, queue: str, vhost: str) -> None:
|
||||
queue_pattern = f"^{re.escape(queue)}$"
|
||||
denied = "^$a"
|
||||
roles = (
|
||||
("api-broker", denied, denied, denied),
|
||||
("attachment-publisher", queue_pattern, denied, r"^amq\.default$"),
|
||||
("attachment-worker", queue_pattern, queue_pattern, denied),
|
||||
)
|
||||
users = [
|
||||
{"name": name, "password_hash": "", "hashing_algorithm": "rabbit_password_hashing_sha256",
|
||||
"tags": []}
|
||||
for name, *_ in roles
|
||||
]
|
||||
permissions = [
|
||||
{"user": name, "vhost": vhost, "configure": configure, "read": read, "write": write}
|
||||
for name, configure, read, write in roles
|
||||
]
|
||||
definitions = json.dumps({"users": users, "vhosts": [{"name": vhost}],
|
||||
"permissions": permissions})
|
||||
path = directory / "broker_definitions.json"
|
||||
if not path.exists():
|
||||
path.write_text(json.dumps({"users": users, "vhosts": [{"name": vhost}],
|
||||
"permissions": permissions}))
|
||||
path.chmod(0o444)
|
||||
if not path.exists() or path.read_text() != definitions:
|
||||
temporary = path.with_suffix(".tmp")
|
||||
temporary.write_text(definitions)
|
||||
temporary.chmod(0o444)
|
||||
temporary.replace(path)
|
||||
|
||||
@@ -26,9 +26,6 @@ def main() -> None:
|
||||
"cleanup_db_password",
|
||||
"redis_password",
|
||||
"test_redis_password",
|
||||
"broker_password",
|
||||
"worker_broker_password",
|
||||
"publisher_broker_password",
|
||||
"minio_root_user",
|
||||
"minio_root_password",
|
||||
"s3_access_key_id",
|
||||
@@ -49,8 +46,7 @@ def main() -> None:
|
||||
if line and not line.lstrip().startswith("#") and "=" in line
|
||||
for key, value in [line.split("=", 1)]
|
||||
}
|
||||
initialize_broker(directory, settings["BROKER_QUEUE"], settings["BROKER_VHOST"],
|
||||
settings["BROKER_USER"])
|
||||
initialize_broker(directory, settings["BROKER_QUEUE"], settings["BROKER_VHOST"])
|
||||
initialize(root)
|
||||
print("Настройки готовы; существующие значения сохранены")
|
||||
|
||||
|
||||
+9
-2
@@ -64,7 +64,7 @@ def issue(directory: Path, name: str, san: str, usage: str, ca: str) -> None:
|
||||
def initialize(root: Path) -> None:
|
||||
directory = root / "secrets" / "tls"
|
||||
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
for name in ("ca", "foreign-ca", "otel-ingest-ca", "otel-write-ca", "otel-read-ca", "otel-scrape-ca"):
|
||||
for name in ("ca", "foreign-ca", "broker-client-ca", "otel-ingest-ca", "otel-write-ca", "otel-read-ca", "otel-scrape-ca"):
|
||||
create_ca(directory, name)
|
||||
for name, san in SERVER_NAMES.items():
|
||||
issue(directory, name, san, "serverAuth", "ca")
|
||||
@@ -74,7 +74,14 @@ def initialize(root: Path) -> None:
|
||||
for name, authority in (
|
||||
("otel-writer", "otel-write-ca"),
|
||||
("otel-reader", "otel-read-ca"),
|
||||
("otel-agent", "otel-ingest-ca"),
|
||||
("otel-api", "otel-ingest-ca"),
|
||||
("otel-worker", "otel-ingest-ca"),
|
||||
("otel-publisher", "otel-ingest-ca"),
|
||||
("otel-wrong-role", "otel-ingest-ca"),
|
||||
("api-broker", "broker-client-ca"),
|
||||
("attachment-worker", "broker-client-ca"),
|
||||
("attachment-publisher", "broker-client-ca"),
|
||||
("broker-wrong-role", "broker-client-ca"),
|
||||
("otel-scraper", "otel-scrape-ca"),
|
||||
):
|
||||
issue(directory, name, f"DNS:{name}", "clientAuth", authority)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Ротация пароля PostgreSQL и cert identity worker из операторского контура."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
from tempfile import TemporaryDirectory
|
||||
from urllib.error import HTTPError
|
||||
|
||||
from scripts.bootstrap_bao import ROOT, TLS, Bao, operator_token
|
||||
from scripts.deployment import ComposeProject
|
||||
from scripts.init_tls import issue
|
||||
|
||||
|
||||
def cert_role(certificate: Path, role: str, ttl: str = "10m") -> dict:
|
||||
return {
|
||||
"certificate": certificate.read_text(),
|
||||
"token_policies": [role],
|
||||
"token_no_default_policy": True,
|
||||
"token_ttl": ttl,
|
||||
"token_max_ttl": ttl,
|
||||
}
|
||||
|
||||
|
||||
def change_db_password(project: ComposeProject, password: str) -> None:
|
||||
salt = secrets.token_bytes(16)
|
||||
iterations = 4096
|
||||
salted = hashlib.pbkdf2_hmac("sha256", password.encode(), salt, iterations)
|
||||
client_key = hmac.digest(salted, b"Client Key", "sha256")
|
||||
server_key = hmac.digest(salted, b"Server Key", "sha256")
|
||||
|
||||
def encode(value):
|
||||
return base64.b64encode(value).decode()
|
||||
|
||||
verifier = (f"SCRAM-SHA-256${iterations}:{encode(salt)}"
|
||||
f"${encode(hashlib.sha256(client_key).digest())}:{encode(server_key)}")
|
||||
project.run("exec", "-T", "postgres", "sh", "-ec",
|
||||
'exec psql --no-psqlrc -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"',
|
||||
input_text=f"ALTER ROLE worker PASSWORD '{verifier}';\n")
|
||||
|
||||
|
||||
def rotate(project: ComposeProject, port: int) -> None:
|
||||
bao, token = Bao(port), operator_token(port)
|
||||
role_path = "auth/cert/certs/worker"
|
||||
secret_path = "messenger/data/worker"
|
||||
previous = bao.call(secret_path, token=token)["data"]["data"]
|
||||
current_cert = TLS / "worker-bao.crt"
|
||||
current_key = TLS / "worker-bao.key"
|
||||
password_file = ROOT / "secrets/worker_db_password"
|
||||
password = secrets.token_urlsafe(24)
|
||||
|
||||
with TemporaryDirectory(prefix=".worker-rotation-", dir=TLS) as directory:
|
||||
staging = Path(directory)
|
||||
for suffix in ("crt", "key"):
|
||||
(staging / f"ca.{suffix}").symlink_to(TLS / f"ca.{suffix}")
|
||||
issue(staging, "worker-bao", "DNS:worker-bao", "clientAuth", "ca")
|
||||
new_cert, new_key = (staging / f"worker-bao.{suffix}" for suffix in ("crt", "key"))
|
||||
old_certificate, old_key = current_cert.read_bytes(), current_key.read_bytes()
|
||||
project.run("stop", "worker")
|
||||
try:
|
||||
change_db_password(project, password)
|
||||
bao.call(secret_path, {"data": {"db_password": password}}, token=token)
|
||||
bao.call(role_path, cert_role(new_cert, "worker"), token=token)
|
||||
password_stage = staging / "password"
|
||||
password_stage.write_text(password)
|
||||
password_stage.chmod(0o444)
|
||||
os.replace(password_stage, password_file)
|
||||
os.replace(new_cert, current_cert)
|
||||
os.replace(new_key, current_key)
|
||||
project.run("up", "-d", "--no-build", "--no-deps", "--force-recreate", "worker")
|
||||
except (HTTPError, OSError, RuntimeError):
|
||||
change_db_password(project, previous["db_password"])
|
||||
bao.call(secret_path, {"data": previous}, token=token)
|
||||
for path, data in ((current_cert, old_certificate), (current_key, old_key),
|
||||
(password_file, previous["db_password"].encode())):
|
||||
temporary = staging / (path.name + ".rollback")
|
||||
temporary.write_bytes(data)
|
||||
temporary.chmod(0o444)
|
||||
os.replace(temporary, path)
|
||||
bao.call(role_path, cert_role(current_cert, "worker"), token=token)
|
||||
project.run("up", "-d", "--no-build", "--no-deps", "--force-recreate", "worker")
|
||||
raise
|
||||
print("PASS: пароль PostgreSQL и клиентский сертификат worker заменены без сборки")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--bao-port", type=int, default=18200)
|
||||
rotate(ComposeProject(), parser.parse_args().bao_port)
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Сканирует lock-файл и локальные образы, не передавая сокет Docker сканеру."""
|
||||
|
||||
from datetime import UTC, datetime
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from scripts.deployment import ROOT, ComposeProject
|
||||
|
||||
SCANNER = "ghcr.io/aquasecurity/trivy:0.76.2"
|
||||
|
||||
|
||||
def scan() -> dict:
|
||||
project = ComposeProject()
|
||||
images = sorted({service["image"] for service in project.config["services"].values()
|
||||
if "build" in service})
|
||||
with TemporaryDirectory(prefix="lab7-scan-") as directory:
|
||||
workspace = Path(directory)
|
||||
packages = workspace / "packages"
|
||||
packages.mkdir()
|
||||
shutil.copyfile(ROOT / "requirements.txt", packages / "requirements.txt")
|
||||
cache = workspace / "cache"
|
||||
cache.mkdir()
|
||||
|
||||
def trivy(mount, args):
|
||||
result = subprocess.run(
|
||||
["docker", "run", "--rm", "--cap-drop=ALL",
|
||||
"--user", f"{os.getuid()}:{os.getgid()}",
|
||||
"--security-opt=no-new-privileges", "-v", f"{cache}:/cache",
|
||||
"-v", mount, SCANNER, *args, "--cache-dir", "/cache",
|
||||
"--quiet", "--scanners", "vuln", "--severity", "HIGH,CRITICAL",
|
||||
"--format", "json", "--timeout", "6m"],
|
||||
cwd=ROOT, check=True, stdout=subprocess.PIPE,
|
||||
)
|
||||
report = json.loads(result.stdout)
|
||||
return {
|
||||
"targets": [item["Target"] for item in report.get("Results", [])],
|
||||
"findings": [
|
||||
{name: issue.get(name) for name in (
|
||||
"VulnerabilityID", "PkgName", "InstalledVersion", "FixedVersion",
|
||||
"Severity", "PrimaryURL", "Status",
|
||||
)}
|
||||
for item in report.get("Results", [])
|
||||
for issue in item.get("Vulnerabilities", [])
|
||||
],
|
||||
}
|
||||
|
||||
result = {"scanner": SCANNER, "date_utc": datetime.now(UTC).isoformat(), "dependency": trivy(
|
||||
f"{packages}:/scan:ro", ["fs", "/scan"],
|
||||
), "images": {}}
|
||||
archive = workspace / "image.tar"
|
||||
for name in images:
|
||||
subprocess.run(["docker", "image", "save", "-o", archive, name],
|
||||
cwd=ROOT, check=True)
|
||||
findings = trivy(f"{archive}:/image.tar:ro", ["image", "--input", "/image.tar"])
|
||||
result["images"][name] = {
|
||||
"image_id": project.docker("image", "inspect", "--format", "{{.Id}}", name),
|
||||
**findings,
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
destination = ROOT / "evidence/security-scan.json"
|
||||
destination.write_text(json.dumps(scan(), ensure_ascii=False, indent=2) + "\n")
|
||||
print("Результаты Trivy сохранены в evidence/security-scan.json")
|
||||
Reference in New Issue
Block a user