Skip to content

Commit

Permalink
Change: Support paho-mqtt version 1 and 2
Browse files Browse the repository at this point in the history
For GOS we want to keep compatibility to paho-mqtt 1 but Kali for
example already has switched to version 2. Therefore support both
versions.
  • Loading branch information
bjoernricks committed Apr 19, 2024
1 parent 45c5543 commit 6147405
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
23 changes: 20 additions & 3 deletions ospd_openvas/messaging/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

import json
import logging

from functools import partial
from socket import gaierror, timeout
from threading import Thread
from time import sleep
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

Expand All @@ -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,
Expand All @@ -36,7 +39,21 @@ 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")
# pylint: disable=no-member
mqtt_client_args["callback_api_version"] = (
mqtt.CallbackAPIVersion.VERSION1
)
else:
logger.debug("Using Paho MQTT version 1")

super().__init__(**mqtt_client_args)

self.enable_logger()

Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 6147405

Please sign in to comment.