Skip to content

Data Anonymization

Griffen Fargo edited this page Jun 30, 2026 · 2 revisions

Data Anonymization

strut can anonymize PII when syncing production databases to local development environments. This helps teams comply with GDPR, HIPAA, and other data protection requirements.

Quick Start

# 1. Configure anonymization rules
nano stacks/my-stack/anonymize.conf

# 2. Sync with anonymization
strut my-stack local sync-db --from prod --anonymize

Configuration

Create stacks/<stack>/anonymize.conf with rules in TABLE.COLUMN=strategy format:

# anonymize.conf — PII anonymization rules
users.email=fake_email
users.name=fake_name
users.phone=null
orders.address=fake_address
payments.card_number=mask
sessions.token=hash
users.id=preserve

This file is automatically scaffolded (commented out) when you run strut scaffold.

Strategies

Strategy What it does Example
fake_email Replace with user_<id>@example.com john@real.comuser_42@example.com
fake_name Replace with User <id> John SmithUser 42
null Set column to NULL 555-1234NULL
mask Keep first/last char, mask middle 41112222333344444***4
hash SHA256 hash on Postgres/MySQL; hex-encoding (HEX()) on SQLite, which lacks built-in SHA256 (preserves uniqueness) john@real.coma1b2c3d4...
fake_address Replace with generic address 123 Main St42 Test Avenue
preserve Keep original value (no-op) Use for non-PII columns

How It Works

  1. sync-db downloads the production database dump from VPS
  2. The dump is restored to the local database
  3. Anonymization SQL (UPDATE statements) is applied after restore
  4. Each rule generates a database-specific SQL statement

The engine supports Postgres, MySQL, and SQLite — it generates the correct SQL syntax for each.

Dry Run

Preview which columns would be anonymized without making changes:

DRY_RUN=true strut my-stack local sync-db --from prod --anonymize

Output:

[DRY-RUN] Anonymization plan:

  [DRY-RUN] users.email → fake_email
  [DRY-RUN] users.name → fake_name
  [DRY-RUN] users.phone → null

  3 rule(s) would be applied

[DRY-RUN] No changes made.

Safety Warnings

strut warns you when:

  • anonymize.conf exists but --anonymize was not passed — reminds you to anonymize
  • --anonymize is passed but no anonymize.conf exists — tells you to create one

Example: Full Workflow

# Scaffold a new stack (includes anonymize.conf template)
strut scaffold my-app

# Edit anonymize.conf with your PII columns
cat > stacks/my-app/anonymize.conf <<'EOF'
users.email=fake_email
users.full_name=fake_name
users.phone_number=null
users.address=fake_address
payments.card_number=mask
api_keys.secret=hash
EOF

# Sync production data with anonymization
strut my-app local sync-db --from prod --anonymize

# Verify — no real PII in local database
strut my-app local debug exec postgres "psql -U postgres -c 'SELECT email, full_name FROM users LIMIT 5;'"
# user_1@example.com | User 1
# user_2@example.com | User 2
# ...

Related

Clone this wiki locally