Registry / auth-security / thrift-sasl

thrift-sasl

JSON →
library0.4.3pypypi✓ verified 26d ago

This is a Python library (version 0.4.3) that provides SASL (Simple Authentication and Security Layer) transport for Apache Thrift clients. It enables secure communication by implementing `TSaslClientTransport`, allowing Thrift applications to use authentication mechanisms like Kerberos. The library has a maintenance release cadence, with the latest update in May 2021.

pip install thrift-sasl
INSTALL
IMPORT
SIG · THRIFT-SASL
T
thrift-sasl
auth-securitypythonv0.4.3
Install
3.1s avg
Import
12ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 24.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.1s · import 0.012s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TSaslClientTransport
from thrift_sasl import TSaslClientTransport
This is the primary transport class for SASL client authentication.

This quickstart demonstrates how to initialize `TSaslClientTransport` using `TSocket` and `TBufferedTransport`, configure it for a SASL mechanism (like PLAIN), and open the connection. This setup is the foundational step before creating a Thrift client and making RPC calls.

import os from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol from thrift_sasl import TSaslClientTransport # --- Configuration (replace with your actual values) --- SASL_MECHANISM = os.environ.get('SASL_MECHANISM', 'PLAIN') # e.g., 'GSSAPI', 'PLAIN' SASL_USERNAME = os.environ.get('SASL_USERNAME', 'user') SASL_PASSWORD = os.environ.get('SASL_PASSWORD', 'password') # Only for PLAIN mechanism THRIFT_HOST = os.environ.get('THRIFT_HOST', 'localhost') THRIFT_PORT = int(os.environ.get('THRIFT_PORT', '9090')) # For GSSAPI, you might need: SASL_SERVICE_PRINCIPAL = 'thrift/host.example.com@REALM' # 1. Create a base Thrift socket transport socket = TSocket.TSocket(THRIFT_HOST, THRIFT_PORT) # 2. Wrap the socket in a buffered transport (often required) buffered_transport = TTransport.TBufferedTransport(socket) # 3. Create the SASL client transport # For PLAIN mechanism: sasl_transport = TSaslClientTransport( buffered_transport, SASL_MECHANISM, username=SASL_USERNAME, password=SASL_PASSWORD ) # For GSSAPI, you would typically use: # sasl_transport = TSaslClientTransport( # buffered_transport, # SASL_MECHANISM, # service_principal=SASL_SERVICE_PRINCIPAL # ) # 4. Open the SASL transport (initiates SASL handshake) try: sasl_transport.open() print(f"Successfully opened SASL transport to {THRIFT_HOST}:{THRIFT_PORT}") # 5. Create a protocol (e.g., TBinaryProtocol) using the SASL transport protocol = TBinaryProtocol.TBinaryProtocol(sasl_transport) # At this point, you would typically create a Thrift client # and make remote procedure calls (RPCs). # Example: client = MyThriftService.Client(protocol) # result = client.my_method() # print(f"RPC result: {result}") except TTransport.TTransportException as e: print(f"Thrift transport error: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") finally: # 6. Close the transport if sasl_transport.isOpen(): sasl_transport.close() print("SASL transport closed.")
Debug
Known issues
breakingOlder versions of `thrift-sasl` (prior to 0.4.3) depended on the unmaintained `sasl` package, which had compatibility issues with newer Python versions and required `g++` for compilation. Version `0.4.3` switched to `pure-sasl` to resolve this, making it pure-Python compatible.
fix
Upgrade to `thrift-sasl>=0.4.3`. If using an older version, consider installing `pure-sasl` manually and ensuring your environment is correctly configured, or ideally, upgrade.
affects: <0.4.3
gotchaCorrect configuration of SASL parameters (e.g., mechanism, username, password, service principal) is crucial and highly dependent on your server's SASL setup. Incorrect parameters will lead to authentication failures. For GSSAPI (Kerberos), ensure you have valid Kerberos tickets (`kinit`) and the correct service principal.
fix
Consult your Thrift server's SASL configuration documentation. Verify the SASL mechanism, authentication credentials, and any required service principals. Use `os.environ.get` for sensitive data like passwords/principals.
affects: All
gotchaIt's essential to wrap the underlying Thrift transport (e.g., `TSocket`) with a buffered transport (`TBufferedTransport`) before passing it to `TSaslClientTransport`. The `thrift-sasl` library, particularly in versions 0.4.3a1 and later, includes fixes to ensure frames are fully buffered.
fix
Always use `TTransport.TBufferedTransport(TSocket.TSocket(...))` when initializing `TSaslClientTransport` to prevent potential data framing or read issues.
affects: All
breakingThe `TSaslClientTransport` constructor does not accept SASL authentication parameters like `username` or `password` directly. These details must be configured through a `TSaslClient` instance, which is then passed to `TSaslClientTransport`.
fix
Instead of passing authentication parameters directly to `TSaslClientTransport`, create and configure a `TSaslClient` instance (e.g., `sasl_client = TSaslClient(mechanism='PLAIN'); sasl_client.setUname('your_username'); sasl_client.setPasswd('your_password')`). Then, pass this configured `TSaslClient` object as the second argument to `TSaslClientTransport` (e.g., `TSaslClientTransport(buffered_transport, sasl_client)`). Consult `thrift-sasl` documentation for specific API usage.
affects: All
breakingThe `TSaslClientTransport` constructor no longer accepts `username` or `password` as direct keyword arguments. This breaking change was introduced in `thrift-sasl` version 0.5.0.
fix
Consult the `thrift-sasl` documentation for version 0.5.0 or later. For SASL mechanisms requiring username/password (e.g., PLAIN, LOGIN), these credentials must now be passed within the `mechanism_properties` dictionary. For example: `TSaslClientTransport(..., mechanism='PLAIN', mechanism_properties={'username': 'user', 'password': 'pass'})`. For GSSAPI, ensure Kerberos tickets are configured externally.
affects: >=0.5.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'sasl'
The `thrift-sasl` library depends on `python-sasl` for its SASL mechanisms, but `python-sasl` or its required underlying system dependencies are not installed or failed to compile.
fix
Install `python-sasl` and ensure the necessary system development libraries for SASL (e.g., `libsasl2-dev` on Debian/Ubuntu, `cyrus-sasl-devel` on CentOS/RHEL) are present before installing it.
```bash
pip install python-sasl
# On Debian/Ubuntu:
# sudo apt-get install libsasl2-dev python3-dev libkrb5-dev
# On CentOS/RHEL:
# sudo yum install cyrus-sasl-devel python3-devel krb5-devel
```
SASL(-1): generic failure: GSSAPI Error: Unspecified GSS failure. Minor code may provide more information (No Kerberos credentials available)
The Kerberos authentication mechanism (GSSAPI) failed because the client machine lacks valid Kerberos credentials (a ticket-granting ticket), or the existing ticket has expired.
fix
Obtain a valid Kerberos ticket for your principal by running the `kinit` command before attempting to connect.
```bash
kinit your_principal@YOUR.REALM
```
ModuleNotFoundError: No module named 'thrift_sasl'
The `thrift-sasl` Python package itself has not been installed in the current Python environment.
fix
Install the library using pip.
```bash
pip install thrift-sasl
```
SASL(-1): generic failure: GSSAPI Error: An unsupported mechanism was requested
The SASL client could not use the requested GSSAPI mechanism, often due to missing GSSAPI plugins in the system's SASL configuration or an incorrect service principal name (SPN) specified in the client code.
fix
Ensure the GSSAPI SASL plugin is installed (e.g., `libsasl2-modules-gssapi-mit` on Debian/Ubuntu), and verify that the `service` parameter in `TSaslClientTransport` matches the Kerberos service principal for the server (e.g., 'impala' for `impala/host@REALM`).
```python
# In your Python code:
from thrift_sasl import TSaslClientTransport
# ... (inner_transport setup)

sasl_transport = TSaslClientTransport(
    inner_transport,
    host='your_server_host',
    service='impala' # Example: For a service principal like 'impala/host@REALM'
)
```
Upgrade
Version history
0.4.3latest on PyPI · released May 26, 2021
Audit
Dependencies
pure-saslrequiredCore dependency for SASL implementation.
sixrequiredPython 2/3 compatibility layer.
thriftrequiredApache Thrift library for transport protocols.
Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
1
Resources
thrift-sasl — pip install thrift-sasl · libregistry