Files
mtusi2026lab7/scripts/test-telemetry-redaction.ps1
T

107 lines
4.2 KiB
PowerShell

[CmdletBinding()]
param(
[string]$EnvFile = '.env',
[string]$BaseUrl = 'https://localhost:8443',
[int]$ExportWaitSeconds = 15
)
$ErrorActionPreference = 'Stop'
$root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
Set-Location $root
$ca = (Resolve-Path 'secrets\pki\public-ca\ca.crt').Path
$canary = 'otel-canary-' + [guid]::NewGuid().ToString('N')
$bodyPath = Join-Path $env:TEMP ('otel-body-' + [guid]::NewGuid().ToString('N') + '.json')
function Invoke-ExpectedHttp([string[]]$Arguments, [string]$ExpectedStatus) {
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$status = (& curl.exe --silent --show-error --output NUL --write-out '%{http_code}' `
--cacert $ca --ssl-no-revoke --connect-timeout 5 --max-time 15 @Arguments 2>$null) -join ''
$exitCode = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previous
}
if($exitCode -ne 0 -or $status -ne $ExpectedStatus){
throw "Telemetry canary request expected HTTP $ExpectedStatus but received $status"
}
}
function Invoke-GrafanaProbe([string]$Script) {
$Script = $Script.Replace("`r", '')
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Script))
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$command = "echo '$encoded' | base64 -d | sh -s -- '$canary'"
$output = @(& docker compose --env-file $EnvFile exec -T grafana sh -c $command 2>&1) -join "`n"
$exitCode = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previous
}
if($exitCode -ne 0){ throw "Telemetry backend probe failed without exposing the canary: $output" }
return $output
}
try {
# Exercise both sensitive carriers. The first value is an invalid Bearer token;
# the second is inside a deliberately malformed JSON body and is never stored.
Invoke-ExpectedHttp @('-H', "Authorization: Bearer $canary",
"$($BaseUrl.TrimEnd('/'))/api/v1/users/me") '401'
[System.IO.File]::WriteAllText($bodyPath,
'{"username":"telemetry_probe","display_name":"' + $canary + '"',
[System.Text.UTF8Encoding]::new($false))
Invoke-ExpectedHttp @('-H','Content-Type: application/json','--data-binary',"@$bodyPath",
"$($BaseUrl.TrimEnd('/'))/api/v1/users") '400'
Start-Sleep -Seconds $ExportWaitSeconds
$logs = (& docker compose --env-file $EnvFile logs --no-color --tail 3000 2>&1) -join "`n"
if($LASTEXITCODE -ne 0){ throw 'Cannot read Compose logs for telemetry canary check' }
if($logs.Contains($canary)){ throw 'Telemetry canary appeared in container stdout/stderr logs' }
$backendProbe = @'
set -eu
canary=$1
ca=/run/pki/service-ca/ca.crt
cert=/run/pki/grafana-client/tls.crt
key=/run/pki/grafana-client/tls.key
tmp=/tmp/telemetry-redaction-$$
trap 'rm -f "$tmp".*' EXIT
curl -fsS --cacert "$ca" --cert "$cert" --key "$key" --get \
--data-urlencode 'query={service_name=~".+"}' \
--data-urlencode "limit=5000" \
https://loki:3100/loki/api/v1/query_range >"$tmp.loki"
if grep -Fq "$canary" "$tmp.loki"; then
echo 'Loki contains the telemetry canary' >&2
exit 20
fi
for tag in http.request.header.authorization http.request.body http.response.body \
messaging.message.body db.statement.parameters url.query; do
curl -fsS --cacert "$ca" --cert "$cert" --key "$key" \
"https://tempo:3200/api/search/tag/$tag/values" >"$tmp.tempo"
if grep -Fq "$canary" "$tmp.tempo"; then
echo "Tempo tag $tag contains the telemetry canary" >&2
exit 21
fi
done
curl -fsS --cacert "$ca" --cert "$cert" --key "$key" \
'https://prometheus:9090/api/v1/series?match%5B%5D=%7B__name__%3D~%22.%2B%22%7D' \
>"$tmp.prometheus"
if grep -Fq "$canary" "$tmp.prometheus"; then
echo 'Prometheus label set contains the telemetry canary' >&2
exit 22
fi
'@
Invoke-GrafanaProbe $backendProbe | Out-Null
Write-Host 'PASS: unique Authorization/body canary was absent from container logs, Loki records, Tempo sensitive tag values and Prometheus label sets.'
Write-Host 'PASS: telemetry backends were queried over CA-verified mTLS with the dedicated Grafana identity.'
} finally {
Remove-Item -LiteralPath $bodyPath -Force -ErrorAction SilentlyContinue
$canary = $null
}