Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User authentication in Elasticsearch query using python

I'm using elastisearch using Python. My code looks somewhat like this:-

               from elasticsearch import Elasticsearch  if __name__ == '__main__':      index="IndexPosition"     es=Elasticsearch(['https://localhost:8080'])     res = es.search(index='{0}'.format(index), doc_type="log",size=1000, from_=0, body={ "query": {     "match": {                ...Match condition       }     }   }}) 

Now, due to changes in architecture user authentication has been added in the elasticsearch.Let's assume username-user and password-pass.How do I pass the username and password in the query..?

like image 910
Mayank Jha Avatar asked Dec 13 '16 11:12

Mayank Jha


People also ask

Is Elasticsearch Python thread safe?

The client is thread safe and can be used in a multi threaded environment. Best practice is to create a single global instance of the client and use it throughout your application. If your application is long-running consider turning on Sniffing to make sure the client is up to date on the cluster location.

How do I get Elasticsearch connection string?

Creating the Connection const client = new elasticsearch. Client({ hosts: [ // Compose connection strings "https://username:password@portal113-2.latest-elasticsearch.compose-3.composedb.com:10113/", "https://username:password@portal164-1.latest-elasticsearch.compose-3.composedb.com:10113/"] });


2 Answers

You need to pass the username and password to the Elasticsearch object as shown below:

es = Elasticsearch(['http://localhost:8080'], http_auth=('user', 'pass'))

like image 68
Girish kumar Avatar answered Sep 20 '22 07:09

Girish kumar


You can pass username, password in url:

ex:

username: elastic

password: changeme

es = Elasticsearch(hosts="http://elastic:changeme@localhost:9200/") 

using CURL

curl -X GET "elastic:changeme@localhost:9200/" 
like image 40
faisal burhanudin Avatar answered Sep 21 '22 07:09

faisal burhanudin