-
Notifications
You must be signed in to change notification settings - Fork 2
feat: introduce TTL-based caching for Secrets Manager #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fd2eef5
feat: introduce TTL-based caching for Secrets Manager
TheUnderScorer 991031c
chore: replace `isNaN` with `Number.isNaN` for stricter type checking
TheUnderScorer 5022af9
chore: avoid two cache lookups in retrieveSecret
TheUnderScorer 2f20c65
chore: add validation for custom ttl ms
TheUnderScorer 77149d0
chore: validate secret cache ttl ms
TheUnderScorer 122b44a
chore: add test for default TTL fallback when custom TTL is NaN
TheUnderScorer 6982acc
chore: ensure custom TTL validation checks for finite values
TheUnderScorer 9ef50ee
chore: add test for cache expiration and refetch behavior in retrieve…
TheUnderScorer 8a1f4cd
chore: add explicit radix
TheUnderScorer 30dfaeb
chore: fix afterEach import
TheUnderScorer 27af49d
Merge remote-tracking branch 'origin/bugfix/INTER-1894-secret-cache-t…
TheUnderScorer 1efef3d
chore: align behavior with an exact TTL boundary
TheUnderScorer 8eb90bd
chore: centralize TTL validation logic in TTLCache and apply defaults
TheUnderScorer 34d5a67
Merge branch 'refs/heads/main' into bugfix/INTER-1894-secret-cache-ttl
TheUnderScorer da20f1d
chore: rename package to `@fingerprint/aws-cloudfront-proxy` in chang…
TheUnderScorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@fingerprint/aws-cloudfront-proxy': minor | ||
| --- | ||
|
|
||
| Introduce TTL for secret caching |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { TTLCache } from '../../utils/cache' | ||
|
|
||
| describe('TTLCache', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers() | ||
| }) | ||
|
|
||
| test('should store and retrieve values', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
| cache.set('key1', 'value1') | ||
|
|
||
| expect(cache.get('key1')).toBe('value1') | ||
| expect(cache.has('key1')).toBe(true) | ||
| }) | ||
|
|
||
| test('should return undefined for missing keys', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
|
|
||
| expect(cache.get('missingKey')).toBeUndefined() | ||
| expect(cache.has('missingKey')).toBe(false) | ||
| }) | ||
|
|
||
| test('should expire items after default TTL', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
| cache.set('key1', 'value1') | ||
|
|
||
| expect(cache.get('key1')).toBe('value1') | ||
|
|
||
| // Advance time past TTL | ||
| jest.advanceTimersByTime(1001) | ||
|
|
||
| expect(cache.get('key1')).toBeUndefined() | ||
| expect(cache.has('key1')).toBe(false) | ||
| }) | ||
|
|
||
| test('should expire items after default TTL for null values', () => { | ||
| const cache = new TTLCache<string, string | null>(1000) | ||
| cache.set('key1', null) | ||
|
|
||
| expect(cache.get('key1')).toBeNull() | ||
| expect(cache.has('key1')).toBe(true) | ||
|
|
||
| // Advance time past TTL | ||
| jest.advanceTimersByTime(1001) | ||
|
|
||
| expect(cache.get('key1')).toBeUndefined() | ||
| expect(cache.has('key1')).toBe(false) | ||
| }) | ||
|
|
||
| test('should respect custom TTL per item', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
|
|
||
| // Item with shorter TTL | ||
| cache.set('key1', 'value1', 500) | ||
| // Item with longer TTL | ||
| cache.set('key2', 'value2', 2000) | ||
|
|
||
| // Advance past first item's TTL but before second item's TTL | ||
| jest.advanceTimersByTime(1000) | ||
|
|
||
| expect(cache.get('key1')).toBeUndefined() | ||
| expect(cache.get('key2')).toBe('value2') | ||
|
|
||
| // Advance past second item's TTL | ||
| jest.advanceTimersByTime(1001) | ||
|
|
||
| expect(cache.get('key2')).toBeUndefined() | ||
| }) | ||
|
|
||
| test('should fallback to default TTL if passed TTL is NaN', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
|
|
||
| cache.set('key1', 'value1', NaN) | ||
| expect(cache.get('key1')).toBe('value1') | ||
|
|
||
| jest.advanceTimersByTime(1001) | ||
|
|
||
| expect(cache.get('key1')).toBeUndefined() | ||
| }) | ||
|
|
||
| test('should delete items', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
| cache.set('key1', 'value1') | ||
|
|
||
| expect(cache.get('key1')).toBe('value1') | ||
|
|
||
| cache.delete('key1') | ||
|
|
||
| expect(cache.get('key1')).toBeUndefined() | ||
| expect(cache.has('key1')).toBe(false) | ||
| }) | ||
|
|
||
| test('should clear all items', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
| cache.set('key1', 'value1') | ||
| cache.set('key2', 'value2') | ||
|
|
||
| cache.clear() | ||
|
|
||
| expect(cache.get('key1')).toBeUndefined() | ||
| expect(cache.get('key2')).toBeUndefined() | ||
| }) | ||
|
|
||
| test('has() should trigger eviction if item is expired', () => { | ||
| const cache = new TTLCache<string, string>(1000) | ||
| cache.set('key1', 'value1') | ||
|
|
||
| jest.advanceTimersByTime(1001) | ||
|
|
||
| expect(cache.has('key1')).toBe(false) | ||
| // Underlying map should have been cleaned up by has() which calls get() | ||
| expect(cache.get('key1')).toBeUndefined() | ||
| }) | ||
| }) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| interface CacheItem<T> { | ||
| value: T | ||
| expiresAt: number | ||
| } | ||
|
|
||
| export class TTLCache<K, V> { | ||
| private cache: Map<K, CacheItem<V>> | ||
| private readonly ttlMs: number | ||
|
|
||
| // Default TTL is 5 minutes | ||
| private static readonly DEFAULT_TTL_MS = 300_000 | ||
|
|
||
| constructor(ttlMs: number) { | ||
| this.cache = new Map() | ||
| this.ttlMs = TTLCache.isValidTTL(ttlMs) ? ttlMs : TTLCache.DEFAULT_TTL_MS | ||
| } | ||
|
|
||
| get(key: K): V | undefined { | ||
| const item = this.cache.get(key) | ||
| if (item === undefined) { | ||
| return undefined | ||
| } | ||
|
|
||
| if (Date.now() >= item.expiresAt) { | ||
| this.cache.delete(key) | ||
| return undefined | ||
| } | ||
|
|
||
| return item.value | ||
| } | ||
|
|
||
| set(key: K, value: V, customTtlMs?: number): void { | ||
| const ttlMsToUse = TTLCache.isValidTTL(customTtlMs) ? customTtlMs : this.ttlMs | ||
|
|
||
| this.cache.set(key, { | ||
| value, | ||
| expiresAt: Date.now() + ttlMsToUse, | ||
| }) | ||
| } | ||
|
|
||
| has(key: K): boolean { | ||
| return this.get(key) !== undefined | ||
| } | ||
|
|
||
| delete(key: K): void { | ||
| this.cache.delete(key) | ||
| } | ||
|
|
||
| clear(): void { | ||
| this.cache.clear() | ||
| } | ||
|
|
||
| static isValidTTL(value?: number): value is number { | ||
| return typeof value === 'number' && !Number.isNaN(value) && Number.isFinite(value) && value >= 0 | ||
| } | ||
| } | ||
23 changes: 9 additions & 14 deletions
23
proxy/utils/customer-variables/secrets-manager/retrieve-secret.ts
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.