Skip to content

Commit

Permalink
Make authentication procedure and tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Jan 31, 2025
1 parent dbb3d54 commit a93020c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
11 changes: 8 additions & 3 deletions posttroll/backends/zmq/socket.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""ZMQ socket handling functions."""

from functools import cache
from urllib.parse import urlsplit, urlunsplit

import zmq
Expand Down Expand Up @@ -101,6 +102,12 @@ def bind(sock, destination, port_interval):
port_number = port
return port_number

@cache
def get_auth_thread(ctx):
"""Get the authenticator thread for the context."""
thr = ThreadAuthenticator(ctx)
thr.start()
return thr

def create_secure_server_socket(socket_type):
"""Create a secure server socket."""
Expand All @@ -109,10 +116,8 @@ def create_secure_server_socket(socket_type):
authorized_sub_addresses = config.get("authorized_client_addresses", [])

ctx = get_context()

# Start an authenticator for this context.
authenticator_thread = ThreadAuthenticator(ctx)
authenticator_thread.start()
authenticator_thread = get_auth_thread(ctx)
authenticator_thread.allow(*authorized_sub_addresses)
# Tell authenticator to use the certificate in a directory
authenticator_thread.configure_curve(domain="*", location=clients_public_keys_directory)
Expand Down
2 changes: 2 additions & 0 deletions posttroll/message_broadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def __init__(self, default_port, receivers):
if backend == "unsecure_zmq":
from posttroll.backends.zmq.message_broadcaster import ZMQDesignatedReceiversSender
self._sender = ZMQDesignatedReceiversSender(default_port, receivers)
else:
raise NotImplementedError()

Check warning on line 47 in posttroll/message_broadcaster.py

View check run for this annotation

Codecov / codecov/patch

posttroll/message_broadcaster.py#L47

Added line #L47 was not covered by tests

def __call__(self, data):
"""Send messages from all receivers."""
Expand Down
3 changes: 0 additions & 3 deletions posttroll/tests/test_nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ def create_nameserver_instance(max_age=3, multicast_enabled=True):
ns.stop()
thr.join()

def fake_nameserver():
config.set(nameserver_port=1111)
config.set(address_publish_port())

class TestAddressReceiver(unittest.TestCase):
"""Test the AddressReceiver."""
Expand Down
13 changes: 11 additions & 2 deletions posttroll/tests/test_secure_zmq_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import time
from threading import Thread

import pytest
import zmq.auth

import posttroll.backends.zmq
from posttroll import config
from posttroll.backends.zmq import generate_keys
from posttroll.message import Message
Expand All @@ -17,6 +19,15 @@
from posttroll.tests.test_nameserver import create_nameserver_instance


@pytest.fixture(autouse=True)
def new_context(monkeypatch):
"""Create a new context for each test."""
context = zmq.Context()
def get_context():
return context
monkeypatch.setattr(posttroll.backends.zmq, "get_context", get_context)


def create_keys(tmp_path):
"""Create keys."""
base_dir = tmp_path
Expand Down Expand Up @@ -111,8 +122,6 @@ def test_switch_to_secure_zmq_backend(tmp_path):

def test_ipc_pubsub_with_sec_and_factory_sub(tmp_path):
"""Test pub-sub on a secure ipc socket."""
# create_keys(tmp_path)

server_public_key_file, server_secret_key_file = zmq.auth.create_certificates(tmp_path, "server")
client_public_key_file, client_secret_key_file = zmq.auth.create_certificates(tmp_path, "client")

Expand Down

0 comments on commit a93020c

Please sign in to comment.