-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description of changes This PR adds the Chroma `CloudClient` as a client type. It takes an API key (or reads it from the environment, or prompts the user for it if it's not found) , and tries to connect to the Chroma cloud using the API key. Under the hood, this is a specialized HTTPClient with ssl always turned on, and which uses token-based authentication with the API key as the token. This PR also moves the `AuthorizationError` from auth to the general set of errors, so it can be handled elsewhere by the Chroma system. ## Test plan Added a new test, `test_cloud_client`, which creates a mock 'cloud' server and then tests connection with valid and invalid API keys. ## Documentation Changes Cloud is not yet available, so `CloudClient` is for now not to be used by external users of Chroma. Therefore, we won't land documentation changes with this PR. ## TODOs - [x] Tests - [ ] JavaScript @jeffchuber to take a look - [ ] ~Docs~
- Loading branch information
Showing
7 changed files
with
187 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,104 @@ | ||
import multiprocessing | ||
from typing import Any, Dict, Generator, Optional, Tuple | ||
import pytest | ||
from chromadb import CloudClient | ||
from chromadb.api import ServerAPI | ||
from chromadb.auth.token import TokenTransportHeader | ||
from chromadb.config import DEFAULT_DATABASE, DEFAULT_TENANT, Settings, System | ||
from chromadb.errors import AuthorizationError | ||
|
||
from chromadb.test.conftest import _await_server, _run_server, find_free_port | ||
|
||
TOKEN_TRANSPORT_HEADER = TokenTransportHeader.X_CHROMA_TOKEN.name | ||
TEST_CLOUD_HOST = "localhost" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def valid_token() -> str: | ||
return "valid_token" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def mock_cloud_server(valid_token: str) -> Generator[System, None, None]: | ||
chroma_server_auth_provider: str = "chromadb.auth.token.TokenAuthServerProvider" | ||
chroma_server_auth_credentials_provider: str = ( | ||
"chromadb.auth.token.TokenConfigServerAuthCredentialsProvider" | ||
) | ||
chroma_server_auth_credentials: str = valid_token | ||
chroma_server_auth_token_transport_header: str = TOKEN_TRANSPORT_HEADER | ||
|
||
port = find_free_port() | ||
|
||
args: Tuple[ | ||
int, | ||
bool, | ||
Optional[str], | ||
Optional[str], | ||
Optional[str], | ||
Optional[str], | ||
Optional[str], | ||
Optional[str], | ||
Optional[str], | ||
Optional[str], | ||
Optional[Dict[str, Any]], | ||
] = ( | ||
port, | ||
False, | ||
None, | ||
chroma_server_auth_provider, | ||
chroma_server_auth_credentials_provider, | ||
None, | ||
chroma_server_auth_credentials, | ||
chroma_server_auth_token_transport_header, | ||
None, | ||
None, | ||
None, | ||
) | ||
ctx = multiprocessing.get_context("spawn") | ||
proc = ctx.Process(target=_run_server, args=args, daemon=True) | ||
proc.start() | ||
|
||
settings = Settings( | ||
chroma_api_impl="chromadb.api.fastapi.FastAPI", | ||
chroma_server_host=TEST_CLOUD_HOST, | ||
chroma_server_http_port=str(port), | ||
chroma_client_auth_provider="chromadb.auth.token.TokenAuthClientProvider", | ||
chroma_client_auth_credentials=valid_token, | ||
chroma_client_auth_token_transport_header=TOKEN_TRANSPORT_HEADER, | ||
) | ||
|
||
system = System(settings) | ||
api = system.instance(ServerAPI) | ||
system.start() | ||
_await_server(api) | ||
yield system | ||
system.stop() | ||
proc.kill() | ||
|
||
|
||
def test_valid_key(mock_cloud_server: System, valid_token: str) -> None: | ||
valid_client = CloudClient( | ||
tenant=DEFAULT_TENANT, | ||
database=DEFAULT_DATABASE, | ||
api_key=valid_token, | ||
cloud_host=TEST_CLOUD_HOST, | ||
cloud_port=mock_cloud_server.settings.chroma_server_http_port, # type: ignore | ||
enable_ssl=False, | ||
) | ||
|
||
assert valid_client.heartbeat() | ||
|
||
|
||
def test_invalid_key(mock_cloud_server: System, valid_token: str) -> None: | ||
# Try to connect to the default tenant and database with an invalid token | ||
invalid_token = valid_token + "_invalid" | ||
with pytest.raises(AuthorizationError): | ||
client = CloudClient( | ||
tenant=DEFAULT_TENANT, | ||
database=DEFAULT_DATABASE, | ||
api_key=invalid_token, | ||
cloud_host=TEST_CLOUD_HOST, | ||
cloud_port=mock_cloud_server.settings.chroma_server_http_port, # type: ignore | ||
enable_ssl=False, | ||
) | ||
client.heartbeat() |