forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
361 lines (324 loc) · 14.8 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2018 Snowflake Computing Inc. All right reserved.
#
import copy
import json
import logging
import uuid
from threading import Thread
from .compat import (TO_UNICODE, urlencode)
from .errorcode import (ER_FAILED_TO_CONNECT_TO_DB, ER_INVALID_VALUE)
from .errors import (Error,
DatabaseError,
ServiceUnavailableError,
ForbiddenError,
BadGatewayError)
from .network import (CONTENT_TYPE_APPLICATION_JSON,
ACCEPT_TYPE_APPLICATION_SNOWFLAKE,
PYTHON_CONNECTOR_USER_AGENT, PLATFORM, PYTHON_VERSION,
IMPLEMENTATION, COMPILER)
from .sqlstate import (SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED)
from .version import VERSION
logger = logging.getLogger(__name__)
DEFAULT_AUTHENTICATOR = u'SNOWFLAKE' # default authenticator name
EXTERNAL_BROWSER_AUTHENTICATOR = u'EXTERNALBROWSER'
KEY_PAIR_AUTHENTICATOR = u'SNOWFLAKE_JWT'
class AuthByExternalService(object):
"""
External Authenticator interface.
"""
@property
def assertion_content(self):
raise NotImplementedError
def update_body(self, body):
raise NotImplementedError
def authenticate(self, authenticator, account, user, password):
raise NotImplementedError
def handle_failure(self, ret):
""" Handles a failure when connecting to Snowflake to
get the SSO Url.
Args:
ret: dictionary returned from Snowflake.
"""
Error.errorhandler_wrapper(
self._rest._connection, None, DatabaseError,
{
u'msg': (u"Failed to connect to DB: {host}:{port}, "
u"proxies={proxy_host}:{proxy_port}, "
u"proxy_user={proxy_user}, "
u"{message}").format(
host=self._rest._host,
port=self._rest._port,
proxy_host=self._rest._proxy_host,
proxy_port=self._rest._proxy_port,
proxy_user=self._rest._proxy_user,
message=ret[u'message'],
),
u'errno': int(ret.get(u'code', -1)),
u'sqlstate': SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED,
})
class Auth(object):
"""
Snowflake Authenticator
"""
def __init__(self, rest):
self._rest = rest
@staticmethod
def base_auth_data(user, account, application,
internal_application_name,
internal_application_version):
return {
u'data': {
u"CLIENT_APP_ID": internal_application_name,
u"CLIENT_APP_VERSION": internal_application_version,
u"SVN_REVISION": VERSION[3],
u"ACCOUNT_NAME": account,
u"LOGIN_NAME": user,
u"CLIENT_ENVIRONMENT": {
u"APPLICATION": application,
u"OS_VERSION": PLATFORM,
u"PYTHON_VERSION": PYTHON_VERSION,
u"PYTHON_RUNTIME": IMPLEMENTATION,
u"PYTHON_COMPILER": COMPILER,
}
},
}
def authenticate(self, auth_instance, account, user, password,
database=None, schema=None,
warehouse=None, role=None, passcode=None,
passcode_in_password=False,
mfa_callback=None, password_callback=None,
session_parameters=None, timeout=120):
logger.debug(u'authenticate')
if self._rest.token and self._rest.master_token:
logger.debug(
u'token is already set. no further authentication was done.')
return
request_id = TO_UNICODE(uuid.uuid4())
headers = {
u'Content-Type': CONTENT_TYPE_APPLICATION_JSON,
u"accept": ACCEPT_TYPE_APPLICATION_SNOWFLAKE,
u"User-Agent": PYTHON_CONNECTOR_USER_AGENT,
}
url = u"/session/v1/login-request"
body_template = Auth.base_auth_data(
user, account, self._rest._connection.application,
self._rest._connection._internal_application_name,
self._rest._connection._internal_application_version)
body = copy.deepcopy(body_template)
if auth_instance is not None:
# external authenticator may update the request body
logger.debug(u'assertion content: %s',
auth_instance.assertion_content)
auth_instance.update_body(body)
elif password:
body[u'data'][u"PASSWORD"] = password
logger.debug(
u'account=%s, user=%s, database=%s, schema=%s, '
u'warehouse=%s, role=%s, request_id=%s',
account,
user,
database,
schema,
warehouse,
role,
request_id,
)
url_parameters = {u'request_id': request_id}
if database is not None:
url_parameters[u'databaseName'] = database
if schema is not None:
url_parameters[u'schemaName'] = schema
if warehouse is not None:
url_parameters[u'warehouse'] = warehouse
if role is not None:
url_parameters[u'roleName'] = role
url = url + u'?' + urlencode(url_parameters)
# first auth request
if passcode_in_password:
body[u'data'][u'EXT_AUTHN_DUO_METHOD'] = u'passcode'
elif passcode:
body[u'data'][u'EXT_AUTHN_DUO_METHOD'] = u'passcode'
body[u'data'][u'PASSCODE'] = passcode
if session_parameters:
body[u'data'][u'SESSION_PARAMETERS'] = session_parameters
logger.debug(
"body['data']: %s",
{k: v for (k, v) in body[u'data'].items() if k != u'PASSWORD'})
try:
ret = self._rest._post_request(
url, headers, json.dumps(body),
timeout=self._rest._connection._login_timeout,
socket_timeout=self._rest._connection._login_timeout)
except ForbiddenError as err:
# HTTP 403
raise err.__class__(
msg=(u"Failed to connect to DB. "
u"Verify the account name is correct: {host}:{port}, "
u"proxies={proxy_host}:{proxy_port}, "
u"proxy_user={proxy_user}. {message}").format(
host=self._rest._host,
port=self._rest._port,
proxy_host=self._rest._proxy_host,
proxy_port=self._rest._proxy_port,
proxy_user=self._rest._proxy_user,
message=TO_UNICODE(err)
),
errno=ER_FAILED_TO_CONNECT_TO_DB,
sqlstate=SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED)
except (ServiceUnavailableError, BadGatewayError) as err:
# HTTP 502/504
raise err.__class__(
msg=(u"Failed to connect to DB. "
u"Service is unavailable: {host}:{port}, "
u"proxies={proxy_host}:{proxy_port}, "
u"proxy_user={proxy_user}. {message}").format(
host=self._rest._host,
port=self._rest._port,
proxy_host=self._rest._proxy_host,
proxy_port=self._rest._proxy_port,
proxy_user=self._rest._proxy_user,
message=TO_UNICODE(err)
),
errno=ER_FAILED_TO_CONNECT_TO_DB,
sqlstate=SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED)
# waiting for MFA authentication
if ret[u'data'].get(u'nextAction') == u'EXT_AUTHN_DUO_ALL':
body[u'inFlightCtx'] = ret[u'data'][u'inFlightCtx']
body[u'data'][u'EXT_AUTHN_DUO_METHOD'] = u'push'
self.ret = {u'message': "Timeout", u'data': {}}
def post_request_wrapper(self, url, headers, body):
# get the MFA response
self.ret = self._rest._post_request(
url, headers, body,
timeout=self._rest._connection._login_timeout)
# send new request to wait until MFA is approved
t = Thread(target=post_request_wrapper,
args=[self, url, headers, json.dumps(body)])
t.daemon = True
t.start()
if callable(mfa_callback):
c = mfa_callback()
while not self.ret or self.ret.get(u'message') == u'Timeout':
next(c)
else:
t.join(timeout=timeout)
ret = self.ret
if ret and ret[u'data'].get(u'nextAction') == u'EXT_AUTHN_SUCCESS':
body = copy.deepcopy(body_template)
body[u'inFlightCtx'] = ret[u'data'][u'inFlightCtx']
# final request to get tokens
ret = self._rest._post_request(
url, headers, json.dumps(body),
timeout=self._rest._connection._login_timeout,
socket_timeout=self._rest._connection._login_timeout)
elif not ret or not ret[u'data'].get(u'token'):
# not token is returned.
Error.errorhandler_wrapper(
self._rest._connection, None, DatabaseError,
{
u'msg': (u"Failed to connect to DB. MFA "
u"authentication failed: {"
u"host}:{port}, "
u"proxies={proxy_host}:{proxy_port}, "
u"proxy_user={proxy_user}, "
u"{message}").format(
host=self._rest._host,
port=self._rest._port,
proxy_host=self._rest._proxy_host,
proxy_port=self._rest._proxy_port,
proxy_user=self._rest._proxy_user,
message=ret[u'message'],
),
u'errno': ER_FAILED_TO_CONNECT_TO_DB,
u'sqlstate': SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED,
})
return # required for unit test
elif ret[u'data'].get(u'nextAction') == u'PWD_CHANGE':
if callable(password_callback):
body = copy.deepcopy(body_template)
body[u'inFlightCtx'] = ret[u'data'][u'inFlightCtx']
body[u'data'][u"LOGIN_NAME"] = user
body[u'data'][u"PASSWORD"] = password
body[u'data'][u'CHOSEN_NEW_PASSWORD'] = password_callback()
# New Password input
ret = self._rest._post_request(
url, headers, json.dumps(body),
timeout=self._rest._connection._login_timeout,
socket_timeout=self._rest._connection._login_timeout)
logger.debug(u'completed authentication')
if not ret[u'success']:
Error.errorhandler_wrapper(
self._rest._connection, None, DatabaseError,
{
u'msg': (u"Failed to connect to DB: {host}:{port}, "
u"proxies={proxy_host}:{proxy_port}, "
u"proxy_user={proxy_user}, "
u"{message}").format(
host=self._rest._host,
port=self._rest._port,
proxy_host=self._rest._proxy_host,
proxy_port=self._rest._proxy_port,
proxy_user=self._rest._proxy_user,
message=ret[u'message'],
),
u'errno': ER_FAILED_TO_CONNECT_TO_DB,
u'sqlstate': SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED,
})
else:
self._rest.update_tokens(
ret[u'data'][u'token'], ret[u'data'][u'masterToken'])
logger.debug(u'token = %s', self._rest.token)
logger.debug(u'master_token = %s', self._rest.master_token)
if u'sessionId' in ret[u'data']:
self._rest._connection._session_id = ret[u'data'][u'sessionId']
if u'sessionInfo' in ret[u'data']:
session_info = ret[u'data'][u'sessionInfo']
self._validate_default_database(session_info)
self._validate_default_schema(session_info)
self._validate_default_role(session_info)
self._validate_default_warehouse(session_info)
if u'parameters' in ret[u'data']:
with self._rest._connection._lock_converter:
self._rest._connection.converter.set_parameters(
ret[u'data'][u'parameters'])
def _validate_default_database(self, session_info):
default_value = self._rest._connection.database
session_info_value = session_info.get(u'databaseName')
self._rest._connection._database = session_info_value
self._validate_default_parameter(
'database', default_value, session_info_value)
def _validate_default_schema(self, session_info):
default_value = self._rest._connection.schema
session_info_value = session_info.get(u'schemaName')
self._rest._connection._schema = session_info_value
self._validate_default_parameter(
'schema', default_value, session_info_value)
def _validate_default_role(self, session_info):
default_value = self._rest._connection.role
session_info_value = session_info.get(u'roleName')
self._rest._connection._role = session_info_value
self._validate_default_parameter(
'role', default_value, session_info_value)
def _validate_default_warehouse(self, session_info):
default_value = self._rest._connection.warehouse
session_info_value = session_info.get(u'warehouseName')
self._rest._connection._warehouse = session_info_value
self._validate_default_parameter(
'warehouse', default_value, session_info_value)
def _validate_default_parameter(
self, name, default_value, session_info_value):
if self._rest._connection.validate_default_parameters and \
default_value is not None and \
session_info_value is None:
# validate default parameter
Error.errorhandler_wrapper(
self._rest._connection, None, DatabaseError,
{
u'msg': u'Invalid {0} name: {1}'.format(
name, default_value),
u'errno': ER_INVALID_VALUE,
u'sqlstate': SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED,
})