Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions faust/transport/drivers/aiokafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,15 @@ def credentials_to_aiokafka_auth(
"ssl_context": credentials.ssl_context,
}
elif isinstance(credentials, SASLCredentials):
if credentials.mechanism.value == "PLAIN" and (
credentials.username is None or credentials.password is None
):
raise ImproperlyConfigured(
"SASL/PLAIN requires both username and password in "
"broker_credentials=SASLCredentials(...). "
"Check the credentials configured on the App that owns "
"the topic before creating its producer or consumer."
)
return {
"security_protocol": credentials.protocol.value,
"sasl_mechanism": credentials.mechanism.value,
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_aiokafka_sasl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

from faust import SASLCredentials
from faust.exceptions import ImproperlyConfigured
from faust.transport.drivers.aiokafka import credentials_to_aiokafka_auth


def test_plain_credentials_are_forwarded_to_aiokafka() -> None:
settings = credentials_to_aiokafka_auth(
SASLCredentials(
username="test-user",
password="test-password",
mechanism="PLAIN",
)
)

assert settings == {
"security_protocol": "SASL_PLAINTEXT",
"sasl_mechanism": "PLAIN",
"sasl_plain_username": "test-user",
"sasl_plain_password": "test-password",
"ssl_context": None,
}


@pytest.mark.parametrize(
"username,password",
[(None, None), (None, "test-password"), ("test-user", None)],
)
def test_plain_credentials_require_username_and_password(
username: str | None, password: str | None
) -> None:
credentials = SASLCredentials(
username=username,
password=password,
mechanism="PLAIN",
)

with pytest.raises(ImproperlyConfigured, match="broker_credentials") as exc:
credentials_to_aiokafka_auth(credentials)

assert "test-user" not in str(exc.value)
assert "test-password" not in str(exc.value)
Loading