Skip to content

Commit

Permalink
New configuration parameter: api_key - used in x-api-key header for r…
Browse files Browse the repository at this point in the history
…equests to Elasticsearch if set
  • Loading branch information
vetler authored and Vetle Leinonen-Roeim committed Jun 6, 2019
1 parent 631447b commit e93be0e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions curator/defaults/client_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def config_client():
Optional('timeout', default=30): All(
Coerce(int), Range(min=1, max=86400)),
Optional('master_only', default=False): Boolean(),
Optional('api_key', default=None): Any(None, *string_types),
}

# Configuration file: logging
Expand Down
4 changes: 4 additions & 0 deletions curator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ def get_client(**kwargs):
:arg skip_version_test: If `True`, skip the version check as part of the
client connection.
:rtype: :class:`elasticsearch.Elasticsearch`
:arg api_key: value to be used in optional X-Api-key header when accessing Elasticsearch
:type api_key: str
"""
if 'url_prefix' in kwargs:
if (
Expand Down Expand Up @@ -894,6 +896,8 @@ def get_client(**kwargs):
)
try:
client = elasticsearch.Elasticsearch(**kwargs)
if 'api_key' in kwargs and kwargs['api_key'] is not None:
client.transport.connection_pool.connection.headers.update({'x-api-key': kwargs['api_key']})
if skip_version_test:
logger.warn(
'Skipping Elasticsearch version verification. This is '
Expand Down
10 changes: 10 additions & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
Changelog
=========

5.7.7 (? ? ?)
------------------

**New**

* New client configuration option: api_key - used in the X-Api-key header in
requests to Elasticsearch when set, which may be required if ReadonlyREST
plugin is configured to require api-key. Requested in #1409 (vetler)


5.7.6 (6 May 2019)
------------------

Expand Down
10 changes: 10 additions & 0 deletions test/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ def test_certificate_no_verify_logic(self):
curator.get_client, **kwargs
)

def test_api_key_not_set(self):
kwargs = { 'api_key': None }
self.assertIsNotNone(curator.get_client(**kwargs))

def test_api_key_set(self):
kwargs = { 'api_key': 'some-api-key' }
client = curator.get_client(**kwargs)
self.assertEqual('some-api-key', client.transport.connection_pool.connection.headers.get('x-api-key'))


class TestShowDryRun(TestCase):
# For now, since it's a pain to capture logging output, this is just a
# simple code coverage run
Expand Down

0 comments on commit e93be0e

Please sign in to comment.