Skip to content

Commit 9e448a1

Browse files
XuanYang-cnabd-770Abdullah-Ahmed2
authored
Add Optional CA Certificate Support to Bulk Import Functions (#2672) (#2739)
Issue: #2671 This PR addresses the above issue by adding optional CA certificate support to the below bulk import functions: - **bulk_import** - **get_import_progress** - **list_import_jobs** Now, users with TLS-mode milvus can use these functions in pymilvus, using their SSL-certificate. Signed-off-by: yangxuan <[email protected]> Co-authored-by: Abdullah Ahmed <[email protected]> Co-authored-by: Abdullah-Ahmed2 <[email protected]>
1 parent 9025b62 commit 9e448a1

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

pymilvus/bulk_writer/bulk_import.py

+62-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import json
1414
import logging
15-
from typing import List, Optional
15+
from typing import List, Optional, Union
1616

1717
import requests
1818

@@ -45,11 +45,38 @@ def _handle_response(url: str, res: json):
4545

4646

4747
def _post_request(
48-
url: str, api_key: str, params: {}, timeout: int = 20, **kwargs
48+
url: str,
49+
api_key: str,
50+
params: {},
51+
timeout: int = 20,
52+
verify: Optional[Union[bool, str]] = True,
53+
cert: Optional[Union[str, tuple]] = None,
54+
**kwargs,
4955
) -> requests.Response:
56+
"""Send a POST request with 1-way / 2-way optional certificate validation
57+
58+
Args:
59+
url (str): The endpoint URL
60+
api_key (str): API key for authentication
61+
params (dict): JSON parameters for the request
62+
timeout (int): Timeout for the request
63+
verify (bool, str, optional): Either a boolean, to verify the server's TLS certificate
64+
or a string, which must be server's certificate path. Defaults to `True`.
65+
cert (str, tuple, optional): if String, path to ssl client cert file.
66+
if Tuple, ('cert', 'key') pair.
67+
68+
Returns:
69+
requests.Response: Response object.
70+
"""
5071
try:
5172
resp = requests.post(
52-
url=url, headers=_http_headers(api_key), json=params, timeout=timeout, **kwargs
73+
url=url,
74+
headers=_http_headers(api_key),
75+
json=params,
76+
timeout=timeout,
77+
verify=verify,
78+
cert=cert,
79+
**kwargs,
5380
)
5481
if resp.status_code != 200:
5582
_throw(f"Failed to post url: {url}, status code: {resp.status_code}")
@@ -85,6 +112,8 @@ def bulk_import(
85112
api_key: str = "",
86113
access_key: str = "",
87114
secret_key: str = "",
115+
verify: Optional[Union[bool, str]] = True,
116+
cert: Optional[Union[str, tuple]] = None,
88117
**kwargs,
89118
) -> requests.Response:
90119
"""call bulkinsert restful interface to import files
@@ -103,6 +132,10 @@ def bulk_import(
103132
api_key (str): API key to authenticate your requests.
104133
access_key (str): access key to access the object storage
105134
secret_key (str): secret key to access the object storage
135+
verify (bool, str, optional): Either a boolean, to verify the server's TLS certificate
136+
or a string, which must be server's certificate path. Defaults to `True`.
137+
cert (str, tuple, optional): if String, path to ssl client cert file.
138+
if Tuple, ('cert', 'key') pair.
106139
107140
Returns:
108141
response of the restful interface
@@ -125,13 +158,21 @@ def bulk_import(
125158
if isinstance(options, dict):
126159
params["options"] = options
127160

128-
resp = _post_request(url=request_url, api_key=api_key, params=params, **kwargs)
161+
resp = _post_request(
162+
url=request_url, api_key=api_key, params=params, verify=verify, cert=cert, **kwargs
163+
)
129164
_handle_response(request_url, resp.json())
130165
return resp
131166

132167

133168
def get_import_progress(
134-
url: str, job_id: str, cluster_id: str = "", api_key: str = "", **kwargs
169+
url: str,
170+
job_id: str,
171+
cluster_id: str = "",
172+
api_key: str = "",
173+
verify: Optional[Union[bool, str]] = True,
174+
cert: Optional[Union[str, tuple]] = None,
175+
**kwargs,
135176
) -> requests.Response:
136177
"""get job progress
137178
@@ -140,6 +181,10 @@ def get_import_progress(
140181
job_id (str): a job id
141182
cluster_id (str): id of a milvus instance(for cloud)
142183
api_key (str): API key to authenticate your requests.
184+
verify (bool, str, optional): Either a boolean, to verify the server's TLS certificate
185+
or a string, which must be server's certificate path. Defaults to `True`.
186+
cert (str, tuple, optional): if String, path to ssl client cert file.
187+
if Tuple, ('cert', 'key') pair.
143188
144189
Returns:
145190
response of the restful interface
@@ -151,7 +196,9 @@ def get_import_progress(
151196
"clusterId": cluster_id,
152197
}
153198

154-
resp = _post_request(url=request_url, api_key=api_key, params=params, **kwargs)
199+
resp = _post_request(
200+
url=request_url, api_key=api_key, params=params, verify=verify, cert=cert, **kwargs
201+
)
155202
_handle_response(request_url, resp.json())
156203
return resp
157204

@@ -163,6 +210,8 @@ def list_import_jobs(
163210
api_key: str = "",
164211
page_size: int = 10,
165212
current_page: int = 1,
213+
verify: Optional[Union[bool, str]] = True,
214+
cert: Optional[Union[str, tuple]] = None,
166215
**kwargs,
167216
) -> requests.Response:
168217
"""list jobs in a cluster
@@ -174,6 +223,10 @@ def list_import_jobs(
174223
api_key (str): API key to authenticate your requests.
175224
page_size (int): pagination size
176225
current_page (int): pagination
226+
verify (bool, str, optional): Either a boolean, to verify the server's TLS certificate
227+
or a string, which must be server's certificate path. Defaults to `True`.
228+
cert (str, tuple, optional): if String, path to ssl client cert file.
229+
if Tuple, ('cert', 'key') pair.
177230
178231
Returns:
179232
response of the restful interface
@@ -187,6 +240,8 @@ def list_import_jobs(
187240
"currentPage": current_page,
188241
}
189242

190-
resp = _post_request(url=request_url, api_key=api_key, params=params, **kwargs)
243+
resp = _post_request(
244+
url=request_url, api_key=api_key, params=params, verify=verify, cert=cert, **kwargs
245+
)
191246
_handle_response(request_url, resp.json())
192247
return resp

0 commit comments

Comments
 (0)