Skip to content

Commit fe62916

Browse files
committed
Adapt code to elasticsearch 8.x version
Elasticsearch introduced braking change by: - removing RequestsHttpConnection - removing use_ssl from Elasticsearch class SSL is not set as 'scheme' string (http/https) in host dict. Custom authorization need to set node_class to requests.
1 parent 27ee809 commit fe62916

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

Diff for: README.rst

+9-8
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ This library requires the following dependencies
4242

4343
Additional requirements for Kerberos support
4444
============================================
45-
Additionally, the package support optionally kerberos authentication by adding the following dependecy
45+
Additionally, the package support optionally kerberos authentication by adding the following dependency
4646
- requests-kerberos
4747

4848
Additional requirements for AWS IAM user authentication (request signing)
4949
=========================================================================
50-
Additionally, the package support optionally AWS IAM user authentication by adding the following dependecy
50+
Additionally, the package support optionally AWS IAM user authentication by adding the following dependency
5151
- requests-aws4auth
5252

5353
Using the handler in your program
5454
==================================
5555
To initialise and create the handler, just add the handler to your logger as follow ::
5656

5757
from cmreslogging.handlers import CMRESHandler
58-
handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200}],
58+
handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200, 'scheme': 'http'}],
5959
auth_type=CMRESHandler.AuthType.NO_AUTH,
6060
es_index_name="my_python_index")
6161
log = logging.getLogger("PythonTest")
@@ -65,7 +65,7 @@ To initialise and create the handler, just add the handler to your logger as fol
6565
You can add fields upon initialisation, providing more data of the execution context ::
6666

6767
from cmreslogging.handlers import CMRESHandler
68-
handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200}],
68+
handler = CMRESHandler(hosts=[{'host': 'localhost', 'port': 9200, 'scheme': 'http'}],
6969
auth_type=CMRESHandler.AuthType.NO_AUTH,
7070
es_index_name="my_python_index",
7171
es_additional_fields={'App': 'MyAppName', 'Environment': 'Dev'})
@@ -95,17 +95,18 @@ Kibana on top of elasticsearch
9595
Initialisation parameters
9696
=========================
9797
The constructors takes the following parameters:
98-
- hosts: The list of hosts that elasticsearch clients will connect, multiple hosts are allowed, for example ::
98+
- hosts: The list of hosts that elasticsearch clients will connect, multiple hosts are allowed.
99+
Use ```'scheme'``` to determinate if use SSL (`use_ssl` is deprecated). To use SSL set ```'scheme': 'https'```, or if you don't need SSL Sset ```'scheme': 'http'```.
100+
for example::
99101

100-
[{'host':'host1','port':9200}, {'host':'host2','port':9200}]
102+
[{'host':'host1','port':9200, 'scheme': 'https'}, {'host':'host2','port':9200, 'scheme': 'http'}]
101103

102104

103105
- auth_type: The authentication currently support CMRESHandler.AuthType = NO_AUTH, BASIC_AUTH, KERBEROS_AUTH
104106
- auth_details: When CMRESHandler.AuthType.BASIC_AUTH is used this argument must contain a tuple of string with the user and password that will be used to authenticate against the Elasticsearch servers, for example ('User','Password')
105107
- aws_access_key: When ``CMRESHandler.AuthType.AWS_SIGNED_AUTH`` is used this argument must contain the AWS key id of the the AWS IAM user
106108
- aws_secret_key: When ``CMRESHandler.AuthType.AWS_SIGNED_AUTH`` is used this argument must contain the AWS secret key of the the AWS IAM user
107109
- aws_region: When ``CMRESHandler.AuthType.AWS_SIGNED_AUTH`` is used this argument must contain the AWS region of the the AWS Elasticsearch servers, for example ``'us-east'``
108-
- use_ssl: A boolean that defines if the communications should use SSL encrypted communication
109110
- verify_ssl: A boolean that defines if the SSL certificates are validated or not
110111
- buffer_size: An int, Once this size is reached on the internal buffer results are flushed into ES
111112
- flush_frequency_in_sec: A float representing how often and when the buffer will be flushed
@@ -139,7 +140,7 @@ they can be plotted on Kibana, or the SQL statements that Django executed. ::
139140
'elasticsearch': {
140141
'level': 'DEBUG',
141142
'class': 'cmreslogging.handlers.CMRESHandler',
142-
'hosts': [{'host': 'localhost', 'port': 9200}],
143+
'hosts': [{'host': 'localhost', 'port': 9200, 'scheme': 'http'}],
143144
'es_index_name': 'my_python_app',
144145
'es_additional_fields': {'App': 'Test', 'Environment': 'Dev'},
145146
'auth_type': CMRESHandler.AuthType.NO_AUTH,

Diff for: cmreslogging/handlers.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from threading import Timer, Lock
88
from enum import Enum
99
from elasticsearch import helpers as eshelpers
10-
from elasticsearch import Elasticsearch, RequestsHttpConnection
10+
from elasticsearch import Elasticsearch
1111

1212
try:
1313
from requests_kerberos import HTTPKerberosAuth, DISABLED
@@ -58,13 +58,12 @@ class IndexNameFrequency(Enum):
5858
YEARLY = 3
5959

6060
# Defaults for the class
61-
__DEFAULT_ELASTICSEARCH_HOST = [{'host': 'localhost', 'port': 9200}]
61+
__DEFAULT_ELASTICSEARCH_HOST = [{'host': 'localhost', 'port': 9200, 'scheme': 'http'}]
6262
__DEFAULT_AUTH_USER = ''
6363
__DEFAULT_AUTH_PASSWD = ''
6464
__DEFAULT_AWS_ACCESS_KEY = ''
6565
__DEFAULT_AWS_SECRET_KEY = ''
6666
__DEFAULT_AWS_REGION = ''
67-
__DEFAULT_USE_SSL = False
6867
__DEFAULT_VERIFY_SSL = True
6968
__DEFAULT_AUTH_TYPE = AuthType.NO_AUTH
7069
__DEFAULT_INDEX_FREQUENCY = IndexNameFrequency.DAILY
@@ -129,7 +128,6 @@ def __init__(self,
129128
aws_secret_key=__DEFAULT_AWS_SECRET_KEY,
130129
aws_region=__DEFAULT_AWS_REGION,
131130
auth_type=__DEFAULT_AUTH_TYPE,
132-
use_ssl=__DEFAULT_USE_SSL,
133131
verify_ssl=__DEFAULT_VERIFY_SSL,
134132
buffer_size=__DEFAULT_BUFFER_SIZE,
135133
flush_frequency_in_sec=__DEFAULT_FLUSH_FREQ_INSEC,
@@ -142,11 +140,13 @@ def __init__(self,
142140
""" Handler constructor
143141
144142
:param hosts: The list of hosts that elasticsearch clients will connect. The list can be provided
145-
in the format ```[{'host':'host1','port':9200}, {'host':'host2','port':9200}]``` to
146-
make sure the client supports failover of one of the instertion nodes
147-
:param auth_details: When ```CMRESHandler.AuthType.BASIC_AUTH``` is used this argument must contain
148-
a tuple of string with the user and password that will be used to authenticate against
149-
the Elasticsearch servers, for example```('User','Password')
143+
in the format ```[{'host':'host1','port':9200, 'scheme': 'http'},
144+
{'host':'host2','port':9200, 'scheme': 'https'}]``` to make sure the client supports
145+
failover of one of the insertion nodes
146+
:param auth_details: When ```CMRESHandler.AuthType.BASIC_AUTH``` or ```CMRESHandler.AuthType.NTLM_AUTH```
147+
is used this argument must contain a tuple of string with the user and password
148+
that will be used to authenticate against the Elasticsearch servers,
149+
for example```('User','Password')
150150
:param aws_access_key: When ```CMRESHandler.AuthType.AWS_SIGNED_AUTH``` is used this argument must contain
151151
the AWS key id of the the AWS IAM user
152152
:param aws_secret_key: When ```CMRESHandler.AuthType.AWS_SIGNED_AUTH``` is used this argument must contain
@@ -155,7 +155,6 @@ def __init__(self,
155155
the AWS region of the the AWS Elasticsearch servers, for example```'us-east'
156156
:param auth_type: The authentication type to be used in the connection ```CMRESHandler.AuthType```
157157
Currently, NO_AUTH, BASIC_AUTH, KERBEROS_AUTH are supported
158-
:param use_ssl: A boolean that defines if the communications should use SSL encrypted communication
159158
:param verify_ssl: A boolean that defines if the SSL certificates are validated or not
160159
:param buffer_size: An int, Once this size is reached on the internal buffer results are flushed into ES
161160
:param flush_frequency_in_sec: A float representing how often and when the buffer will be flushed, even
@@ -182,7 +181,6 @@ def __init__(self,
182181
self.aws_secret_key = aws_secret_key
183182
self.aws_region = aws_region
184183
self.auth_type = auth_type
185-
self.use_ssl = use_ssl
186184
self.verify_certs = verify_ssl
187185
self.buffer_size = buffer_size
188186
self.flush_frequency_in_sec = flush_frequency_in_sec
@@ -212,19 +210,15 @@ def __get_es_client(self):
212210
if self.auth_type == CMRESHandler.AuthType.NO_AUTH:
213211
if self._client is None:
214212
self._client = Elasticsearch(hosts=self.hosts,
215-
use_ssl=self.use_ssl,
216213
verify_certs=self.verify_certs,
217-
connection_class=RequestsHttpConnection,
218214
serializer=self.serializer)
219215
return self._client
220216

221217
if self.auth_type == CMRESHandler.AuthType.BASIC_AUTH:
222218
if self._client is None:
223219
return Elasticsearch(hosts=self.hosts,
224220
http_auth=self.auth_details,
225-
use_ssl=self.use_ssl,
226221
verify_certs=self.verify_certs,
227-
connection_class=RequestsHttpConnection,
228222
serializer=self.serializer)
229223
return self._client
230224

@@ -233,11 +227,10 @@ def __get_es_client(self):
233227
raise EnvironmentError("Kerberos module not available. Please install \"requests-kerberos\"")
234228
# For kerberos we return a new client each time to make sure the tokens are up to date
235229
return Elasticsearch(hosts=self.hosts,
236-
use_ssl=self.use_ssl,
237230
verify_certs=self.verify_certs,
238-
connection_class=RequestsHttpConnection,
239231
http_auth=HTTPKerberosAuth(mutual_authentication=DISABLED),
240-
serializer=self.serializer)
232+
serializer=self.serializer,
233+
node_class='requests')
241234

242235
if self.auth_type == CMRESHandler.AuthType.AWS_SIGNED_AUTH:
243236
if not AWS4AUTH_SUPPORTED:
@@ -247,9 +240,7 @@ def __get_es_client(self):
247240
self._client = Elasticsearch(
248241
hosts=self.hosts,
249242
http_auth=awsauth,
250-
use_ssl=self.use_ssl,
251243
verify_certs=True,
252-
connection_class=RequestsHttpConnection,
253244
serializer=self.serializer
254245
)
255246
return self._client

Diff for: tests/test_cmreshandler.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@
1010
class CMRESHandlerTestCase(unittest.TestCase):
1111
DEFAULT_ES_SERVER = 'localhost'
1212
DEFAULT_ES_PORT = 9200
13+
DEFAULT_ES_SSL_SCHEME = 'http'
1314

1415
def getESHost(self):
15-
return os.getenv('TEST_ES_SERVER',CMRESHandlerTestCase.DEFAULT_ES_SERVER)
16+
return os.getenv('TEST_ES_SERVER', CMRESHandlerTestCase.DEFAULT_ES_SERVER)
1617

1718
def getESPort(self):
1819
try:
19-
return int(os.getenv('TEST_ES_PORT',CMRESHandlerTestCase.DEFAULT_ES_PORT))
20+
return int(os.getenv('TEST_ES_PORT', CMRESHandlerTestCase.DEFAULT_ES_PORT))
2021
except ValueError:
2122
return CMRESHandlerTestCase.DEFAULT_ES_PORT
2223

24+
@staticmethod
25+
def get_ES_scheme():
26+
return os.getenv('TEST_ES_SSL_SCHEME', CMRESHandlerTestCase.DEFAULT_ES_SSL_SCHEME)
27+
2328
def setUp(self):
2429
self.log = logging.getLogger("MyTestCase")
2530
test_handler = logging.StreamHandler(stream=sys.stderr)
@@ -29,10 +34,11 @@ def tearDown(self):
2934
del self.log
3035

3136
def test_ping(self):
32-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
3337
auth_type=CMRESHandler.AuthType.NO_AUTH,
38+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
39+
'port': self.getESPort(),
40+
'scheme': self.get_ES_scheme()}],
3441
es_index_name="pythontest",
35-
use_ssl=False,
3642
raise_on_indexing_exceptions=True)
3743
es_test_server_is_up = handler.test_es_source()
3844
self.assertEqual(True, es_test_server_is_up)
@@ -41,6 +47,9 @@ def test_buffered_log_insertion_flushed_when_buffer_full(self):
4147
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
4248
auth_type=CMRESHandler.AuthType.NO_AUTH,
4349
use_ssl=False,
50+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
51+
'port': self.getESPort(),
52+
'scheme': self.get_ES_scheme()}],
4453
buffer_size=2,
4554
flush_frequency_in_sec=1000,
4655
es_index_name="pythontest",
@@ -61,9 +70,9 @@ def test_buffered_log_insertion_flushed_when_buffer_full(self):
6170

6271
def test_es_log_extra_argument_insertion(self):
6372
self.log.info("About to test elasticsearch insertion")
64-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
6573
auth_type=CMRESHandler.AuthType.NO_AUTH,
66-
use_ssl=False,
74+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
75+
'port': self.getESPort(),
6776
es_index_name="pythontest",
6877
es_additional_fields={'App': 'Test', 'Environment': 'Dev'},
6978
raise_on_indexing_exceptions=True)
@@ -84,9 +93,10 @@ def test_es_log_extra_argument_insertion(self):
8493
self.assertEqual(0, len(handler._buffer))
8594

8695
def test_buffered_log_insertion_after_interval_expired(self):
87-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
8896
auth_type=CMRESHandler.AuthType.NO_AUTH,
89-
use_ssl=False,
97+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
98+
'port': self.getESPort(),
99+
'scheme': self.get_ES_scheme()}],
90100
flush_frequency_in_sec=0.1,
91101
es_index_name="pythontest",
92102
es_additional_fields={'App': 'Test', 'Environment': 'Dev'},
@@ -108,9 +118,10 @@ def test_buffered_log_insertion_after_interval_expired(self):
108118
self.assertEqual(0, len(handler._buffer))
109119

110120
def test_fast_insertion_of_hundred_logs(self):
111-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
112121
auth_type=CMRESHandler.AuthType.NO_AUTH,
113-
use_ssl=False,
122+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
123+
'port': self.getESPort(),
124+
'scheme': self.get_ES_scheme()}],
114125
buffer_size=500,
115126
flush_frequency_in_sec=0.5,
116127
es_index_name="pythontest",
@@ -125,43 +136,47 @@ def test_fast_insertion_of_hundred_logs(self):
125136

126137
def test_index_name_frequency_functions(self):
127138
index_name = "pythontest"
128-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
129139
auth_type=CMRESHandler.AuthType.NO_AUTH,
140+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
141+
'port': self.getESPort(),
142+
'scheme': self.get_ES_scheme()}],
130143
es_index_name=index_name,
131-
use_ssl=False,
132144
index_name_frequency=CMRESHandler.IndexNameFrequency.DAILY,
133145
raise_on_indexing_exceptions=True)
134146
self.assertEqual(
135147
handler._index_name_func.__func__(index_name),
136148
CMRESHandler._get_daily_index_name(index_name)
137149
)
138150

139-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
140151
auth_type=CMRESHandler.AuthType.NO_AUTH,
152+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
153+
'port': self.getESPort(),
154+
'scheme': self.get_ES_scheme()}],
141155
es_index_name=index_name,
142-
use_ssl=False,
143156
index_name_frequency=CMRESHandler.IndexNameFrequency.WEEKLY,
144157
raise_on_indexing_exceptions=True)
145158
self.assertEqual(
146159
handler._index_name_func.__func__(index_name),
147160
CMRESHandler._get_weekly_index_name(index_name)
148161
)
149162

150-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
151163
auth_type=CMRESHandler.AuthType.NO_AUTH,
164+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
165+
'port': self.getESPort(),
166+
'scheme': self.get_ES_scheme()}],
152167
es_index_name=index_name,
153-
use_ssl=False,
154168
index_name_frequency=CMRESHandler.IndexNameFrequency.MONTHLY,
155169
raise_on_indexing_exceptions=True)
156170
self.assertEqual(
157171
handler._index_name_func.__func__(index_name),
158172
CMRESHandler._get_monthly_index_name(index_name)
159173
)
160174

161-
handler = CMRESHandler(hosts=[{'host': self.getESHost(), 'port': self.getESPort()}],
162175
auth_type=CMRESHandler.AuthType.NO_AUTH,
176+
handler = CMRESHandler(hosts=[{'host': self.getESHost(),
177+
'port': self.getESPort(),
178+
'scheme': self.get_ES_scheme()}],
163179
es_index_name=index_name,
164-
use_ssl=False,
165180
index_name_frequency=CMRESHandler.IndexNameFrequency.YEARLY,
166181
raise_on_indexing_exceptions=True)
167182
self.assertEqual(

0 commit comments

Comments
 (0)