I hope to use the Amazon Elasticsearch server to provide a search for longtext fields in a Django database. However, I also donโt want to disclose this search to those who donโt have a login, and you donโt want to rely on security through obscurity or some kind of IP restriction tactics (if this does not work with the existing heroku application where the Django application is deployed )
Haystack seems to be important for this, but there seems to be no easy way to configure it to use Amazon IAM credentials to access the Elasticsearch service. This functionality exists in elasticsearch-py, which it uses.
https://elasticsearch-py.readthedocs.org/en/master/#running-with-aws-elasticsearch-service
from elasticsearch import Elasticsearch, RequestsHttpConnection from requests_aws4auth import AWS4Auth host = 'YOURHOST.us-east-1.es.amazonaws.com' awsauth = AWS4Auth(YOUR_ACCESS_KEY, YOUR_SECRET_KEY, REGION, 'es') es = Elasticsearch( hosts=[{'host': host, 'port': 443}], http_auth=awsauth, use_ssl=True, verify_certs=True, connection_class=RequestsHttpConnection ) print(es.info())
Regarding the use of HTTP authorization, I found this in the https://github.com/django-haystack/django-haystack/issues/1046 section
from urlparse import urlparse parsed = urlparse('https://user: pass@host :port') HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', 'URL': parsed.hostname, 'INDEX_NAME': 'haystack', 'KWARGS': { 'port': parsed.port, 'http_auth': (parsed.username, parsed.password), 'use_ssl': True, } } }
I am wondering if there is a way to combine these two, something like the following (which, as expected, gives an error, since it is more than just a username and password):
from requests_aws4auth import AWS4Auth awsauth = AWS4Auth([AACCESS_KEY],[SECRET_KEY],[REGION],'es') HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', 'URL': [AWSHOST], 'INDEX_NAME': 'haystack', 'KWARGS': { 'port': 443, 'http_auth': awsauth, 'use_ssl': True, 'verify_certs': True } }, }
The error is here:
TypeError at /admin/ must be convertible to a buffer, not AWS4Auth Request Method: GET Request URL: http://127.0.0.1:8000/admin/ Django Version: 1.7.7 Exception Type: TypeError Exception Value: must be convertible to a buffer, not AWS4Auth Exception Location: /usr/lib/python2.7/base64.py in b64encode, line 53
Any ideas on how to do this?