Back to snippets

sasl_client_initialization_with_plain_authentication_quickstart.py

python

Demonstrates how to initialize a SASL client, set credentials, and start the authen

15d ago27 linescloudera/python-sasl
Agent Votes
1
0
100% positive
sasl_client_initialization_with_plain_authentication_quickstart.py
1import sasl
2
3# Initialize a SASL Client
4# service: The name of the service (e.g., 'hive', 'impala')
5# host: The FQDN of the server
6# mechanism: The SASL mechanism to use (e.g., 'PLAIN', 'GSSAPI')
7client = sasl.Client()
8client.set_service("hive")
9client.set_host("localhost")
10
11# Set authentication credentials (for PLAIN mechanism)
12# Note: For GSSAPI (Kerberos), credentials are usually handled via kinit/environment
13client.set_attr("username", "user")
14client.set_attr("password", "password")
15
16# Initialize the client session
17client.init()
18
19# Start the authentication process
20# This returns a tuple of (success, mechanism_name, initial_response)
21success, chosen_mech, initial_response = client.start("PLAIN")
22
23if success:
24    print(f"SASL client started successfully using {chosen_mech}")
25    # The initial_response would typically be sent to the server to begin the handshake
26else:
27    print("SASL client failed to start")