从node.js创建对AWS ES实例的有效签名请求

时间:2016-12-05 05:01:22

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

我试图找到如何连接到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实例的工作示例。有人有见解吗?

0 个答案:

没有答案