【问题标题】:Access aws elastic search role based using python elastic search package使用python弹性搜索包访问aws弹性搜索角色
【发布时间】:2021-07-13 08:56:22
【问题描述】:

我正在使用deepset/haystack 并与弹性搜索进行通信。使用 OpenDistroElasticsearchDocumentStore 方法适用于用户名、密码访问 aws 弹性搜索。在 ec2 中部署时似乎不适用于基于角色的访问。请建议我使用 python 弹性搜索包访问 aws 弹性搜索的解决方案,给定角色访问权限

【问题讨论】:

    标签: python elasticsearch huggingface-transformers aws-elasticsearch haystack


    【解决方案1】:

    您是指像this 这样的基于 IAM 的 AWS 访问吗?我们最近刚刚合并了一个可能对您有所帮助的功能 (#965)。请从 master 分支安装最新的 Haystack 版本,然后尝试以下方式:

    import boto3
    
    from requests_aws4auth import AWS4Auth
    from haystack.document_store.elasticsearch import ElasticsearchDocumentStore
    from elasticsearch import RequestsHttpConnection
    
    host = '<vpc_host>'
    port = 443
    region = 'eu-central-1'
    service = 'es'
     
    credentials = boto3.Session().get_credentials()
    aws4auth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
     
    document_store = OpenDistroElasticsearchDocumentStore(host=host,
                                                port=port,
                                                aws4auth=aws4auth,
                                                # can't be used with default es client version used in e.g. aws sagemaker
                                                embedding_field=None,
                                                index="document")
    

    【讨论】:

    • 嗨,在这一步中,credentials = boto3.Session().get_credentials() 它似乎在用户访问密钥和密钥的情况下工作。在角色的情况下没有按预期工作。请帮我解决这个问题
    • 你能澄清一下你所说的“角色案例”是什么意思吗?您能否提供一个在您的设置中没有 Haystack 的普通(开放发行版)elasticsearch 示例?从 elasticsearch 文档我的理解是,以上是 AWS 上 IAM 的“最佳实践”(elasticsearch-py.readthedocs.io/en/v7.12.0/…
    • 嗨,我似乎已经解决了与角色相关的问题。使用 OpenDistroElasticsearchDocumentStore 方法时出现新错误 __init__() got an unexpected keyword argument 'aws4auth'。谢谢
    【解决方案2】:
    from requests_aws4auth import AWS4Auth
    from botocore.session import Session
    credentials = Session().get_credentials()
    auth = AWS4Auth(region='eu-west-1', service='es', refreshable_credentials=credentials)
    

    此示例展示了如何构建一个具有自动刷新凭证的 AWS4Auth 实例,适用于使用 AWS IAM 承担角色的长时间运行的应用程序。 RefreshableCredentials 实例用于为每个请求生成有效的静态凭证,无需在临时凭证过期时重新创建 AWS4Auth 实例。

    来源:https://github.com/tedder/requests-aws4auth#dynamic-sts-credentials-using-botocore-refreshablecredentials

    这是merged into AWS4Auth in May 2021

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 2018-10-26
      • 2021-10-11
      • 1970-01-01
      • 2014-08-19
      • 2023-01-14
      • 1970-01-01
      相关资源
      最近更新 更多