From 812b1d134eb32915d461f70c812a302e5e718387 Mon Sep 17 00:00:00 2001 From: AlonSoko <25619863+AlonSoko@users.noreply.github.com> Date: Sat, 24 Oct 2026 12:06:23 +0300 Subject: [PATCH 1/2] PHOENIX-8033 phoenixdb: use native SSPI for SPNEGO on Windows On Windows, authenticate through pyspnego (SSPI, the logged on user's LSA Kerberos tickets) instead of requests-gssapi, so MIT Kerberos no longer has to be installed. The gssapi and requests_gssapi imports are now done only when SPNEGO is used, so importing phoenixdb no longer requires a GSSAPI library. --- python-phoenixdb/phoenixdb/__init__.py | 15 +++-- python-phoenixdb/phoenixdb/sspi_auth.py | 78 +++++++++++++++++++++++++ python-phoenixdb/requirements.txt | 3 +- python-phoenixdb/setup.py | 5 +- 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 python-phoenixdb/phoenixdb/sspi_auth.py diff --git a/python-phoenixdb/phoenixdb/__init__.py b/python-phoenixdb/phoenixdb/__init__.py index 900e4da..080a8d9 100644 --- a/python-phoenixdb/phoenixdb/__init__.py +++ b/python-phoenixdb/phoenixdb/__init__.py @@ -14,8 +14,6 @@ # limitations under the License. import sys -import gssapi - from phoenixdb import errors, types from phoenixdb.avatica import AvaticaClient from phoenixdb.connection import Connection @@ -24,8 +22,6 @@ from requests.auth import HTTPBasicAuth, HTTPDigestAuth -from requests_gssapi import HTTPSPNEGOAuth - if sys.version_info.major == 3: from urllib.parse import urlencode, urlparse, urlunparse, parse_qs else: @@ -127,6 +123,17 @@ def connect(url, max_retries=None, auth=None, authentication=None, avatica_user= def _get_SPNEGOAuth(): + if sys.platform == 'win32': + try: + from phoenixdb.sspi_auth import HTTPSPNEGOAuthSSPI + except ImportError: + # pyspnego needs Python 3.8+, fall back to requests_gssapi below + pass + else: + return HTTPSPNEGOAuthSSPI() + + import gssapi + from requests_gssapi import HTTPSPNEGOAuth try: spnego = gssapi.mechs.Mechanism.from_sasl_name("SPNEGO") except AttributeError: diff --git a/python-phoenixdb/phoenixdb/sspi_auth.py b/python-phoenixdb/phoenixdb/sspi_auth.py new file mode 100644 index 0000000..0c64b1b --- /dev/null +++ b/python-phoenixdb/phoenixdb/sspi_auth.py @@ -0,0 +1,78 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""SPNEGO authentication for requests on Windows. + +Uses pyspnego, which goes through the native SSPI (and so the logged on user's +LSA Kerberos tickets) on Windows, so no MIT Kerberos installation is needed. +""" + +import base64 +import re +from urllib.parse import urlparse + +import spnego +from requests.auth import AuthBase + +_NEGOTIATE_RE = re.compile(r'(?:^|,)\s*Negotiate\s*([^,]*)', re.IGNORECASE) + + +def _negotiate_token(response): + match = _NEGOTIATE_RE.search(response.headers.get('www-authenticate', '')) + if match is None: + return None + token = match.group(1).strip() + return base64.b64decode(token) if token else b'' + + +class HTTPSPNEGOAuthSSPI(AuthBase): + """Opportunistic SPNEGO ("Negotiate") authentication, like + requests_gssapi.HTTPSPNEGOAuth(opportunistic_auth=True) with mutual + authentication disabled.""" + + def __init__(self, service='HTTP'): + self.service = service + + def _new_context(self, url): + return spnego.client(hostname=urlparse(url).hostname, service=self.service, + protocol='negotiate') + + def __call__(self, r): + ctx = self._new_context(r.url) + r.headers['Authorization'] = 'Negotiate ' + base64.b64encode(ctx.step()).decode() + r.register_hook('response', self._handle_401(ctx)) + return r + + def _handle_401(self, ctx): + def handle(response, **kwargs): + if response.status_code != 401 or getattr(response.request, '_spnego_retried', False): + return response + in_token = _negotiate_token(response) + if not in_token or ctx.complete: + # The server rejected the token outright, retrying won't help + return response + + # Multi-leg exchange: answer the server's challenge on the same connection + response.content + response.close() + prep = response.request.copy() + prep.headers['Authorization'] = 'Negotiate ' + \ + base64.b64encode(ctx.step(in_token)).decode() + prep._spnego_retried = True + new_response = response.connection.send(prep, **kwargs) + new_response.history.append(response) + new_response.request = prep + return new_response + return handle diff --git a/python-phoenixdb/requirements.txt b/python-phoenixdb/requirements.txt index 3000b74..41c6545 100644 --- a/python-phoenixdb/requirements.txt +++ b/python-phoenixdb/requirements.txt @@ -18,4 +18,5 @@ # protobuf>=3.0.0 requests -requests-gssapi \ No newline at end of file +requests-gssapi; sys_platform != "win32" +pyspnego; sys_platform == "win32" \ No newline at end of file diff --git a/python-phoenixdb/setup.py b/python-phoenixdb/setup.py index 3727e1e..c87decd 100644 --- a/python-phoenixdb/setup.py +++ b/python-phoenixdb/setup.py @@ -43,6 +43,8 @@ def readme(): ] if sys.version_info < (3,8): install_requires.append('requests-gssapi<1.3.0') + elif sys.platform == 'win32': + install_requires.append('pyspnego') else: install_requires.append('requests-gssapi') if sys.version_info < (3,6): @@ -53,7 +55,8 @@ def readme(): install_requires=[ 'protobuf>=3.0.0', 'requests', - 'requests-gssapi; python_version>="3.8.0"', + 'requests-gssapi; python_version>="3.8.0" and sys_platform!="win32"', + 'pyspnego; python_version>="3.8.0" and sys_platform=="win32"', 'requests-gssapi<1.3.0; python_version<"3.8.0"', 'gssapi<1.6.0; python_version<"3.6.0"', ] From d65508a81710aff85a08ee9408084778b6cc451c Mon Sep 17 00:00:00 2001 From: AlonSoko <25619863+AlonSoko@users.noreply.github.com> Date: Mon, 26 Oct 2026 13:48:51 +0200 Subject: [PATCH 2/2] PHOENIX-8033 phoenixdb: clear error when a broken gssapi blocks pyspnego on Windows A gssapi package installed without MIT Kerberos for Windows raises OSError on import. pyspnego only catches ImportError around that import, so "import spnego" failed with a misleading "Could not find KfW installation". Raise an error that names the cause and the fix instead. --- python-phoenixdb/phoenixdb/sspi_auth.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python-phoenixdb/phoenixdb/sspi_auth.py b/python-phoenixdb/phoenixdb/sspi_auth.py index 0c64b1b..673df9a 100644 --- a/python-phoenixdb/phoenixdb/sspi_auth.py +++ b/python-phoenixdb/phoenixdb/sspi_auth.py @@ -23,9 +23,18 @@ import re from urllib.parse import urlparse -import spnego from requests.auth import AuthBase +try: + import spnego +except OSError as e: + # pyspnego only catches ImportError around its optional gssapi import. A gssapi + # package installed without MIT Kerberos for Windows raises OSError instead. + raise RuntimeError( + "pyspnego could not be imported because the installed 'gssapi' package is " + "broken (%s). SSPI does not need it: run 'pip uninstall gssapi requests-gssapi'." + % e) from e + _NEGOTIATE_RE = re.compile(r'(?:^|,)\s*Negotiate\s*([^,]*)', re.IGNORECASE)