49
49
}
50
50
51
51
52
+ def get_sasl_client (host , sasl_auth , service = None , username = None , password = None ):
53
+ import sasl
54
+ sasl_client = sasl .Client ()
55
+ sasl_client .setAttr ('host' , host )
56
+
57
+ if sasl_auth == 'GSSAPI' :
58
+ sasl_client .setAttr ('service' , service )
59
+ elif sasl_auth == 'PLAIN' :
60
+ sasl_client .setAttr ('username' , username )
61
+ sasl_client .setAttr ('password' , password )
62
+ else :
63
+ raise ValueError ("sasl_auth only supports GSSAPI and PLAIN" )
64
+
65
+ sasl_client .init ()
66
+ return sasl_client
67
+
68
+
69
+ def get_pure_sasl_client (host , sasl_auth , service = None , username = None , password = None ):
70
+ from pyhive .sasl_compat import PureSASLClient
71
+
72
+ if sasl_auth == 'GSSAPI' :
73
+ sasl_kwargs = {'service' : service }
74
+ elif sasl_auth == 'PLAIN' :
75
+ sasl_kwargs = {'username' : username , 'password' : password }
76
+ else :
77
+ raise ValueError ("sasl_auth only supports GSSAPI and PLAIN" )
78
+
79
+ return PureSASLClient (host = host , ** sasl_kwargs )
80
+
81
+
82
+ def get_installed_sasl (host , sasl_auth , service = None , username = None , password = None ):
83
+ try :
84
+ return get_sasl_client (host = host , sasl_auth = sasl_auth , service = service , username = username , password = password )
85
+ # The sasl library is available
86
+ except ImportError :
87
+ # Fallback to pure-sasl library
88
+ return get_pure_sasl_client (host = host , sasl_auth = sasl_auth , service = service , username = username , password = password )
89
+
90
+
52
91
def _parse_timestamp (value ):
53
92
if value :
54
93
match = _TIMESTAMP_PATTERN .match (value )
@@ -224,7 +263,6 @@ def __init__(
224
263
self ._transport = thrift .transport .TTransport .TBufferedTransport (socket )
225
264
elif auth in ('LDAP' , 'KERBEROS' , 'NONE' , 'CUSTOM' ):
226
265
# Defer import so package dependency is optional
227
- import sasl
228
266
import thrift_sasl
229
267
230
268
if auth == 'KERBEROS' :
@@ -235,20 +273,8 @@ def __init__(
235
273
if password is None :
236
274
# Password doesn't matter in NONE mode, just needs to be nonempty.
237
275
password = 'x'
238
-
239
- def sasl_factory ():
240
- sasl_client = sasl .Client ()
241
- sasl_client .setAttr ('host' , host )
242
- if sasl_auth == 'GSSAPI' :
243
- sasl_client .setAttr ('service' , kerberos_service_name )
244
- elif sasl_auth == 'PLAIN' :
245
- sasl_client .setAttr ('username' , username )
246
- sasl_client .setAttr ('password' , password )
247
- else :
248
- raise AssertionError
249
- sasl_client .init ()
250
- return sasl_client
251
- self ._transport = thrift_sasl .TSaslClientTransport (sasl_factory , sasl_auth , socket )
276
+
277
+ self ._transport = thrift_sasl .TSaslClientTransport (lambda : get_installed_sasl (host = host , sasl_auth = sasl_auth , service = kerberos_service_name , username = username , password = password ), sasl_auth , socket )
252
278
else :
253
279
# All HS2 config options:
254
280
# https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2#SettingUpHiveServer2-Configuration
0 commit comments