Skip to content

Commit cfb2cf7

Browse files
authored
Adding async support for AWSSigV4 (opensearch-project#254)
* Adding async support for AWSSigV4 Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> * Fix names for connection classes Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> * Update tests to async name space Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> * Add import exceptions to python < 3.6 Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com> Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
1 parent 4c4091b commit cfb2cf7

14 files changed

+541
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99
- Added overload decorators to helpers-actions.pyi-"bulk" ([#239](https://github.com/opensearch-project/opensearch-py/pull/239))
1010
- Document Keberos authenticaion ([214](https://github.com/opensearch-project/opensearch-py/pull/214))
1111
- Add release workflows ([#240](https://github.com/opensearch-project/opensearch-py/pull/240))
12+
- Added SigV4 support for Async Opensearch Client ([#254](https://github.com/opensearch-project/opensearch-py/pull/254))
1213
### Changed
1314
- Updated getting started to user guide ([#233](https://github.com/opensearch-project/opensearch-py/pull/233))
1415
- Updated CA certificate handling to check OpenSSL environment variables before defaulting to certifi ([#196](https://github.com/opensearch-project/opensearch-py/pull/196))

USER_GUIDE.md

+49-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- [User guide of OpenSearch Python Client](#user-guide-of-opensearch-python-client)
1+
- [User guide of OpenSearch Python client](#user-guide-of-opensearch-python-client)
22
- [Setup](#setup)
33
- [Example](#example)
44
- [Creating a client](#creating-a-client)
@@ -9,8 +9,8 @@
99
- [Searching for a document](#searching-for-a-document)
1010
- [Deleting a document](#deleting-a-document)
1111
- [Deleting an index](#deleting-an-index)
12-
- [Making API Calls](#making-api-calls)
13-
- [Point in Time API](#point-in-time-api)
12+
- [Making API calls](#making-api-calls)
13+
- [Point in time API](#point-in-time-api)
1414
- [Using plugins](#using-plugins)
1515
- [Alerting plugin](#alerting-plugin)
1616
- [**Searching for monitors**](#searching-for-monitors)
@@ -22,6 +22,7 @@
2222
- [Using different authentication methods](#using-different-authentication-methods)
2323
- [Using IAM credentials](#using-iam-credentials)
2424
- [Pre-requisites to use `AWSV4SignerAuth`](#pre-requisites-to-use-awsv4signerauth)
25+
- [Using IAM authentication with an async client](#using-iam-authentication-with-an-async-client)
2526
- [Using Kerberos](#using-kerberos)
2627

2728
# User guide of OpenSearch Python client
@@ -439,6 +440,51 @@ print('\nSearch results:')
439440
print(response)
440441
```
441442

443+
## Using IAM authentication with an async client
444+
445+
Make sure to use the `AsyncHttpConnection` connection class with the async `AWSV4SignerAsyncAuth` signer.
446+
447+
```python
448+
from opensearchpy import OpenSearch, AsyncHttpConnection, AWSV4SignerAsyncAuth
449+
import boto3
450+
451+
host = '' # cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
452+
region = 'us-west-2'
453+
credentials = boto3.Session().get_credentials()
454+
auth = AWSV4SignerAsyncAuth(credentials, region)
455+
index_name = 'python-test-index3'
456+
457+
client = OpenSearch(
458+
hosts = [{'host': host, 'port': 443}],
459+
http_auth = auth,
460+
use_ssl = True,
461+
verify_certs = True,
462+
connection_class = AsyncHttpConnection
463+
)
464+
465+
async def search():
466+
q = 'miller'
467+
query = {
468+
'size': 5,
469+
'query': {
470+
'multi_match': {
471+
'query': q,
472+
'fields': ['title^2', 'director']
473+
}
474+
}
475+
}
476+
477+
response = await client.search(
478+
body = query,
479+
index = index_name
480+
)
481+
482+
print('\nSearch results:')
483+
print(response)
484+
485+
search()
486+
```
487+
=======
442488
### Using Kerberos
443489

444490
There are several python packages that provide Kerberos support over HTTP connections, such as [requests-kerberos](http://pypi.org/project/requests-kerberos) and [requests-gssapi](https://pypi.org/project/requests-gssapi). The following example shows how to setup the authentication. Note that some of the parameters, such as `mutual_authentication` might depend on the server settings.

opensearchpy/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
SSLError,
6363
TransportError,
6464
)
65-
from .helpers import AWSV4SignerAuth
65+
from .helpers import AWSV4SignerAsyncAuth, AWSV4SignerAuth
6666
from .serializer import JSONSerializer
6767
from .transport import Transport
6868

@@ -79,6 +79,7 @@
7979
"JSONSerializer",
8080
"Connection",
8181
"RequestsHttpConnection",
82+
"AsyncHttpConnection",
8283
"Urllib3HttpConnection",
8384
"ImproperlyConfigured",
8485
"OpenSearchException",
@@ -95,6 +96,7 @@
9596
"OpenSearchWarning",
9697
"OpenSearchDeprecationWarning",
9798
"AWSV4SignerAuth",
99+
"AWSV4SignerAsyncAuth",
98100
]
99101

100102
try:
@@ -105,12 +107,14 @@
105107
from ._async.client import AsyncOpenSearch
106108
from ._async.http_aiohttp import AIOHttpConnection, AsyncConnection
107109
from ._async.transport import AsyncTransport
110+
from .connection import AsyncHttpConnection
108111

109112
__all__ += [
110113
"AIOHttpConnection",
111114
"AsyncConnection",
112115
"AsyncTransport",
113116
"AsyncOpenSearch",
117+
"AsyncHttpConnection",
114118
]
115119
except (ImportError, SyntaxError):
116120
pass

opensearchpy/__init__.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import sys
2828
from typing import Tuple
2929

3030
from .client import OpenSearch as OpenSearch
31+
from .connection import AsyncHttpConnection as AsyncHttpConnection
3132
from .connection import Connection as Connection
3233
from .connection import RequestsHttpConnection as RequestsHttpConnection
3334
from .connection import Urllib3HttpConnection as Urllib3HttpConnection
@@ -57,6 +58,7 @@ try:
5758
from ._async.client import AsyncOpenSearch as AsyncOpenSearch
5859
from ._async.http_aiohttp import AIOHttpConnection as AIOHttpConnection
5960
from ._async.transport import AsyncTransport as AsyncTransport
61+
from .helpers import AWSV4SignerAsyncAuth as AWSV4SignerAsyncAuth
6062
from .helpers import AWSV4SignerAuth as AWSV4SignerAuth
6163
except (ImportError, SyntaxError):
6264
pass

opensearchpy/connection/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
# under the License.
2626

2727

28+
import sys
29+
2830
from .base import Connection
2931
from .http_requests import RequestsHttpConnection
3032
from .http_urllib3 import Urllib3HttpConnection, create_ssl_context
@@ -35,3 +37,16 @@
3537
"Urllib3HttpConnection",
3638
"create_ssl_context",
3739
]
40+
41+
try:
42+
# Asyncio only supported on Python 3.6+
43+
if sys.version_info < (3, 6):
44+
raise ImportError
45+
46+
from .http_async import AsyncHttpConnection
47+
48+
__all__ += [
49+
"AsyncHttpConnection",
50+
]
51+
except (ImportError, SyntaxError):
52+
pass

opensearchpy/connection/__init__.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# under the License.
2626

2727
from .base import Connection as Connection
28+
from .http_async import AsyncHttpConnection as AsyncHttpConnection
2829
from .http_requests import RequestsHttpConnection as RequestsHttpConnection
2930
from .http_urllib3 import Urllib3HttpConnection as Urllib3HttpConnection
3031
from .http_urllib3 import create_ssl_context as create_ssl_context

0 commit comments

Comments
 (0)