diff --git a/chromadb/auth/fastapi.py b/chromadb/auth/fastapi.py index 1f9d3c900c3..a634096805e 100644 --- a/chromadb/auth/fastapi.py +++ b/chromadb/auth/fastapi.py @@ -1,4 +1,3 @@ -import chromadb from contextvars import ContextVar from functools import wraps import logging @@ -28,7 +27,7 @@ ) from chromadb.auth.registry import resolve_provider from chromadb.errors import AuthorizationError -from chromadb.server.fastapi.utils import fastapi_json_response +from chromadb.utils.fastapi import fastapi_json_response from chromadb.telemetry.opentelemetry import ( OpenTelemetryGranularity, trace_method, @@ -117,7 +116,7 @@ def instrument_server(self, app: ASGIApp) -> None: raise NotImplementedError("Not implemented yet") -class FastAPIChromaAuthMiddlewareWrapper(BaseHTTPMiddleware): # type: ignore +class FastAPIChromaAuthMiddlewareWrapper(BaseHTTPMiddleware): def __init__( self, app: ASGIApp, auth_middleware: FastAPIChromaAuthMiddleware ) -> None: @@ -302,7 +301,7 @@ def instrument_server(self, app: ASGIApp) -> None: raise NotImplementedError("Not implemented yet") -class FastAPIChromaAuthzMiddlewareWrapper(BaseHTTPMiddleware): # type: ignore +class FastAPIChromaAuthzMiddlewareWrapper(BaseHTTPMiddleware): def __init__( self, app: ASGIApp, authz_middleware: FastAPIChromaAuthzMiddleware ) -> None: diff --git a/chromadb/auth/fastapi_utils.py b/chromadb/auth/fastapi_utils.py index 2612bf6716f..9dc652feeb9 100644 --- a/chromadb/auth/fastapi_utils.py +++ b/chromadb/auth/fastapi_utils.py @@ -1,6 +1,6 @@ from functools import partial from typing import Any, Callable, Dict, Optional, Sequence, cast -from chromadb.server.fastapi.utils import string_to_uuid +from chromadb.utils.fastapi import string_to_uuid from chromadb.api import ServerAPI from chromadb.auth import AuthzResourceTypes diff --git a/chromadb/server/fastapi/__init__.py b/chromadb/server/fastapi/__init__.py index a38225de7f3..fa057b90d29 100644 --- a/chromadb/server/fastapi/__init__.py +++ b/chromadb/server/fastapi/__init__.py @@ -51,7 +51,7 @@ import logging -from chromadb.server.fastapi.utils import fastapi_json_response, string_to_uuid as _uuid +from chromadb.utils.fastapi import fastapi_json_response, string_to_uuid as _uuid from chromadb.telemetry.opentelemetry.fastapi import instrument_fastapi from chromadb.types import Database, Tenant from chromadb.telemetry.product import ServerContext, ProductTelemetryClient diff --git a/chromadb/test/client/create_http_client_with_basic_auth.py b/chromadb/test/client/create_http_client_with_basic_auth.py new file mode 100644 index 00000000000..2f884c571c1 --- /dev/null +++ b/chromadb/test/client/create_http_client_with_basic_auth.py @@ -0,0 +1,27 @@ +# This file is used by test_create_http_client.py to test the initialization +# of an HttpClient class with auth settings. +# +# See https://github.com/chroma-core/chroma/issues/1554 + +import chromadb +from chromadb.config import Settings +import sys + +def main() -> None: + try: + chromadb.HttpClient( + host='localhost', + port=8000, + settings=Settings( + chroma_client_auth_provider="chromadb.auth.basic.BasicAuthClientProvider", + chroma_client_auth_credentials="admin:testDb@home2" + ) + ) + except ValueError: + # We don't expect to be able to connect to Chroma. We just want to make sure + # there isn't an ImportError. + sys.exit(0) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/chromadb/test/client/test_create_http_client.py b/chromadb/test/client/test_create_http_client.py new file mode 100644 index 00000000000..293e5031ba9 --- /dev/null +++ b/chromadb/test/client/test_create_http_client.py @@ -0,0 +1,15 @@ +import subprocess + +# Needs to be a module, not a file, so that local imports work. +TEST_MODULE = "chromadb.test.client.create_http_client_with_basic_auth" + + +def test_main() -> None: + # This is the only way to test what we want to test: pytest does a bunch of + # importing and other module stuff in the background, so we need a clean + # python process to make sure we're not circular-importing. + # + # See https://github.com/chroma-core/chroma/issues/1554 + + res = subprocess.run(['python', '-m', TEST_MODULE]) + assert res.returncode == 0 diff --git a/chromadb/server/fastapi/utils.py b/chromadb/utils/fastapi.py similarity index 98% rename from chromadb/server/fastapi/utils.py rename to chromadb/utils/fastapi.py index b7e781dae68..8300880e402 100644 --- a/chromadb/server/fastapi/utils.py +++ b/chromadb/utils/fastapi.py @@ -10,8 +10,9 @@ def fastapi_json_response(error: ChromaError) -> JSONResponse: status_code=error.code(), ) + def string_to_uuid(uuid_str: str) -> UUID: try: return UUID(uuid_str) except ValueError: - raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID") \ No newline at end of file + raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID")