|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 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 | + |
| 12 | +from kafka.sasl.abc import SaslMechanism |
| 13 | + |
| 14 | + |
| 15 | +class SaslMechanismGSSAPI(SaslMechanism): |
| 16 | + # Establish security context and negotiate protection level |
| 17 | + # For reference RFC 2222, section 7.2.1 |
| 18 | + |
| 19 | + SASL_QOP_AUTH = 1 |
| 20 | + SASL_QOP_AUTH_INT = 2 |
| 21 | + SASL_QOP_AUTH_CONF = 4 |
| 22 | + |
| 23 | + def __init__(self, **config): |
| 24 | + assert gssapi is not None, 'GSSAPI lib not available' |
| 25 | + assert config['sasl_kerberos_service_name'] is not None, 'sasl_kerberos_service_name required for GSSAPI sasl' |
| 26 | + self._is_done = False |
| 27 | + self._is_authenticated = False |
| 28 | + self.kerberos_damin_name = config['sasl_kerberos_domain_name'] or config['host'] |
| 29 | + self.auth_id = config['sasl_kerberos_service_name'] + '@' + kerberos_damin_name |
| 30 | + self.gssapi_name = gssapi.Name(auth_id, name_type=gssapi.NameType.hostbased_service).canonicalize(gssapi.MechType.kerberos) |
| 31 | + self._client_ctx = gssapi.SecurityContext(name=self.gssapi_name, usage='initiate') |
| 32 | + self._next_token = self._client_ctx.step(None) |
| 33 | + |
| 34 | + def auth_bytes(self): |
| 35 | + # GSSAPI Auth does not have a final broker->client message |
| 36 | + # so mark is_done after the final auth_bytes are provided |
| 37 | + # in practice we'll still receive a response when using SaslAuthenticate |
| 38 | + # but not when using the prior unframed approach. |
| 39 | + if self._client_ctx.complete: |
| 40 | + self._is_done = True |
| 41 | + self._is_authenticated = True |
| 42 | + return self._next_token or b'' |
| 43 | + |
| 44 | + def receive(self, auth_bytes): |
| 45 | + if not self._client_ctx.complete: |
| 46 | + # The server will send a token back. Processing of this token either |
| 47 | + # establishes a security context, or it needs further token exchange. |
| 48 | + # The gssapi will be able to identify the needed next step. |
| 49 | + self._next_token = self._client_ctx.step(auth_bytes) |
| 50 | + elif self._is_done: |
| 51 | + # The final step of gssapi is send, so we do not expect any additional bytes |
| 52 | + # however, allow an empty message to support SaslAuthenticate response |
| 53 | + if auth_bytes != b'': |
| 54 | + raise ValueError("Unexpected receive auth_bytes after sasl/gssapi completion") |
| 55 | + else: |
| 56 | + # unwraps message containing supported protection levels and msg size |
| 57 | + msg = client_ctx.unwrap(received_token).message |
| 58 | + # Kafka currently doesn't support integrity or confidentiality security layers, so we |
| 59 | + # simply set QoP to 'auth' only (first octet). We reuse the max message size proposed |
| 60 | + # by the server |
| 61 | + message_parts = [ |
| 62 | + Int8.encode(self.SASL_QOP_AUTH & Int8.decode(io.BytesIO(msg[0:1]))), |
| 63 | + msg[:1], |
| 64 | + self.auth_id.encode(), |
| 65 | + ] |
| 66 | + # add authorization identity to the response, and GSS-wrap |
| 67 | + self._next_token = self._client_ctx.wrap(b''.join(message_parts), False).message |
| 68 | + |
| 69 | + def is_done(self): |
| 70 | + return self._is_done |
| 71 | + |
| 72 | + def is_authenticated(self): |
| 73 | + return self._is_authenticated |
0 commit comments