Files
mtusi2026lab7/scripts/check-compose-hardening.ps1
T

45 lines
2.1 KiB
PowerShell

[CmdletBinding()]
param([string]$EnvFile = '.env')
$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
$json = & docker compose --profile verification --env-file $EnvFile config --format json
if($LASTEXITCODE -ne 0){ throw 'docker compose config failed' }
$config = $json | ConvertFrom-Json
$allowedPublished = @{ nginx = 8443; openbao = 8200; grafana = 3000 }
$statefulWritableRoots = @('postgres','redis','rabbitmq','minio')
foreach($property in $config.services.PSObject.Properties) {
$name = $property.Name
$service = $property.Value
if($service.privileged -eq $true){ throw "$name is privileged" }
if($service.network_mode -eq 'host'){ throw "$name uses host networking" }
if(-not $service.pids_limit -or -not $service.cpus -or -not $service.mem_limit){
throw "$name is missing pids/cpu/memory limits"
}
if(-not ($service.security_opt -contains 'no-new-privileges:true')){
throw "$name is missing no-new-privileges"
}
if(-not $service.cap_drop -or $service.cap_drop.Count -eq 0){ throw "$name has no capability drop policy" }
if($name -notin $statefulWritableRoots -and $service.read_only -ne $true){
throw "$name must use a read-only root filesystem"
}
foreach($volume in @($service.volumes) | Where-Object { $null -ne $_ }) {
if($volume.source -match 'docker\.sock' -or $volume.target -match 'docker\.sock'){
throw "$name mounts the Docker socket"
}
}
foreach($port in @($service.ports) | Where-Object { $null -ne $_ }) {
if(-not $allowedPublished.ContainsKey($name) -or [int]$port.target -ne $allowedPublished[$name]){
throw "$name publishes unexpected port $($port.published):$($port.target)"
}
if($port.host_ip -notin @('127.0.0.1','::1')){ throw "$name port is not loopback-bound" }
}
}
foreach($expected in $allowedPublished.Keys){
if(-not $config.services.$expected.ports){ throw "$expected operator/public port is missing" }
}
Write-Host 'PASS: ports, privileges, capabilities, filesystems and resource limits satisfy the local hardening contract.'