【问题标题】:Creating a valid signed request to an AWS ES instance from node.js从 node.js 创建对 AWS ES 实例的有效签名请求
【发布时间】:2017-04-19 10:35:17
【问题描述】:

我正在尝试找到一个示例,说明如何在 node.js 中连接到 AWS ES 实例,然后通过简单的请求访问 ES 集群。

我正在尝试使用elasticsearch node package 以及一个名为http-aws-es 的开源插件来执行此操作。

我已将我的 aws ES 访问策略配置为如下所示:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<account-id>:root"
      },
      "Action": "es:*",
      "Resource": "example-domain.us-east-1.es.amazonaws.com:<account-id>:domain/*"
    },
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "es:*",
      "Resource": "example-domain.us-east-1.es.amazonaws.com:<account-id>:domain/*",
      "Condition": {
        "IpAddress": {
          "aws:SourceIp": "<my-ip>"
        }
      }
    }
  ]
} 

因此,我希望能够从 ip 地址或链接到我的 aws 帐户的 IAM 用户对 es 实例发出 put 和 get 请求。

我在 node.js 中有以下代码尝试这样做:

var aws_access_key = 'example';
var aws_secret_key = 'key';

var es = require('elasticsearch').Client({
    hosts: 'example-domain.us-east-1.es.amazonaws.com',
    connectionClass: require('http-aws-es'),
    amazonES: {
        region: 'us-east-1',
        accessKey: aws_access_key,
        secretKey: aws_secret_key
    }
});

es.ping({
    // ping usually has a 3000ms timeout
    requestTimeout: Infinity,

    // undocumented params are appended to the query string
    hello: "elasticsearch!"
}, function (error) {
    if (error) {
        console.log(error);
        console.trace('elasticsearch cluster is down!');
    } else {
        console.log('All is well');
    }
});

目前返回授权错误:

{ [Error: Authorization Exception]
  status: 403,
  displayName: 'AuthorizationException',
  message: 'Authorization Exception' }

我还没有看到通过在 node.js 中使用签名策略来使用 aws ES 实例的工作示例。有人有见解吗?

【问题讨论】:

    标签: javascript node.js amazon-web-services elasticsearch amazon-elasticsearch


    【解决方案1】:

    事实证明,我在我的问题中几乎所有事情都做对了,除了一个步骤。

    与上述代码中的aws_access_keyaws_secret_key 关联的IAM 用户必须具有与弹性搜索实例交互的特定权限。所以我登录到 AWS 控制台并向需要与 elasticsearch 实例交互的 IAM 用户添加以下策略

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "Stmt1480915344000",
                "Effect": "Allow",
                "Action": [
                    "es:*"
                ],
                "Resource": [
                    "arn:aws:es:us-east-1:<account-id>:domain/*"
                ]
            }
        ]
    }
    

    【讨论】:

    • 该解决方案有效。我尝试为我的搜索请求授予只读访问权限(es:Get*es:List*es:Describe*es:ESHttpHeades:ESHttpGet),但这还不够。它必须是完全访问权限 (es:*)。
    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2015-09-20
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多