Skip to content

feat(encryption): bind ciphertexts to their job, config section, scheduled purge, TestQueue encryption (#38) - #39

Merged
CodingAnarchy merged 1 commit into
masterfrom
feat/encryption-followups
Oct 8, 2026
Merged

CodingAnarchy merged 1 commit into
masterfrom
feat/encryption-followups

Conversation

@CodingAnarchy

Copy link
Copy Markdown
Owner

Closes #38

Follow-ups to the payload encryption from #36.

Bind ciphertext to the job (security)

  • Job payloads are now encrypted with AEAD associated data: a context string, the job id, queue name, key id, algorithm, the job's pii_fields, and the fields that were actually encrypted. The fields are length-prefixed (encryption::job_payload::job_associated_data). Decryption recomputes the associated data from the stored row. These changes make decryption fail:
    • swapping encrypted_payload / nonce / tag between rows
    • moving a row to another queue
    • changing its key id, algorithm or PII fields
  • Format versioning: EncryptionMetadata.format_version (#[serde(default)]) is written into encryption_metadata.
    • 1 (FORMAT_JOB_BOUND): written for every newly sealed job.
    • 0 or missing (FORMAT_UNBOUND): decrypted without associated data, so payloads written by the current release still read. They are not re-encrypted.
    • Any other value: refused.
    • Setting a bound row back to 0 does not bypass the binding, because the AEAD tag then fails.
  • Archive and restore keep the id, queue name and encryption columns, so restored jobs decrypt. The existing archive→restore→decrypt test covers this.
  • New public API: EncryptionEngine::{encrypt,decrypt}_payload_with_associated_data.
  • Backend change: the shared SQL is unchanged. The only addition is one line per EncryptionMetadata literal in postgres.rs / mysql.rs, which reads format_version.

[encryption] config section

  • HammerworkConfig::encryption: PayloadEncryptionConfig is optional (#[serde(default)]), so existing files still load. Its fields:
    • enabled, algorithm, key_source, key_id, compression
    • default_retention_secs, purge_interval_secs
    • encrypted_queues
    • decryption_keys (decrypt-only legacy keys)
  • JobQueue::from_config calls the new apply_encryption_config. It builds the engine with new_with_pool, so aws:// and gcp:// keys work, adds the decryption keys, and sets with_encryption and the new with_encrypted_queues. A configured queue therefore encrypts without any code changes.
  • Secrets: key sources are KeySourceRef. The allowed forms are env://VAR, aws://, gcp://, vault:// and azure://. Static or raw keys are rejected at load, and the error does not echo the value. Debug and save_to_file only ever contain the references.
  • Fails closed on any of these:
    • unknown fields (deny_unknown_fields) or an unknown algorithm
    • a key that cannot be loaded
    • encrypted_queues set while disabled
    • a decryption key that reuses the active key id
    • purge_interval_secs = 0
    • an enabled section in a build without the encryption feature
  • from_env reads HAMMERWORK_ENCRYPTION_* and returns an error on invalid values.

Scheduled retention purge

  • WorkerPool::with_encrypted_job_purge(interval) and without_encrypted_job_purge(). It is off by default.
  • WorkerPool::from_hammerwork_config(worker, &config) takes the interval from encryption.purge_interval_secs.
  • Maintenance passes go through a new spawn_periodic. Each pass is spawned as its own task and awaited, so neither the shutdown signal nor the supervisor's abort() cancels a pass mid-transaction (the fix: never abandon database transactions on worker shutdown #35 pattern). The stale-job reaper now uses it too, because it previously ran the DB call inside the select!.

TestQueue encryption

TestQueue::with_encryption, with_encrypted_queues, encryption_engine and decrypt_job share queue::PayloadEncryption (new, pub(crate)) with JobQueue. That gives TestQueue the same behaviour as the database backends:

  • sealing in enqueue / batch / workflow / cron
  • fail-closed enqueue when no engine is configured
  • job-bound ciphertexts
  • archived ciphertext hidden until restore
  • purge_expired_encrypted_jobs against the MockClock

Archived encrypted jobs

Decision: restore is required, and this is documented. decrypt_job on an archived job (no ciphertext loaded) now returns an error that says to call restore_archived_job. Tests cover this on both backends and on TestQueue.

Tests

  • Unit tests:
    • job_payload: swapped ciphertexts, changed queue / PII fields / encrypted fields, format downgrade, unknown format, legacy decrypt, AAD unambiguity
    • config: TOML and file round-trip, optional section, rejected keys and typos, validation, env vars, engine config mapping
    • spawn_periodic: runs until shutdown, never cancels a pass in flight
    • TestQueue encryption: 7 tests
  • Integration tests on both backends (tests/payload_encryption_tests.rs):
    • swapped ciphertexts fail, and a worker never runs the handler
    • a moved job fails
    • a legacy unbound payload decrypts and runs
    • a WorkerPool purges expired jobs
    • from_config encrypts encrypted_queues, decrypts old key ids, and fails closed
    • the archived-job decrypt error

Verified locally:

  • cargo fmt --check and workspace clippy with --all-targets --all-features -D warnings
  • clippy for the CI feature-matrix entries
  • --lib tests
  • the full Postgres and MySQL integration suites, CI style
  • doctests
  • cargo +1.91.1 check

🤖 Generated with Claude Code

…duled purge (#38)

- Job payloads are encrypted with AEAD associated data covering the job id,
  queue name, key id, algorithm, PII fields and encrypted fields, so swapped
  ciphertexts or a job moved to another queue fail to decrypt. The format is
  recorded as `format_version` in `encryption_metadata`; payloads without it
  (written before this change) still decrypt without associated data.
- `[encryption]` section in HammerworkConfig (PayloadEncryptionConfig),
  consumed by JobQueue::from_config. Keys are only referenced (env://, aws://,
  gcp://, vault://, azure://), never stored, printed or saved. Fails closed on
  invalid sections. JobQueue::with_encrypted_queues encrypts whole queues.
- WorkerPool::with_encrypted_job_purge / from_hammerwork_config schedule the
  retention purge. Maintenance passes (purge and stale-job reaper) run in
  their own task so shutdown never cancels one mid-transaction.
- TestQueue applies the same sealing, redaction and decryption semantics.
- Archived encrypted jobs must be restored to decrypt; decrypt_job says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CodingAnarchy
CodingAnarchy force-pushed the feat/encryption-followups branch from 0b88650 to e7b2b9c Compare October 8, 2026 13:35
@CodingAnarchy
CodingAnarchy merged commit 711bd0b into master Oct 8, 2026
16 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.

Encryption follow-ups: bind ciphertext to job id, config section, scheduled retention purge

1 participant