【问题标题】:User authentication in Elasticsearch query using python使用 python 在 Elasticsearch 查询中进行用户身份验证
【发布时间】:2017-04-28 10:28:47
【问题描述】:

我正在使用 Python 的弹性搜索。我的代码看起来有点像这样:-

              
from elasticsearch import Elasticsearch

if __name__ == '__main__': 
    index="IndexPosition"
    es=Elasticsearch(['https://localhost:8080'])
    res = es.search(index='{0}'.format(index), doc_type="log",size=1000, from_=0, body={ "query": {
    "match": {
      
        ...Match condition
      }
    }
  }})

现在,由于架构的变化,在 elasticsearch 中添加了用户身份验证。假设 username-user 和 password-pass。如何在查询中传递用户名和密码..?

【问题讨论】:

    标签: python python-2.7 elasticsearch elasticsearch-plugin elasticsearch-2.0


    【解决方案1】:

    您需要将用户名和密码传递给 Elasticsearch 对象,如下所示:

    es = Elasticsearch(['http://localhost:8080'], http_auth=('user', 'pass'))

    【讨论】:

      【解决方案2】:

      你可以在url中传递用户名、密码:

      例如:

      用户名:弹性

      密码:更改密码

      es = Elasticsearch(hosts="http://elastic:changeme@localhost:9200/")
      

      使用 CURL

      curl -X GET "elastic:changeme@localhost:9200/"
      

      【讨论】:

      • 为什么 this 解决方案对我有用,而不是上面的那个 (http_auth=(user,pwd))?此外,以明文形式保存密码对于概念验证来说很好,但我不能长期这样做。关于如何隐藏密码的建议?
      • @Guy 在代码中避免硬编码凭据的一种常见方法是使用环境变量。 pypi.org/project/python-dotenv 允许您将凭据放入 .env 文件中并从那里加载它们作为环境变量
      【解决方案3】:

      您可以将 elasticsearch 连接到下面的主机 url 配置

      es = Elasticsearch(hosts="http://user:pass@localhost:9200/")
      

      【讨论】:

        【解决方案4】:
        es = Elasticsearch([{'host': 'localhost', 'port': '8080'}], http_auth=('user', 'pass'))
        

        【讨论】:

          【解决方案5】:

          是的,使用es = Elasticsearch(hosts="http://username:password@es-endpoint:es-port/")

          es版测试成功7.7.1

          【讨论】:

          • 嗨,在尝试查看索引是否存在时出现身份验证错误?您能否提供一些示例代码。在使用用户名/密码创建 esClient 对象后,我正在使用“res = esClient.indices.exists('metadata-store')”
          【解决方案6】:

          我用过

          es = Elasticsearch(['http://kibana.mydomain.com:8080'], http_auth=('user', 'pass'))
          es.search(index="logstash-my_index_log", body={}, size=99)
          

          NotFoundError: NotFoundError(404, 'Not Found', 'Not Found')

          【讨论】:

          • 我看到您的域包含关键字 Kibana。 Kibana 只是用于可视化和管理存储在 ElasticSearch 中的数据的前端,如果您尝试使用 Python 以编程方式访问 ES,则主机应该指向 ES。
          猜你喜欢
          • 2020-04-11
          • 2013-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-16
          相关资源
          最近更新 更多