Connecting to Elasticsearch - Amazon Elasticsearch service - IAM user

坚强是说给别人听的谎言 提交于 2020-04-14 08:08:56

问题


I have selected "Allow access to one or more AWS accounts or IAM users"

My access policy

{
  "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/*"
    }
  ]
}

I have created an IAM profile -

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

when I try to connect

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

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

It throws the following error:

{"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"}

I found it can be accessed by signed version 4 signature requests. I tried doing it, but could not . Maybe the way is wrong.

I would be happy if some one suggests ideas in creating signed version 4 request to elasticsearch domain. An example using parameters I stated above would be very helpful. Thanks in advance.


回答1:


The application needs to sign the requests going to Elasticsearch. The AWS SDK for your language of choice should have a method which creates the credentials for the sign request.

When you provide your requests with the credentials, it should be ok and good to go.

This is a code snippet using the 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);
    });


来源:https://stackoverflow.com/questions/34086661/connecting-to-elasticsearch-amazon-elasticsearch-service-iam-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!