简体   繁体   English

使用 Sig v4 将数据从 AWS Lambda 索引到 Elasticsearch 时出现 403 错误:'[indices:data/write/bulk] 无权限'

[英]403 Error when indexing data from AWS Lambda to Elasticsearch using Sig v4: 'no permissions for [indices:data/write/bulk] '

My function can index documents in single and bulk to my AWS Elasticsearch from a local Jupyter notebook, but when I deploy to Lambda it keeps returning this error:我的 function 可以从本地 Jupyter 笔记本将单个文档和批量文档索引到我的 AWS Elasticsearch,但是当我部署到 Lambda 时,它一直返回此错误:

"errorMessage": "AuthorizationException(403, 'security_exception', 'no permissions for
[indices:data/write/bulk] and User [name=arn:aws:iam::xxxxxxxxxxxx:role/MyLambdaRole,
backend_roles=[arn:aws:iam::xxxxxxxxxxxx:role/MyLambdaRole], requestedTenant=null]')"

My Elasticsearch domain (v7.7) is configured as such:我的 Elasticsearch 域 (v7.7) 配置如下:

Fine-grained access control: Enabled
Master user type: Internal user database
SAML authentication: Disabled
Amazon Cognito for authentication: Disabled
Require HTTPS: Enabled
Encryption at rest: Enabled
KMS master keyarn:aws:kms:us-east-1:xxxxxxxxxxxxx:key/<aws/es key>
Node-to-node encryption: Enabled

The domain's access policy contains:域的访问策略包含:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:*",
      "Resource": "*"
    }
  ]
}

The IAM policy for MyLambdaRole contains: MyLambdaRole 的 IAM 策略包含:

...
        {
            "Action": [
                "es:*"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        }
    ]

In Kibana I have mapped both my AWS admin IAM user and MyLambdaRole under Security -> Role Mappings -> all_access.在 Kibana 中,我在安全 -> 角色映射 -> all_access 下映射了我的 AWS 管理员 IAM 用户和 MyLambdaRole。 I have tried different combinations of adding them to Backend roles and also adding them to security_manager.我尝试了将它们添加到后端角色以及将它们添加到 security_manager 的不同组合。

The Lambda uses AWS Signature v4 authentication and the elasticsearch client is version 7.7.0: Lambda 使用 AWS Signature v4 身份验证,elasticsearch 客户端版本为 7.7.0:

import boto3
from elasticsearch import Elasticsearch, RequestsHttpConnection, helpers
from requests_aws4auth import AWS4Auth

session = boto3.Session()
credentials = session.get_credentials().get_frozen_credentials()

awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, \
    session.region_name, 'es', session_token=credentials.token)

host = 'search-es-domain.us-east-1.es.amazonaws.com'

es = Elasticsearch(
    hosts = [{'host': host, 'port': 443}],
    http_auth = awsauth
    use_ssl = True,
    verify_certs = True,
    connection_class = RequestsHttpConnection
)

# Single indexing call
document = { my data }
es.index(index="my_index", doc_type="_doc", id=doc_id, body=document)

# Bulk indexing call
k = ({ my data })
helpers.bulk(es, k)

If I replace http_auth = awsauth with my Kibana credentials http_auth = (kibana_username, kibana_password) it returns status 200 but then no new documents are created in the index which is weird.如果我将http_auth = awsauth替换为我的 Kibana 凭据http_auth = (kibana_username, kibana_password)它返回状态 200,但随后没有在索引中创建新文档,这很奇怪。

I would like to know what I could be missing or where my configuration could be off.我想知道我可能遗漏了什么或我的配置可能在哪里关闭。

This is because of fine grained access control being enabled.这是因为启用了细粒度的访问控制。 I ran into the exact same problem and fine grained access control causes issues.我遇到了完全相同的问题,细粒度的访问控制导致了问题。 Your notebook might be using the master ARN you specified which has access and is always allowed basically.您的笔记本可能正在使用您指定的主 ARN,它具有访问权限并且基本上始终被允许。

I recreated my ES instance and disabled fine grained access control and used domain policy only as it suited our set up.我重新创建了我的 ES 实例并禁用了细粒度的访问控制,并仅在适合我们的设置时才使用域策略。

Read more here and notice the highlighted section re user / IAM mixing and not working correctly.在此处阅读更多内容并注意突出显示的部分 re user / IAM mixing and not working correctly。

https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/fgac.html https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/fgac.html

I am using Opensearch.我正在使用开放搜索。 I was facing the same issue when inserting the document.插入文档时我遇到了同样的问题。 This blog help me.这个博客帮助我。 They suggested add a lambda role ARN in that accesses Open search dashboards/Kibana in Roles -> Mapp users -> Manage mapping -> Backend Roles.他们建议在 Roles -> Mapp users -> Manage mapping -> Backend Roles 中添加一个 lambda 角色 ARN 来访问 Open search dashboards/Kibana。 For me it worked.对我来说它奏效了。 Detail steps are mentioned in below url详细步骤如下url

https://aws.amazon.com/premiumsupport/knowledge-center/opensearch-troubleshoot-cloudwatch-logs/ enter link description here https://aws.amazon.com/premiumsupport/knowledge-center/opensearch-troubleshoot-cloudwatch-logs/ 在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM