|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +# Windows-only |
| 6 | +try: |
| 7 | + import sspi |
| 8 | + import pywintypes |
| 9 | + import sspicon |
| 10 | + import win32security |
| 11 | +except ImportError: |
| 12 | + sspi = None |
| 13 | + |
| 14 | +from kafka.sasl.abc import SaslMechanism |
| 15 | + |
| 16 | + |
| 17 | +log = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class SaslMechanismSSPI(SaslMechanism): |
| 21 | + # Establish security context and negotiate protection level |
| 22 | + # For reference see RFC 4752, section 3 |
| 23 | + |
| 24 | + SASL_QOP_AUTH = 1 |
| 25 | + SASL_QOP_AUTH_INT = 2 |
| 26 | + SASL_QOP_AUTH_CONF = 4 |
| 27 | + |
| 28 | + def __init__(self, **config): |
| 29 | + assert sspi is not None, 'No GSSAPI lib available (gssapi or sspi)' |
| 30 | + if 'sasl_kerberos_name' not in config and 'sasl_kerberos_service_name' not in config: |
| 31 | + raise ValueError('sasl_kerberos_service_name or sasl_kerberos_name required for GSSAPI sasl configuration') |
| 32 | + self._is_done = False |
| 33 | + self._is_authenticated = False |
| 34 | + if config.get('sasl_kerberos_name', None) is not None: |
| 35 | + self.auth_id = str(config['sasl_kerberos_name']) |
| 36 | + else: |
| 37 | + kerberos_domain_name = config.get('sasl_kerberos_domain_name', '') or config.get('host', '') |
| 38 | + self.auth_id = config['sasl_kerberos_service_name'] + '/' + kerberos_domain_name |
| 39 | + scheme = "Kerberos" # Do not try with Negotiate for SASL authentication. Tokens are different. |
| 40 | + # https://docs.microsoft.com/en-us/windows/win32/secauthn/context-requirements |
| 41 | + flags = ( |
| 42 | + sspicon.ISC_REQ_MUTUAL_AUTH | # mutual authentication |
| 43 | + sspicon.ISC_REQ_INTEGRITY | # check for integrity |
| 44 | + sspicon.ISC_REQ_SEQUENCE_DETECT | # enable out-of-order messages |
| 45 | + sspicon.ISC_REQ_CONFIDENTIALITY # request confidentiality |
| 46 | + ) |
| 47 | + self._client_ctx = sspi.ClientAuth(scheme, targetspn=self.auth_id, scflags=flags) |
| 48 | + self._next_token = self._client_ctx.step(None) |
| 49 | + |
| 50 | + def auth_bytes(self): |
| 51 | + # GSSAPI Auth does not have a final broker->client message |
| 52 | + # so mark is_done after the final auth_bytes are provided |
| 53 | + # in practice we'll still receive a response when using SaslAuthenticate |
| 54 | + # but not when using the prior unframed approach. |
| 55 | + if self._client_ctx.authenticated: |
| 56 | + self._is_done = True |
| 57 | + self._is_authenticated = True |
| 58 | + return self._next_token or b'' |
| 59 | + |
| 60 | + def receive(self, auth_bytes): |
| 61 | + log.debug("Received token from server (size %s)", len(auth_bytes)) |
| 62 | + if not self._client_ctx.authenticated: |
| 63 | + # calculate an output token from kafka token (or None on first iteration) |
| 64 | + # https://docs.microsoft.com/en-us/windows/win32/api/sspi/nf-sspi-initializesecuritycontexta |
| 65 | + # https://docs.microsoft.com/en-us/windows/win32/secauthn/initializesecuritycontext--kerberos |
| 66 | + # authorize method will wrap for us our token in sspi structures |
| 67 | + error, auth = self._client_ctx.authorize(auth_bytes) |
| 68 | + if len(auth) > 0 and len(auth[0].Buffer): |
| 69 | + log.debug("Got token from context") |
| 70 | + # this buffer must be sent to the server whatever the result is |
| 71 | + self._next_token = auth[0].Buffer |
| 72 | + else: |
| 73 | + log.debug("Got no token, exchange finished") |
| 74 | + # seems to be the end of the loop |
| 75 | + self._next_token = b'' |
| 76 | + elif self._is_done: |
| 77 | + # The final step of gssapi is send, so we do not expect any additional bytes |
| 78 | + # however, allow an empty message to support SaslAuthenticate response |
| 79 | + if auth_bytes != b'': |
| 80 | + raise ValueError("Unexpected receive auth_bytes after sasl/gssapi completion") |
| 81 | + else: |
| 82 | + # Process the security layer negotiation token, sent by the server |
| 83 | + # once the security context is established. |
| 84 | + |
| 85 | + # The following part is required by SASL, but not by classic Kerberos. |
| 86 | + # See RFC 4752 |
| 87 | + |
| 88 | + # unwraps message containing supported protection levels and msg size |
| 89 | + msg, _was_encrypted = self._client_ctx.unwrap(auth_bytes) |
| 90 | + |
| 91 | + # Kafka currently doesn't support integrity or confidentiality security layers, so we |
| 92 | + # simply set QoP to 'auth' only (first octet). We reuse the max message size proposed |
| 93 | + # by the server |
| 94 | + client_flags = self.SASL_QOP_AUTH |
| 95 | + server_flags = msg[0] |
| 96 | + message_parts = [ |
| 97 | + bytes(client_flags & server_flags), |
| 98 | + msg[:1], |
| 99 | + self.auth_id.encode('utf-8'), |
| 100 | + ] |
| 101 | + # add authorization identity to the response, and GSS-wrap |
| 102 | + self._next_token = self._client_ctx.wrap(b''.join(message_parts), False) |
| 103 | + |
| 104 | + def is_done(self): |
| 105 | + return self._is_done |
| 106 | + |
| 107 | + def is_authenticated(self): |
| 108 | + return self._is_authenticated |
| 109 | + |
| 110 | + def auth_details(self): |
| 111 | + return 'Authenticated as %s to %s via SASL / SSPI/GSSAPI \\o/' % (self._client_ctx.initiator_name, self._client_ctx.service_name) |
0 commit comments