Skip to content

Instantly share code, notes, and snippets.

@crunchie84
Last active August 11, 2022 19:55
Show Gist options
  • Save crunchie84/c870f724328e848aee9e to your computer and use it in GitHub Desktop.
Save crunchie84/c870f724328e848aee9e to your computer and use it in GitHub Desktop.
Basic HTTP-auth ElasticSearch with Nest client
namespace Elasticsearch.Net.Connection
{
using System.Globalization;
using System;
using System.Text;
/// <summary>
/// HttpConnection with basic auth usage
/// </summary>
public sealed class BasicAuthHttpConnection : HttpConnection
{
private readonly string password;
private readonly string userName;
/// <summary>
/// constructor with username + password for the basic auth
/// </summary>
/// <param name="settings"></param>
/// <param name="username"></param>
/// <param name="password"></param>
public BasicAuthHttpConnection(IConnectionConfigurationValues settings, string username, string password)
: base(settings)
{
this.userName = username;
this.password = password;
}
protected override System.Net.HttpWebRequest CreateWebRequest(Uri uri, string method, byte[] data, IRequestConnectionConfiguration requestSpecificConfig)
{
var request = base.CreateWebRequest(uri, method, data, requestSpecificConfig);
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", userName, password)));
return request;
}
protected override System.Net.HttpWebRequest CreateHttpWebRequest(Uri uri, string method, byte[] data, IRequestConnectionConfiguration requestSpecificConfig)
{
var request = base.CreateHttpWebRequest(uri, method, data, requestSpecificConfig);
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", userName, password)));
return request;
}
}
}

We will use the basic-auth plugin for ElasticSearch by Asquera.

cd /usr/share/elasticsearch/plugins
mkdir http-basic
cd http-basic
wget "https://github.com/Asquera/elasticsearch-http-basic/releases/download/1.2.0/elasticsearch-http-basic-1.2.0.jar"
  • configure username + password in your elasticsearch.yml
http.basic.enabled: true
http.basic.user: "my-user"
http.basic.password: "my-secret-password"
  • reboot instance, rejoice!
var elasticSearchConnectionSettings = new ConnectionSettings("http://my-elasticsearch.mydomain.com:9200")
.SetDefaultIndex(CloudConfigurationManager.GetSetting("my-indexname"));
var basicAuthHttpConnection = new BasicAuthHttpConnection(elasticSearchConnectionSettings, "username", "password");
var elasticClient = new ElasticClient(elasticSearchConnectionSettings, basicAuthHttpConnection);
// checking network connecion with .RootNodeInfo() no longer works (basic-auth plugin modifies the / result response), try something like .NodesInfo()
var nodes = elasticClient.NodesInfo();
if (!nodes.ConnectionStatus.Success)
{
log.Fatal("Could not connect to ElasticSearch!", nodes.ConnectionStatus.OriginalException);
throw nodes.ConnectionStatus.OriginalException;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment