[FEATURE] Add support for AWS Sigv4 request signer
Closed this issue · 4 comments
VachaShah commented
Add support for signed requests via AWS sigv4.
hanopensearch commented
@VachaShah Support for AWS Sigv4 reqeust signer in opensearch-py is done in opensearch-project/opensearch-py#133. opensearch-dsl-py uses OpenSearch in opensearch-py for user authentication. Below code shows how it works:
from opensearch_dsl import Search
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
import boto3
import os
# cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
host = os.getenv('host')
region = 'us-west-2' # e.g. us-west-1
credentials = boto3.Session().get_credentials()
auth = AWSV4SignerAuth(credentials, region)
client = OpenSearch(
hosts = [{'host': host, 'port': 443}],
http_auth = auth,
use_ssl = True,
verify_certs = True,
connection_class = RequestsHttpConnection
)
index_name = 'my-dsl-index'
response = client.indices.create(index_name)
print('\nCreating index:')
print(response)
# Add a document to the index.
document = {
'title': 'python',
'description': 'beta',
'category': 'search'
}
id = '1'
response = client.index(
index = index_name,
body = document,
id = id,
refresh = True
)
print('\nAdding document:')
print(response)
# Search for the document.
s = Search(using=client, index=index_name) \
.filter("term", category="search") \
.query("match", title="python")
response = s.execute()
print('\nSearch results:')
for hit in response:
print(hit.meta.score, hit.title)
# Delete the document.
print('\nDeleting document:')
print(response)
# Delete the index.
response = client.indices.delete(
index = index_name
)
print('\nDeleting index:')
print(response)
Here is the output:
Creating index:
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'my-dsl-index'}
Adding document:
{'_index': 'my-dsl-index', '_type': '_doc', '_id': '1', '_version': 1, 'result': 'created', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
Search results:
0.2876821 python
Deleting document:
<Response: [<Hit(my-dsl-index/1): {'title': 'python', 'description': 'beta', 'category': 'sear...}>]>
Deleting index:
{'acknowledged': True}
VachaShah commented
@hanopensearch That is awesome! Thank you very much for sharing this example. Would love a PR for adding this example in the USER_GUIDE.
hanopensearch commented
@VachaShah here is the PR #65.
wbeckler commented
I would suggest we close this issue given that the feature exists and all that remains is documenting, which is tracked in a separate issue.