Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config param to pass additional parameters to confluent-kafka-python #1505

Merged
merged 10 commits into from
Jun 11, 2024
5 changes: 1 addition & 4 deletions docs/docs_src/confluent/security/basic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import ssl

from faststream.confluent import KafkaBroker
from faststream.security import BaseSecurity

ssl_context = ssl.create_default_context()
security = BaseSecurity(ssl_context=ssl_context)
security = BaseSecurity(use_ssl=True)

broker = KafkaBroker("localhost:9092", security=security)
5 changes: 1 addition & 4 deletions docs/docs_src/confluent/security/plaintext.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import ssl

from faststream.confluent import KafkaBroker
from faststream.security import SASLPlaintext

ssl_context = ssl.create_default_context()
security = SASLPlaintext(
ssl_context=ssl_context,
username="admin",
password="password", # pragma: allowlist secret
use_ssl=True,
)

broker = KafkaBroker("localhost:9092", security=security)
5 changes: 1 addition & 4 deletions docs/docs_src/confluent/security/sasl_scram256.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import ssl

from faststream.confluent import KafkaBroker
from faststream.security import SASLScram256

ssl_context = ssl.create_default_context()
security = SASLScram256(
ssl_context=ssl_context,
username="admin",
password="password", # pragma: allowlist secret
use_ssl=True,
)

broker = KafkaBroker("localhost:9092", security=security)
5 changes: 1 addition & 4 deletions docs/docs_src/confluent/security/sasl_scram512.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import ssl

from faststream.confluent import KafkaBroker
from faststream.security import SASLScram512

ssl_context = ssl.create_default_context()
security = SASLScram512(
ssl_context=ssl_context,
username="admin",
password="password", # pragma: allowlist secret
use_ssl=True,
)

broker = KafkaBroker("localhost:9092", security=security)
6 changes: 5 additions & 1 deletion faststream/confluent/security.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ssl
import warnings
from typing import TYPE_CHECKING, Optional

Expand All @@ -14,6 +15,9 @@


def parse_security(security: Optional[BaseSecurity]) -> "AnyDict":
if security and isinstance(security.ssl_context, ssl.SSLContext):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, we should use SetupError instead of basic ValueError in wrong broker parameters case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. Thanks.

raise ValueError("ssl_context in not supported by confluent-kafka-python, please use config instead.")

if security is None:
return {}
elif type(security) == BaseSecurity:
Expand All @@ -35,7 +39,7 @@ def _parse_base_security(security: BaseSecurity) -> "AnyDict":


def _parse_sasl_plaintext(security: SASLPlaintext) -> "AnyDict":
if security.ssl_context is None:
if not security.use_ssl:
warnings.warn(
message=ssl_not_set_error_msg,
category=RuntimeWarning,
Expand Down
26 changes: 21 additions & 5 deletions tests/brokers/confluent/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,30 @@ async def test_base_security():
producer_call_kwargs = producer.call_args.kwargs

call_kwargs = {}
call_kwargs["security_protocol"] = "SSL"

assert call_kwargs.items() <= producer_call_kwargs.items()

assert (
producer_call_kwargs["security_protocol"]
== call_kwargs["security_protocol"]
)

@pytest.mark.asyncio()
@pytest.mark.confluent()
async def test_base_security_pass_ssl_context():
import ssl

from faststream.confluent import KafkaBroker
from faststream.security import BaseSecurity

ssl_context = ssl.create_default_context()
security = BaseSecurity(ssl_context=ssl_context)

basic_broker = KafkaBroker("localhost:9092", security=security)


with patch_aio_consumer_and_producer() as producer:
with pytest.raises(ValueError) as e:
async with basic_broker:
pass

assert str(e.value) == "ssl_context in not supported by confluent-kafka-python, please use config instead."


@pytest.mark.asyncio()
Expand Down