Skip to content

Commit f323d22

Browse files
SNOW-901157 new expired feature (#1810)
1 parent eceed98 commit f323d22

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

DESCRIPTION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
1414
- Added support for Vector types
1515
- Changed urllib3 version pin to only affect Python versions < 3.10.
1616
- Support for `private_key_file` and `private_key_file_pwd` connection parameters
17+
- Added a new flag `expired` to `SnowflakeConnection` class, that keeps track of whether the connection's master token has expired.
1718

1819
- v3.5.0(November 13,2023)
1920

src/snowflake/connector/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ def __init__(
412412
self.__set_error_attributes()
413413
self.connect(**kwargs)
414414
self._telemetry = TelemetryClient(self._rest)
415+
self.expired = False
415416

416417
# get the imported modules from sys.modules
417418
self._log_telemetry_imported_packages()

src/snowflake/connector/network.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,9 @@ def _post_request(
743743
)
744744
)
745745

746-
if ret.get("code") == SESSION_EXPIRED_GS_CODE:
746+
if ret.get("code") == MASTER_TOKEN_EXPIRED_GS_CODE:
747+
self._connection.expired = True
748+
elif ret.get("code") == SESSION_EXPIRED_GS_CODE:
747749
try:
748750
ret = self._renew_session()
749751
except ReauthenticationRequest as ex:

test/unit/test_connection.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,41 @@ def test_private_key_file_reading(tmp_path: Path):
426426
)
427427
assert m.call_count == 1
428428
assert m.call_args_list[0].kwargs["private_key"] == pkb
429+
430+
431+
def test_expired_detection():
432+
with mock.patch(
433+
"snowflake.connector.network.SnowflakeRestful._post_request",
434+
return_value={
435+
"data": {
436+
"masterToken": "some master token",
437+
"token": "some token",
438+
"validityInSeconds": 3600,
439+
"masterValidityInSeconds": 14400,
440+
"displayUserName": "TEST_USER",
441+
"serverVersion": "7.42.0",
442+
},
443+
"code": None,
444+
"message": None,
445+
"success": True,
446+
},
447+
):
448+
conn = fake_connector()
449+
assert not conn.expired
450+
with conn.cursor() as cur:
451+
with mock.patch(
452+
"snowflake.connector.network.SnowflakeRestful.fetch",
453+
return_value={
454+
"data": {
455+
"errorCode": "390114",
456+
"reAuthnMethods": ["USERNAME_PASSWORD"],
457+
},
458+
"code": "390114",
459+
"message": "Authentication token has expired. The user must authenticate again.",
460+
"success": False,
461+
"headers": None,
462+
},
463+
):
464+
with pytest.raises(ProgrammingError):
465+
cur.execute("select 1;")
466+
assert conn.expired

0 commit comments

Comments
 (0)