Skip to content

Commit 4091a07

Browse files
authored
Merge pull request #158 from dbt-msft/token_timeout_fix
refresh token if within 5 minutes of expiring; else reuse
2 parents f2a4806 + d74f85c commit 4091a07

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### v0.20.1
4+
5+
#### fixes:
6+
7+
- workaround for Azure CLI token expires after one hour. Now we get new tokens for every transaction. [#156](https://github.com/dbt-msft/dbt-sqlserver/issue/156) [#158](https://github.com/dbt-msft/dbt-sqlserver/pull/158)
8+
39
### v0.20.0
410

511
#### features:

dbt/adapters/sqlserver/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '0.20.0'
1+
version = '0.20.1'

dbt/adapters/sqlserver/connections.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
185185
"""
186186
global _TOKEN
187187
attrs_before: Dict
188+
MAX_REMAINING_TIME = 300
188189

189190
azure_auth_function_type = Callable[[SQLServerCredentials], AccessToken]
190191
azure_auth_functions: Mapping[str, azure_auth_function_type] = {
@@ -193,21 +194,24 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
193194
}
194195

195196
authentication = credentials.authentication.lower()
196-
if authentication not in azure_auth_functions:
197-
attrs_before = {}
198-
else:
199-
azure_auth_function = azure_auth_functions[authentication]
200-
_TOKEN = _TOKEN or azure_auth_function(credentials)
201-
token_bytes = convert_access_token_to_mswindows_byte_string(_TOKEN)
197+
if authentication in azure_auth_functions:
198+
time_remaining = (_TOKEN.expires_on - time.time()) if _TOKEN else MAX_REMAINING_TIME
199+
200+
if _TOKEN is None or (time_remaining < MAX_REMAINING_TIME):
201+
azure_auth_function = azure_auth_functions[authentication]
202+
_TOKEN = azure_auth_function(credentials)
202203

204+
token_bytes = convert_access_token_to_mswindows_byte_string(_TOKEN)
203205
sql_copt_ss_access_token = 1256 # see source in docstring
204206
attrs_before = {sql_copt_ss_access_token: token_bytes}
207+
else:
208+
attrs_before = {}
209+
205210
return attrs_before
206211

207212

208213
class SQLServerConnectionManager(SQLConnectionManager):
209214
TYPE = "sqlserver"
210-
TOKEN = None
211215

212216
@contextmanager
213217
def exception_handler(self, sql):

0 commit comments

Comments
 (0)