Skip to content

fix(encryption): close the encryption and data-safety gaps of #64 (W3) - #67

Merged
CodingAnarchy merged 2 commits into
masterfrom
fix/encryption-data-safety
Oct 9, 2026
Merged

CodingAnarchy merged 2 commits into
masterfrom
fix/encryption-data-safety

Conversation

@CodingAnarchy

Copy link
Copy Markdown
Owner

Part of #64: workstream W3 (encryption and data safety). Fixes findings C3, H1, H2, H4, M14, M15, M17.

What changes

C3: cargo hammerwork backup loses encrypted payloads and most job attributes

  • backup create (JSON, format 2.0) now writes every column of hammerwork_jobs. The column list comes from information_schema, so new columns from future migrations are included without anyone keeping a list in sync. A column type the backup can't copy is an error, never silently dropped.
  • Values use one encoding on both backends, so a backup can be restored into either one:
    • bytes as base64
    • timestamps as RFC 3339 with microseconds
    • PostgreSQL arrays as JSON arrays
  • backup restore writes every column back:
    • It checks the whole file before writing anything, then inserts in one transaction.
    • Existing ids are skipped and counted, never overwritten or duplicated. This keeps the existing behaviour, now documented. MySQL no longer uses INSERT IGNORE, which turned conversion errors into warnings. It now does a plain INSERT and treats a duplicate-key error as "skipped".
    • A non-null value for a column the target table lacks is an error.
    • Format 1.0 backups still restore.
  • CSV stays an export and is documented as not restorable.
  • New module: cargo-hammerwork/src/utils/job_rows.rs.

H1: the CLI and the dashboard enqueue plaintext on encrypted queues

  • New PayloadEncryptionConfig::load(path). It reads only the [encryption] section of the application's hammerwork.toml, then applies HAMMERWORK_ENCRYPTION_*.
  • CLI. config set encryption_config <path> or HAMMERWORK_ENCRYPTION_CONFIG. These commands now build their queue with apply_encryption_config:
    • job enqueue
    • batch enqueue
    • cron create
    • workflow create
  • Dashboard. HAMMERWORK_ENCRYPTION_CONFIG is read where the queue is built in server.rs. The web change is kept to queue construction plus the M14 Debug impls, to avoid colliding with W1.
  • Fail safe. If encryption is enabled but the key can't be loaded, the CLI refuses to write and the dashboard refuses to start.
  • New JobQueue::with_plaintext_guard(true). Used by both tools; off by default for applications. When on, enqueue, enqueue_batch, enqueue_workflow and enqueue_cron_job refuse to store a plaintext job on a queue that already holds encrypted jobs, and write nothing. This also covers tools that were given no settings at all. A queue that has never held an encrypted job can't be detected this way, which the docs say.
  • job enqueue --encrypt / --pii-field <f> encrypt a job with the application's key on any queue.
  • cargo-hammerwork and hammerwork-web now enable hammerwork's encryption feature. cargo-hammerwork gains aws-kms, gcp-kms, vault-kms and azure-kv pass-through features.

H2: get_dead_jobs / get_dead_jobs_by_queue drop the encryption metadata

  • Both backends now select JOB_SELECT_FIELDS into JobRow. DeadJobRow is removed.
  • The new tests read an encrypted job back through every read path on both backends and on TestQueue, then decrypt it:
    • get_job
    • get_ready_jobs
    • get_dead_jobs
    • get_dead_jobs_by_queue
    • get_jobs_completed_in_range
    • get_batch_jobs
    • get_workflow_jobs
    • get_recurring_jobs

H4: KMS data-key rotation sends other workers' jobs to Dead

  • Engines built with new_with_pool keep an owned store (PoolStore).
  • When a payload doesn't decrypt with the keys an engine holds, it loads the stored versions it is missing, decrypts them with the KMS and retries. Lookup is by the payload's key id, so other key ids stored in the table are found too.
  • After that reload the engine encrypts with the newest active version.
  • New EncryptionEngine::reload_kms_keys().
  • If the reload itself fails (database or KMS down), the run fails and is retried as before. Tampered payloads still fail.
  • Tests:
    • two engines sharing an in-memory store across two rotations;
    • a DB test where a worker started before another process's rotation still completes the job, on PostgreSQL and MySQL.

M14: secrets in Debug output, logs and config show

  • Redacting Debug impls:
    • DatabaseConfig
    • WebhookConfig and WebhookAuth
    • AlertTarget
    • StreamBackend
    • ExternalKmsConfig
    • the Azure credential choice
    • the CLI Config
    • web DashboardConfig and AuthConfig
  • Adding a webhook logs only the scheme and host of its URL.
  • config show masks the database password.
  • New hammerwork::config::redact_url and redact_url_path. The CLI's redact_url now delegates to the library one.
  • Each type has a test asserting its Debug output does not contain the secret.

M15: Vault/Azure key material is padded instead of rejected

  • Vault (key field) and Azure material must be exactly 32 bytes, otherwise EncryptionError::InvalidConfiguration.
  • This applies to engine data keys and KeyManager master keys alike. Passphrases are no longer hashed into keys.

M17: key material is never zeroized

  • zeroize was already in Cargo.lock (1.9.1). It is added as an optional dependency of the encryption feature.
  • Engine keys are now Arc<Zeroizing<Vec<u8>>>, shared rather than cloned per operation.
  • These are also held in Zeroizing:
    • KMS data keys
    • KeyManager master, root, retired and cached keys
    • keys read from env, files, Vault and Azure
  • Public APIs that already returned Vec<u8> (KeyManager::get_key) still do; the caller owns that copy.

Behaviour changes for users

  • vault:// and azure:// keys that relied on padding or passphrase hashing no longer load. Jobs encrypted under the old derived key need draining or re-enqueueing before the switch. This is in the CHANGELOG upgrade notes.
  • CLI JSON backups are format 2.0 (all columns; priority is numeric). Restore still reads 1.0.
  • The CLI's job-writing commands, and the dashboard's POST /api/jobs, now error instead of storing plaintext on a queue that holds encrypted jobs. The dashboard doesn't start if its configured key can't be loaded.
  • KMS-backed engines pick up rotations made by other processes without a restart.

Tests

Run locally against PostgreSQL 16 and MySQL 8, the way CI runs them:

  • cargo test --workspace --all-features --no-fail-fast -- --include-ignored --skip mysql: 1755 passed, 0 failed
  • cargo test --workspace --all-features --no-fail-fast -- --include-ignored mysql, with both URLs pointing at MySQL: 145 passed, 0 failed
  • cargo run -p postgres-integration and cargo run -p mysql-integration: pass
  • cargo test -p hammerwork --doc --all-features (523 passed) and cargo test -p hammerwork --doc (186 passed)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • The CI feature matrix ("", postgres, mysql, postgres,mysql, postgres,encryption, mysql,tracing, postgres,streaming, postgres,webhooks) with clippy -p hammerwork --lib --tests --no-default-features: clean
  • cargo +1.91.1 check --workspace --all-features --all-targets --locked and cargo metadata --locked: pass. Lockfile diff: two dependency entries.

New database tests (each on both backends):

  • backup_restores_every_column: compares full-row fingerprints computed by the database before and after a wipe and restore. Encrypted jobs still decrypt afterwards, a second restore is a no-op, and a backup carrying an unknown column restores nothing.
  • enqueue_follows_the_application_encryption
  • every_read_path_keeps_the_ciphertext
  • plaintext_guard_refuses_plaintext_on_encrypted_queues
  • worker_survives_a_rotation_by_another_process

Docs updated: docs/encryption.md (fleet rotation, Vault/Azure key size, the new "The CLI and the web dashboard" section, read paths, memory protection), README, the CLI README and the web README. CHANGELOG has entries under Security and Changed (upgrade notes).

🤖 Generated with Claude Code

CodingAnarchy and others added 2 commits October 9, 2026 19:00
Part of #64 (workstream W3): C3, H1, H2, H4, M14, M15, M17.

- C3: `backup create` writes every column of hammerwork_jobs (read from
  information_schema, one encoding for both backends) and `backup restore`
  writes them back in one transaction, skipping existing ids. Encrypted
  payloads, cron, timeouts, retry strategies, dependencies, workflows,
  batches, results, tracing and retention round-trip.
- H1: the CLI and the dashboard load the application's [encryption]
  settings (PayloadEncryptionConfig::load) and fail instead of writing
  plaintext; JobQueue::with_plaintext_guard refuses plaintext jobs on
  queues that hold encrypted jobs. `job enqueue --encrypt/--pii-field`.
- H2: get_dead_jobs / get_dead_jobs_by_queue select the full row, so
  decrypt_job works on dead jobs.
- H4: KMS-backed engines reload stored key versions on a decryption
  failure (and reload_kms_keys), so workers survive a rotation by another
  process.
- M14: redacting Debug for configs holding database URLs, webhook, alert,
  streaming and KMS secrets and the dashboard password hash; webhook URLs
  are logged without their path; `config show` masks the password.
- M15: Vault and Azure key material must be exactly 32 bytes.
- M17: key material is held in zeroize::Zeroizing and shared, not copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CodingAnarchy
CodingAnarchy force-pushed the fix/encryption-data-safety branch from 411f0bc to ec9a5db Compare October 9, 2026 23:01
@CodingAnarchy
CodingAnarchy merged commit 9db5313 into master Oct 9, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant