Skip to content

Commit

Permalink
Optionally use generated api_key auth to ES
Browse files Browse the repository at this point in the history
This commit adds a optional client arg, use_api_key, which will generate
and use an api key for every client and async client created by
Rally. It uses an initial call via the existing auth scheme to ES to
generate the key, and then creates a new client using the api_key for
general use by Rally.

The async client also does this, and temporarily creates a sync client
to generate the key, and then uses it when creating an async client.

Closes elastic#1067
  • Loading branch information
hub-cap committed Oct 22, 2020
1 parent 33a7a70 commit 61db7d8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion esrally/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,17 @@ def _is_set(self, client_opts, k):

def create(self):
import elasticsearch
return elasticsearch.Elasticsearch(hosts=self.hosts, ssl_context=self.ssl_context, **self.client_options)
instance = elasticsearch.Elasticsearch(hosts=self.hosts, ssl_context=self.ssl_context, **self.client_options)
if self._is_set(self.client_options, "use_api_key"):
# an api_key is generated per client, which happens after an initial handshake using an existing auth scheme
# once this key is generated, a new client is created using the api_key client option and http_auth is removed
# the reason the options were not removed in place was because of the logic to coalesce the key tuple into a
# header that the client could use.
api_key_response = instance.security.create_api_key({"name": "rally-api-key"})
self.client_options.pop("http_auth")
self.client_options["api_key"] = (api_key_response["id"], api_key_response["api_key"])
instance = elasticsearch.Elasticsearch(hosts=self.hosts, ssl_context=self.ssl_context, **self.client_options)
return instance

def create_async(self):
import elasticsearch
Expand All @@ -139,6 +149,10 @@ def create_async(self):

from elasticsearch.serializer import JSONSerializer

if self._is_set(self.client_options, "use_api_key"):
# Temporarily create a non async version of the client to generate an api_key and put it into client_options
self.create()

class LazyJSONSerializer(JSONSerializer):
def loads(self, s):
meta = RallyAsyncElasticsearch.request_context.get()
Expand Down

0 comments on commit 61db7d8

Please sign in to comment.