-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[ENH] For chroma cloud efs, extract api key from header if available to authenticate #5914
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
jairad26
wants to merge
1
commit into
main
Choose a base branch
from
jai/cloud-ef-api-key-extraction
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.
+248
−5
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
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,166 @@ | ||
| import pytest | ||
| from unittest.mock import MagicMock | ||
| from chromadb.api.shared_system_client import SharedSystemClient | ||
| from chromadb.config import System | ||
| from chromadb.api import ServerAPI | ||
| from typing import Optional, Dict, Generator | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def clear_cache() -> Generator[None, None, None]: | ||
| """Automatically clear the system cache before and after each test.""" | ||
| SharedSystemClient.clear_system_cache() | ||
| yield | ||
| SharedSystemClient.clear_system_cache() | ||
|
|
||
|
|
||
| def create_mock_server_api( | ||
| api_url: Optional[str] = None, | ||
| headers: Optional[Dict[str, str]] = None, | ||
| has_session: bool = True, | ||
| has_headers_attr: bool = True, | ||
| ) -> MagicMock: | ||
| """Create a mock ServerAPI instance with the specified configuration.""" | ||
| mock_server_api = MagicMock(spec=ServerAPI) | ||
|
|
||
| if api_url: | ||
| mock_server_api._api_url = api_url | ||
|
|
||
| if has_session: | ||
| mock_session = MagicMock() | ||
| if has_headers_attr: | ||
| mock_session.headers = headers or {} | ||
| else: | ||
| # Create a mock without headers attribute | ||
| del mock_session.headers | ||
| mock_server_api._session = mock_session | ||
| else: | ||
| if hasattr(mock_server_api, "_session"): | ||
| del mock_server_api._session | ||
|
|
||
| return mock_server_api | ||
|
|
||
|
|
||
| def register_mock_system(system_id: str, mock_server_api: MagicMock) -> MagicMock: | ||
| """Register a mock system with the given ID and server API.""" | ||
| mock_system = MagicMock(spec=System) | ||
| mock_system.instance.return_value = mock_server_api | ||
| SharedSystemClient._identifier_to_system[system_id] = mock_system | ||
| return mock_system | ||
|
|
||
|
|
||
| def test_extracts_api_key_from_chroma_cloud_client() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| headers={"X-Chroma-Token": "test-api-key-123"}, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key == "test-api-key-123" | ||
|
|
||
|
|
||
| def test_extracts_api_key_with_lowercase_header() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| headers={"x-chroma-token": "test-api-key-456"}, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key == "test-api-key-456" | ||
|
|
||
|
|
||
| def test_skips_non_chroma_cloud_clients() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url="https://localhost:8000/api/v2", | ||
| headers={"X-Chroma-Token": "local-api-key"}, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None | ||
|
|
||
|
|
||
| def test_skips_clients_without_session() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| has_session=False, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None | ||
|
|
||
|
|
||
| def test_skips_clients_without_api_url() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url=None, | ||
| headers={"X-Chroma-Token": "test-api-key"}, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None | ||
|
|
||
|
|
||
| def test_returns_none_when_no_api_key_in_headers() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| headers={}, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None | ||
|
|
||
|
|
||
| def test_returns_first_api_key_found_from_multiple_clients() -> None: | ||
| mock_server_api_1 = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| headers={"X-Chroma-Token": "first-key"}, | ||
| ) | ||
| mock_server_api_2 = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| headers={"X-Chroma-Token": "second-key"}, | ||
| ) | ||
| register_mock_system("test-id-1", mock_server_api_1) | ||
| register_mock_system("test-id-2", mock_server_api_2) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key == "first-key" | ||
|
|
||
|
|
||
| def test_handles_exception_gracefully() -> None: | ||
| mock_system = MagicMock(spec=System) | ||
| mock_system.instance.side_effect = Exception("Test exception") | ||
| SharedSystemClient._identifier_to_system["test-id"] = mock_system | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None | ||
|
|
||
|
|
||
| def test_returns_none_when_no_clients_exist() -> None: | ||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None | ||
|
|
||
|
|
||
| def test_skips_chroma_cloud_client_without_headers_attribute() -> None: | ||
| mock_server_api = create_mock_server_api( | ||
| api_url="https://api.trychroma.com/api/v2", | ||
| has_headers_attr=False, | ||
| ) | ||
| register_mock_system("test-id", mock_server_api) | ||
|
|
||
| api_key = SharedSystemClient.get_chroma_cloud_api_key_from_clients() | ||
|
|
||
| assert api_key is None |
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.
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.
[Reliability] The broad
except Exceptionsilently catches all errors, including critical issues likeAttributeErrorfrom API changes orKeyErrorfrom malformed data structures. This makes debugging difficult when the code fails.This distinguishes expected structural variations from genuine errors that need attention.
Context for Agents