Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refresh token if within 5 minutes of expiring; else reuse #158

Merged
merged 5 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### v0.20.1

#### fixes:

- 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)

### v0.20.0

#### features:
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/sqlserver/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.20.0'
version = '0.20.1'
18 changes: 11 additions & 7 deletions dbt/adapters/sqlserver/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
"""
global _TOKEN
attrs_before: Dict
MAX_REMAINING_TIME = 300

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

authentication = credentials.authentication.lower()
if authentication not in azure_auth_functions:
attrs_before = {}
else:
azure_auth_function = azure_auth_functions[authentication]
_TOKEN = _TOKEN or azure_auth_function(credentials)
token_bytes = convert_access_token_to_mswindows_byte_string(_TOKEN)
if authentication in azure_auth_functions:
time_remaining = (_TOKEN.expires_on - time.time()) if _TOKEN else MAX_REMAINING_TIME

if _TOKEN is None or (time_remaining < MAX_REMAINING_TIME):
azure_auth_function = azure_auth_functions[authentication]
_TOKEN = azure_auth_function(credentials)

token_bytes = convert_access_token_to_mswindows_byte_string(_TOKEN)
sql_copt_ss_access_token = 1256 # see source in docstring
attrs_before = {sql_copt_ss_access_token: token_bytes}
else:
attrs_before = {}

return attrs_before


class SQLServerConnectionManager(SQLConnectionManager):
TYPE = "sqlserver"
TOKEN = None

@contextmanager
def exception_handler(self, sql):
Expand Down