Контейнерный hardening и dependency/image scan

This commit is contained in:
EgorAnti
2026-11-27 13:36:34 +03:00
parent dd3427aa4c
commit 415edbf722
9 changed files with 379 additions and 11 deletions
+34 -4
View File
@@ -8,7 +8,17 @@ $json = & docker compose --profile verification --env-file $EnvFile config --for
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')
# 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
@@ -22,9 +32,25 @@ foreach($property in $config.services.PSObject.Properties) {
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"
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"
@@ -41,4 +67,8 @@ foreach($property in $config.services.PSObject.Properties) {
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.'
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)"
}