JamesMatson
JamesMatson

Reputation: 2942

ElasticSearch.NET passing AWS Signature v4 in POST requests?

I am very new to ElasticSearch, and have set up an AWS Lambda function in c# to take the content of S3 object(s) (which contain JSON data) with the hopes of posting them to ES to be searchable.

I'm using the Elasticsearch.Net nuget library.

In the documentation here - https://github.com/elastic/elasticsearch-net there is samples of configuring the node URI etc, but my understanding is that any requests to ES need to be signed with AWS Signature V4 (based on Access/Secret key). I have created an IAM user for this purpose, but nowhere in the documentation can I find how to sign the POST requests. The samples show post methods, but no place to include the signature?

E.g.

var person = new Person
{
    FirstName = "Martijn",
    LastName = "Laarman"
};

var indexResponse = lowlevelClient.Index<BytesResponse>("people", "person", "1", PostData.Serializable(person)); 
byte[] responseBytes = indexResponse.Body;

var asyncIndexResponse = await lowlevelClient.IndexAsync<StringResponse>("people", "person", "1", PostData.Serializable(person)); 
string responseString = asyncIndexResponse.Body;

Even when instantiating the connection, nowhere is there a place to add your credentials?

var settings = new ConnectionConfiguration(new Uri("http://example.com:9200"))
    .RequestTimeout(TimeSpan.FromMinutes(2));

var lowlevelClient = new ElasticLowLevelClient(settings);

I have checked ConnectionConfiguration object but there's no methods or properties that seem related. What have I missed?

Upvotes: 4

Views: 3173

Answers (2)

Brandon Cuff
Brandon Cuff

Reputation: 1478

Using the elasticsearch-net-aws package you should be able to set up the low level client like so:


var httpConnection = new AwsHttpConnection("us-east-1"); // or whatever region you're using
var pool = new SingleNodeConnectionPool(new Uri(TestConfig.Endpoint));
var config = new ConnectionConfiguration(pool, httpConnection);
config.DisableDirectStreaming();
var client = new ElasticLowLevelClient(config);
// ...

Upvotes: 3

JamesMatson
JamesMatson

Reputation: 2942

// if using an access key
var httpConnection = new AwsHttpConnection("us-east-1", new StaticCredentialsProvider(new AwsCredentials
{
    AccessKey = "My AWS access key",
    SecretKey = "My AWS secret key",
}));

// if using app.config, environment variables, or roles
var httpConnection = new AwsHttpConnection("us-east-1");

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var config = new ConnectionSettings(pool, httpConnection);
var client = new ElasticClient(config);

Upvotes: 0

Related Questions