Problems with simple python Elastic connection. Documentation Wrong?

I spent about an hour on what seems like a simple getting started exercise. Just connecting to the elastic stack with the python client. Leaving out all of the trial and error detail. The documentation shows that the api_key parameter should be a tuple consisting of the api_key_id and the api_key. However, the only way I was able to get this to work was to not include the api_key_id.
This does not work due to authentication error:

es_client = Elasticsearch(es_url, ssl_assert_fingerprint=es_ca_fingerprint, api_key=(es_api_key_id, es_api_key), verify_certs=True) 

This works:

es_client = Elasticsearch(es_url, ssl_assert_fingerprint=es_ca_fingerprint, api_key=es_api_key, verify_certs=True)

Here is a link to the documentation that the api_key_id must be used:

What is the correct way to connect?

Can you share the error you got?

It accepts both a tuple with the id and the secret and a string with the encoded id and secret.

How did you created the API Key? Using the Create API endoint or using the Create API UI in Kibana?

When you use the endpoint you get something like this as the response:

{
  "id": "vDtLa4wB9Bp7LZL4mBx9",        
  "name": "my-api-key",
  "expiration": 1544068612110,         
  "api_key": "Ky9gr42jSFC7sTuaS3gMNw", 
  "encoded": "dkR0TGE0d0I5QnA3TFpMNG1CeDk6S3k5Z3I0MmpTRkM3c1R1YVMzZ01Odw=="  
}

And if you use the Create API Kibana UI, you will get the encoded value only and need to manually select to see the id:key pair by choosing Beats or Logstash in the format drop down menu.

But it should work no matter if you are using the encoded value or the id:key pair.

On the example above you could use:

api_key=('vDtLa4wB9Bp7LZL4mBx9', 'Ky9gr42jSFC7sTuaS3gMNw')

or

api_key='dkR0TGE0d0I5QnA3TFpMNG1CeDk6S3k5Z3I0MmpTRkM3c1R1YVMzZ01Odw=='
1 Like

Ok. Thank you for the clarification. I created the API key via the UI and then queried the api_key via curl (using the encoded value which worked) to get the api_key_id. When I set this up in python I was using the api_key_id for the api_key_id but using the encoded value (combination of the api_key_id and the api_key) as the api_key.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.