【问题标题】:Elasticsearch - Authentication error when check to see if index existElasticsearch - 检查索引是否存在时的身份验证错误
【发布时间】:2021-02-02 00:38:28
【问题描述】:

我为 kibana 启用了 cognito 身份验证的弹性搜索。这按预期工作正常。

在我的 python 脚本中,我通过在 http_auth() 中提供用户名/密码来连接到 elasticsearch,同时创建连接对象。但是当我尝试检查索引是否存在时,会出现身份验证错误吗?有人可以帮忙吗。这是您模拟的示例代码。

from __future__ import print_function
import json
import time
import urllib
import re
import sys
import requests
import base64
import time
from elasticsearch import Elasticsearch
from datetime import datetime

esEndpoint = ""
uname
pwd
indexName = 'index_1'

mappings_rds = {
    "settings": {
        "number_of_shards": 2,
        "number_of_replicas": 1
    },
    "mappings": {
      "properties" : {
         "tableName": {
            "type": "keyword"
          },
         "tableRows": {
            "type": "integer"
          },        
         "updatedTime": {
            "type": "date",
            "format":"date_optional_time||yyyy-MM-dd'T'HH:mm:ss"
          },
        "created_timestamp":{
            "type": "date",
            "format":"date_optional_time||yyyy-MM-dd'T'HH:mm:ss"
          }
      }
    }
}

esClient = Elasticsearch([esEndPoint],http_auth=(uname,pwd))

try:
      res = esClient.indices.exists(indexName)
      print(res)
      if res is False:
          r = esClient.indices.create(indexName, body=mapping_rds, ignore=400)
      return 1
  except Exception as E:
          print("Unable to Create Index {0}".format(indexName))

【问题讨论】:

  • 使用 (uname,pwd) 访问 kibana 并告诉我们您在运行 HEAD /index_1 时看到了什么
  • 感谢 Assael,我尝试了命令并得到 "{"statusCode":404,"error":"Not Found","message":"404 - Not Found"}" 作为响应kibana 控制台。
  • 您能分享一下您在运行代码时遇到的错误吗?
  • esClient.ping() 的输出是什么?

标签: python python-3.x elasticsearch elasticsearch-py


【解决方案1】:

谢谢大家的cmets。我就此与 AWS Support 团队进行了通话。发现问题是我使用的用户名和密码。我使用的是使用 AWS Cognito 创建的用户凭证。 AWS 支持团队确认,上述凭证仅用于 Kibana 访问,不能用于从 databricks / python 脚本连接到 Elasticsearch。

更多信息在这里:https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html

一旦我生成了访问/秘密密钥,并从我的 python 脚本中使用它,我就能够连接到 elasticsearch 来创建索引。

分享示例代码以供将来参考:

session=boto3.session.Session(aws_access_key_id=akey, aws_secret_access_key=skey, region_name=region)
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
print(credentials.access_key, credentials.secret_key, credentials.token)
es = Elasticsearch(
    hosts = [{'host': esEndpoint, 'port': 443}],
    http_auth = awsauth,
    use_ssl = True,
    verify_certs = True,
    connection_class = RequestsHttpConnection
)

谢谢

【讨论】:

    猜你喜欢
    • 2015-09-16
    • 1970-01-01
    • 2020-07-19
    • 2014-10-29
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多