diff --git a/ospd_openvas/messaging/mqtt.py b/ospd_openvas/messaging/mqtt.py index 1ad27429..7e5245dc 100644 --- a/ospd_openvas/messaging/mqtt.py +++ b/ospd_openvas/messaging/mqtt.py @@ -5,7 +5,6 @@ import json import logging - from functools import partial from socket import gaierror, timeout from threading import Thread @@ -13,9 +12,9 @@ from typing import Callable, Type import paho.mqtt.client as mqtt +from paho.mqtt import __version__ as paho_mqtt_version from ..messages.message import Message - from .publisher import Publisher from .subscriber import Subscriber @@ -26,6 +25,10 @@ QOS_AT_LEAST_ONCE = 1 +def is_paho_mqtt_version_2() -> bool: + return paho_mqtt_version.startswith("2") + + class MQTTClient(mqtt.Client): def __init__( self, @@ -36,7 +39,20 @@ def __init__( self._mqtt_broker_address = mqtt_broker_address self._mqtt_broker_port = mqtt_broker_port - super().__init__(client_id=client_id, protocol=mqtt.MQTTv5) + mqtt_client_args = { + "client_id": client_id, + "protocol": mqtt.MQTTv5, + } + + if is_paho_mqtt_version_2(): + logger.debug("Using Paho MQTT version 2") + mqtt_client_args["callback_api_version"] = ( + mqtt.CallbackAPIVersion.VERSION1 + ) # pylint: disable=no-member + else: + logger.debug("Using Paho MQTT version 1") + + super().__init__(**mqtt_client_args) self.enable_logger() diff --git a/poetry.lock b/poetry.lock index 8fc5184f..1a5fa602 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1003,4 +1003,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "edaa085ec93bb8e1864dba7a256f77a39b122bc24d98f62e4189c19dad0b4158" +content-hash = "277f771b3e353414c8a816d6a1768757a7818a3aacfdd197eaa0edeb283d10ab" diff --git a/pyproject.toml b/pyproject.toml index 2ba2a48c..90d0df88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,8 +50,7 @@ packaging = ">=20.4,<25.0" lxml = ">=4.5.2,<6.0.0" defusedxml = ">=0.6,<0.8" deprecated = "^1.2.10" -# use v1.x due to https://github.com/eclipse/paho.mqtt.python/issues/814 -paho-mqtt = "^1.6" +paho-mqtt = ">=1.6,<3" python-gnupg = ">=0.4.8,<0.6.0" [tool.poetry.dev-dependencies]