Skip to content

Commit

Permalink
[Bug] Silent SSO token expiration (#1072)
Browse files Browse the repository at this point in the history
* changelog

* add an integration test for token expiration

* catch missing access_token key error

* update test to connect initially, then swap out token for a invalid one

* modify test and how we setup profile

* attempt at unit test

* remove functional test attempt

* add name to changelog

---------

Co-authored-by: McKnight-42 <[email protected]>
Co-authored-by: Colin Rogers <[email protected]>
Co-authored-by: Matthew McKnight <[email protected]>
  • Loading branch information
4 people authored Jun 24, 2024
1 parent d0a259f commit 2576a08
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240605-125611.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Surface SSO token expiration in logs
time: 2024-06-05T12:56:11.802237-04:00
custom:
Author: mikealfare, McKnight-42
Issue: "851"
6 changes: 5 additions & 1 deletion dbt/adapters/snowflake/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ def _get_access_token(self) -> str:
f"""Did not receive valid json with access_token.
Showing json response: {result_json}"""
)

elif "access_token" not in result_json:
raise FailedToConnectError(
"This error occurs when authentication has expired. "
"Please reauth with your auth provider."
)
return result_json["access_token"]

def _get_private_key(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/oauth/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
integration the same, just the refresh token changed)
"""

import pytest
import os
from dbt.tests.util import run_dbt, check_relations_equal
from dbt.tests.util import check_relations_equal, run_dbt
import pytest


_MODELS__MODEL_1_SQL = """
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/test_connections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import pytest
from importlib import reload
from unittest.mock import Mock
from unittest.mock import Mock, patch
import multiprocessing
from dbt.adapters.exceptions.connection import FailedToConnectError
import dbt.adapters.snowflake.connections as connections
import dbt.adapters.events.logging

Expand Down Expand Up @@ -36,3 +39,31 @@ def test_connnections_credentials_replaces_underscores_with_hyphens():
}
creds = connections.SnowflakeCredentials(**credentials)
assert creds.account == "account-id-with-underscores"


def test_snowflake_oauth_expired_token_raises_error():
credentials = {
"account": "test_account",
"user": "test_user",
"authenticator": "oauth",
"token": "expired_or_incorrect_token",
"database": "database",
"schema": "schema",
}

mp_context = multiprocessing.get_context("spawn")
mock_credentials = connections.SnowflakeCredentials(**credentials)

with patch.object(
connections.SnowflakeConnectionManager,
"open",
side_effect=FailedToConnectError(
"This error occurs when authentication has expired. "
"Please reauth with your auth provider."
),
):

adapter = connections.SnowflakeConnectionManager(mock_credentials, mp_context)

with pytest.raises(FailedToConnectError):
adapter.open()

0 comments on commit 2576a08

Please sign in to comment.