Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds an opt-in, in-process decrypted DEK cache to the encryption.Pool to avoid repeated DynamoDB/KMS/Shamir work on repeated encrypt/decrypt operations for the same keyRef.
Changes:
- Introduces
CacheConfig, an in-memory LRU+TTLdekCache, and singleflight-style miss deduplication. - Updates
Poolconstruction to acceptPoolOptions (notablyWithCache) and threads cache usage through Encrypt/Decrypt/RotateKey. - Adds unit tests covering cache hit/miss behavior, cache population, invalidation, and singleflight behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| encryption/pool.go | Adds cache plumbing to Pool plus a new fetchDEK helper to centralize the miss path and deduplicate concurrent fetches. |
| encryption/pool_test.go | Adds integration-style tests proving cache behavior across Encrypt/Decrypt/RotateKey and concurrent decrypts. |
| encryption/cache.go | Implements the LRU+TTL DEK cache with key zeroing on eviction and singleflight coordination. |
| encryption/cache_test.go | Adds unit tests for cache correctness (copy semantics, TTL/LRU eviction, delete/clear, singleflight). |
Comments suppressed due to low confidence (1)
encryption/pool.go:235
Decryptno longer annotates the trace span with the resolved key generation / key index (these annotations existed before the refactor). SincefetchDEKhas thekeyobject, consider re-adding these annotations (e.g., by setting them on the active span inctxor returning key metadata) so cache misses still emit the same observability signals.
key, found, err := p.keysTable.GetLatestByKeyRef(ctx, keyRef, false)
if err != nil {
return nil, fmt.Errorf("get latest key: %w", err)
}
if !found {
return nil, fmt.Errorf("key not found")
}
if err := p.VerifyKey(ctx, att, key); err != nil {
return nil, fmt.Errorf("verify key: %w", err)
}
config, err := p.getConfig(key.Generation)
if err != nil {
return nil, fmt.Errorf("get config: %w", err)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
161
to
+183
| @@ -139,6 +174,50 @@ func (p *Pool) Decrypt(ctx context.Context, att *enclave.Attestation, keyRef str | |||
| return nil, fmt.Errorf("decode ciphertext: %w", err) | |||
| } | |||
|
|
|||
| // Try cache. | |||
| var privateKey []byte | |||
| if p.cache != nil { | |||
| if dek, ok := p.cache.get(keyRef); ok { | |||
| privateKey = dek | |||
| } | |||
| } | |||
Comment on lines
163
to
+164
| // The key is verified against the attestation and migrated to the current generation if needed. | ||
| // If a DEK cache is configured, cached keys bypass DynamoDB and KMS on hit. |
Comment on lines
+245
to
255
| if p.cache != nil { | ||
| p.cache.put(keyRef, privateKey) | ||
| } | ||
|
|
||
| // Trigger migration if needed. Migration is synchronous but non-fatal: | ||
| // failure is logged and does not affect the returned DEK. | ||
| if p.keyNeedsMigration(key) { | ||
| err := p.migrateKey(ctx, att, key, privateKey) | ||
| if err != nil { | ||
| // We don't want to fail the decryption if migration fails, log the error and continue | ||
| if err := p.migrateKey(ctx, att, key, privateKey); err != nil { | ||
| p.logger.ErrorContext(ctx, "migrating key failed", "error", err, "key_ref", key.KeyRef, "generation", key.Generation, "key_index", key.KeyIndex) | ||
| } | ||
| } |
Comment on lines
+59
to
+65
| // Grab internal slice before expiry. | ||
| c.mu.Lock() | ||
| internalSlice := c.entries["ref1"].dek | ||
| c.mu.Unlock() | ||
|
|
||
| time.Sleep(5 * time.Millisecond) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an optional in-memory LRU cache for decrypted data encryption keys (DEKs), eliminating KMS round-trips on cache hits. Each decrypt/encrypt operation currently requires 2-3 KMS calls to recover the DEK via Shamir secret sharing — the cache reduces this to zero on hit.
Design
sync.Mutex+container/listWithCache()— existing callers are unaffected (no cache by default)Security
RotateKeyexplicitly invalidates the cache entry