Unable to set auth header using javascript client elasticsearch.js


(SK) #1

Hi,

I am using elasticsearch-angular.js client to connect from my UI built using angularjs to connect to elasticserach 1.4. Our es instance has a custom plugin with Base authentication to intercept every request to check if the incoming request has authorization header set. I am setting the auth param as described in the docs when instantiating the esFactory. However the auth headers are not getting set. Can you please let me know how to set auth headers from java script client for every request

var myServices = angular.module('myServices', ['elasticsearch']);

myServices.service('es', ['esFactory',
function (esFactory, $http) {
return esFactory({
host: 'localhost:9200',
auth: 'user:password'
});
}]);


(SK) #2

I have enabled log:trace param and i see the below error in chrome console

XMLHttpRequest cannot load http://localhost:9200/_cluster/state/cluster_name%2Cnodes%2Cmaster_node%2Cversion. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

I have set the cors in ES yml file as below

http.cors:
enabled: true
allow-origin: /https?://localhost(:[0-9]+)?/


(system) #3