Skip to content

Commit 7509a15

Browse files
authored
Fix Amazon OpenSearch Serverless integration with LangChain. (opensearch-project#603)
Signed-off-by: dblock <dblock@amazon.com>
1 parent c66e1e3 commit 7509a15

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
88
### Removed
99
### Fixed
1010
- Fix `TypeError` on `parallel_bulk` ([#601](https://github.com/opensearch-project/opensearch-py/pull/601))
11+
- Fix Amazon OpenSearch Serverless integration with LangChain ([#603](https://github.com/opensearch-project/opensearch-py/pull/603))
1112
### Security
1213

1314
## [2.4.1]

opensearchpy/helpers/signer.py

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class RequestsAWSV4SignerAuth(requests.auth.AuthBase):
7878

7979
def __init__(self, credentials, region, service: str = "es") -> None: # type: ignore
8080
self.signer = AWSV4Signer(credentials, region, service)
81+
self.service = service # tools like LangChain rely on this, see https://github.com/opensearch-project/opensearch-py/issues/600
8182

8283
def __call__(self, request): # type: ignore
8384
return self._sign_request(request) # type: ignore
@@ -133,6 +134,7 @@ class AWSV4SignerAuth(RequestsAWSV4SignerAuth):
133134
class Urllib3AWSV4SignerAuth(Callable): # type: ignore
134135
def __init__(self, credentials, region, service: str = "es") -> None: # type: ignore
135136
self.signer = AWSV4Signer(credentials, region, service)
137+
self.service = service # tools like LangChain rely on this, see https://github.com/opensearch-project/opensearch-py/issues/600
136138

137139
def __call__(self, method: str, url: str, body: Any) -> Dict[str, str]:
138140
return self.signer.sign(method, url, body)

test_opensearchpy/test_connection/test_requests_http_connection.py

+2
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ def test_aws_signer_as_http_auth(self) -> None:
460460
from opensearchpy.helpers.signer import RequestsAWSV4SignerAuth
461461

462462
auth = RequestsAWSV4SignerAuth(self.mock_session(), region)
463+
self.assertEqual(auth.service, "es")
463464
con = RequestsHttpConnection(http_auth=auth)
464465
prepared_request = requests.Request("GET", "http://localhost").prepare()
465466
auth(prepared_request)
@@ -478,6 +479,7 @@ def test_aws_signer_when_service_is_specified(self) -> None:
478479
from opensearchpy.helpers.signer import RequestsAWSV4SignerAuth
479480

480481
auth = RequestsAWSV4SignerAuth(self.mock_session(), region, service)
482+
self.assertEqual(auth.service, service)
481483
con = RequestsHttpConnection(http_auth=auth)
482484
prepared_request = requests.Request("GET", "http://localhost").prepare()
483485
auth(prepared_request)

test_opensearchpy/test_connection/test_urllib3_http_connection.py

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def test_aws_signer_as_http_auth_adds_headers(self, mock_open: Any) -> None:
192192
from opensearchpy.helpers.signer import Urllib3AWSV4SignerAuth
193193

194194
auth = Urllib3AWSV4SignerAuth(self.mock_session(), "us-west-2")
195+
self.assertEqual(auth.service, "es")
195196
con = Urllib3HttpConnection(http_auth=auth, headers={"x": "y"})
196197
con.perform_request("GET", "/")
197198
self.assertEqual(mock_open.call_count, 1)
@@ -249,6 +250,7 @@ def test_aws_signer_when_service_is_specified(self) -> None:
249250
from opensearchpy.helpers.signer import Urllib3AWSV4SignerAuth
250251

251252
auth = Urllib3AWSV4SignerAuth(self.mock_session(), region, service)
253+
self.assertEqual(auth.service, service)
252254
headers = auth("GET", "http://localhost", None)
253255
self.assertIn("Authorization", headers)
254256
self.assertIn("X-Amz-Date", headers)

0 commit comments

Comments
 (0)