Files
mtusi2026lab7/scripts/test-mtls.ps1
T

26 lines
1.3 KiB
PowerShell

[CmdletBinding()]
param([string]$EnvFile = '.env')
$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
function Invoke-Probe([string]$TlsArguments) {
$script = "printf 'GET /health/ready HTTP/1.1\r\nHost: api\r\nConnection: close\r\n\r\n' | openssl s_client -connect api:8443 -servername api -CAfile /run/pki/service-ca/ca.crt $TlsArguments 2>&1"
$output = docker compose --env-file $EnvFile run --rm --no-deps --entrypoint /bin/sh nginx -c $script
return ($output -join "`n")
}
$noCertificate = Invoke-Probe ''
if ($noCertificate -match 'HTTP/1.1 200') { throw 'mTLS failure: API accepted a client without a certificate' }
$foreignCa = Invoke-Probe '-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key'
if ($foreignCa -match 'HTTP/1.1 200') { throw 'mTLS failure: API accepted a foreign client CA' }
$wrongIdentity = Invoke-Probe '-cert /run/pki/wrong-lb-client/tls.crt -key /run/pki/wrong-lb-client/tls.key'
if ($wrongIdentity -notmatch 'HTTP/1.1 403') { throw 'mTLS failure: trusted CA with forbidden SAN was not rejected with 403' }
$allowed = Invoke-Probe '-cert /run/pki/lb/tls.crt -key /run/pki/lb/tls.key'
if ($allowed -notmatch 'HTTP/1.1 200') { throw 'mTLS failure: allowed LB identity did not reach API health' }
Write-Host 'PASS: no cert, foreign CA and forbidden SAN rejected; LB identity accepted.'