From cfb971024124085da37bd53f3d303e33d4e3701e Mon Sep 17 00:00:00 2001 From: Tim McCormack Date: Wed, 24 Aug 2022 09:32:20 -0400 Subject: [PATCH] fix: Properly load auth settings for producer/consumer (#27) Common auth settings were ignored since 0.3.1. --- CHANGELOG.rst | 8 ++++ edx_event_bus_kafka/__init__.py | 2 +- edx_event_bus_kafka/config.py | 6 ++- edx_event_bus_kafka/tests/test_config.py | 51 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 edx_event_bus_kafka/tests/test_config.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d4478ae..0c6e884 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,14 @@ Unreleased * +[0.4.2] - 2022-08-24 +******************** + +Fixed +===== + +* Properly load auth settings for producer/consumer. (Auth settings were ignored since 0.3.1.) + [0.4.1] - 2022-08-18 ******************** diff --git a/edx_event_bus_kafka/__init__.py b/edx_event_bus_kafka/__init__.py index 6710f7c..5cd477b 100644 --- a/edx_event_bus_kafka/__init__.py +++ b/edx_event_bus_kafka/__init__.py @@ -2,4 +2,4 @@ Kafka implementation for Open edX event bus. """ -__version__ = '0.4.1' +__version__ = '0.4.2' diff --git a/edx_event_bus_kafka/config.py b/edx_event_bus_kafka/config.py index 1d8ecb9..aa1654d 100644 --- a/edx_event_bus_kafka/config.py +++ b/edx_event_bus_kafka/config.py @@ -45,6 +45,8 @@ def create_schema_registry_client(): def load_common_settings() -> Optional[dict]: """ Load common settings, a base for either producer or consumer configuration. + + Warns and returns None if essential settings are missing. """ bootstrap_servers = getattr(settings, 'EVENT_BUS_KAFKA_BOOTSTRAP_SERVERS', None) if bootstrap_servers is None: @@ -55,8 +57,8 @@ def load_common_settings() -> Optional[dict]: 'bootstrap.servers': bootstrap_servers, } - key = getattr(base_settings, 'EVENT_BUS_KAFKA_API_KEY', None) - secret = getattr(base_settings, 'EVENT_BUS_KAFKA_API_SECRET', None) + key = getattr(settings, 'EVENT_BUS_KAFKA_API_KEY', None) + secret = getattr(settings, 'EVENT_BUS_KAFKA_API_SECRET', None) if key and secret: base_settings.update({ diff --git a/edx_event_bus_kafka/tests/test_config.py b/edx_event_bus_kafka/tests/test_config.py new file mode 100644 index 0000000..80e684a --- /dev/null +++ b/edx_event_bus_kafka/tests/test_config.py @@ -0,0 +1,51 @@ +""" +Test common configuration loading. +""" + +from unittest import TestCase + +from django.test.utils import override_settings + +from edx_event_bus_kafka import config + +# See https://github.com/openedx/event-bus-kafka/blob/main/docs/decisions/0005-optional-import-of-confluent-kafka.rst +try: + from confluent_kafka.schema_registry import SchemaRegistryClient +except ImportError: # pragma: no cover + pass + + +class TestSchemaRegistryClient(TestCase): + def test_unconfigured(self): + assert config.create_schema_registry_client() is None + + def test_configured(self): + with override_settings(EVENT_BUS_KAFKA_SCHEMA_REGISTRY_URL='http://localhost:12345'): + assert isinstance(config.create_schema_registry_client(), SchemaRegistryClient) + + +class TestCommonSettings(TestCase): + def test_unconfigured(self): + assert config.load_common_settings() is None + + def test_minimal(self): + with override_settings( + EVENT_BUS_KAFKA_BOOTSTRAP_SERVERS='http://localhost:54321', + ): + assert config.load_common_settings() == { + 'bootstrap.servers': 'http://localhost:54321', + } + + def test_full(self): + with override_settings( + EVENT_BUS_KAFKA_BOOTSTRAP_SERVERS='http://localhost:54321', + EVENT_BUS_KAFKA_API_KEY='some_other_key', + EVENT_BUS_KAFKA_API_SECRET='some_other_secret', + ): + assert config.load_common_settings() == { + 'bootstrap.servers': 'http://localhost:54321', + 'sasl.mechanism': 'PLAIN', + 'security.protocol': 'SASL_SSL', + 'sasl.username': 'some_other_key', + 'sasl.password': 'some_other_secret', + }