连接到Elasticsearch - Amazon Elasticsearch服务 - IAM用户

时间:2015-12-04 10:52:52

标签: amazon-web-services elasticsearch amazon-elasticsearch

我已选择"允许访问一个或多个AWS账户或IAM用户"

我的访问政策

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::12345678910:user/elastic"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:eu-west-1:123456789:domain/elastic-cluster/*"
    }
  ]
}

我创建了一个IAM配置文件 -

user - elastic 
password -hisdfdsfds
Access key Id - sdsfdssdfdsfdsfdsfsdfsd
Secret Access Key - sdsfdsfdsfsdfdsfds

当我尝试连接时

$params = array();
$params['hosts'] = array (
    'search-elastic-cluster-sdfsdfsdfs.eu-east.es.amazonaws.com:80',                 
    );

$client = new Elasticsearch\Client($params);

它会抛出以下错误:

{"Message":"User: anonymous is not authorized to perform: es:ESHttpPost on resource: arn:aws:es:eu-west-1:dsfdsfsdfsdsd:domain/elastic-cluster/sdsfsfds/sdfdsfdssd/_search"}

我发现签名版本4签名请求可以访问它。我试过这样做,但不能。也许方法是错的。

如果有人建议在向elasticsearch域创建签名版本4请求时,我会很高兴。使用上述参数的示例将非常有用。提前谢谢。

1 个答案:

答案 0 :(得分:0)

应用程序需要将请求签名到Elasticsearch。适用于您选择的语言的AWS开发工具包应该有一个为签名请求创建凭据的方法。

当您使用凭据提供请求时,它应该没问题并且很好。

这是使用javascript sdk的代码段:



var AWS = require('aws-sdk');
var creds = new AWS.EnvironmentCredentials('AWS');

var esDomain = {
    region: 'us-east-1',
    endpoint: 'yoursearchdomain.region.amazonaws.com',
    index: 'myindex',
    doctype: 'mytype'
};

var endpoint = new AWS.Endpoint(esDomain.endpoint);

var req = new AWS.HttpRequest(endpoint);

    req.method = 'POST';
    req.path = path.join('/', esDomain.index, esDomain.doctype);
    req.region = esDomain.region;
    req.headers['presigned-expires'] = false;
    req.headers['Host'] = endpoint.host;
    req.headers['Content-Type'] = 'application/json';
    req.body = doc;

var signer = new AWS.Signers.V4(req , 'es'); 
    signer.addAuthorization(creds, new Date());
    
    var send = new AWS.NodeHttpClient();
    send.handleRequest(req, null, function(httpResp) {
        var respBody = '';
        httpResp.on('data', function (chunk) {
            respBody += chunk;
        });
        httpResp.on('end', function (chunk) {
            console.log('Response: ' + respBody);
            context.succeed('Lambda added document ' + doc);
        });
    }, function(err) {
        console.log('Error: ' + err);
        context.fail('Lambda failed with error ' + err);
    });