From 13572781ed1c9f970f20fb68c5883dec2844c035 Mon Sep 17 00:00:00 2001 From: DrunkBatya Date: Mon, 25 Sep 2023 09:48:29 -0400 Subject: [PATCH 1/4] Add basic auth support, add http code check --- pygelf/handlers.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pygelf/handlers.py b/pygelf/handlers.py index fefc681..e2f5406 100644 --- a/pygelf/handlers.py +++ b/pygelf/handlers.py @@ -8,8 +8,12 @@ from logging.handlers import SocketHandler, DatagramHandler from logging import Handler as LoggingHandler +from base64 import b64encode from pygelf import gelf +def get_basic_auth_header(username, password): + token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii") + return f'Basic {token}' class BaseHandler(object): def __init__(self, debug=False, version='1.1', include_extra_fields=False, compress=False, @@ -135,7 +139,7 @@ def makeSocket(self, timeout=1): class GelfHttpHandler(BaseHandler, LoggingHandler): - def __init__(self, host, port, compress=True, path='/gelf', timeout=5, **kwargs): + def __init__(self, host, port, username=None, password=None, compress=True, path='/gelf', timeout=5, **kwargs): """ Logging handler that transforms each record into GELF (graylog extended log format) and sends it over HTTP. @@ -159,15 +163,21 @@ def __init__(self, host, port, compress=True, path='/gelf', timeout=5, **kwargs) if compress: self.headers['Content-Encoding'] = 'gzip,deflate' + if username and password: + self.headers['Authorization'] = get_basic_auth_header(username, password) + def emit(self, record): data = self.convert_record_to_gelf(record) connection = httplib.HTTPConnection(host=self.host, port=self.port, timeout=self.timeout) connection.request('POST', self.path, data, self.headers) + res = connection.getresponse() + if res.status != httplib.ACCEPTED: + raise Exception(f'GELF reply is: {res.status} {res.reason}') class GelfHttpsHandler(BaseHandler, LoggingHandler): - def __init__(self, host, port, compress=True, path='/gelf', timeout=5, validate=False, ca_certs=None, certfile=None, keyfile=None, keyfile_password=None, **kwargs): + def __init__(self, host, port, username=None, password=None, compress=True, path='/gelf', timeout=5, validate=False, ca_certs=None, certfile=None, keyfile=None, keyfile_password=None, **kwargs): """ Logging handler that transforms each record into GELF (graylog extended log format) and sends it over HTTP. @@ -211,11 +221,16 @@ def __init__(self, host, port, compress=True, path='/gelf', timeout=5, validate= # Load our CA file self.ctx.load_verify_locations(cafile=self.ca_certs) - if compress: self.headers['Content-Encoding'] = 'gzip,deflate' + if username and password: + self.headers['Authorization'] = get_basic_auth_header(username, password) + def emit(self, record): data = self.convert_record_to_gelf(record) connection = httplib.HTTPSConnection(host=self.host, port=self.port, context=self.ctx, timeout=self.timeout) connection.request('POST', self.path, data, self.headers) + res = connection.getresponse() + if res.status != httplib.ACCEPTED: + raise Exception(f'GELF reply is: {res.status} {res.reason}') From 84dba29cac6ebc1ce14af62739838117f296fdf8 Mon Sep 17 00:00:00 2001 From: DrunkBatya Date: Mon, 11 Dec 2023 16:55:00 +0300 Subject: [PATCH 2/4] Remove exceptions --- pygelf/handlers.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pygelf/handlers.py b/pygelf/handlers.py index e2f5406..b1bf57f 100644 --- a/pygelf/handlers.py +++ b/pygelf/handlers.py @@ -170,9 +170,6 @@ def emit(self, record): data = self.convert_record_to_gelf(record) connection = httplib.HTTPConnection(host=self.host, port=self.port, timeout=self.timeout) connection.request('POST', self.path, data, self.headers) - res = connection.getresponse() - if res.status != httplib.ACCEPTED: - raise Exception(f'GELF reply is: {res.status} {res.reason}') class GelfHttpsHandler(BaseHandler, LoggingHandler): @@ -231,6 +228,3 @@ def emit(self, record): data = self.convert_record_to_gelf(record) connection = httplib.HTTPSConnection(host=self.host, port=self.port, context=self.ctx, timeout=self.timeout) connection.request('POST', self.path, data, self.headers) - res = connection.getresponse() - if res.status != httplib.ACCEPTED: - raise Exception(f'GELF reply is: {res.status} {res.reason}') From d469d2b885749648f3f1579a8e5ace1ed99f2f25 Mon Sep 17 00:00:00 2001 From: DrunkBatya Date: Mon, 11 Dec 2023 19:45:46 +0300 Subject: [PATCH 3/4] Add error checking --- pygelf/handlers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pygelf/handlers.py b/pygelf/handlers.py index b1bf57f..ac19e21 100644 --- a/pygelf/handlers.py +++ b/pygelf/handlers.py @@ -170,6 +170,9 @@ def emit(self, record): data = self.convert_record_to_gelf(record) connection = httplib.HTTPConnection(host=self.host, port=self.port, timeout=self.timeout) connection.request('POST', self.path, data, self.headers) + res = connection.getresponse() + if res.status != httplib.ACCEPTED: + self.handleError(record) class GelfHttpsHandler(BaseHandler, LoggingHandler): @@ -228,3 +231,6 @@ def emit(self, record): data = self.convert_record_to_gelf(record) connection = httplib.HTTPSConnection(host=self.host, port=self.port, context=self.ctx, timeout=self.timeout) connection.request('POST', self.path, data, self.headers) + res = connection.getresponse() + if res.status != httplib.ACCEPTED: + self.handleError(record) From ea6f49335424eb972eaf29cb29b642a0b1a002e9 Mon Sep 17 00:00:00 2001 From: DrunkBatya Date: Mon, 11 Dec 2023 20:03:56 +0300 Subject: [PATCH 4/4] Add try/catch, assert --- pygelf/handlers.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pygelf/handlers.py b/pygelf/handlers.py index ac19e21..8556639 100644 --- a/pygelf/handlers.py +++ b/pygelf/handlers.py @@ -167,11 +167,13 @@ def __init__(self, host, port, username=None, password=None, compress=True, path self.headers['Authorization'] = get_basic_auth_header(username, password) def emit(self, record): - data = self.convert_record_to_gelf(record) - connection = httplib.HTTPConnection(host=self.host, port=self.port, timeout=self.timeout) - connection.request('POST', self.path, data, self.headers) - res = connection.getresponse() - if res.status != httplib.ACCEPTED: + try: + data = self.convert_record_to_gelf(record) + connection = httplib.HTTPConnection(host=self.host, port=self.port, timeout=self.timeout) + connection.request('POST', self.path, data, self.headers) + res = connection.getresponse() + assert res.status == httplib.ACCEPTED + except: self.handleError(record) @@ -228,9 +230,11 @@ def __init__(self, host, port, username=None, password=None, compress=True, path self.headers['Authorization'] = get_basic_auth_header(username, password) def emit(self, record): - data = self.convert_record_to_gelf(record) - connection = httplib.HTTPSConnection(host=self.host, port=self.port, context=self.ctx, timeout=self.timeout) - connection.request('POST', self.path, data, self.headers) - res = connection.getresponse() - if res.status != httplib.ACCEPTED: + try: + data = self.convert_record_to_gelf(record) + connection = httplib.HTTPSConnection(host=self.host, port=self.port, context=self.ctx, timeout=self.timeout) + connection.request('POST', self.path, data, self.headers) + res = connection.getresponse() + assert res.status == httplib.ACCEPTED + except: self.handleError(record)