Защита внутренних соединений и сервисных прав
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
[CmdletBinding()]
|
||||
param([string]$EnvFile = '.env')
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
|
||||
# Expected denials write to stderr; keep them from becoming terminating errors
|
||||
# under $ErrorActionPreference = 'Stop'.
|
||||
function Invoke-InContainer {
|
||||
param([string]$Service, [string]$Script)
|
||||
$previous = $ErrorActionPreference
|
||||
$ErrorActionPreference = 'Continue'
|
||||
try {
|
||||
$output = @($Script | & docker compose --env-file $EnvFile exec -T $Service sh -s 2>&1) -join "`n"
|
||||
$exitCode = $LASTEXITCODE
|
||||
} finally {
|
||||
$ErrorActionPreference = $previous
|
||||
}
|
||||
[pscustomobject]@{ ExitCode = $exitCode; Output = $output }
|
||||
}
|
||||
|
||||
$probe = @'
|
||||
set -eu
|
||||
ca=/run/pki/service-ca/ca.crt
|
||||
|
||||
probe_ok() {
|
||||
label=$1; target=$2; name=$3; shift 3
|
||||
if ! openssl s_client -brief -verify_return_error -verify_hostname "$name" \
|
||||
-connect "$target" -servername "$name" -CAfile "$ca" "$@" \
|
||||
</dev/null >/tmp/tls-probe.log 2>&1; then
|
||||
echo "FAIL: $label did not pass CA/hostname verification" >&2
|
||||
sed -n '1,12p' /tmp/tls-probe.log >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
probe_rejected() {
|
||||
label=$1; target=$2; name=$3
|
||||
set +e
|
||||
printf '\n' | openssl s_client -tls1_2 -brief -verify_return_error -verify_hostname "$name" \
|
||||
-connect "$target" -servername "$name" -CAfile "$ca" \
|
||||
>/tmp/tls-probe.log 2>&1
|
||||
status=$?
|
||||
set -e
|
||||
if [ "$status" -eq 0 ] || ! grep -Eq 'certificate required|handshake failure|certificate unknown|bad certificate' /tmp/tls-probe.log; then
|
||||
echo "FAIL: $label accepted a connection without its required client certificate" >&2
|
||||
sed -n '1,12p' /tmp/tls-probe.log >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
probe_ok postgres postgres:5432 postgres -starttls postgres
|
||||
probe_ok minio minio:9000 minio
|
||||
probe_ok openbao openbao:8200 openbao
|
||||
|
||||
probe_ok redis redis:6379 redis \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key
|
||||
probe_rejected redis redis:6379 redis
|
||||
|
||||
probe_ok rabbitmq rabbitmq:5671 rabbitmq \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key
|
||||
probe_rejected rabbitmq rabbitmq:5671 rabbitmq
|
||||
|
||||
probe_ok otel-ingest-api otel-collector:4318 otel-collector \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key
|
||||
probe_ok otel-ingest-worker otel-collector:4318 otel-collector \
|
||||
-cert /run/pki/worker-client/tls.crt -key /run/pki/worker-client/tls.key
|
||||
probe_rejected otel-ingest otel-collector:4318 otel-collector
|
||||
|
||||
probe_ok otel-metrics otel-collector:8889 otel-collector \
|
||||
-cert /run/pki/prometheus-client/tls.crt -key /run/pki/prometheus-client/tls.key
|
||||
probe_rejected otel-metrics otel-collector:8889 otel-collector
|
||||
|
||||
probe_ok loki loki:3100 loki \
|
||||
-cert /run/pki/collector-client/tls.crt -key /run/pki/collector-client/tls.key
|
||||
probe_rejected loki loki:3100 loki
|
||||
|
||||
probe_ok tempo-ingest tempo:4318 tempo \
|
||||
-cert /run/pki/collector-client/tls.crt -key /run/pki/collector-client/tls.key
|
||||
probe_rejected tempo-ingest tempo:4318 tempo
|
||||
|
||||
probe_ok tempo-query tempo:3200 tempo \
|
||||
-cert /run/pki/grafana-client/tls.crt -key /run/pki/grafana-client/tls.key
|
||||
probe_rejected tempo-query tempo:3200 tempo
|
||||
|
||||
probe_ok prometheus prometheus:9090 prometheus \
|
||||
-cert /run/pki/grafana-client/tls.crt -key /run/pki/grafana-client/tls.key
|
||||
probe_rejected prometheus prometheus:9090 prometheus
|
||||
|
||||
echo 'PASS: DB, S3, broker, Redis, Collector, telemetry backends and OpenBao verify server CA/hostname; all declared mTLS endpoints reject no-cert clients.'
|
||||
'@
|
||||
|
||||
$probe | & docker compose --env-file $EnvFile run --rm -T --no-deps --entrypoint /bin/sh nginx -s
|
||||
if($LASTEXITCODE -ne 0){ throw 'Internal TLS verification failed' }
|
||||
|
||||
# Database probes use distinct runtime credentials and verify the server over TLS.
|
||||
$apiDdl = 'PGPASSWORD="$DATABASE_API_PASSWORD" PGSSLMODE=verify-full PGSSLROOTCERT=/run/pki/service-ca/ca.crt psql -h postgres -U messenger_api -d messenger -v ON_ERROR_STOP=1 -c "CREATE TABLE forbidden_api_probe(id integer)"'
|
||||
$apiDdlResult = Invoke-InContainer postgres $apiDdl
|
||||
if($apiDdlResult.ExitCode -eq 0 -or $apiDdlResult.Output -notmatch 'permission denied'){
|
||||
throw 'Database ACL failure: API role DDL was not denied with a permission error'
|
||||
}
|
||||
|
||||
$workerUsers = 'PGPASSWORD="$DATABASE_WORKER_PASSWORD" PGSSLMODE=verify-full PGSSLROOTCERT=/run/pki/service-ca/ca.crt psql -h postgres -U messenger_worker -d messenger -v ON_ERROR_STOP=1 -c "SELECT password_hash FROM users LIMIT 1"'
|
||||
$workerUsersResult = Invoke-InContainer postgres $workerUsers
|
||||
if($workerUsersResult.ExitCode -eq 0 -or $workerUsersResult.Output -notmatch 'permission denied'){
|
||||
throw 'Database ACL failure: worker password-hash read was not denied with a permission error'
|
||||
}
|
||||
|
||||
# Redis identities have disjoint command/key permissions in addition to mTLS.
|
||||
$apiOutsideKey = 'redis-cli --tls --cacert /run/pki/service-ca/ca.crt --cert /run/pki/api-client/tls.crt --key /run/pki/api-client/tls.key --user messenger-api --pass "$REDIS_API_PASSWORD" --no-auth-warning SET forbidden:probe value'
|
||||
$apiOutsideResult = (Invoke-InContainer redis $apiOutsideKey).Output
|
||||
if($apiOutsideResult -notmatch 'NOPERM'){ throw 'Redis ACL failure: API wrote outside messenger:*' }
|
||||
|
||||
$workerRead = 'redis-cli --tls --cacert /run/pki/service-ca/ca.crt --cert /run/pki/worker-client/tls.crt --key /run/pki/worker-client/tls.key --user messenger-worker --pass "$REDIS_WORKER_PASSWORD" --no-auth-warning GET messenger:user:probe'
|
||||
$workerReadResult = (Invoke-InContainer redis $workerRead).Output
|
||||
if($workerReadResult -notmatch 'NOPERM'){ throw 'Redis ACL failure: worker read an API cache key' }
|
||||
|
||||
$apiAdmin = 'redis-cli --tls --cacert /run/pki/service-ca/ca.crt --cert /run/pki/api-client/tls.crt --key /run/pki/api-client/tls.key --user messenger-api --pass "$REDIS_API_PASSWORD" --no-auth-warning CONFIG GET "*"'
|
||||
$apiAdminResult = (Invoke-InContainer redis $apiAdmin).Output
|
||||
if($apiAdminResult -notmatch 'NOPERM'){ throw 'Redis ACL failure: API executed an administrative command' }
|
||||
|
||||
$permissions = (& docker compose --env-file $EnvFile exec -T rabbitmq rabbitmqctl list_permissions --formatter table) -join "`n"
|
||||
if($permissions -notmatch 'messenger-api' -or $permissions -notmatch 'messenger-worker'){
|
||||
throw 'RabbitMQ ACL failure: separate application identities are missing'
|
||||
}
|
||||
if($permissions -match 'messenger-(api|worker)\s+\.\*\s+\.\*\s+\.\*'){
|
||||
throw 'RabbitMQ ACL failure: an application identity has unrestricted permissions'
|
||||
}
|
||||
|
||||
Write-Host 'PASS: PostgreSQL, Redis and RabbitMQ runtime identities are separate and forbidden operations are denied.'
|
||||
Reference in New Issue
Block a user