lab7: Argon2id из env (замер bench/argon2_bench.py), лимит неудачных входов в Valkey (429 + Retry-After), cap_drop ALL и no-new-privileges, X-Instance-Id = случайный alias
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Argon2id parameter measurement ON THIS STAND (README grade 3: "параметры обоснованы измерением").
|
||||
|
||||
Runs INSIDE an API replica, i.e. with exactly the CPU/RAM limits of production (compose: 1 CPU,
|
||||
512 MiB) and the same argon2-cffi build:
|
||||
|
||||
docker compose exec -T api python - < bench/argon2_bench.py > evidence/argon2/bench.json
|
||||
|
||||
For every (m, t) of the grid (p = 1): N sequential hashes -> median / p95 latency and CPU time per
|
||||
hash; then, for the candidate parameters, K threads hashing at once (K = 1, 2, 4) -> latency
|
||||
under contention and hashes/s of the replica = the most logins/s one replica can verify
|
||||
(the cost of a password guessing / login DoS attempt). Prints one JSON document (no passwords:
|
||||
a fixed synthetic one is hashed).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import statistics
|
||||
import threading
|
||||
import time
|
||||
|
||||
import importlib.metadata
|
||||
from argon2 import PasswordHasher, Type
|
||||
|
||||
PASSWORD = "synthetic-benchmark-password"
|
||||
GRID_M = [19456, 32768, 47104, 65536] # KiB: 19 MiB (OWASP minimum), 32, 46, 64 MiB
|
||||
GRID_T = [2, 3, 4]
|
||||
N = int(os.environ.get("BENCH_N", "15"))
|
||||
THREADS = [1, 2, 4]
|
||||
PER_THREAD = int(os.environ.get("BENCH_PER_THREAD", "6"))
|
||||
|
||||
|
||||
def cgroup_limits() -> dict:
|
||||
out = {}
|
||||
for name, path in (("cpu_max", "/sys/fs/cgroup/cpu.max"), ("memory_max", "/sys/fs/cgroup/memory.max")):
|
||||
try:
|
||||
with open(path) as f:
|
||||
out[name] = f.read().strip()
|
||||
except OSError:
|
||||
out[name] = None
|
||||
return out
|
||||
|
||||
|
||||
def sequential(m: int, t: int) -> dict:
|
||||
ph = PasswordHasher(time_cost=t, memory_cost=m, parallelism=1, hash_len=32, salt_len=16, type=Type.ID)
|
||||
ph.hash(PASSWORD) # warm-up (allocator, first-touch of the pages)
|
||||
wall, cpu = [], []
|
||||
for _ in range(N):
|
||||
w0, c0 = time.perf_counter(), time.thread_time()
|
||||
ph.hash(PASSWORD)
|
||||
wall.append((time.perf_counter() - w0) * 1000)
|
||||
cpu.append((time.thread_time() - c0) * 1000)
|
||||
wall.sort()
|
||||
return {"m_kib": m, "t": t, "p": 1, "n": N, "median_ms": round(statistics.median(wall), 1),
|
||||
"p95_ms": round(wall[min(len(wall) - 1, int(0.95 * len(wall)))], 1),
|
||||
"cpu_ms_per_hash": round(statistics.median(cpu), 1),
|
||||
"hash_example_prefix": ph.hash(PASSWORD).split("$")[1:4]}
|
||||
|
||||
|
||||
def concurrent(m: int, t: int, threads: int) -> dict:
|
||||
ph = PasswordHasher(time_cost=t, memory_cost=m, parallelism=1, hash_len=32, salt_len=16, type=Type.ID)
|
||||
lat: list[float] = []
|
||||
lock = threading.Lock()
|
||||
|
||||
def work():
|
||||
for _ in range(PER_THREAD):
|
||||
w0 = time.perf_counter()
|
||||
ph.hash(PASSWORD)
|
||||
with lock:
|
||||
lat.append((time.perf_counter() - w0) * 1000)
|
||||
|
||||
c0, w0 = time.process_time(), time.perf_counter()
|
||||
pool = [threading.Thread(target=work) for _ in range(threads)]
|
||||
for th in pool:
|
||||
th.start()
|
||||
for th in pool:
|
||||
th.join()
|
||||
wall = time.perf_counter() - w0
|
||||
lat.sort()
|
||||
return {"m_kib": m, "t": t, "threads": threads, "hashes": len(lat), "wall_s": round(wall, 2),
|
||||
"hashes_per_s": round(len(lat) / wall, 1), "median_ms": round(statistics.median(lat), 1),
|
||||
"p95_ms": round(lat[min(len(lat) - 1, int(0.95 * len(lat)))], 1),
|
||||
"cpu_utilisation": round((time.process_time() - c0) / wall, 2),
|
||||
"ram_mib_hashing": round(m * threads / 1024, 1)}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
report = {"when": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "python": platform.python_version(),
|
||||
"argon2_cffi": importlib.metadata.version("argon2-cffi"), "cpus_visible": os.cpu_count(), "cgroup": cgroup_limits(),
|
||||
"sequential": [sequential(m, t) for m in GRID_M for t in GRID_T]}
|
||||
report["concurrent"] = [concurrent(m, t, k) for (m, t) in ((19456, 2), (47104, 2), (19456, 3)) for k in THREADS]
|
||||
print(json.dumps(report, indent=1))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user