Django-Haystack 使用带有 IAM 凭证的 Amazon Elasticsearch 托管
Django-Haystack using Amazon Elasticsearch hosting with IAM credentials
我希望使用 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 Identity and Access Management (IAM) 允许您管理 AWS 服务 的用户和用户权限,以控制 AWS 本身的用户可以访问哪些 AWS 资源。
您不能使用 IAM 凭据通过 http_auth 在应用程序级别授权用户,因为看起来您正在尝试通过此处的 Haystack 进行操作。它们是针对不同服务的不同身份验证方案。它们不兼容。
在您的安全用例中,您已声明需要 1) 限制对您的应用程序的访问,以及 2) 保护 Elasticsearch 服务端口不被开放访问。这两个要求可以通过以下方法来满足:
限制对您的应用程序的访问
I also don't want to expose this search to those who don't have a log in
对于前端搜索应用程序,您想在 Web 服务器上使用服务器级别 Basic access authentication(HTTP 身份验证)配置。这是您要通过标准 http_auth 用户名和密码(同样,不是 IAM)控制用户登录您的应用程序的地方。这将在应用程序级别保护您的应用程序。
保护 Elasticsearch 服务端口
don't want to rely on security through obscurity or some
IP restriction tactic (unless it would work well with an existing
heroku app, where the Django app is deployed).
IP 限制正是在这里起作用的,并且与 AWS 安全最佳实践一致。您想要使用 security groups and security group rules 作为防火墙来控制 EC2 实例的流量。
给定 Haystack 配置:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}
您需要在该 IP 和端口 127.0.0.1 的安全组 and/or ACL 级别实施 IP 限制,以限制仅来自您的 Django 主机或其他授权主机的访问。这将保护它免受服务级别的任何未经授权的访问。
在您的实施中,URL 可能会解析为 public 或私有 IP,具体取决于您的网络架构。
您离成功仅一步之遥,将 connection_class
添加到 KWARGS
,一切都会按预期进行。
import elasticsearch
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,
'connection_class': elasticsearch.RequestsHttpConnection,
}
},
}
我希望使用 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 Identity and Access Management (IAM) 允许您管理 AWS 服务 的用户和用户权限,以控制 AWS 本身的用户可以访问哪些 AWS 资源。
您不能使用 IAM 凭据通过 http_auth 在应用程序级别授权用户,因为看起来您正在尝试通过此处的 Haystack 进行操作。它们是针对不同服务的不同身份验证方案。它们不兼容。
在您的安全用例中,您已声明需要 1) 限制对您的应用程序的访问,以及 2) 保护 Elasticsearch 服务端口不被开放访问。这两个要求可以通过以下方法来满足:
限制对您的应用程序的访问
I also don't want to expose this search to those who don't have a log in
对于前端搜索应用程序,您想在 Web 服务器上使用服务器级别 Basic access authentication(HTTP 身份验证)配置。这是您要通过标准 http_auth 用户名和密码(同样,不是 IAM)控制用户登录您的应用程序的地方。这将在应用程序级别保护您的应用程序。
保护 Elasticsearch 服务端口
don't want to rely on security through obscurity or some IP restriction tactic (unless it would work well with an existing heroku app, where the Django app is deployed).
IP 限制正是在这里起作用的,并且与 AWS 安全最佳实践一致。您想要使用 security groups and security group rules 作为防火墙来控制 EC2 实例的流量。
给定 Haystack 配置:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}
您需要在该 IP 和端口 127.0.0.1 的安全组 and/or ACL 级别实施 IP 限制,以限制仅来自您的 Django 主机或其他授权主机的访问。这将保护它免受服务级别的任何未经授权的访问。
在您的实施中,URL 可能会解析为 public 或私有 IP,具体取决于您的网络架构。
您离成功仅一步之遥,将 connection_class
添加到 KWARGS
,一切都会按预期进行。
import elasticsearch
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,
'connection_class': elasticsearch.RequestsHttpConnection,
}
},
}