python - Elasticsearch Service with Cognito -- 如何通过 Python HTTP 请求访问
问题描述
我最近设置了一个 Elasticsearch 服务并将其配置为使用 Cognito 进行身份管理。我遵循了本指南,到目前为止一切都按预期工作。我能够按预期添加新用户,他们可以按预期访问 Kibana。
但是,我还想使用 Python 与 Elasticsearch 服务进行交互。我已遵循本指南,但我收到有关没有正确访问权限的权限错误。
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import boto3
host = 'hostname.us-east-2.es.amazonaws.com/'
region = 'us-east-2'
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
es = Elasticsearch(
hosts = [{'host': host, 'port': 443}],
http_auth = awsauth,
use_ssl = True,
verify_certs = True,
connection_class = RequestsHttpConnection
)
print(es.info())
AuthorizationException: AuthorizationException(403, 'security_exception', 'no permissions for [indices:admin/get] and User [name=arn:aws:iam::12345678:user/username, backend_roles=[], requestedTenant=null]')
我不确定问题是否与我配置 Conginto 的方式有关,或者是否与我提交此请求的方式有关。任何有关找出我的问题可能出在哪里的帮助将不胜感激。
解决方案
它也发生在我身上。我找到了两种方法,您必须为它们创建一个 IAM 用户。
- 打开 IAM 控制台并为 IAM 用户提供必要的策略以访问您的弹性搜索域(或者我仅附加了“AdministratorAccess”策略(它提供 AWS 的所有访问权限)),然后您应该使用此 IAM 帐户的凭证。
- 或者打开kibana -> security -> roles -> 点击all_access role -> mapped_users -> manage_mapping。然后,您应该将 IAM 用户的 arn 添加到后端角色。您必须在代码上使用此用户的凭据而不是“boto3.Session().get_credentials()”来连接 ES。如果您不熟悉 IAM 角色和策略,我建议您使用第二个。
推荐阅读
- javascript - 异步/等待返回未定义的值
- python - Python散点图多个颜色条问题
- asp.net - Angular js ng-model 无法访问母版页 asp.net 上模板指令中的文本框控件
- java - JComboBox.setEditable(true) 不改变 JTable 中的值
- php - 在 Woocommerce 档案中将链接按钮添加到可变产品的变体
- python - Python 检查两个或多个列表是否是 List 的子集
- oracle - Oracle:对 regexp_like 表达式的结果进行排序
- c - 为什么我的递归函数即使到达返回(0)也不会停止?
- python - 在 python 中通过循环从一个文本文件到另一个文本文件进行搜索,同时每个搜索项仅返回一个结果
- corda - 在 Windows 上运行 Cordite 网络映射 JAR 时无法运行程序 npm 错误