diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bb7c86b..b718c84 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,8 +11,10 @@ jobs: - '3.9' - '3.10' - '3.11' - - 'pypy3.9' + - '3.12' + - '3.13' - 'pypy3.10' + - 'pypy3.11' steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 diff --git a/.pylintrc b/.pylintrc index 929417e..b75c6a3 100644 --- a/.pylintrc +++ b/.pylintrc @@ -13,7 +13,7 @@ indent-after-paren=4 expected-line-ending-format=LF [MESSAGES CONTROL] -disable=C0111,too-many-arguments,too-many-instance-attributes,deprecated-method +disable=C0111,too-many-arguments,too-many-instance-attributes,too-many-positional-arguments [REPORTS] -reports=no \ No newline at end of file +reports=no diff --git a/pygelf/handlers.py b/pygelf/handlers.py index a42d07b..d4f1062 100644 --- a/pygelf/handlers.py +++ b/pygelf/handlers.py @@ -116,22 +116,26 @@ def __init__(self, validate=False, ca_certs=None, certfile=None, keyfile=None, * GelfTcpHandler.__init__(self, **kwargs) - self.ca_certs = ca_certs - self.reqs = ssl.CERT_REQUIRED if validate else ssl.CERT_NONE - self.certfile = certfile - self.keyfile = keyfile if keyfile else certfile + self.ctx = ssl.create_default_context() - def makeSocket(self, timeout=1): - plain_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if not validate: + self.ctx.check_hostname = False + self.ctx.verify_mode = ssl.CERT_NONE + + if ca_certs: + self.ctx.load_verify_locations(cafile=ca_certs) - if hasattr(plain_socket, 'settimeout'): - plain_socket.settimeout(timeout) + if certfile: + self.ctx.load_cert_chain(certfile, keyfile) + + def makeSocket(self, timeout=1): + plain = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + plain.settimeout(timeout) - wrapped_socket = ssl.wrap_socket(plain_socket, ca_certs=self.ca_certs, cert_reqs=self.reqs, - keyfile=self.keyfile, certfile=self.certfile) - wrapped_socket.connect((self.host, self.port)) + wrapped = self.ctx.wrap_socket(plain, server_hostname=self.host) + wrapped.connect((self.host, self.port)) - return wrapped_socket + return wrapped class GelfHttpHandler(BaseHandler, LoggingHandler): diff --git a/setup.py b/setup.py index ccedcb7..d58332d 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pygelf', - version='0.4.2', + version='0.4.3', packages=['pygelf'], description='Logging handlers with GELF support', keywords='logging udp tcp ssl tls graylog2 graylog gelf', @@ -20,6 +20,8 @@ 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', 'Programming Language :: Python :: 3.11', + 'Programming Language :: Python :: 3.12', + 'Programming Language :: Python :: 3.13', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: System :: Logging', diff --git a/tests/config/docker-compose.yml b/tests/config/docker-compose.yml index 986d14b..6999c4a 100644 --- a/tests/config/docker-compose.yml +++ b/tests/config/docker-compose.yml @@ -15,8 +15,8 @@ services: - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 - GRAYLOG_HTTP_EXTERNAL_URI=http://127.0.0.1:9000/ volumes: - - ./cert.pem:/usr/share/graylog/data/cert.pem - - ./key.pem:/usr/share/graylog/data/key.pem + - ./cert.pem:/usr/share/graylog/data/cert.pem:ro + - ./key.pem:/usr/share/graylog/data/key.pem:ro links: - mongo - elasticsearch diff --git a/tests/config/graylog-setup.sh b/tests/config/graylog-setup.sh index 1b5debb..fd58807 100755 --- a/tests/config/graylog-setup.sh +++ b/tests/config/graylog-setup.sh @@ -58,7 +58,7 @@ HTTPS_INPUT='{ "tls_key_file": "/usr/share/graylog/data/key.pem" }, "type": "org.graylog2.inputs.gelf.http.GELFHttpInput", - "global": true + "global": true }' curl -u admin:admin "$API_URL/search/universal/relative?query=test&range=5&fields=message" > /dev/null @@ -76,9 +76,4 @@ sleep 10 for _ in {1..5}; do curl -X "POST" -H "Content-Type: application/json" "http://localhost:12203/gelf" -p0 -d '{"short_message": "warm-up", "host": "localhost"}' sleep 1 - curl -k -X "POST" -H "Content-Type: application/json" "https://localhost:12205/gelf" -p0 -d '{"short_message": "warm-up", "host": "localhost"}' - sleep 1 done - -docker exec -u 0 $(docker ps |grep graylog | awk '{print $1}') chown graylog data/key.pem -docker exec -u 0 $(docker ps |grep graylog | awk '{print $1}') chmod 0600 data/key.pem \ No newline at end of file diff --git a/tests/test_common_fields.py b/tests/test_common_fields.py index e2ead81..f794e62 100644 --- a/tests/test_common_fields.py +++ b/tests/test_common_fields.py @@ -10,15 +10,15 @@ @pytest.fixture(params=[ - GelfTcpHandler(host='127.0.0.1', port=12201), - GelfUdpHandler(host='127.0.0.1', port=12202), - GelfUdpHandler(host='127.0.0.1', port=12202, compress=False), - GelfHttpHandler(host='127.0.0.1', port=12203), - GelfHttpHandler(host='127.0.0.1', port=12203, compress=False), - GelfTlsHandler(host='127.0.0.1', port=12204), - GelfHttpsHandler(host='127.0.0.1', port=12205, validate=False), + GelfTcpHandler(host='localhost', port=12201), + GelfUdpHandler(host='localhost', port=12202), + GelfUdpHandler(host='localhost', port=12202, compress=False), + GelfHttpHandler(host='localhost', port=12203), + GelfHttpHandler(host='localhost', port=12203, compress=False), + GelfTlsHandler(host='localhost', port=12204), + GelfHttpsHandler(host='localhost', port=12205, validate=False), GelfHttpsHandler(host='localhost', port=12205, validate=True, ca_certs='tests/config/cert.pem'), - GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem'), + GelfTlsHandler(host='localhost', port=12204, validate=True, ca_certs='tests/config/cert.pem'), ]) def handler(request): return request.param diff --git a/tests/test_debug_mode.py b/tests/test_debug_mode.py index 466d2f0..18d730a 100644 --- a/tests/test_debug_mode.py +++ b/tests/test_debug_mode.py @@ -4,13 +4,13 @@ @pytest.fixture(params=[ - GelfTcpHandler(host='127.0.0.1', port=12201, debug=True), - GelfUdpHandler(host='127.0.0.1', port=12202, debug=True), - GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, debug=True), - GelfHttpHandler(host='127.0.0.1', port=12203, debug=True), - GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, debug=True), - GelfTlsHandler(host='127.0.0.1', port=12204, debug=True), - GelfTlsHandler(host='127.0.0.1', port=12204, debug=True, validate=True, ca_certs='tests/config/cert.pem'), + GelfTcpHandler(host='localhost', port=12201, debug=True), + GelfUdpHandler(host='localhost', port=12202, debug=True), + GelfUdpHandler(host='localhost', port=12202, compress=False, debug=True), + GelfHttpHandler(host='localhost', port=12203, debug=True), + GelfHttpHandler(host='localhost', port=12203, compress=False, debug=True), + GelfTlsHandler(host='localhost', port=12204, debug=True), + GelfTlsHandler(host='localhost', port=12204, debug=True, validate=True, ca_certs='tests/config/cert.pem'), GelfHttpsHandler(host='localhost', port=12205, debug=True, validate=True, ca_certs='tests/config/cert.pem') ]) diff --git a/tests/test_dynamic_fields.py b/tests/test_dynamic_fields.py index 7d9393d..909ef7c 100644 --- a/tests/test_dynamic_fields.py +++ b/tests/test_dynamic_fields.py @@ -13,14 +13,14 @@ def filter(self, record): @pytest.fixture(params=[ - GelfTcpHandler(host='127.0.0.1', port=12201, include_extra_fields=True), - GelfUdpHandler(host='127.0.0.1', port=12202, include_extra_fields=True), - GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, include_extra_fields=True), - GelfHttpHandler(host='127.0.0.1', port=12203, include_extra_fields=True), - GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, include_extra_fields=True), - GelfTlsHandler(host='127.0.0.1', port=12204, include_extra_fields=True), - GelfHttpsHandler(host='127.0.0.1', port=12205, validate=False, include_extra_fields=True), - GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem', include_extra_fields=True), + GelfTcpHandler(host='localhost', port=12201, include_extra_fields=True), + GelfUdpHandler(host='localhost', port=12202, include_extra_fields=True), + GelfUdpHandler(host='localhost', port=12202, compress=False, include_extra_fields=True), + GelfHttpHandler(host='localhost', port=12203, include_extra_fields=True), + GelfHttpHandler(host='localhost', port=12203, compress=False, include_extra_fields=True), + GelfTlsHandler(host='localhost', port=12204, include_extra_fields=True), + GelfHttpsHandler(host='localhost', port=12205, validate=False, include_extra_fields=True), + GelfTlsHandler(host='localhost', port=12204, validate=True, ca_certs='tests/config/cert.pem', include_extra_fields=True), ]) def handler(request): return request.param diff --git a/tests/test_handler_specific.py b/tests/test_handler_specific.py index 1c89f34..601c225 100644 --- a/tests/test_handler_specific.py +++ b/tests/test_handler_specific.py @@ -4,12 +4,12 @@ def test_tls_handler_init(): with pytest.raises(ValueError): - GelfTlsHandler(host='127.0.0.1', port=12204, validate=True) + GelfTlsHandler(host='localhost', port=12204, validate=True) with pytest.raises(ValueError): - GelfTlsHandler(host='127.0.0.1', port=12204, keyfile='/dev/null') + GelfTlsHandler(host='localhost', port=12204, keyfile='/dev/null') def test_https_handler_init(): with pytest.raises(ValueError): - GelfHttpsHandler(host='127.0.0.1', port=12205, validate=True) + GelfHttpsHandler(host='localhost', port=12205, validate=True) diff --git a/tests/test_queuehandler_support.py b/tests/test_queuehandler_support.py index e3a409f..7e3e011 100644 --- a/tests/test_queuehandler_support.py +++ b/tests/test_queuehandler_support.py @@ -6,14 +6,14 @@ @pytest.fixture(params=[ - GelfTcpHandler(host='127.0.0.1', port=12201), - GelfUdpHandler(host='127.0.0.1', port=12202), - GelfUdpHandler(host='127.0.0.1', port=12202, compress=False), - GelfHttpHandler(host='127.0.0.1', port=12203), - GelfHttpHandler(host='127.0.0.1', port=12203, compress=False), - GelfTlsHandler(host='127.0.0.1', port=12204), - GelfHttpsHandler(host='127.0.0.1', port=12205, validate=False), - GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem'), + GelfTcpHandler(host='localhost', port=12201), + GelfUdpHandler(host='localhost', port=12202), + GelfUdpHandler(host='localhost', port=12202, compress=False), + GelfHttpHandler(host='localhost', port=12203), + GelfHttpHandler(host='localhost', port=12203, compress=False), + GelfTlsHandler(host='localhost', port=12204), + GelfHttpsHandler(host='localhost', port=12205, validate=False), + GelfTlsHandler(host='localhost', port=12204, validate=True, ca_certs='tests/config/cert.pem'), GelfHttpsHandler(host='localhost', port=12205, validate=True, ca_certs='tests/config/cert.pem'), ]) def handler(request): diff --git a/tests/test_static_fields.py b/tests/test_static_fields.py index fb4eb08..17b747f 100644 --- a/tests/test_static_fields.py +++ b/tests/test_static_fields.py @@ -11,21 +11,21 @@ @pytest.fixture(params=[ - GelfTcpHandler(host='127.0.0.1', port=12201, **STATIC_FIELDS), - GelfUdpHandler(host='127.0.0.1', port=12202, **STATIC_FIELDS), - GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, **STATIC_FIELDS), - GelfHttpHandler(host='127.0.0.1', port=12203, **STATIC_FIELDS), - GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, **STATIC_FIELDS), - GelfTlsHandler(host='127.0.0.1', port=12204, **STATIC_FIELDS), - GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem', **STATIC_FIELDS), - GelfTcpHandler(host='127.0.0.1', port=12201, static_fields=STATIC_FIELDS, _ozzy='billie jean'), - GelfUdpHandler(host='127.0.0.1', port=12202, static_fields=STATIC_FIELDS, _ozzy='billie jean'), - GelfUdpHandler(host='127.0.0.1', port=12202, compress=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'), - GelfHttpHandler(host='127.0.0.1', port=12203, static_fields=STATIC_FIELDS, _ozzy='billie jean'), - GelfHttpHandler(host='127.0.0.1', port=12203, compress=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'), - GelfTlsHandler(host='127.0.0.1', port=12204, static_fields=STATIC_FIELDS), - GelfHttpsHandler(host='127.0.0.1', port=12205, validate=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'), - GelfTlsHandler(host='127.0.0.1', port=12204, validate=True, ca_certs='tests/config/cert.pem', static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfTcpHandler(host='localhost', port=12201, **STATIC_FIELDS), + GelfUdpHandler(host='localhost', port=12202, **STATIC_FIELDS), + GelfUdpHandler(host='localhost', port=12202, compress=False, **STATIC_FIELDS), + GelfHttpHandler(host='localhost', port=12203, **STATIC_FIELDS), + GelfHttpHandler(host='localhost', port=12203, compress=False, **STATIC_FIELDS), + GelfTlsHandler(host='localhost', port=12204, **STATIC_FIELDS), + GelfTlsHandler(host='localhost', port=12204, validate=True, ca_certs='tests/config/cert.pem', **STATIC_FIELDS), + GelfTcpHandler(host='localhost', port=12201, static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfUdpHandler(host='localhost', port=12202, static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfUdpHandler(host='localhost', port=12202, compress=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfHttpHandler(host='localhost', port=12203, static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfHttpHandler(host='localhost', port=12203, compress=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfTlsHandler(host='localhost', port=12204, static_fields=STATIC_FIELDS), + GelfHttpsHandler(host='localhost', port=12205, validate=False, static_fields=STATIC_FIELDS, _ozzy='billie jean'), + GelfTlsHandler(host='localhost', port=12204, validate=True, ca_certs='tests/config/cert.pem', static_fields=STATIC_FIELDS, _ozzy='billie jean'), ]) def handler(request): return request.param