Skip to content

Commit

Permalink
Fix Amazon OpenSearch Serverless integration with LangChain. (opensea…
Browse files Browse the repository at this point in the history
…rch-project#603)

Signed-off-by: dblock <dblock@amazon.com>
Signed-off-by: roma2023 <romasaparhan19@gmail.com>
  • Loading branch information
dblock authored and roma2023 committed Dec 28, 2023
1 parent a1ed1ab commit 7b4d93b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed
### Fixed
- Fix `TypeError` on `parallel_bulk` ([#601](https://github.com/opensearch-project/opensearch-py/pull/601))
- Fix Amazon OpenSearch Serverless integration with LangChain ([#603](https://github.com/opensearch-project/opensearch-py/pull/603))
### Security

## [2.4.1]
Expand Down
2 changes: 2 additions & 0 deletions opensearchpy/helpers/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class RequestsAWSV4SignerAuth(requests.auth.AuthBase):

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

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

def __call__(self, method: str, url: str, body: Any) -> Dict[str, str]:
return self.signer.sign(method, url, body)
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ def test_aws_signer_as_http_auth(self) -> None:
from opensearchpy.helpers.signer import RequestsAWSV4SignerAuth

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

auth = RequestsAWSV4SignerAuth(self.mock_session(), region, service)
self.assertEqual(auth.service, service)
con = RequestsHttpConnection(http_auth=auth)
prepared_request = requests.Request("GET", "http://localhost").prepare()
auth(prepared_request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def test_aws_signer_as_http_auth_adds_headers(self, mock_open: Any) -> None:
from opensearchpy.helpers.signer import Urllib3AWSV4SignerAuth

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

auth = Urllib3AWSV4SignerAuth(self.mock_session(), region, service)
self.assertEqual(auth.service, service)
headers = auth("GET", "http://localhost", None)
self.assertIn("Authorization", headers)
self.assertIn("X-Amz-Date", headers)
Expand Down

0 comments on commit 7b4d93b

Please sign in to comment.