-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular/cli): allow Algolia search key override via env var #33081
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
gn00295120
wants to merge
1
commit into
angular:main
Choose a base branch
from
gn00295120:feat-algolia-key-override
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.
+70
−12
Open
Changes from all commits
Commits
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,11 +14,32 @@ import { at, iv, k1 } from '../constants'; | |||||||||
| import { type McpToolContext, declareTool } from './tool-registry'; | ||||||||||
|
|
||||||||||
| const ALGOLIA_APP_ID = 'L1XWT2UJ7F'; | ||||||||||
| // https://www.algolia.com/doc/guides/security/api-keys/#search-only-api-key | ||||||||||
| // This is a search only, rate limited key. It is sent within the URL of the query request. | ||||||||||
| // This is not the actual key. | ||||||||||
| // Default Algolia API key used when NG_DOCS_SEARCH_API_KEY is not set. | ||||||||||
| // Operators (e.g. self-hosted documentation, internal CI, rotation testing) | ||||||||||
| // can override this by setting NG_DOCS_SEARCH_API_KEY in the environment. | ||||||||||
| const ALGOLIA_API_E = '34738e8ae1a45e58bbce7b0f9810633d8b727b44a6479cf5e14b6a337148bd50'; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Resolves the Algolia API key to use for documentation search. If the | ||||||||||
| * `NG_DOCS_SEARCH_API_KEY` environment variable is set to a non-empty value | ||||||||||
| * it is used verbatim; otherwise the bundled default is used. | ||||||||||
| * | ||||||||||
| * Exported for testing. | ||||||||||
| */ | ||||||||||
| export function resolveAlgoliaApiKey(): string { | ||||||||||
| const override = process.env['NG_DOCS_SEARCH_API_KEY']; | ||||||||||
| if (typeof override === 'string' && override !== '') { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is generally safer to trim environment variable values to prevent issues caused by accidental leading or trailing whitespace in the shell configuration.
Suggested change
|
||||||||||
| return override; | ||||||||||
| } | ||||||||||
| const dcip = createDecipheriv( | ||||||||||
| 'aes-256-gcm', | ||||||||||
| (k1 + ALGOLIA_APP_ID).padEnd(32, '^'), | ||||||||||
| iv, | ||||||||||
| ).setAuthTag(Buffer.from(at, 'base64')); | ||||||||||
|
|
||||||||||
| return dcip.update(ALGOLIA_API_E, 'hex', 'utf-8') + dcip.final('utf-8'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * The minimum major version of Angular for which a version-specific documentation index is known to exist. | ||||||||||
| * Searches for versions older than this will be clamped to this version. | ||||||||||
|
|
@@ -129,16 +150,8 @@ function createDocSearchHandler({ logger }: McpToolContext) { | |||||||||
|
|
||||||||||
| return async ({ query, includeTopContent, version }: DocSearchInput) => { | ||||||||||
| if (!client) { | ||||||||||
| const dcip = createDecipheriv( | ||||||||||
| 'aes-256-gcm', | ||||||||||
| (k1 + ALGOLIA_APP_ID).padEnd(32, '^'), | ||||||||||
| iv, | ||||||||||
| ).setAuthTag(Buffer.from(at, 'base64')); | ||||||||||
| const { searchClient } = await import('algoliasearch'); | ||||||||||
| client = searchClient( | ||||||||||
| ALGOLIA_APP_ID, | ||||||||||
| dcip.update(ALGOLIA_API_E, 'hex', 'utf-8') + dcip.final('utf-8'), | ||||||||||
| ); | ||||||||||
| client = searchClient(ALGOLIA_APP_ID, resolveAlgoliaApiKey()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| let finalSearchedVersion = Math.max( | ||||||||||
|
|
||||||||||
45 changes: 45 additions & 0 deletions
45
packages/angular/cli/src/commands/mcp/tools/doc-search_spec.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { resolveAlgoliaApiKey } from './doc-search'; | ||
|
|
||
| describe('resolveAlgoliaApiKey', () => { | ||
| const ENV_VAR = 'NG_DOCS_SEARCH_API_KEY'; | ||
| let saved: string | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| saved = process.env[ENV_VAR]; | ||
| delete process.env[ENV_VAR]; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (saved === undefined) { | ||
| delete process.env[ENV_VAR]; | ||
| } else { | ||
| process.env[ENV_VAR] = saved; | ||
| } | ||
| }); | ||
|
|
||
| it('returns the env var value when set to a non-empty string', () => { | ||
| process.env[ENV_VAR] = 'override-key-1234'; | ||
|
|
||
| expect(resolveAlgoliaApiKey()).toBe('override-key-1234'); | ||
| }); | ||
|
|
||
| it('falls back to the bundled default when the env var is unset', () => { | ||
| delete process.env[ENV_VAR]; | ||
|
|
||
| expect(resolveAlgoliaApiKey()).toMatch(/^[0-9a-f]{32}$/); | ||
| }); | ||
|
|
||
| it('falls back to the bundled default when the env var is an empty string', () => { | ||
| process.env[ENV_VAR] = ''; | ||
|
|
||
| expect(resolveAlgoliaApiKey()).toMatch(/^[0-9a-f]{32}$/); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment mentions support for self-hosted documentation, but
ALGOLIA_APP_ID(line 16) remains hardcoded. A self-hosted Algolia index would require a different Application ID in addition to the API key. To fully support the self-hosted use case as described, consider providing an environment variable override for the App ID as well (e.g.NG_DOCS_SEARCH_APP_ID).References