Skip to content

Per-tenant credential isolation on PaaS — secrets, DB users, file system boundaries

A shared environment with 40 tenants and a single DB superuser shared by every PHP-FPM pool is one SQL injection away from a cross-tenant data breach. Isolation by tenant slug starts at provisioning, not at the WAF.

You inherit a shared PaaS environment that hosts 40 customer applications. The first thing you find: a single app user owns every database, the credentials are in a file on disk readable by every vhost, and the file uploads directory for tenant_42 is writable by tenant_7’s PHP process because they all run as nobody. The platform has not yet been breached. The platform has not yet been audited either. The interesting question is which event arrives first.

What’s happening

Credential isolation on multi-tenant PaaS has four boundaries that all need to hold. Any one of them broken makes the other three irrelevant.

  1. Database boundary. Each tenant has its own database role with LOGIN, password set, and GRANT ALL only on that tenant’s database — never SUPERUSER, never cross-database USAGE. The application connects with that tenant’s role, never the master role.
  2. Secret boundary. Each tenant’s secrets (DB password, S3 key, third-party API tokens) live in a location only that tenant’s process can read. On Jelastic, this means env_vars set on the per-tenant container with chmod 600 on any mounted secret file. Never a secrets.yml accessible to every PHP vhost.
  3. Filesystem boundary. Each tenant’s writable directory is owned by a per-tenant system user with mode 750 — not 755, not 770. The PHP-FPM pool for that tenant runs as that user. A pool running as a different user cannot read the directory at all.
  4. Process boundary. PHP-FPM, Node, or whatever runtime serves the tenant has its own pool, with user = tenant_<slug> set in the pool config. A code path in tenant A cannot read tenant B’s session files, tenant B’s compiled templates, or tenant B’s .env.

Most platforms get boundary 1 right and get one or more of 2, 3, 4 wrong. Boundary 4 is the one that’s typically skipped because PHP-FPM pool-per-tenant is more setup — and the cost of skipping it is that any RCE in any tenant has read access to every other tenant on the box.

How to verify

Inventory database roles vs tenants:

-- One role per tenant, no shared master
SELECT r.rolname, r.rolsuper, r.rolcanlogin,
       array_agg(d.datname) FILTER (WHERE has_database_privilege(r.rolname, d.datname, 'CONNECT')) AS dbs
FROM pg_roles r
CROSS JOIN pg_database d
WHERE r.rolname NOT LIKE 'pg_%'
  AND r.rolcanlogin
GROUP BY r.rolname, r.rolsuper, r.rolcanlogin
ORDER BY r.rolname;

If any application role shows rolsuper = t, or if any role can CONNECT to more than one tenant database, that’s the breach path.

Verify the PHP-FPM pool config has a per-tenant user (not the default nobody):

# Show pool user from every pool config
grep -E "^user\s*=" /etc/php-fpm.d/*.conf
# tenant_42.conf:  user = tenant_42
# tenant_43.conf:  user = tenant_43

Verify the writable directory permissions on each tenant:

for d in /data/tenants/*/; do
  slug=$(basename "$d")
  owner=$(stat -c %U "$d")
  mode=$(stat -c %a "$d")
  if [[ "$owner" != "tenant_${slug}" || "$mode" != "750" ]]; then
    echo "WRONG: $d owner=$owner mode=$mode (expected tenant_${slug} 750)"
  fi
done

Verify secrets are not in a globally readable location:

# Any secrets file that's world-readable is the breach
find /data/tenants -type f \( -name '.env' -o -name 'secrets*' \) \
  -not -perm 600 -not -perm 640 -ls

The fix

A provisioning script that creates all four boundaries at the moment a tenant is onboarded, on a Jelastic shared environment with one PHP-FPM container and one Postgres node.

#!/usr/bin/env bash
set -euo pipefail

TENANT_SLUG="${1:?usage: $0 <tenant-slug>}"
[[ "$TENANT_SLUG" =~ ^[a-z][a-z0-9-]+$ ]] || { echo "bad slug"; exit 1; }

DB_USER="tenant_${TENANT_SLUG//-/_}"
DB_NAME="$DB_USER"
DB_PASS=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | head -c 32)
SYS_USER="$DB_USER"
TENANT_HOME="/data/tenants/${TENANT_SLUG}"
SECRET_FILE="${TENANT_HOME}/.env"

echo "==> Boundary 1: per-tenant Postgres role + database"
psql -h pgsql.shared-prod -U pgadmin -d postgres <<SQL
CREATE ROLE ${DB_USER} LOGIN PASSWORD '${DB_PASS}';
CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};
REVOKE ALL ON DATABASE ${DB_NAME} FROM PUBLIC;
GRANT CONNECT ON DATABASE ${DB_NAME} TO ${DB_USER};
SQL

echo "==> Boundary 3: per-tenant system user + writable home"
if ! id -u "${SYS_USER}" >/dev/null 2>&1; then
  useradd --system --no-create-home --shell /usr/sbin/nologin "${SYS_USER}"
fi
mkdir -p "${TENANT_HOME}"
chown "${SYS_USER}:${SYS_USER}" "${TENANT_HOME}"
chmod 750 "${TENANT_HOME}"

echo "==> Boundary 2: secrets file readable only by tenant user"
cat > "${SECRET_FILE}" <<EOF
DB_HOST=pgsql.shared-prod
DB_NAME=${DB_NAME}
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASS}
S3_BUCKET=tenant-${TENANT_SLUG}-uploads
S3_ACCESS_KEY=$(openssl rand -hex 16)
S3_SECRET_KEY=$(openssl rand -base64 32 | tr -d /=+)
APP_KEY=base64:$(openssl rand -base64 32)
EOF
chown "${SYS_USER}:${SYS_USER}" "${SECRET_FILE}"
chmod 600 "${SECRET_FILE}"

echo "==> Boundary 4: per-tenant PHP-FPM pool"
cat > "/etc/php-fpm.d/${TENANT_SLUG}.conf" <<EOF
[${TENANT_SLUG}]
user = ${SYS_USER}
group = ${SYS_USER}
listen = /run/php-fpm/${TENANT_SLUG}.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
pm = ondemand
pm.max_children = 8
pm.process_idle_timeout = 60s
chdir = ${TENANT_HOME}
clear_env = no
catch_workers_output = yes
php_admin_value[open_basedir] = ${TENANT_HOME}:/tmp/${SYS_USER}
php_admin_value[upload_tmp_dir] = /tmp/${SYS_USER}
php_admin_value[session.save_path] = /tmp/${SYS_USER}/sessions
EOF

# Per-tenant tmp dir (also boundary 3)
install -d -o "${SYS_USER}" -g "${SYS_USER}" -m 750 \
  "/tmp/${SYS_USER}" "/tmp/${SYS_USER}/sessions"

systemctl reload php-fpm

echo "==> Done. Tenant ${TENANT_SLUG} provisioned with isolation."

A Terraform-friendly form using Jelastic provider variables — env vars are the canonical secret store on Jelastic, since they’re set at the container level and aren’t readable from another container:

resource "random_password" "tenant_db" {
  length  = 32
  special = false
}

resource "jelastic_environment_property" "tenant_secrets" {
  env_name = "shared-prod"
  node_group = "cp"     # compute / PHP node group
  properties = {
    "TENANT_${upper(replace(var.tenant_slug, "-", "_"))}_DB_PASSWORD" = random_password.tenant_db.result
    "TENANT_${upper(replace(var.tenant_slug, "-", "_"))}_DB_USER"     = "tenant_${replace(var.tenant_slug, "-", "_")}"
    "TENANT_${upper(replace(var.tenant_slug, "-", "_"))}_DB_NAME"     = "tenant_${replace(var.tenant_slug, "-", "_")}"
  }
}

Rotation is the second half. A quarterly rotation runbook should: generate a new password, write it to a new tagged secret version, deploy the application referencing the new tag, drop the old password in Postgres only after the deploy is healthy. Never rotate by overwriting in place — that’s a guaranteed outage when a process is still holding the old credential.

For tenant offboarding, the inverse runs in order: drop PHP-FPM pool, drop system user, archive directory, drop database, drop role. Skipping any step leaves an orphan you’ll find six months later in an audit.

Stack Harbor builds tenant isolation pipelines under environment management for SaaS operators on managed PaaS — provisioning that creates all four boundaries at once and an offboarding playbook that doesn’t leave orphan roles. If your platform has a single DB user across many tenants and you’d like to migrate to per-tenant isolation without downtime, our agency partner program handles the rollout — book an isolation review.