Elastic Python API Authentication

I have installed elasticsearch Python library into my PyCharm IDE and using sample code found at link below:

https://elasticsearch-py.readthedocs.io/en/master/ (see Example Usage)

I am getting the following error and not using SSL:

elasticsearch.exceptions.AuthenticationException: TransportError(401, 'security_exception', 'action [indices:data/write/index] requires authentication')

Authentication in this context is not covered on the above web page as near as I can tell.

If anyone has either a solution to this issue or sample code that does work it would be greatly appreciated. I am most interested in simple capabilities right now such as index creation, uploading / indexing many files from a local machine directory, running searches, etc.

One more note if that my cluster is on Elastic Cloud (not sure if this is a separate issue). Thank you for any guidance!

Authentication in this context is not covered on the above web page as near as I can tell.

What about the SSL and Authentication section in aforementioned document?

Thank you for the thought, Magnus. I think that is used if using SSL but is not required if not using SSL. I am just trying to get basic functions working before adding more like SSL. It appears the sample code I alluded to is incomplete in some form. My hope is to find another source for sample code. Thank you!

The example I referred you to happens to cover SSL and HTTP authentication at the same time, but the authentication pieces apply regardless of SSL as they are orthogonal. In other words, this is what you're looking for:

elasticsearch.Elasticsearch(['hostname'], http_auth=('username', 'password'))
4 Likes

I am sorry that I misunderstood your prior input. That is good to know and I will test this later today. Thank you!

1 Like

Well, I tried every variation of my username ('pstarconsult' and, separately, the email I use when I sign into Elastic Cloud) and password and I still get the same error. Just to remove any ambiguity, here is how I am calling the function (with personal information removed):

es = Elasticsearch(["http://[info-omitted].us-east-1.aws.found.io:9200"], httpauth=('username', 'password'))

Then I create the doc and call the next function (as in the example given in the link we are referring to):

doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}

res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])

The error occurs during the above function call. It makes sense that the error would occur here but I cannot figure out why I cannot authenticate!!??

Appreciate any further input to get me to the next level. Thank you!

Paul Starrett, CEO, Starrett Consulting, Inc.

www.starrettconsultinginc.comhttp://www.starrettconsultinginc.com
Licensed Private Investigator and Attorney
Certified Fraud Examiner
EnCase Certified Computer Forensics Examiner (EnCE)
Master of Science in Predictive Analytics (Northwestern U.)

Magnus, I am attaching a screenshot of my security settings and you will see that I have created a user ('pstarrett'), who has admin rights and admins are allowed to write indices (see red arrows showing all three things). So, I do not understand why I am getting the authentication error. There must be something wrong with my cluster or something - in other words, the problem is on the ES side, not my code or other configuration issue. Appreciate any help as this is a show stopper otherwise. Thank you!

What exception is the index() call raising? It's not clear whether it's an authentication or an authorization issue.

I copied the line of error code in my previous post (I would paste it here but I'm not near my computer). Interestingly if I remove the httpauth entirely (but leaving the endpoint as the sole argument in the function call) I get the same error. The credentials don't seem to matter.