lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
Executable
+302
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Measure real password.Hash/Verify and optional real HTTPS login denials.
|
||||
|
||||
python3 scripts/lab7_auth_benchmark.py --concurrency 1 2 8 --operations 120 --output evidence/lab7/auth.json
|
||||
Optional: --api --ca-file <CA> --base-url https://localhost:8443
|
||||
|
||||
The temporary Go program imports this repository's password implementation,
|
||||
reads measured PHC m/t/p, and reports operation latency, busy denial and process
|
||||
peak RSS. It is deleted after execution; passwords, hashes and tokens never
|
||||
appear in output. Compiler memory/time are excluded from process RSS/latency.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import datetime as dt
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'student-tests'))
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from lab7_helpers import ProbeFailure, command, require, Stand
|
||||
from client import Client, Failure
|
||||
|
||||
GO_PROGRAM = r'''package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/platform/password"
|
||||
)
|
||||
|
||||
type sample struct {
|
||||
Operation string `json:"operation"`
|
||||
Seconds float64 `json:"seconds"`
|
||||
Result string `json:"result"`
|
||||
}
|
||||
|
||||
func fatal(classification string) {
|
||||
if err := json.NewEncoder(os.Stdout).Encode(map[string]string{"classification": classification}); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
concurrency := flag.Int("concurrency", 1, "parallel callers")
|
||||
operations := flag.Int("operations", 120, "total operations")
|
||||
flag.Parse()
|
||||
if *concurrency < 1 || *operations < 1 {
|
||||
fatal("invalid_configuration")
|
||||
}
|
||||
plainBytes := make([]byte, 24)
|
||||
if _, err := rand.Read(plainBytes); err != nil {
|
||||
fatal("entropy_unavailable")
|
||||
}
|
||||
plain := hex.EncodeToString(plainBytes)
|
||||
encoded, err := password.Hash(plain)
|
||||
if err != nil {
|
||||
fatal("seed_hash_failed")
|
||||
}
|
||||
fields := strings.Split(encoded, "$")
|
||||
if len(fields) != 6 || fields[1] != "argon2id" {
|
||||
fatal("measured_hash_not_phc_argon2id")
|
||||
}
|
||||
params := map[string]int{}
|
||||
for _, item := range strings.Split(fields[3], ",") {
|
||||
pair := strings.SplitN(item, "=", 2)
|
||||
if len(pair) != 2 {
|
||||
fatal("measured_phc_parameters_invalid")
|
||||
}
|
||||
value, err := strconv.Atoi(pair[1])
|
||||
if err != nil {
|
||||
fatal("measured_phc_parameters_invalid")
|
||||
}
|
||||
params[pair[0]] = value
|
||||
}
|
||||
jobs := make(chan int)
|
||||
samples := make([]sample, *operations)
|
||||
var group sync.WaitGroup
|
||||
started := time.Now()
|
||||
for range *concurrency {
|
||||
group.Go(func() {
|
||||
for index := range jobs {
|
||||
began := time.Now()
|
||||
operation, result := "hash", "ok"
|
||||
switch index % 3 {
|
||||
case 0:
|
||||
_, err := password.Hash(plain)
|
||||
if errors.Is(err, password.ErrBusy) {
|
||||
result = "busy_denied"
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
result = "hash_error"
|
||||
}
|
||||
case 1, 2:
|
||||
operation = "verify_correct"
|
||||
candidate := plain
|
||||
if index % 3 == 2 {
|
||||
operation = "verify_incorrect"
|
||||
candidate += "-incorrect"
|
||||
}
|
||||
verified, err := password.Verify(candidate, encoded)
|
||||
if errors.Is(err, password.ErrBusy) {
|
||||
result = "busy_denied"
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
result = "verify_error"
|
||||
break
|
||||
}
|
||||
expectedMatch := index % 3 == 1
|
||||
if verified.Match != expectedMatch {
|
||||
result = "verification_mismatch"
|
||||
}
|
||||
}
|
||||
samples[index] = sample{
|
||||
Operation: operation,
|
||||
Seconds: time.Since(began).Seconds(),
|
||||
Result: result,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
for index := range *operations {
|
||||
jobs <- index
|
||||
}
|
||||
close(jobs)
|
||||
group.Wait()
|
||||
elapsed := time.Since(started).Seconds()
|
||||
counts := map[string]int{}
|
||||
timings := map[string][]float64{}
|
||||
for _, value := range samples {
|
||||
counts[value.Result]++
|
||||
if value.Result == "ok" {
|
||||
timings[value.Operation] = append(timings[value.Operation], value.Seconds)
|
||||
}
|
||||
}
|
||||
latency := map[string]map[string]float64{}
|
||||
for operation, values := range timings {
|
||||
sort.Float64s(values)
|
||||
latency[operation] = map[string]float64{
|
||||
"p50_seconds": values[(len(values)-1)/2],
|
||||
"p95_seconds": values[(len(values)-1)*95/100],
|
||||
"max_seconds": values[len(values)-1],
|
||||
}
|
||||
}
|
||||
var usage syscall.Rusage
|
||||
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &usage); err != nil {
|
||||
fatal("peak_rss_unavailable")
|
||||
}
|
||||
peakRSS := uint64(usage.Maxrss)
|
||||
if runtime.GOOS != "darwin" {
|
||||
peakRSS *= 1024
|
||||
}
|
||||
result := map[string]any{
|
||||
"classification": "measured",
|
||||
"algorithm": "argon2id",
|
||||
"parameters": params,
|
||||
"concurrency": *concurrency,
|
||||
"operations": *operations,
|
||||
"elapsed_seconds": elapsed,
|
||||
"successful_operations_per_second": float64(counts["ok"]) / elapsed,
|
||||
"busy_denied_rate": float64(counts["busy_denied"]) / float64(*operations),
|
||||
"counts": counts,
|
||||
"latency": latency,
|
||||
"peak_rss_bytes": peakRSS,
|
||||
"goos": runtime.GOOS,
|
||||
"gomaxprocs": runtime.GOMAXPROCS(0),
|
||||
"raw_samples": samples,
|
||||
}
|
||||
if err := json.NewEncoder(os.Stdout).Encode(result); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def hash_benchmark(concurrency, operations):
|
||||
# Internal package import is legal only under this module. No permanent
|
||||
# source or build artifact is created, and the module itself is not edited.
|
||||
with tempfile.TemporaryDirectory(prefix='lab7-auth-bench-', dir=ROOT) as directory:
|
||||
source = Path(directory) / 'main.go'
|
||||
source.write_text(GO_PROGRAM)
|
||||
# Homebrew's default `go` can be older than go.mod's toolchain. Resolve
|
||||
# the cached toolchain GOROOT once, then pin that binary with local mode.
|
||||
go_bin = 'go'
|
||||
discovery = command(['go', 'env', 'GOROOT'], env={'GOTOOLCHAIN': 'auto'},
|
||||
check=False, timeout=30)
|
||||
candidate = Path(discovery.stdout.strip()) / 'bin' / 'go'
|
||||
if candidate.is_file():
|
||||
go_bin = str(candidate)
|
||||
result = command([go_bin, 'run', '-mod=readonly', str(source),
|
||||
'--concurrency', str(concurrency), '--operations', str(operations)],
|
||||
env={'GOPROXY': 'off', 'GOSUMDB': 'off', 'GOTOOLCHAIN': 'local'}, timeout=300)
|
||||
try:
|
||||
payload = json.loads(result.stdout)
|
||||
except ValueError:
|
||||
raise ProbeFailure('hash_benchmark_invalid_json') from None
|
||||
require(payload.get('classification') == 'measured', 'hash_benchmark_not_measured')
|
||||
require(payload['counts'].get('hash_error', 0) == 0 and payload['counts'].get('verify_error', 0) == 0
|
||||
and payload['counts'].get('verification_mismatch', 0) == 0, 'real_hash_verification_failure')
|
||||
return payload
|
||||
|
||||
|
||||
def api_benchmark(args, concurrency):
|
||||
stand = Stand(base_url=args.base_url)
|
||||
client = Client(stand.base_url, args.ca_file, timeout=5)
|
||||
actor = client.user('authbench')
|
||||
credential = base64.b64encode((actor['registration']['username'] + ':incorrect-local-benchmark-password').encode()).decode()
|
||||
ids = [item['Id'] for item in stand.containers('api')]
|
||||
require(len(ids) == 2, 'auth_benchmark_requires_two_replicas')
|
||||
# cgroup v2 memory.peak measures actual API containers, not the Python or
|
||||
# compiler process. It can include a previous higher peak: report honestly.
|
||||
def peak_memory():
|
||||
peaks = []
|
||||
for container in ids:
|
||||
value = command(['docker', 'exec', container, 'cat', '/sys/fs/cgroup/memory.peak'], check=False, timeout=10)
|
||||
peaks.append(int(value.stdout.strip()) if value.returncode == 0 and value.stdout.strip().isdigit() else None)
|
||||
return peaks
|
||||
peaks_before = peak_memory()
|
||||
def one(index):
|
||||
began = time.monotonic()
|
||||
try:
|
||||
current = Client(stand.base_url, args.ca_file, timeout=5)
|
||||
response = current.call('POST', '/api/v1/auth/sessions', (401, 429), headers={'Authorization': 'Basic ' + credential})
|
||||
status = response['status']
|
||||
if status == 429:
|
||||
retry = response['headers'].get('Retry-After', '')
|
||||
require(retry.isdigit() and int(retry) > 0, 'api_busy_missing_retry_after')
|
||||
return {'index': index, 'status': status, 'latency_seconds': time.monotonic() - began,
|
||||
'instance': response['headers'].get('X-Instance-Id')}
|
||||
except (Failure, ProbeFailure):
|
||||
return {'index': index, 'status': None, 'classification': 'api_benchmark_request_failed',
|
||||
'latency_seconds': time.monotonic() - began}
|
||||
started = time.monotonic()
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||||
samples = list(pool.map(one, range(args.operations)))
|
||||
elapsed = time.monotonic() - started
|
||||
peaks_after = peak_memory()
|
||||
require(all(s['status'] in (401, 429) for s in samples), 'api_auth_benchmark_unexpected_failure')
|
||||
denied = sum(s['status'] == 429 for s in samples)
|
||||
latencies = sorted(s['latency_seconds'] for s in samples)
|
||||
latency = {'p50_seconds': latencies[(len(latencies) - 1) // 2],
|
||||
'p95_seconds': latencies[(len(latencies) - 1) * 95 // 100], 'max_seconds': latencies[-1]}
|
||||
return {'concurrency': concurrency, 'operations': args.operations, 'elapsed_seconds': elapsed,
|
||||
'requests_per_second': len(samples) / elapsed, 'admission_denied_rate': denied / len(samples),
|
||||
'incorrect_password_401': sum(s['status'] == 401 for s in samples),
|
||||
'api_cgroup_peak_bytes_before': peaks_before, 'api_cgroup_peak_bytes_after': peaks_after,
|
||||
'latency': latency, 'raw_samples': samples,
|
||||
'limitations': ['Cgroup memory.peak includes container lifetime history; account/shared/local rate limits all contribute to429.']}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--concurrency', type=int, nargs='+', default=[1, 2, 8])
|
||||
parser.add_argument('--operations', type=int, default=120)
|
||||
parser.add_argument('--api', action='store_true')
|
||||
parser.add_argument('--base-url', default='https://localhost:8443')
|
||||
parser.add_argument('--ca-file')
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
require(all(0 < c <= 64 for c in args.concurrency) and args.operations >= 3, 'invalid_benchmark_configuration')
|
||||
require(not args.api or bool(args.ca_file), 'api_benchmark_ca_required')
|
||||
measured = [hash_benchmark(c, args.operations) for c in args.concurrency]
|
||||
result = {'study': 'lab7-auth-benchmark', 'timestamp': dt.datetime.now(dt.timezone.utc).isoformat(),
|
||||
'hash_verify': measured, 'api_auth': [api_benchmark(args, c) for c in args.concurrency] if args.api else [],
|
||||
'limitations': ['Native benchmark imports actual configured password implementation, not a reimplementation.',
|
||||
'Operation scheduling is closed-loop; memory includes Go runtime and seed hash, excludes compiler.',
|
||||
'Optional API benchmark uses one fresh account per concurrency level and real shared rejection paths.']}
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(result, indent=2, sort_keys=True) + '\n')
|
||||
print(json.dumps({'study': result['study'], 'classification': 'measured', 'output': str(args.output)}, sort_keys=True))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except ProbeFailure as exc:
|
||||
print(json.dumps({'classification': exc.classification, 'detail': exc.detail}))
|
||||
raise SystemExit(1)
|
||||
except Exception as exc:
|
||||
print(json.dumps({'classification': 'benchmark_exception', 'type': type(exc).__name__}))
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user