Разделение mTLS идентификаторов
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
[CmdletBinding()]
|
||||
param([string]$PkiDirectory, [int]$LeafValidityDays = 30, [switch]$Force)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
if([string]::IsNullOrWhiteSpace($PkiDirectory)){
|
||||
$PkiDirectory = Join-Path $PSScriptRoot '..\secrets\pki'
|
||||
}
|
||||
$pki = (Resolve-Path -LiteralPath $PkiDirectory).Path
|
||||
$caCertificate = Join-Path $pki 'service-ca\ca.crt'
|
||||
$caKey = Join-Path $pki 'service-ca\ca.key'
|
||||
if(-not (Test-Path -LiteralPath $caCertificate) -or -not (Test-Path -LiteralPath $caKey)){
|
||||
throw 'Existing local service CA certificate/private key is required'
|
||||
}
|
||||
|
||||
$opensslCommand = Get-Command openssl -ErrorAction SilentlyContinue
|
||||
$openssl = if($opensslCommand){ $opensslCommand.Source } else { $null }
|
||||
if(-not $openssl){
|
||||
$git = Get-Command git -ErrorAction SilentlyContinue
|
||||
if($git){
|
||||
$gitRoot = Split-Path (Split-Path $git.Source -Parent) -Parent
|
||||
$candidate = Join-Path $gitRoot 'usr\bin\openssl.exe'
|
||||
if(Test-Path -LiteralPath $candidate){ $openssl = $candidate }
|
||||
}
|
||||
}
|
||||
if(-not $openssl){ throw 'OpenSSL is required to extend the existing local PKI' }
|
||||
|
||||
function Invoke-OpenSsl([string[]]$Arguments) {
|
||||
$previous = $ErrorActionPreference
|
||||
$ErrorActionPreference = 'Continue'
|
||||
try {
|
||||
& $openssl @Arguments 2>$null
|
||||
$exitCode = $LASTEXITCODE
|
||||
} finally {
|
||||
$ErrorActionPreference = $previous
|
||||
}
|
||||
if($exitCode -ne 0){ throw "OpenSSL command failed: $($Arguments[0])" }
|
||||
}
|
||||
|
||||
function New-SerialHex {
|
||||
$bytes = [byte[]]::new(16)
|
||||
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
|
||||
$bytes[0] = $bytes[0] -band 0x7f
|
||||
return '0x' + (($bytes | ForEach-Object { $_.ToString('x2') }) -join '')
|
||||
}
|
||||
|
||||
function New-GatewayLeaf([string]$Name, [bool]$ServerAuth, [string]$DnsName, [string]$Uri) {
|
||||
$directory = Join-Path $pki $Name
|
||||
if((Test-Path -LiteralPath $directory) -and -not $Force){
|
||||
throw "$directory already exists; use -Force only for intentional gateway certificate rotation"
|
||||
}
|
||||
New-Item -ItemType Directory -Path $directory -Force | Out-Null
|
||||
$key = Join-Path $directory 'tls.key'
|
||||
$csr = Join-Path $directory 'request.csr'
|
||||
$certificate = Join-Path $directory 'tls.crt'
|
||||
$caCopy = Join-Path $directory 'ca.crt'
|
||||
$extensions = Join-Path $directory 'extensions.cnf'
|
||||
$eku = if($ServerAuth){ 'serverAuth' } else { 'clientAuth' }
|
||||
$sans = @()
|
||||
if($DnsName){ $sans += "DNS:$DnsName" }
|
||||
if($Uri){ $sans += "URI:$Uri" }
|
||||
$extensionText = @(
|
||||
'basicConstraints=critical,CA:FALSE',
|
||||
'keyUsage=critical,digitalSignature,keyEncipherment',
|
||||
"extendedKeyUsage=$eku",
|
||||
"subjectAltName=$($sans -join ',')",
|
||||
'authorityKeyIdentifier=keyid,issuer'
|
||||
) -join "`n"
|
||||
[System.IO.File]::WriteAllText($extensions, $extensionText + "`n", [System.Text.Encoding]::ASCII)
|
||||
try {
|
||||
Invoke-OpenSsl @('genpkey','-algorithm','RSA','-pkeyopt','rsa_keygen_bits:3072','-out',$key)
|
||||
Invoke-OpenSsl @('req','-new','-key',$key,'-subj',"/CN=$Name",'-out',$csr)
|
||||
Invoke-OpenSsl @('x509','-req','-in',$csr,'-CA',$caCertificate,'-CAkey',$caKey,
|
||||
'-set_serial',(New-SerialHex),'-days',"$LeafValidityDays",'-sha256','-extfile',$extensions,'-out',$certificate)
|
||||
Copy-Item -LiteralPath $caCertificate -Destination $caCopy -Force
|
||||
} finally {
|
||||
Remove-Item -LiteralPath $csr,$extensions -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
New-GatewayLeaf 'rabbit-api-gateway' $true 'rabbit-api-gateway' 'spiffe://hla-lab7/rabbit-api-gateway'
|
||||
New-GatewayLeaf 'rabbit-worker-gateway' $true 'rabbit-worker-gateway' 'spiffe://hla-lab7/rabbit-worker-gateway'
|
||||
New-GatewayLeaf 'otel-gateway' $true 'otel-gateway' 'spiffe://hla-lab7/otel-gateway'
|
||||
New-GatewayLeaf 'rabbit-api-gateway-client' $false '' 'spiffe://hla-lab7/rabbit-api-gateway'
|
||||
New-GatewayLeaf 'rabbit-worker-gateway-client' $false '' 'spiffe://hla-lab7/rabbit-worker-gateway'
|
||||
New-GatewayLeaf 'otel-gateway-client' $false '' 'spiffe://hla-lab7/otel-gateway'
|
||||
|
||||
Write-Host "Created six local gateway certificates under $pki; private keys were not printed."
|
||||
@@ -98,6 +98,9 @@ New-Leaf 'ingress' 'public-ca' $true @('localhost') @('127.0.0.1') @()
|
||||
foreach($name in 'api','postgres','redis','rabbitmq','minio','otel-collector','prometheus','loki','tempo'){
|
||||
New-Leaf $name 'service-ca' $true @($name) @() @("spiffe://hla-lab7/$name")
|
||||
}
|
||||
foreach($name in 'rabbit-api-gateway','rabbit-worker-gateway','otel-gateway'){
|
||||
New-Leaf $name 'service-ca' $true @($name) @() @("spiffe://hla-lab7/$name")
|
||||
}
|
||||
New-Leaf 'openbao' 'service-ca' $true @('openbao','localhost') @('127.0.0.1') @('spiffe://hla-lab7/openbao')
|
||||
New-Leaf 'grafana' 'service-ca' $true @('grafana','localhost') @('127.0.0.1') @('spiffe://hla-lab7/grafana')
|
||||
New-Leaf 'lb' 'lb-client-ca' $false @() @() @('spiffe://hla-lab7/lb')
|
||||
@@ -108,6 +111,9 @@ New-Leaf 'migration-client' 'workload-client-ca' $false @() @() @('spiffe://hla-
|
||||
New-Leaf 'collector-client' 'telemetry-client-ca' $false @() @() @('spiffe://hla-lab7/otel-collector')
|
||||
New-Leaf 'prometheus-client' 'telemetry-client-ca' $false @() @() @('spiffe://hla-lab7/prometheus')
|
||||
New-Leaf 'grafana-client' 'telemetry-client-ca' $false @() @() @('spiffe://hla-lab7/grafana')
|
||||
New-Leaf 'rabbit-api-gateway-client' 'service-ca' $false @() @() @('spiffe://hla-lab7/rabbit-api-gateway')
|
||||
New-Leaf 'rabbit-worker-gateway-client' 'service-ca' $false @() @() @('spiffe://hla-lab7/rabbit-worker-gateway')
|
||||
New-Leaf 'otel-gateway-client' 'service-ca' $false @() @() @('spiffe://hla-lab7/otel-gateway')
|
||||
|
||||
Write-Host "Created private PKI in $OutputDirectory"
|
||||
Write-Host "Public test CA: $(Join-Path $OutputDirectory 'public-ca\ca.crt')"
|
||||
|
||||
@@ -57,15 +57,15 @@ 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 \
|
||||
probe_ok rabbitmq-api rabbit-api-gateway:5671 rabbit-api-gateway \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key
|
||||
probe_rejected rabbitmq rabbitmq:5671 rabbitmq
|
||||
probe_ok rabbitmq-worker rabbit-worker-gateway:5671 rabbit-worker-gateway \
|
||||
-cert /run/pki/worker-client/tls.crt -key /run/pki/worker-client/tls.key
|
||||
|
||||
probe_ok otel-ingest-api otel-collector:4318 otel-collector \
|
||||
probe_ok otel-ingest-api otel-gateway:4318 otel-gateway \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key
|
||||
probe_ok otel-ingest-worker otel-collector:4318 otel-collector \
|
||||
probe_ok otel-ingest-worker otel-gateway:4318 otel-gateway \
|
||||
-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
|
||||
@@ -127,4 +127,7 @@ if($permissions -match 'messenger-(api|worker)\s+\.\*\s+\.\*\s+\.\*'){
|
||||
throw 'RabbitMQ ACL failure: an application identity has unrestricted permissions'
|
||||
}
|
||||
|
||||
& (Join-Path $PSScriptRoot 'test-service-mtls-identities.ps1') -EnvFile $EnvFile
|
||||
if(-not $?){ throw 'Service mTLS identity verification failed' }
|
||||
|
||||
Write-Host 'PASS: PostgreSQL, Redis and RabbitMQ runtime identities are separate and forbidden operations are denied.'
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
[CmdletBinding()]
|
||||
param([string]$EnvFile = '.env')
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
|
||||
$configText = & docker compose --env-file $EnvFile config --format json
|
||||
if($LASTEXITCODE -ne 0){ throw 'docker compose config failed' }
|
||||
$config = ($configText -join "`n") | ConvertFrom-Json
|
||||
if($config.services.api.environment.RABBITMQ_HOST -ne 'rabbit-api-gateway'){
|
||||
throw 'API does not use its dedicated RabbitMQ identity gateway'
|
||||
}
|
||||
if($config.services.worker.environment.RABBITMQ_HOST -ne 'rabbit-worker-gateway'){
|
||||
throw 'Worker does not use its dedicated RabbitMQ identity gateway'
|
||||
}
|
||||
foreach($serviceName in 'api','worker'){
|
||||
if($config.services.$serviceName.environment.OTEL_EXPORTER_OTLP_ENDPOINT -ne 'https://otel-gateway:4318'){
|
||||
throw "$serviceName bypasses the OTel identity gateway"
|
||||
}
|
||||
}
|
||||
|
||||
$probe = @'
|
||||
set -eu
|
||||
|
||||
amqp_bytes() {
|
||||
target=$1; cert=$2
|
||||
set +e
|
||||
printf 'AMQP\000\000\011\001' | timeout 4 openssl s_client -quiet \
|
||||
-verify_return_error -verify_hostname "$target" -connect "$target:5671" \
|
||||
-servername "$target" -CAfile /run/pki/service-ca/ca.crt \
|
||||
-cert "/run/pki/$cert/tls.crt" -key "/run/pki/$cert/tls.key" \
|
||||
>/tmp/amqp.out 2>/tmp/amqp.err
|
||||
set -e
|
||||
wc -c </tmp/amqp.out
|
||||
}
|
||||
|
||||
api_valid=$(amqp_bytes rabbit-api-gateway api-client)
|
||||
worker_valid=$(amqp_bytes rabbit-worker-gateway worker-client)
|
||||
api_wrong_role=$(amqp_bytes rabbit-api-gateway migration-client)
|
||||
worker_wrong_role=$(amqp_bytes rabbit-worker-gateway api-client)
|
||||
[ "$api_valid" -gt 0 ]
|
||||
[ "$worker_valid" -gt 0 ]
|
||||
[ "$api_wrong_role" -eq 0 ]
|
||||
[ "$worker_wrong_role" -eq 0 ]
|
||||
set +e
|
||||
printf 'AMQP\000\000\011\001' | timeout 4 openssl s_client -quiet \
|
||||
-verify_return_error -verify_hostname rabbit-api-gateway \
|
||||
-connect rabbit-api-gateway:5671 -servername rabbit-api-gateway \
|
||||
-CAfile /run/pki/service-ca/ca.crt >/tmp/amqp-no-cert.out 2>/tmp/amqp-no-cert.err
|
||||
set -e
|
||||
[ "$(wc -c </tmp/amqp-no-cert.out)" -eq 0 ]
|
||||
|
||||
otel_status() {
|
||||
cert=$1
|
||||
set +e
|
||||
printf 'POST /v1/traces HTTP/1.1\r\nHost: otel-gateway\r\nContent-Type: application/x-protobuf\r\nContent-Length: 0\r\nConnection: close\r\n\r\n' | \
|
||||
timeout 5 openssl s_client -quiet -verify_return_error \
|
||||
-verify_hostname otel-gateway -connect otel-gateway:4318 -servername otel-gateway \
|
||||
-CAfile /run/pki/service-ca/ca.crt -cert "/run/pki/$cert/tls.crt" \
|
||||
-key "/run/pki/$cert/tls.key" >/tmp/otel.out 2>/tmp/otel.err
|
||||
set -e
|
||||
sed -n '1p' /tmp/otel.out | tr -d '\r'
|
||||
}
|
||||
|
||||
api_otel=$(otel_status api-client)
|
||||
worker_otel=$(otel_status worker-client)
|
||||
wrong_otel=$(otel_status migration-client)
|
||||
echo "$api_otel" | grep -Eq '^HTTP/1\.1 200 '
|
||||
echo "$worker_otel" | grep -Eq '^HTTP/1\.1 200 '
|
||||
echo "$wrong_otel" | grep -Eq '^HTTP/1\.1 403 '
|
||||
set +e
|
||||
printf 'POST /v1/traces HTTP/1.1\r\nHost: otel-gateway\r\nContent-Length: 0\r\nConnection: close\r\n\r\n' | \
|
||||
timeout 4 openssl s_client -quiet -verify_return_error -verify_hostname otel-gateway \
|
||||
-connect otel-gateway:4318 -servername otel-gateway \
|
||||
-CAfile /run/pki/service-ca/ca.crt >/tmp/otel-no-cert.out 2>/tmp/otel-no-cert.err
|
||||
set -e
|
||||
grep -Eq '^HTTP/1\.1 400 ' /tmp/otel-no-cert.out
|
||||
grep -q 'No required SSL certificate was sent' /tmp/otel-no-cert.out
|
||||
|
||||
set +e
|
||||
openssl s_client -brief -tls1_2 -verify_return_error -verify_hostname rabbitmq \
|
||||
-connect rabbitmq:5671 -servername rabbitmq -CAfile /run/pki/service-ca/ca.crt \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key \
|
||||
</dev/null >/tmp/direct-rabbit 2>&1
|
||||
direct_rabbit=$?
|
||||
openssl s_client -brief -tls1_2 -verify_return_error -verify_hostname otel-collector \
|
||||
-connect otel-collector:4318 -servername otel-collector -CAfile /run/pki/service-ca/ca.crt \
|
||||
-cert /run/pki/api-client/tls.crt -key /run/pki/api-client/tls.key \
|
||||
</dev/null >/tmp/direct-otel 2>&1
|
||||
direct_otel=$?
|
||||
set -e
|
||||
[ "$direct_rabbit" -ne 0 ] && grep -Eq 'unknown ca|certificate unknown|bad certificate' /tmp/direct-rabbit
|
||||
[ "$direct_otel" -ne 0 ] && grep -Eq 'unknown ca|certificate unknown|bad certificate' /tmp/direct-otel
|
||||
|
||||
echo 'PASS: API/worker use distinct broker identities; trusted-CA wrong roles receive no AMQP access or OTel ingest; direct backend bypass is rejected.'
|
||||
'@
|
||||
|
||||
$normalizedProbe = $probe.Replace("`r", '')
|
||||
$previous = $ErrorActionPreference
|
||||
$ErrorActionPreference = 'Continue'
|
||||
try {
|
||||
$output = @($normalizedProbe | & docker compose --env-file $EnvFile run --rm -T --no-deps --entrypoint /bin/sh nginx -s 2>&1) -join "`n"
|
||||
$exitCode = $LASTEXITCODE
|
||||
} finally {
|
||||
$ErrorActionPreference = $previous
|
||||
}
|
||||
if($exitCode -ne 0){ throw 'Service mTLS identity verification failed' }
|
||||
Write-Host $output
|
||||
Reference in New Issue
Block a user