perf(api): cache Fernet instance in _get_fernet() - #6230
Conversation
Adds @lru_cache(maxsize=1) to avoid recreating the Fernet instance on every encrypt/decrypt call. Reduces SHA-256 + base64 overhead for vault operations under load.
|
@shafeeq27edu-ai is attempting to deploy a commit to the agenta projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review. 📝 WalkthroughSummary by CodeRabbit
WalkthroughThe crypting utility imports ChangesCrypting Cache
Estimated code review effort: 1 (Trivial) | ~2 minutes Merge Risk: 🟡 Moderate · up to The change reuses one Fernet instance per process, but a runtime encryption-key change could leave that process using stale key material and unable to interoperate with processes using the new key. Required API validation checks are also not recorded as completed, so merge should wait for key-lifetime confirmation and those checks. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Description
This PR fixes a performance issue in the vault encryption utility.
_get_fernet()was recreating the Fernet instance on everyencrypt()/decrypt()call by recomputing SHA-256 and base64-encoding the key material each time. Under load (vault operations, secret reads on every request), this is unnecessary overhead.Summary
What changed? Added
@lru_cache(maxsize=1)to_get_fernet()inapi/oss/src/utils/crypting.py.Why was this change needed? Every call to
encrypt()ordecrypt()triggered_get_fernet(), which re-derived the Fernet key fromAGENTA_CRYPT_KEYvia SHA-256 + base64 on every invocation.What problem does it solve? Eliminates redundant cryptographic key derivation under load. The Fernet instance is now created once and reused for the lifetime of the process.
How the change addresses the root cause:
lru_cache(maxsize=1)caches the single Fernet instance. Since the crypt key is static per deployment, there is no benefit to recreating the instance on every call.Testing
Verified locally
ruff check api/oss/src/utils/crypting.py— passes with no errorsruff format api/oss/src/utils/crypting.py— no changes needed_get_fernet()still raisesValueErrorwhenAGENTA_CRYPT_KEYis missingAdded or updated tests
N/A — This is a performance optimization with no behavior change. Existing vault/encryption tests cover the functional path.
QA follow-up
N/A — The fix is self-contained and verified by static analysis.
Demo
BEFORE FIX :
AFTER FIX 👍 :
Checklist