Skip to content

Commit f856fb3

Browse files
committed
Add fix for exposing api key in metadata when running to_dict
1 parent 6985a8f commit f856fb3

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
from copy import deepcopy
23
from itertools import islice
34
from typing import Any, AsyncGenerator, ClassVar, Dict, Generator, List, Optional, Set, Tuple, Union
45

@@ -18,10 +19,10 @@
1819
from .converters import (
1920
DENSE_VECTORS_NAME,
2021
SPARSE_VECTORS_NAME,
22+
QdrantPoint,
2123
convert_haystack_documents_to_qdrant_points,
2224
convert_id,
2325
convert_qdrant_point_to_haystack_document,
24-
QdrantPoint
2526
)
2627
from .filters import convert_filters_to_qdrant
2728

@@ -131,7 +132,7 @@ def __init__(
131132
) -> None:
132133
"""
133134
:param location:
134-
If `memory` - use in-memory Qdrant instance.
135+
If `":memory:"` - use in-memory Qdrant instance.
135136
If `str` - use it as a URL parameter.
136137
If `None` - use default values for host and port.
137138
:param url:
@@ -261,6 +262,7 @@ def __init__(
261262
def _initialize_client(self) -> None:
262263
if self._client is None:
263264
client_params = self._prepare_client_params()
265+
# This step adds the api-key and User-Agent to metadata
264266
self._client = qdrant_client.QdrantClient(**client_params)
265267
# Make sure the collection is properly set up
266268
self._set_up_collection(
@@ -1538,21 +1540,26 @@ def _prepare_client_params(self) -> Dict[str, Any]:
15381540
Prepares the common parameters for client initialization.
15391541
15401542
"""
1541-
return {
1542-
"location": self.location,
1543-
"url": self.url,
1544-
"port": self.port,
1545-
"grpc_port": self.grpc_port,
1546-
"prefer_grpc": self.prefer_grpc,
1547-
"https": self.https,
1548-
"api_key": self.api_key.resolve_value() if self.api_key else None,
1549-
"prefix": self.prefix,
1550-
"timeout": self.timeout,
1551-
"host": self.host,
1552-
"path": self.path,
1553-
"metadata": self.metadata,
1554-
"force_disable_check_same_thread": self.force_disable_check_same_thread,
1555-
}
1543+
# NOTE: We need to use deepcopy here to avoid modifying the original class attributes.
1544+
# For example, the resolved api key is added to metadata by the QdrantClient class when using a hosted
1545+
# Qdrant service, which means running to_dict() exposes the api key.
1546+
return deepcopy(
1547+
{
1548+
"location": self.location,
1549+
"url": self.url,
1550+
"port": self.port,
1551+
"grpc_port": self.grpc_port,
1552+
"prefer_grpc": self.prefer_grpc,
1553+
"https": self.https,
1554+
"api_key": self.api_key.resolve_value() if self.api_key else None,
1555+
"prefix": self.prefix,
1556+
"timeout": self.timeout,
1557+
"host": self.host,
1558+
"path": self.path,
1559+
"metadata": self.metadata,
1560+
"force_disable_check_same_thread": self.force_disable_check_same_thread,
1561+
}
1562+
)
15561563

15571564
def _prepare_collection_config(
15581565
self,

integrations/qdrant/tests/test_document_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from haystack.dataclasses import SparseEmbedding
77
from haystack.document_stores.errors import DuplicateDocumentError
88
from haystack.document_stores.types import DuplicatePolicy
9-
from haystack.utils import Secret
109
from haystack.testing.document_store import (
1110
CountDocumentsTest,
1211
DeleteDocumentsTest,
1312
WriteDocumentsTest,
1413
_random_embeddings,
1514
)
15+
from haystack.utils import Secret
1616
from qdrant_client.http import models as rest
1717

1818
from haystack_integrations.document_stores.qdrant.document_store import (

0 commit comments

Comments
 (0)