Skip to content

Commit e905143

Browse files
authored
Move kafka.oauth.AbstractTokenProvider -> kafka.sasl.oauth.AbstractTokenProvider (#2525)
1 parent 5c7fc8b commit e905143

File tree

8 files changed

+56
-58
lines changed

8 files changed

+56
-58
lines changed

kafka/admin/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ class KafkaAdminClient(object):
149149
sasl mechanism handshake. Default: 'kafka'
150150
sasl_kerberos_domain_name (str): kerberos domain name to use in GSSAPI
151151
sasl mechanism handshake. Default: one of bootstrap servers
152-
sasl_oauth_token_provider (AbstractTokenProvider): OAuthBearer token provider
153-
instance. (See kafka.oauth.abstract). Default: None
152+
sasl_oauth_token_provider (kafka.sasl.oauth.AbstractTokenProvider): OAuthBearer
153+
token provider instance. Default: None
154154
kafka_client (callable): Custom class / callable for creating KafkaClient instances
155155
156156
"""

kafka/client_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class KafkaClient(object):
171171
sasl mechanism handshake. Default: 'kafka'
172172
sasl_kerberos_domain_name (str): kerberos domain name to use in GSSAPI
173173
sasl mechanism handshake. Default: one of bootstrap servers
174-
sasl_oauth_token_provider (AbstractTokenProvider): OAuthBearer token provider
175-
instance. (See kafka.oauth.abstract). Default: None
174+
sasl_oauth_token_provider (kafka.sasl.oauth.AbstractTokenProvider): OAuthBearer
175+
token provider instance. Default: None
176176
"""
177177

178178
DEFAULT_CONFIG = {

kafka/conn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ class BrokerConnection(object):
183183
sasl mechanism handshake. Default: 'kafka'
184184
sasl_kerberos_domain_name (str): kerberos domain name to use in GSSAPI
185185
sasl mechanism handshake. Default: one of bootstrap servers
186-
sasl_oauth_token_provider (AbstractTokenProvider): OAuthBearer token provider
187-
instance. (See kafka.oauth.abstract). Default: None
186+
sasl_oauth_token_provider (kafka.sasl.oauth.AbstractTokenProvider): OAuthBearer
187+
token provider instance. Default: None
188188
"""
189189

190190
DEFAULT_CONFIG = {

kafka/consumer/group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ class KafkaConsumer(six.Iterator):
258258
sasl mechanism handshake. Default: 'kafka'
259259
sasl_kerberos_domain_name (str): kerberos domain name to use in GSSAPI
260260
sasl mechanism handshake. Default: one of bootstrap servers
261-
sasl_oauth_token_provider (AbstractTokenProvider): OAuthBearer token provider
262-
instance. (See kafka.oauth.abstract). Default: None
261+
sasl_oauth_token_provider (kafka.sasl.oauth.AbstractTokenProvider): OAuthBearer
262+
token provider instance. Default: None
263263
kafka_client (callable): Custom class / callable for creating KafkaClient instances
264264
265265
Note:

kafka/oauth/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

kafka/oauth/abstract.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

kafka/producer/kafka.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ class KafkaProducer(object):
297297
sasl mechanism handshake. Default: 'kafka'
298298
sasl_kerberos_domain_name (str): kerberos domain name to use in GSSAPI
299299
sasl mechanism handshake. Default: one of bootstrap servers
300-
sasl_oauth_token_provider (AbstractTokenProvider): OAuthBearer token provider
301-
instance. (See kafka.oauth.abstract). Default: None
300+
sasl_oauth_token_provider (kafka.sasl.oauth.AbstractTokenProvider): OAuthBearer
301+
token provider instance. Default: None
302302
kafka_client (callable): Custom class / callable for creating KafkaClient instances
303303
304304
Note:

kafka/sasl/oauth.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from __future__ import absolute_import
22

3+
import abc
4+
35
from kafka.sasl.abc import SaslMechanism
46

57

68
class SaslMechanismOAuth(SaslMechanism):
79

810
def __init__(self, **config):
911
assert 'sasl_oauth_token_provider' in config, 'sasl_oauth_token_provider required for OAUTHBEARER sasl'
12+
assert isinstance(config['sasl_oauth_token_provider'], AbstractTokenProvider), \
13+
'sasl_oauth_token_provider must implement kafka.sasl.oauth.AbstractTokenProvider'
1014
self.token_provider = config['sasl_oauth_token_provider']
11-
assert callable(getattr(self.token_provider, 'token', None)), 'sasl_oauth_token_provider must implement method #token()'
1215
self._is_done = False
1316
self._is_authenticated = False
1417

@@ -32,13 +35,53 @@ def _token_extensions(self):
3235
Return a string representation of the OPTIONAL key-value pairs that can be sent with an OAUTHBEARER
3336
initial request.
3437
"""
35-
# Only run if the #extensions() method is implemented by the clients Token Provider class
3638
# Builds up a string separated by \x01 via a dict of key value pairs
37-
extensions = getattr(self.token_provider, 'extensions', lambda: [])()
39+
extensions = self.token_provider.extensions()
3840
msg = '\x01'.join(['{}={}'.format(k, v) for k, v in extensions.items()])
3941
return '\x01' + msg if msg else ''
4042

4143
def auth_details(self):
4244
if not self.is_authenticated:
4345
raise RuntimeError('Not authenticated yet!')
4446
return 'Authenticated via SASL / OAuth'
47+
48+
# This statement is compatible with both Python 2.7 & 3+
49+
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
50+
51+
class AbstractTokenProvider(ABC):
52+
"""
53+
A Token Provider must be used for the SASL OAuthBearer protocol.
54+
55+
The implementation should ensure token reuse so that multiple
56+
calls at connect time do not create multiple tokens. The implementation
57+
should also periodically refresh the token in order to guarantee
58+
that each call returns an unexpired token. A timeout error should
59+
be returned after a short period of inactivity so that the
60+
broker can log debugging info and retry.
61+
62+
Token Providers MUST implement the token() method
63+
"""
64+
65+
def __init__(self, **config):
66+
pass
67+
68+
@abc.abstractmethod
69+
def token(self):
70+
"""
71+
Returns a (str) ID/Access Token to be sent to the Kafka
72+
client.
73+
"""
74+
pass
75+
76+
def extensions(self):
77+
"""
78+
This is an OPTIONAL method that may be implemented.
79+
80+
Returns a map of key-value pairs that can
81+
be sent with the SASL/OAUTHBEARER initial client request. If
82+
not implemented, the values are ignored. This feature is only available
83+
in Kafka >= 2.1.0.
84+
85+
All returned keys and values should be type str
86+
"""
87+
return {}

0 commit comments

Comments
 (0)