-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathconnections.py
391 lines (308 loc) · 12 KB
/
connections.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from contextlib import contextmanager
import pyodbc
import os
import time
import struct
from itertools import chain, repeat
from typing import Callable, Mapping
import dbt.exceptions
from dbt.adapters.base import Credentials
from dbt.adapters.sql import SQLConnectionManager
from dbt.adapters.sqlserver import __version__
from dbt.contracts.connection import AdapterResponse
from azure.core.credentials import AccessToken
from azure.identity import AzureCliCredential, DefaultAzureCredential
from dbt.logger import GLOBAL_LOGGER as logger
from dataclasses import dataclass
from typing import Optional
AZURE_CREDENTIAL_SCOPE = "https://database.windows.net//.default"
@dataclass
class SQLServerCredentials(Credentials):
driver: str
host: str
database: str
schema: str
port: Optional[int] = 1433
UID: Optional[str] = None
PWD: Optional[str] = None
windows_login: Optional[bool] = False
tenant_id: Optional[str] = None
client_id: Optional[str] = None
client_secret: Optional[str] = None
# "sql", "ActiveDirectoryPassword" or "ActiveDirectoryInteractive", or
# "ServicePrincipal"
authentication: Optional[str] = "sql"
encrypt: Optional[bool] = False
trust_cert: Optional[bool] = False
_ALIASES = {
"user": "UID",
"username": "UID",
"pass": "PWD",
"password": "PWD",
"server": "host",
"trusted_connection": "windows_login",
"auth": "authentication",
"app_id": "client_id",
"app_secret": "client_secret",
"TrustServerCertificate": "trust_cert",
}
@property
def type(self):
return "sqlserver"
def _connection_keys(self):
# return an iterator of keys to pretty-print in 'dbt debug'
# raise NotImplementedError
if self.windows_login is True:
self.authentication = "Windows Login"
return (
"server",
"database",
"schema",
"port",
"UID",
"client_id",
"authentication",
"encrypt",
"trust_cert"
)
def convert_bytes_to_mswindows_byte_string(value: bytes) -> bytes:
"""
Convert bytes to a Microsoft windows byte string.
Parameters
----------
value : bytes
The bytes.
Returns
-------
out : bytes
The Microsoft byte string.
"""
encoded_bytes = bytes(chain.from_iterable(zip(value, repeat(0))))
return struct.pack("<i", len(encoded_bytes)) + encoded_bytes
def convert_access_token_to_mswindows_byte_string(token: AccessToken) -> bytes:
"""
Convert an access token to a Microsoft windows byte string.
Parameters
----------
token : AccessToken
The token.
Returns
-------
out : bytes
The Microsoft byte string.
"""
value = bytes(token.token, "UTF-8")
return convert_bytes_to_mswindows_byte_string(value)
def get_cli_access_token(credentials: SQLServerCredentials) -> AccessToken:
"""
Get an Azure access token using the CLI credentials
First login with:
```bash
az login
```
Parameters
----------
credentials: SQLServerConnectionManager
The credentials.
Returns
-------
out : AccessToken
Access token.
"""
_ = credentials
token = AzureCliCredential().get_token(AZURE_CREDENTIAL_SCOPE)
return token
def get_sp_access_token(credentials: SQLServerCredentials) -> AccessToken:
"""
Get an Azure access token using the SP credentials.
Parameters
----------
credentials : SQLServerCredentials
Credentials.
Returns
-------
out : AccessToken
The access token.
"""
# bc DefaultAzureCredential will look in env variables
os.environ["AZURE_TENANT_ID"] = credentials.tenant_id
os.environ["AZURE_CLIENT_ID"] = credentials.client_id
os.environ["AZURE_CLIENT_SECRET"] = credentials.client_secret
token = DefaultAzureCredential().get_token(AZURE_CREDENTIAL_SCOPE)
return token
AZURE_AUTH_FUNCTION_TYPE = Callable[[SQLServerCredentials], AccessToken]
AZURE_AUTH_FUNCTIONS: Mapping[str, AZURE_AUTH_FUNCTION_TYPE] = {
"ServicePrincipal": get_sp_access_token,
"CLI": get_cli_access_token,
}
class SQLServerConnectionManager(SQLConnectionManager):
TYPE = "sqlserver"
TOKEN = None
@contextmanager
def exception_handler(self, sql):
try:
yield
except pyodbc.DatabaseError as e:
logger.debug("Database error: {}".format(str(e)))
try:
# attempt to release the connection
self.release()
except pyodbc.Error:
logger.debug("Failed to release connection!")
pass
raise dbt.exceptions.DatabaseException(str(e).strip()) from e
except Exception as e:
logger.debug(f"Error running SQL: {sql}")
logger.debug("Rolling back transaction.")
self.release()
if isinstance(e, dbt.exceptions.RuntimeException):
# during a sql query, an internal to dbt exception was raised.
# this sounds a lot like a signal handler and probably has
# useful information, so raise it without modification.
raise
raise dbt.exceptions.RuntimeException(e)
@classmethod
def open(cls, connection):
if connection.state == "open":
logger.debug("Connection is already open, skipping open.")
return connection
credentials = connection.credentials
try:
con_str = []
con_str.append(f"DRIVER={{{credentials.driver}}}")
if "\\" in credentials.host:
# if there is a backslash \ in the host name the host is a sql-server named instance
# in this case then port number has to be omitted
con_str.append(f"SERVER={credentials.host}")
else:
con_str.append(f"SERVER={credentials.host},{credentials.port}")
con_str.append(f"Database={credentials.database}")
type_auth = getattr(credentials, "authentication", "sql")
if "ActiveDirectory" in type_auth:
con_str.append(f"Authentication={credentials.authentication}")
if type_auth == "ActiveDirectoryPassword":
con_str.append(f"UID={{{credentials.UID}}}")
con_str.append(f"PWD={{{credentials.PWD}}}")
elif type_auth == "ActiveDirectoryInteractive":
con_str.append(f"UID={{{credentials.UID}}}")
elif type_auth == "ActiveDirectoryIntegrated":
# why is this necessary???
con_str.remove("UID={None}")
elif type_auth == "ActiveDirectoryMsi":
raise ValueError("ActiveDirectoryMsi is not supported yet")
elif type_auth == "ServicePrincipal":
app_id = getattr(credentials, "AppId", None)
app_secret = getattr(credentials, "AppSecret", None)
elif getattr(credentials, "windows_login", False):
con_str.append(f"trusted_connection=yes")
elif type_auth == "sql":
#con_str.append("Authentication=SqlPassword")
con_str.append(f"UID={{{credentials.UID}}}")
con_str.append(f"PWD={{{credentials.PWD}}}")
# still confused whether to use "Yes", "yes", "True", or "true"
# to learn more visit
# https://docs.microsoft.com/en-us/sql/relational-databases/native-client/features/using-encryption-without-validation?view=sql-server-ver15
if getattr(credentials, "encrypt", False) is True:
con_str.append(f"Encrypt=Yes")
if getattr(credentials, "trust_cert", False) is True:
con_str.append(f"TrustServerCertificate=Yes")
plugin_version = __version__.version
application_name = f"dbt-{credentials.type}/{plugin_version}"
con_str.append(f"Application Name={application_name}")
con_str_concat = ';'.join(con_str)
index = []
for i, elem in enumerate(con_str):
if 'pwd=' in elem.lower():
index.append(i)
if len(index) !=0 :
con_str[index[0]]="PWD=***"
con_str_display = ';'.join(con_str)
logger.debug(f'Using connection string: {con_str_display}')
if type_auth in AZURE_AUTH_FUNCTIONS.keys():
# create token if it does not exist
if cls.TOKEN is None:
azure_auth_function = AZURE_AUTH_FUNCTIONS[type_auth]
token = azure_auth_function(credentials)
cls.TOKEN = convert_access_token_to_mswindows_byte_string(
token
)
# Source:
# https://docs.microsoft.com/en-us/sql/connect/odbc/using-azure-active-directory?view=sql-server-ver15#authenticating-with-an-access-token
SQL_COPT_SS_ACCESS_TOKEN = 1256
attrs_before = {SQL_COPT_SS_ACCESS_TOKEN: cls.TOKEN}
else:
attrs_before = {}
handle = pyodbc.connect(
con_str_concat,
attrs_before=attrs_before,
autocommit=True,
)
connection.state = "open"
connection.handle = handle
logger.debug(f"Connected to db: {credentials.database}")
except pyodbc.Error as e:
logger.debug(f"Could not connect to db: {e}")
connection.handle = None
connection.state = "fail"
raise dbt.exceptions.FailedToConnectException(str(e))
return connection
def cancel(self, connection):
logger.debug("Cancel query")
pass
def add_begin_query(self):
# return self.add_query('BEGIN TRANSACTION', auto_begin=False)
pass
def add_commit_query(self):
# return self.add_query('COMMIT TRANSACTION', auto_begin=False)
pass
def add_query(self, sql, auto_begin=True, bindings=None, abridge_sql_log=False):
connection = self.get_thread_connection()
if auto_begin and connection.transaction_open is False:
self.begin()
logger.debug('Using {} connection "{}".'.format(self.TYPE, connection.name))
with self.exception_handler(sql):
if abridge_sql_log:
logger.debug("On {}: {}....".format(connection.name, sql[0:512]))
else:
logger.debug("On {}: {}".format(connection.name, sql))
pre = time.time()
cursor = connection.handle.cursor()
# pyodbc does not handle a None type binding!
if bindings is None:
cursor.execute(sql)
else:
cursor.execute(sql, bindings)
logger.debug(
"SQL status: {} in {:0.2f} seconds".format(
self.get_response(cursor), (time.time() - pre)
)
)
return connection, cursor
@classmethod
def get_credentials(cls, credentials):
return credentials
@classmethod
def get_response(cls, cursor) -> AdapterResponse:
#message = str(cursor.statusmessage)
message = 'OK'
rows = cursor.rowcount
#status_message_parts = message.split() if message is not None else []
#status_messsage_strings = [
# part
# for part in status_message_parts
# if not part.isdigit()
#]
#code = ' '.join(status_messsage_strings)
return AdapterResponse(
_message=message,
#code=code,
rows_affected=rows
)
def execute(self, sql, auto_begin=True, fetch=False):
_, cursor = self.add_query(sql, auto_begin)
status = self.get_response(cursor)
if fetch:
table = self.get_result_from_cursor(cursor)
else:
table = dbt.clients.agate_helper.empty_table()
return status, table