AWS KMS and credential storage¶
Portal passwords had to be recoverable for Playwright login, so a one-way hash was not enough. Signoff used envelope encryption: a customer-managed KMS key wraps a per-save data key (DEK); the DEK encrypts username and password with AES-GCM. Postgres stores ciphertext only.
AWS setup (as deployed)¶
Region used: us-east-2.
- Customer managed KMS key →
AWS_KMS_KEY_ID(id or ARN). - Two IAM users (split encrypt/decrypt):
timecard-web— intendedkms:GenerateDataKeyon that CMK. Env:AWS_ACCESS_KEY_ID_ENCRYPT,AWS_SECRET_ACCESS_KEY_ENCRYPT(API service).timecard-celery— intendedkms:Decrypton that CMK. Env:AWS_ACCESS_KEY_ID_DECRYPT,AWS_SECRET_ACCESS_KEY_DECRYPT(worker).AWS_DEFAULT_REGIONset on both services.
src/kms/config.py loads mode-specific keys (encrypt / decrypt). Fallback to shared AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY exists but collapses the split—production used the two pairs.
src/env.example shows a placeholder ARN in us-east-1; the live key was in us-east-2.
On save (web)¶
- Playwright validates portal login.
KMSEncryptService.generate_data_key()→GenerateDataKeywithKeySpec=AES_256→ plaintext DEK +CiphertextBlob.encrypt_aes_gcmencrypts username and password separately (12-byte random nonce each, no AAD).- Overwrite local DEK with zeros;
obfuscate_credentialreplaces username/password variables with random-length null bytes (drops Python refs; does not guarantee immutable string GC). - Upsert
credentialsrow; bumpdek_versionon update. Clearneeds_password.
On Sunday run (worker)¶
- Record
dek_versionon theTimecardRun. KMSDecryptService.decrypt_dek(dek_wrapped)→Decrypt(key id is inside the blob).- Decrypt username/password; pass to Playwright.
- If credentials changed while queued, reload and use the newer version.
Plaintext password exists in the worker for the duration of login. That was required to type into the portal.
What this did and did not solve¶
Helped
- DB dump alone is not enough while CMK + decrypt principal stay private.
- Compromised web AWS key alone cannot decrypt existing rows (no
kms:Decrypt). - Destroying the CMK makes old dumps unreadable.
Did not solve
- Worker env + DB = full recovery of all stored logins.
- Ciphertext is not bound to user id (no AAD).
- Python string immutability limits in-process scrubbing.