Files
mtusi2026lab7/scripts/collect-evidence.ps1
T
sendormeco d15f2a3631
Application stack (lab7) / stack (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
7 — Т2302 — ебедев — на 5
2026-11-22 23:26:17 +03:00

115 lines
8.6 KiB
PowerShell

# Windows PowerShell 5.1+ equivalent of scripts/collect-evidence.sh (lab7).
# Starts the stand (init-secrets -> up: OpenBao init/unseal/bootstrap -> all services), runs every check and writes
# outputs to evidence\ (no tokens/passwords/keys). WARNING: restarts containers, rotates certificates/credentials,
# seals OpenBao (then unseals with operator material). Volumes are never removed. ~60-75 min
# (-SkipRegression: -30 min, -SkipResearch: -8 min). Dependency scan: set $env:GHSA_DB to an advisory-database clone.
# powershell -NoProfile -ExecutionPolicy Bypass -File scripts\collect-evidence.ps1 [-SkipRegression] [-SkipResearch]
param([switch]$SkipRegression, [switch]$SkipResearch)
$ErrorActionPreference = 'Continue'
$root = Split-Path -Parent $PSScriptRoot
Set-Location $root
$env:PYTHONUTF8 = '1'
$env:PYTHONIOENCODING = 'utf-8'
# IPv4 explicitly: on Windows 'localhost' resolves to ::1 first; the LB listens on 127.0.0.1 only.
$env:NO_PROXY = '127.0.0.1,localhost'
$B = 'https://127.0.0.1:8443'
$CA = Join-Path $root 'secrets\tls\trust\ca.crt'
$env:BASE_URL = $B
$env:CA_FILE = $CA
$env:TTL_BASE_URL = 'https://127.0.0.1:8444'
$ev = Join-Path $root 'evidence'
New-Item -ItemType Directory -Force -Path $ev, (Join-Path $ev 'track1'), (Join-Path $ev 'scan') | Out-Null
$pyApp = Get-Command py -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
$pythonApp = Get-Command python -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if ($pyApp) { $script:PyExe = $pyApp.Source; $script:PyPre = @('-3') }
elseif ($pythonApp) { $script:PyExe = $pythonApp.Source; $script:PyPre = @() }
else { throw 'Python 3.9+ not found (install from python.org, enable "Add to PATH")' }
function Invoke-Python { & $script:PyExe @script:PyPre @args }
function Dc { & docker compose --env-file .env @args }
$hookStop = 'scripts\stop-one.cmd'
$hookRestart = 'scripts\restart.cmd'
$startReplicas = Join-Path $PSScriptRoot 'start-replicas.ps1'
function Get-Stamp {
$commit = 'n/a'
if (Get-Command git -ErrorAction SilentlyContinue) {
$c = (& git rev-parse --short HEAD 2>$null)
if ($LASTEXITCODE -eq 0 -and $c) { $commit = $c }
}
'# ' + (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') + ' commit=' + $commit + ' host=' + [Environment]::OSVersion.VersionString + ' cpus=' + [Environment]::ProcessorCount
}
function Invoke-Step([string]$name, [string]$title, [scriptblock]$block) {
Write-Host ('>>> ' + $name) -ForegroundColor Cyan
$global:LASTEXITCODE = 0
$lines = @(& $block 2>&1 | ForEach-Object { "$_" })
$code = $global:LASTEXITCODE
Set-Content -Path (Join-Path $ev ($name + '.txt')) -Encoding UTF8 -Value (@((Get-Stamp), ('$ ' + $title)) + $lines + @('exit=' + $code))
$lines | Select-Object -Last 2 | ForEach-Object { Write-Host (' ' + $_) }
if ($code -ne 0) { Write-Host (' exit=' + $code + ' (see evidence\' + $name + '.txt)') -ForegroundColor Yellow }
}
& docker version --format '{{.Server.Version}}' *> $null
if ($LASTEXITCODE -ne 0) { throw 'Docker is not running: start Docker Desktop first' }
& (Join-Path $PSScriptRoot 'init-secrets.ps1')
& (Join-Path $PSScriptRoot 'up.ps1') -Profiles ttl
Set-Location $root
Dc --profile tools up -d ops | Out-Null
$mount = (Join-Path $root 'student-tests') + ':/app/student-tests:ro'
Invoke-Step 'stack' 'docker version; compose images; ps' { & docker version --format 'docker {{.Server.Version}}'; Dc version; Dc --profile ttl images; Dc --profile ttl ps -a }
Invoke-Step 'pki-status' 'docker compose --profile tools run --rm pki status' { Dc --profile tools run --rm --no-deps -T pki status }
Invoke-Step 'openbao-status' 'bao_admin.py status / kv-keys / tokens (no values)' {
Invoke-Python scripts/bao_admin.py status; Invoke-Python scripts/bao_admin.py kv-keys api
Invoke-Python scripts/bao_admin.py kv-keys worker; Invoke-Python scripts/bao_admin.py tokens api
}
Invoke-Step 'migrations' 'schema_migrations; roles; docker compose logs migrate' {
Dc exec -T -u postgres postgres psql -d messenger -c 'SELECT version, left(checksum,12) AS checksum, applied_at FROM schema_migrations ORDER BY version' -c '\du'
Dc logs --no-log-prefix migrate
}
Invoke-Step 'broker-bootstrap' 'docker compose logs broker-init' { Dc logs --no-log-prefix broker-init }
Invoke-Step 's3-bootstrap' 'docker compose logs s3-init' { Dc logs --no-log-prefix s3-init }
Invoke-Step 'published-ports' 'docker compose ps: published ports' { Dc ps --format '{{.Service}} {{.Ports}}' | Sort-Object -Unique }
Invoke-Step 'kdf-bench' 'python -m messenger kdf-bench (in api)' { Dc exec -T api python -m messenger kdf-bench }
Invoke-Step 'make-check' 'python scripts/check_contract.py' { Invoke-Python scripts/check_contract.py }
Invoke-Step 'smoke' "python tests/smoke.py --base-url $B --ca-file CA" { Invoke-Python tests/smoke.py --base-url $B --ca-file $CA }
Invoke-Step 'test-security-public' 'python tests/security.py' { Invoke-Python tests/security.py --base-url $B --ca-file $CA }
Invoke-Step 'internal-security' 'docker compose --profile tests run --rm probe' { Dc --profile tests run --rm -T probe student-tests/internal_security.py }
Invoke-Step 'test-security' 'python student-tests/test_security.py' { Invoke-Python student-tests/test_security.py }
Invoke-Step 'test-hardening' 'python student-tests/test_hardening.py' { Invoke-Python student-tests/test_hardening.py --json evidence/hardening.json }
if ($env:GHSA_DB -and (Test-Path (Join-Path $env:GHSA_DB 'advisories'))) {
Invoke-Step 'dep-scan' 'python scripts/dep_scan.py' { Invoke-Python scripts/dep_scan.py --db $env:GHSA_DB --json evidence/scan/deps.json }
}
if (-not $SkipRegression) {
$env:FAULT_HOOKS = 'lab' # lab5 fault hooks only for the regression run
Dc up -d --no-deps --force-recreate --wait api worker | Out-Null
foreach ($t in 'test_api', 'test_idempotency', 'test_attachments', 'test_expiry', 'test_pipeline', 'test_failures',
'test_observability', 'test_jobs_failures', 'test_storage_failures', 'test_pipeline_idempotency',
'test_telemetry_persistence') {
Invoke-Step ($t -replace '_', '-') "python student-tests/$t.py" { Invoke-Python "student-tests/$t.py" }
}
Invoke-Step 'test-cache-race' 'test_cache_race.py in api image' { Dc run --rm --no-deps -T -v $mount --entrypoint python api student-tests/test_cache_race.py }
Invoke-Step 'test-imgproc' 'test_imgproc.py in worker image' { Dc run --rm --no-deps -T -v $mount --entrypoint python worker student-tests/test_imgproc.py }
Remove-Item Env:FAULT_HOOKS
Dc --profile fault stop api-fault | Out-Null
Dc up -d --no-deps --force-recreate --wait api worker | Out-Null
}
Set-Content -Path (Join-Path $ev 'failover-hook.txt') -Value '' -Encoding UTF8
Invoke-Step 'failover-1' 'tests/resilience.py --mode failover' { Invoke-Python tests/resilience.py --mode failover --action-script $hookStop --base-url $B --ca-file $CA }
Invoke-Step 'restore-1' 'start-replicas.ps1' { & $startReplicas }
Invoke-Step 'failover-2' 'tests/resilience.py --mode failover' { Invoke-Python tests/resilience.py --mode failover --action-script $hookStop --base-url $B --ca-file $CA }
Invoke-Step 'restore-2' 'start-replicas.ps1' { & $startReplicas }
Set-Content -Path (Join-Path $ev 'restart-hook.txt') -Value '' -Encoding UTF8
Invoke-Step 'persistence' 'tests/resilience.py --mode persistence' { Invoke-Python tests/resilience.py --mode persistence --action-script $hookRestart --base-url $B --ca-file $CA }
Invoke-Step 'test-lifecycle' 'python student-tests/test_lifecycle.py' { Invoke-Python student-tests/test_lifecycle.py --json evidence/lifecycle.json }
if (-not $SkipResearch) {
Invoke-Step 'track1-worker-compromise' 'python research/worker_compromise.py' { Invoke-Python research/worker_compromise.py --out evidence/track1 }
Invoke-Step 'login-dos' 'python research/login_dos.py' { Invoke-Python research/login_dos.py --out evidence/login_dos.json }
}
Invoke-Step 'internal-security-final' 'probe after rotations' { Dc --profile tests run --rm -T probe student-tests/internal_security.py }
Invoke-Step 'smoke-final' 'tests/smoke.py' { Invoke-Python tests/smoke.py --base-url $B --ca-file $CA }
Invoke-Step 'jobs-invariants' 'jobs invariants --settled --verify-bytes' { Dc --profile tools exec -T ops python -m messenger jobs invariants --settled --verify-bytes }
Invoke-Step 'load' 'tests/load.py 30 s x 4' { Invoke-Python tests/load.py --base-url $B --ca-file $CA --duration 30 --concurrency 4 --output evidence/load.json }
Invoke-Step 'secret-scan' 'python scripts/secret_scan.py' { Invoke-Python scripts/secret_scan.py }
Write-Host 'evidence collected in evidence\' -ForegroundColor Green