4
4
import hashlib
5
5
import hmac
6
6
import json
7
+ import logging
7
8
import string
8
9
9
10
# needed for AWS_MSK_IAM authentication:
13
14
# no botocore available, will disable AWS_MSK_IAM mechanism
14
15
BotoSession = None
15
16
17
+ from kafka .errors import KafkaConfigurationError
16
18
from kafka .sasl .abc import SaslMechanism
17
19
from kafka .vendor .six .moves import urllib
18
20
19
21
22
+ log = logging .getLogger (__name__ )
23
+
24
+
20
25
class SaslMechanismAwsMskIam (SaslMechanism ):
21
26
def __init__ (self , ** config ):
22
27
assert BotoSession is not None , 'AWS_MSK_IAM requires the "botocore" package'
@@ -27,22 +32,28 @@ def __init__(self, **config):
27
32
self ._is_done = False
28
33
self ._is_authenticated = False
29
34
30
- def auth_bytes (self ):
35
+ def _build_client (self ):
31
36
session = BotoSession ()
32
37
credentials = session .get_credentials ().get_frozen_credentials ()
33
- client = AwsMskIamClient (
38
+ if not session .get_config_variable ('region' ):
39
+ raise KafkaConfigurationError ('Unable to determine region for AWS MSK cluster. Is AWS_DEFAULT_REGION set?' )
40
+ return AwsMskIamClient (
34
41
host = self .host ,
35
42
access_key = credentials .access_key ,
36
43
secret_key = credentials .secret_key ,
37
44
region = session .get_config_variable ('region' ),
38
45
token = credentials .token ,
39
46
)
47
+
48
+ def auth_bytes (self ):
49
+ client = self ._build_client ()
50
+ log .debug ("Generating auth token for MSK scope: %s" , client ._scope )
40
51
return client .first_message ()
41
52
42
53
def receive (self , auth_bytes ):
43
54
self ._is_done = True
44
55
self ._is_authenticated = auth_bytes != b''
45
- self ._auth = auth_bytes .deode ('utf-8' )
56
+ self ._auth = auth_bytes .decode ('utf-8' )
46
57
47
58
def is_done (self ):
48
59
return self ._is_done
0 commit comments