forked from dpkp/kafka-python
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscram.py
68 lines (54 loc) · 2.32 KB
/
scram.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
import logging
import struct
import kafka.errors as Errors
from kafka.protocol.types import Int32
from kafka.scram import ScramClient
log = logging.getLogger()
def validate_config(conn):
assert conn.config['sasl_plain_username'] is not None, (
'sasl_plain_username required when sasl_mechanism=SCRAM-*'
)
assert conn.config['sasl_plain_password'] is not None, (
'sasl_plain_password required when sasl_mechanism=SCRAM-*'
)
def try_authenticate(conn, future):
if conn.config['security_protocol'] == 'SASL_PLAINTEXT':
log.warning('%s: Exchanging credentials in the clear', conn)
scram_client = ScramClient(
conn.config['sasl_plain_username'],
conn.config['sasl_plain_password'],
conn.config['sasl_mechanism'],
)
err = None
close = False
with conn._lock:
if not conn._can_send_recv():
err = Errors.NodeNotReadyError(str(conn))
close = False
else:
try:
client_first = scram_client.first_message().encode('utf-8')
size = Int32.encode(len(client_first))
conn._send_bytes_blocking(size + client_first)
(data_len,) = struct.unpack('>i', conn._recv_bytes_blocking(4))
server_first = conn._recv_bytes_blocking(data_len).decode('utf-8')
scram_client.process_server_first_message(server_first)
client_final = scram_client.final_message().encode('utf-8')
size = Int32.encode(len(client_final))
conn._send_bytes_blocking(size + client_final)
(data_len,) = struct.unpack('>i', conn._recv_bytes_blocking(4))
server_final = conn._recv_bytes_blocking(data_len).decode('utf-8')
scram_client.process_server_final_message(server_final)
except (ConnectionError, TimeoutError) as e:
log.exception("%s: Error receiving reply from server", conn)
err = Errors.KafkaConnectionError(f"{conn}: {e}")
close = True
if err is not None:
if close:
conn.close(error=err)
return future.failure(err)
log.info(
'%s: Authenticated as %s via %s',
conn, conn.config['sasl_plain_username'], conn.config['sasl_mechanism']
)
return future.success(True)