首页 > 解决方案 > 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 的方式有关,或者是否与我提交此请求的方式有关。任何有关找出我的问题可能出在哪里的帮助将不胜感激。

标签: pythonamazon-web-serviceselasticsearch

解决方案


它也发生在我身上。我找到了两种方法,您必须为它们创建一个 IAM 用户。

  • 打开 IAM 控制台并为 IAM 用户提供必要的策略以访问您的弹性搜索域(或者我仅附加了“AdministratorAccess”策略(它提供 AWS 的所有访问权限)),然后您应该使用此 IAM 帐户的凭证。
  • 或者打开kibana -> security -> roles -> 点击all_access role -> mapped_users -> manage_mapping。然后,您应该将 IAM 用户的 arn 添加到后端角色。您必须在代码上使用此用户的凭据而不是“boto3.Session().get_credentials()”来连接 ES。如果您不熟悉 IAM 角色和策略,我建议您使用第二个。

您应该在 kibana 中转到此页面:

您可以使用此按钮为 IAM 用户创建凭证


推荐阅读