Skip to content

Commit 32e2dbb

Browse files
Import botocore and gssapi lazily in SASL mechanisms (#3171)
1 parent c374a82 commit 32e2dbb

4 files changed

Lines changed: 31 additions & 23 deletions

File tree

‎kafka/net/sasl/gssapi.py‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import struct
22

3-
# needed for SASL_GSSAPI authentication:
4-
try:
5-
import gssapi
6-
from gssapi.raw.misc import GSSError
7-
except (ImportError, OSError):
8-
#no gssapi available, will disable gssapi mechanism
9-
gssapi = None
10-
GSSError = None
11-
123
from .abc import SaslMechanism
134

145

@@ -21,7 +12,9 @@ class SaslMechanismGSSAPI(SaslMechanism):
2112
SASL_QOP_AUTH_CONF = 4
2213

2314
def __init__(self, **config):
24-
if gssapi is None:
15+
try:
16+
import gssapi
17+
except (ImportError, OSError):
2518
raise RuntimeError('GSSAPI lib not found!')
2619
if 'sasl_kerberos_name' not in config and 'sasl_kerberos_service_name' not in config:
2720
raise ValueError('sasl_kerberos_service_name or sasl_kerberos_name required for GSSAPI sasl configuration')

‎kafka/net/sasl/msk.py‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
import string
77
import urllib
88

9-
# needed for AWS_MSK_IAM authentication:
10-
try:
11-
from botocore.session import Session as BotoSession
12-
except ImportError:
13-
# no botocore available, will disable AWS_MSK_IAM mechanism
14-
BotoSession = None
15-
169
from .abc import SaslMechanism
1710
from kafka.errors import KafkaConfigurationError
1811

@@ -22,8 +15,11 @@
2215

2316
class SaslMechanismAwsMskIam(SaslMechanism):
2417
def __init__(self, **config):
25-
if BotoSession is None:
18+
try:
19+
from botocore.session import Session as BotoSession
20+
except ImportError:
2621
raise RuntimeError('AWS_MSK_IAM requires the "botocore" package')
22+
self._boto_session_cls = BotoSession
2723
if config.get('security_protocol', '') != 'SASL_SSL':
2824
raise KafkaConfigurationError('AWS_MSK_IAM requires SASL_SSL')
2925
if 'host' not in config:
@@ -34,7 +30,7 @@ def __init__(self, **config):
3430
self._is_authenticated = False
3531

3632
def _build_client(self):
37-
session = BotoSession()
33+
session = self._boto_session_cls()
3834
credentials = session.get_credentials().get_frozen_credentials()
3935
if not session.get_config_variable('region'):
4036
raise KafkaConfigurationError('Unable to determine region for AWS MSK cluster. Is AWS_DEFAULT_REGION set?')

‎test/net/sasl/test_gssapi.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import sys
12
from unittest import mock
23

4+
import pytest
5+
36
from kafka.net.sasl import get_sasl_mechanism
47
import kafka.net.sasl.gssapi
58

@@ -18,9 +21,10 @@ def mocked_message_wrapper(msg, *args):
1821
return wrapped
1922
client_ctx.unwrap.side_effect = mocked_message_wrapper
2023
client_ctx.wrap.side_effect = mocked_message_wrapper
21-
kafka.net.sasl.gssapi.gssapi = mock.Mock()
22-
kafka.net.sasl.gssapi.gssapi.SecurityContext.return_value = client_ctx
23-
gssapi = get_sasl_mechanism('GSSAPI')(**config)
24+
gssapi_mock = mock.Mock()
25+
gssapi_mock.SecurityContext.return_value = client_ctx
26+
with mock.patch.dict(sys.modules, {'gssapi': gssapi_mock}):
27+
gssapi = get_sasl_mechanism('GSSAPI')(**config)
2428
assert isinstance(gssapi, kafka.net.sasl.gssapi.SaslMechanismGSSAPI)
2529
client_ctx.step.assert_called_with(None)
2630

@@ -35,3 +39,9 @@ def mocked_message_wrapper(msg, *args):
3539

3640
assert gssapi.is_done()
3741
assert gssapi.is_authenticated()
42+
43+
44+
def test_gssapi_unavailable():
45+
with mock.patch.dict(sys.modules, {'gssapi': None}):
46+
with pytest.raises(RuntimeError, match='GSSAPI lib not found!'):
47+
kafka.net.sasl.gssapi.SaslMechanismGSSAPI(sasl_kerberos_service_name='bar')

‎test/net/sasl/test_msk.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import datetime
22
import json
3+
import sys
34
from unittest import mock
45

6+
import pytest
7+
58
from kafka.net.sasl.msk import AwsMskIamClient, SaslMechanismAwsMskIam
69

710

@@ -65,7 +68,7 @@ def test_aws_msk_iam_client_temporary_credentials():
6568

6669

6770
def test_aws_msk_iam_sasl_mechanism():
68-
with mock.patch('kafka.net.sasl.msk.BotoSession'):
71+
with mock.patch.dict(sys.modules, {'botocore': mock.MagicMock(), 'botocore.session': mock.MagicMock()}):
6972
sasl = SaslMechanismAwsMskIam(security_protocol='SASL_SSL', host='localhost')
7073
with mock.patch.object(sasl, '_build_client', return_value=client_factory(token=None)):
7174
assert sasl.auth_bytes() != b''
@@ -76,3 +79,9 @@ def test_aws_msk_iam_sasl_mechanism():
7679
assert sasl.is_done()
7780
assert sasl.is_authenticated()
7881
assert sasl.auth_details()
82+
83+
84+
def test_aws_msk_iam_botocore_unavailable():
85+
with mock.patch.dict(sys.modules, {'botocore': None, 'botocore.session': None}):
86+
with pytest.raises(RuntimeError, match='AWS_MSK_IAM requires the "botocore" package'):
87+
SaslMechanismAwsMskIam(security_protocol='SASL_SSL', host='localhost')

0 commit comments

Comments
 (0)