Skip to content

Commit fef86c8

Browse files
committed
[kafka] feat: Making SASL variables optional, as not every Kafka setup may have them
1 parent 4fb8006 commit fef86c8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

sources/kafka/helpers.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List
1+
from typing import Any, Dict, List, Optional
22

33
from confluent_kafka import Consumer, Message, TopicPartition # type: ignore
44
from confluent_kafka.admin import TopicMetadata # type: ignore
@@ -8,7 +8,7 @@
88
from dlt.common.configuration import configspec
99
from dlt.common.configuration.specs import CredentialsConfiguration
1010
from dlt.common.time import ensure_pendulum_datetime
11-
from dlt.common.typing import DictStrAny, TSecretValue, TAnyDateTime
11+
from dlt.common.typing import DictStrAny, TSecretValue
1212
from dlt.common.utils import digest128
1313

1414

@@ -218,9 +218,11 @@ class KafkaCredentials(CredentialsConfiguration):
218218
bootstrap_servers: str = config.value
219219
group_id: str = config.value
220220
security_protocol: str = config.value
221-
sasl_mechanisms: str = config.value
222-
sasl_username: str = config.value
223-
sasl_password: TSecretValue = secrets.value
221+
222+
# Optional SASL credentials
223+
sasl_mechanisms: Optional[str] = config.value
224+
sasl_username: Optional[str] = config.value
225+
sasl_password: Optional[TSecretValue] = secrets.value
224226

225227
def init_consumer(self) -> Consumer:
226228
"""Init a Kafka consumer from this credentials.
@@ -232,9 +234,16 @@ def init_consumer(self) -> Consumer:
232234
"bootstrap.servers": self.bootstrap_servers,
233235
"group.id": self.group_id,
234236
"security.protocol": self.security_protocol,
235-
"sasl.mechanisms": self.sasl_mechanisms,
236-
"sasl.username": self.sasl_username,
237-
"sasl.password": self.sasl_password,
238237
"auto.offset.reset": "earliest",
239238
}
239+
240+
if self.sasl_mechanisms and self.sasl_username and self.sasl_password:
241+
config.update(
242+
{
243+
"sasl.mechanisms": self.sasl_mechanisms,
244+
"sasl.username": self.sasl_username,
245+
"sasl.password": self.sasl_password,
246+
}
247+
)
248+
240249
return Consumer(config)

0 commit comments

Comments
 (0)