【发布时间】:2016-05-07 13:21:34
【问题描述】:
我希望使用 Amazon 的 Elasticsearch 服务器来支持在 Django 数据库中搜索长文本字段。但是,我也不想将这个搜索暴露给那些没有登录并且不想通过默默无闻或某些 IP 限制策略来依赖安全性的人(除非它可以很好地与现有的 heroku 应用程序一起使用,部署 Django 应用程序的位置)。
Haystack 在这方面似乎走了很长一段路,但似乎没有一种简单的方法可以将其配置为使用 Amazon 的 IAM 凭证来访问 Elasticsearch 服务。此功能确实存在于它使用的 elasticsearch-py 中。
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())
关于使用 HTTP 授权,我在 https://github.com/django-haystack/django-haystack/issues/1046 的问题下发现了这一点
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,
}
}
}
我想知道是否有一种方法可以将这两者结合起来,如下所示(正如预期的那样,它会给出错误,因为它不仅仅是用户名和密码):
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
}
},
}
这里的错误:
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
关于如何实现这一点的任何想法?
【问题讨论】:
-
您是否尝试使用 AWS 凭证根据您的私有 ElasticSearch 实施对用户进行身份验证?
-
我为应用程序创建了一个 Amazon IAM 用户。我只希望那些可以访问该应用程序的人能够使用它向 Elasticsearch 服务器提交请求。所以只需要一个 AWS 凭证。
标签: django amazon-web-services heroku elasticsearch django-haystack