-
Notifications
You must be signed in to change notification settings - Fork 79
feat(ofrep-web): ADR-0009 domain-aware cache key + domainScoped #1569
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
Open
jonathannorris
wants to merge
7
commits into
main
Choose a base branch
from
feat/ofrep-domain-cache-key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
59a0c62
feat(ofrep-web): adopt ADR-0009 cache key with domain scoping
jonathannorris 90f4fdb
fix(ofrep-web): limit cache key auth to known header names
jonathannorris 864dc78
refactor(ofrep-web): create storage in initialize with bound domain
jonathannorris 4fa2540
docs(ofrep-web): document ADR-0009 cache key and domain scoping
jonathannorris 72662bd
fix(ofrep-web): normalize auth header casing and fix test cacheKeyHas…
jonathannorris b726b8b
feat(ofrep-web): replace cacheKeyPrefix with cacheKeyGenerator per AD…
jonathannorris eb7d5d6
fix(ofrep-web): keep persisted cache schema at v2
jonathannorris 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
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
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,72 @@ | ||
| import { deriveAuthCredential, encodeCacheKeyInput } from './cache-key'; | ||
|
|
||
| describe('cache key encoding', () => { | ||
| it('uses JSON encoding so delimiter-like values do not collide', () => { | ||
| const keyA = encodeCacheKeyInput({ | ||
| baseUrl: 'https://a:b', | ||
| auth: 'c', | ||
| domain: 'd', | ||
| targetingKey: 'e', | ||
| }); | ||
| const keyB = encodeCacheKeyInput({ | ||
| baseUrl: 'https://a', | ||
| auth: 'b:c', | ||
| domain: 'd:e', | ||
| targetingKey: '', | ||
| }); | ||
| expect(keyA).not.toBe(keyB); | ||
| }); | ||
|
|
||
| it('includes cacheKeyPrefix as the first component when set', () => { | ||
| const withPrefix = encodeCacheKeyInput({ | ||
| cacheKeyPrefix: 'my-app', | ||
| baseUrl: 'https://example.com', | ||
| auth: '[]', | ||
| domain: 'billing', | ||
| targetingKey: 'user-1', | ||
| }); | ||
| const withoutPrefix = encodeCacheKeyInput({ | ||
| baseUrl: 'https://example.com', | ||
| auth: '[]', | ||
| domain: 'billing', | ||
| targetingKey: 'user-1', | ||
| }); | ||
| expect(withPrefix).not.toBe(withoutPrefix); | ||
| }); | ||
|
|
||
| it('serializes Authorization from static headers', async () => { | ||
| const auth = await deriveAuthCredential({ | ||
| baseUrl: 'https://example.com', | ||
| headers: [ | ||
| ['Content-Type', 'application/json'], | ||
| ['Authorization', 'Bearer token'], | ||
| ['X-My-Header', 'ignored'], | ||
| ], | ||
| }); | ||
| expect(auth).toBe(JSON.stringify([['Authorization', 'Bearer token']])); | ||
| }); | ||
|
|
||
| it('serializes known auth headers from headersFactory', async () => { | ||
| const auth = await deriveAuthCredential({ | ||
| baseUrl: 'https://example.com', | ||
| headersFactory: () => Promise.resolve([['X-Api-Key', 'secret']]), | ||
| }); | ||
| expect(auth).toBe(JSON.stringify([['X-Api-Key', 'secret']])); | ||
| }); | ||
|
|
||
| it('returns an empty array when no auth headers are configured', async () => { | ||
| const auth = await deriveAuthCredential({ | ||
| baseUrl: 'https://example.com', | ||
| headers: [['X-Custom', 'value']], | ||
| }); | ||
| expect(auth).toBe('[]'); | ||
| }); | ||
|
|
||
| it('matches auth header names case-insensitively', async () => { | ||
| const auth = await deriveAuthCredential({ | ||
| baseUrl: 'https://example.com', | ||
| headers: [['x-api-key', 'secret']], | ||
| }); | ||
| expect(auth).toBe(JSON.stringify([['x-api-key', 'secret']])); | ||
| }); | ||
| }); |
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,46 @@ | ||
| import type { OFREPProviderBaseOptions } from '@openfeature/ofrep-core'; | ||
|
|
||
| /** Header names treated as auth credentials for cache key derivation (matched case-insensitively). */ | ||
| const AUTH_HEADER_NAMES = new Set([ | ||
|
jonathannorris marked this conversation as resolved.
|
||
| 'authorization', | ||
| 'api-key', | ||
| 'x-api-key', | ||
| 'x-auth-token', | ||
| 'x-access-token', | ||
| ]); | ||
|
|
||
| export type CacheKeyParts = { | ||
| cacheKeyPrefix?: string; | ||
| baseUrl: string; | ||
| auth: string; | ||
| domain: string; | ||
| targetingKey: string; | ||
| }; | ||
|
|
||
| function isAuthHeader(name: string): boolean { | ||
| return AUTH_HEADER_NAMES.has(name.toLowerCase()); | ||
| } | ||
|
|
||
| /** | ||
| * Serializes known auth headers from static and factory-supplied options for cache keying. | ||
| * Rotating tokens will change the cache key on each rotation; stable credentials separate caches as intended. | ||
| */ | ||
| export async function deriveAuthCredential(options: OFREPProviderBaseOptions): Promise<string> { | ||
| const entries = [...(options.headers ?? []), ...((await options.headersFactory?.()) ?? [])]; | ||
| const authHeaders = entries.filter(([name]) => isAuthHeader(name)).sort(([a], [b]) => a.localeCompare(b)); | ||
| return JSON.stringify(authHeaders); | ||
|
jonathannorris marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Encodes cache key components without ambiguous delimiter collisions. | ||
| * Order matches ADR-0009: optional prefix, base URL, auth, domain, targeting key. | ||
| */ | ||
| export function encodeCacheKeyInput(parts: CacheKeyParts): string { | ||
| return JSON.stringify([ | ||
| parts.cacheKeyPrefix ?? '', | ||
| parts.baseUrl, | ||
| parts.auth, | ||
| parts.domain, | ||
| parts.targetingKey, | ||
| ]); | ||
| } | ||
Oops, something went wrong.
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.