forked from mtusi2026/mtusi2026lab7
75 lines
4.0 KiB
PowerShell
75 lines
4.0 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 }
|
|
|
|
# The only four services with a writable root filesystem. Their stock entrypoints
|
|
# prepare a persistent named volume before dropping privileges, so read_only would
|
|
# break startup. They still drop the six capabilities listed below.
|
|
$statefulWritableRoots = @{
|
|
postgres = @{ reason = 'entrypoint chown/chmod of /var/lib/postgresql/data before dropping to the postgres user'; data = '/var/lib/postgresql/data' }
|
|
redis = @{ reason = 'entrypoint writes /tmp/users.acl and persists RDB snapshots to the /data volume'; data = '/data' }
|
|
rabbitmq = @{ reason = 'entrypoint prepares /var/lib/rabbitmq and the Erlang cookie'; data = '/var/lib/rabbitmq' }
|
|
minio = @{ reason = 'Bitnami entrypoint prepares /bitnami/minio/data and the /certs links'; data = '/bitnami/minio/data' }
|
|
}
|
|
$statefulDroppedCapabilities = @('AUDIT_WRITE','MKNOD','NET_RAW','SETFCAP','SETPCAP','SYS_CHROOT')
|
|
|
|
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(-not @($service.tmpfs) -or @($service.tmpfs).Count -eq 0){
|
|
throw "$name has no private tmpfs for runtime scratch data"
|
|
}
|
|
|
|
if($name -in $statefulWritableRoots.Keys) {
|
|
if($service.read_only -eq $true){ throw "$name is declared as a stateful writable-root exception but is read_only" }
|
|
foreach($capability in $statefulDroppedCapabilities) {
|
|
if($service.cap_drop -notcontains $capability){
|
|
throw "$name does not drop $capability from its stateful exception profile"
|
|
}
|
|
}
|
|
$expectedData = $statefulWritableRoots[$name].data
|
|
$dataVolume = @($service.volumes) | Where-Object { $_.target -eq $expectedData }
|
|
if(-not $dataVolume){ throw "$name has no $expectedData persistent volume justifying the writable root" }
|
|
} else {
|
|
if($service.cap_drop -notcontains 'ALL'){ throw "$name must drop ALL capabilities" }
|
|
if($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: every service has no-new-privileges, a capability drop policy, private tmpfs and resource limits.'
|
|
Write-Host 'PASS: non-stateful services use a read-only root and drop ALL capabilities.'
|
|
foreach($name in ($statefulWritableRoots.Keys | Sort-Object)){
|
|
Write-Host "EXCEPTION $name writable root: $($statefulWritableRoots[$name].reason)"
|
|
}
|