-
Notifications
You must be signed in to change notification settings - Fork 1
/
kafka_settings.py
101 lines (83 loc) · 3.19 KB
/
kafka_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
from enum import Enum
from typing import Callable
from aiokafka.helpers import create_ssl_context
from pydantic import BaseSettings, validator
base_env = os.path.join(os.path.dirname(os.path.dirname(__file__)), ".env")
env_file = os.getenv("KAFKA_ENV_FILE", base_env)
class SecurityProtocolEnum(str, Enum):
plaintext = "PLAINTEXT"
ssl = "SASL_SSL"
class SaslMechanismEnum(str, Enum):
plain = "PLAIN"
gssapi = "GSSAPI"
scram_sha_256 = "SCRAM-SHA-256"
scram_sha_512 = "SCRAM-SHA-512"
oauthbearer = "OAUTHBEARER"
class KafkaConsumerSettings(BaseSettings):
auto_commit_interval_ms: int = 5000
session_timeout_ms: int = 15000
max_poll_records: int = 5
max_poll_interval_ms: int = 600_000 # 10 minutes
heartbeat_interval_ms: int = 3000
auto_offset_reset: str = "earliest"
group_id: str = "SimpleAIOKafka"
key_deserializer: Callable = bytes.decode
value_deserializer: Callable = bytes.decode
class Config:
env_prefix = "kafka_consumer_"
env_file = env_file
class KafkaProducerSettings(BaseSettings):
compression_type: str = "gzip"
max_request_size: int = 5242880
key_serializer: Callable = str.encode
value_serializer: Callable = str.encode
class Config:
env_prefix = "kafka_producer_"
env_file = env_file
class KafkaSettings(BaseSettings):
consumer = KafkaConsumerSettings()
producer = KafkaProducerSettings()
log_level: str = "warning"
input_topic: str = "aiokafka.input"
output_topic: str = "aiokafka.output"
bootstrap_servers: str = "localhost:9092"
send_errors_to_dlq: bool = True
dlq_topic: str = f"error.{input_topic}.{consumer.group_id}"
# SSL Settings
security_protocol: SecurityProtocolEnum = SecurityProtocolEnum.plaintext
tls_dir: str = "/opt/"
ssl_cafile: str = os.path.join(tls_dir, "ca.crt")
ssl_certfile: str = os.path.join(tls_dir, "user.crt")
ssl_keyfile: str = os.path.join(tls_dir, "user.key")
# SASL Settings
sasl_plain_username: str = None
sasl_plain_password: str = None
sasl_mechanism: SaslMechanismEnum = None
class Config:
env_prefix = "kafka_"
env_file = env_file
use_enum_values = True
def get_ssl_context(self):
if self.security_protocol != "PLAINTEXT":
return create_ssl_context(
cafile=self.ssl_cafile,
certfile=self.ssl_certfile,
keyfile=self.ssl_keyfile,
)
return None
def get_connection_context(self):
return {
"ssl_context": self.get_ssl_context(),
"bootstrap_servers": self.bootstrap_servers,
"security_protocol": self.security_protocol,
"sasl_plain_username": self.sasl_plain_username,
"sasl_plain_password": self.sasl_plain_password,
"sasl_mechanism": self.sasl_mechanism,
}
# For using SASL without SSL certificates the *file args need to be None.
# Otherwise AIOKafkaClient will try to parse them even if they
# consist of an empty string.
@validator("ssl_cafile", "ssl_certfile", "ssl_keyfile")
def parse_to_none(cls, v):
return None if v in ["", "None", 0, False] else v