|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. |
| 4 | +# |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +# This test requires the SSO and Snowflake admin connection parameters. |
| 9 | +# |
| 10 | +# CONNECTION_PARAMETERS_SSO = { |
| 11 | +# 'account': 'testaccount', |
| 12 | + |
| 13 | +# 'protocol': 'http', |
| 14 | +# 'host': 'testaccount.reg.snowflakecomputing.com', |
| 15 | +# 'port': '8082', |
| 16 | +# 'authenticator': 'externalbrowser', |
| 17 | +# 'timezone': 'UTC', |
| 18 | +# } |
| 19 | +# |
| 20 | +# CONNECTION_PARAMETERS_ADMIN = { ... Snowflake admin ... } |
| 21 | +import os |
| 22 | +import sys |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +import snowflake.connector.aio |
| 27 | +from snowflake.connector.auth._auth import delete_temporary_credential |
| 28 | + |
| 29 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 30 | + |
| 31 | +try: |
| 32 | + from parameters import CONNECTION_PARAMETERS_SSO |
| 33 | +except ImportError: |
| 34 | + CONNECTION_PARAMETERS_SSO = {} |
| 35 | + |
| 36 | +try: |
| 37 | + from parameters import CONNECTION_PARAMETERS_ADMIN |
| 38 | +except ImportError: |
| 39 | + CONNECTION_PARAMETERS_ADMIN = {} |
| 40 | + |
| 41 | +ID_TOKEN = "ID_TOKEN" |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +async def token_validity_test_values(request): |
| 46 | + async with snowflake.connector.aio.SnowflakeConnection( |
| 47 | + **CONNECTION_PARAMETERS_ADMIN |
| 48 | + ) as cnx: |
| 49 | + await cnx.cursor().execute( |
| 50 | + """ |
| 51 | +ALTER SYSTEM SET |
| 52 | + MASTER_TOKEN_VALIDITY=60, |
| 53 | + SESSION_TOKEN_VALIDITY=5, |
| 54 | + ID_TOKEN_VALIDITY=60 |
| 55 | +""" |
| 56 | + ) |
| 57 | + # ALLOW_UNPROTECTED_ID_TOKEN is going to be deprecated in the future |
| 58 | + # cnx.cursor().execute("alter account testaccount set ALLOW_UNPROTECTED_ID_TOKEN=true;") |
| 59 | + await cnx.cursor().execute("alter account testaccount set ALLOW_ID_TOKEN=true;") |
| 60 | + await cnx.cursor().execute( |
| 61 | + "alter account testaccount set ID_TOKEN_FEATURE_ENABLED=true;" |
| 62 | + ) |
| 63 | + |
| 64 | + async def fin(): |
| 65 | + async with snowflake.connector.connect(**CONNECTION_PARAMETERS_ADMIN) as cnx: |
| 66 | + await cnx.cursor().execute( |
| 67 | + """ |
| 68 | +ALTER SYSTEM SET |
| 69 | + MASTER_TOKEN_VALIDITY=default, |
| 70 | + SESSION_TOKEN_VALIDITY=default, |
| 71 | + ID_TOKEN_VALIDITY=default |
| 72 | +""" |
| 73 | + ) |
| 74 | + |
| 75 | + request.addfinalizer(fin) |
| 76 | + return None |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.skipif( |
| 80 | + not ( |
| 81 | + CONNECTION_PARAMETERS_SSO |
| 82 | + and CONNECTION_PARAMETERS_ADMIN |
| 83 | + and delete_temporary_credential |
| 84 | + ), |
| 85 | + reason="SSO and ADMIN connection parameters must be provided.", |
| 86 | +) |
| 87 | +async def test_connect_externalbrowser(token_validity_test_values): |
| 88 | + """SSO Id Token Cache tests. This test should only be ran if keyring optional dependency is installed. |
| 89 | +
|
| 90 | + In order to run this test, remove the above pytest.mark.skip annotation and run it. It will popup a windows once |
| 91 | + but the rest connections should not create popups. |
| 92 | + """ |
| 93 | + delete_temporary_credential( |
| 94 | + host=CONNECTION_PARAMETERS_SSO["host"], |
| 95 | + user=CONNECTION_PARAMETERS_SSO["user"], |
| 96 | + cred_type=ID_TOKEN, |
| 97 | + ) # delete existing temporary credential |
| 98 | + CONNECTION_PARAMETERS_SSO["client_store_temporary_credential"] = True |
| 99 | + |
| 100 | + # change database and schema to non-default one |
| 101 | + print( |
| 102 | + "[INFO] 1st connection gets id token and stores in the local cache (keychain/credential manager/cache file). " |
| 103 | + "This popup a browser to SSO login" |
| 104 | + ) |
| 105 | + cnx = snowflake.connector.aio.SnowflakeConnection(**CONNECTION_PARAMETERS_SSO) |
| 106 | + await cnx.connect() |
| 107 | + assert cnx.database == "TESTDB" |
| 108 | + assert cnx.schema == "PUBLIC" |
| 109 | + assert cnx.role == "SYSADMIN" |
| 110 | + assert cnx.warehouse == "REGRESS" |
| 111 | + ret = await ( |
| 112 | + await cnx.cursor().execute( |
| 113 | + "select current_database(), current_schema(), " |
| 114 | + "current_role(), current_warehouse()" |
| 115 | + ) |
| 116 | + ).fetchall() |
| 117 | + assert ret[0][0] == "TESTDB" |
| 118 | + assert ret[0][1] == "PUBLIC" |
| 119 | + assert ret[0][2] == "SYSADMIN" |
| 120 | + assert ret[0][3] == "REGRESS" |
| 121 | + await cnx.close() |
| 122 | + |
| 123 | + print( |
| 124 | + "[INFO] 2nd connection reads the local cache and uses the id token. " |
| 125 | + "This should not popups a browser." |
| 126 | + ) |
| 127 | + CONNECTION_PARAMETERS_SSO["database"] = "testdb" |
| 128 | + CONNECTION_PARAMETERS_SSO["schema"] = "testschema" |
| 129 | + cnx = snowflake.connector.aio.SnowflakeConnection(**CONNECTION_PARAMETERS_SSO) |
| 130 | + await cnx.connect() |
| 131 | + print( |
| 132 | + "[INFO] Running a 10 seconds query. If the session expires in 10 " |
| 133 | + "seconds, the query should renew the token in the middle, " |
| 134 | + "and the current objects should be refreshed." |
| 135 | + ) |
| 136 | + await cnx.cursor().execute("select seq8() from table(generator(timelimit=>10))") |
| 137 | + assert cnx.database == "TESTDB" |
| 138 | + assert cnx.schema == "TESTSCHEMA" |
| 139 | + assert cnx.role == "SYSADMIN" |
| 140 | + assert cnx.warehouse == "REGRESS" |
| 141 | + |
| 142 | + print("[INFO] Running a 1 second query. ") |
| 143 | + await cnx.cursor().execute("select seq8() from table(generator(timelimit=>1))") |
| 144 | + assert cnx.database == "TESTDB" |
| 145 | + assert cnx.schema == "TESTSCHEMA" |
| 146 | + assert cnx.role == "SYSADMIN" |
| 147 | + assert cnx.warehouse == "REGRESS" |
| 148 | + |
| 149 | + print( |
| 150 | + "[INFO] Running a 90 seconds query. This pops up a browser in the " |
| 151 | + "middle of the query." |
| 152 | + ) |
| 153 | + await cnx.cursor().execute("select seq8() from table(generator(timelimit=>90))") |
| 154 | + assert cnx.database == "TESTDB" |
| 155 | + assert cnx.schema == "TESTSCHEMA" |
| 156 | + assert cnx.role == "SYSADMIN" |
| 157 | + assert cnx.warehouse == "REGRESS" |
| 158 | + |
| 159 | + await cnx.close() |
| 160 | + |
| 161 | + # change database and schema again to ensure they are overridden |
| 162 | + CONNECTION_PARAMETERS_SSO["database"] = "testdb" |
| 163 | + CONNECTION_PARAMETERS_SSO["schema"] = "testschema" |
| 164 | + cnx = snowflake.connector.aio.SnowflakeConnection(**CONNECTION_PARAMETERS_SSO) |
| 165 | + await cnx.connect() |
| 166 | + assert cnx.database == "TESTDB" |
| 167 | + assert cnx.schema == "TESTSCHEMA" |
| 168 | + assert cnx.role == "SYSADMIN" |
| 169 | + assert cnx.warehouse == "REGRESS" |
| 170 | + await cnx.close() |
| 171 | + |
| 172 | + async with snowflake.connector.aio.SnowflakeConnection( |
| 173 | + **CONNECTION_PARAMETERS_ADMIN |
| 174 | + ) as cnx_admin: |
| 175 | + # cnx_admin.cursor().execute("alter account testaccount set ALLOW_UNPROTECTED_ID_TOKEN=false;") |
| 176 | + await cnx_admin.cursor().execute( |
| 177 | + "alter account testaccount set ALLOW_ID_TOKEN=false;" |
| 178 | + ) |
| 179 | + await cnx_admin.cursor().execute( |
| 180 | + "alter account testaccount set ID_TOKEN_FEATURE_ENABLED=false;" |
| 181 | + ) |
| 182 | + print( |
| 183 | + "[INFO] Login again with ALLOW_UNPROTECTED_ID_TOKEN unset. Please make sure this pops up the browser" |
| 184 | + ) |
| 185 | + cnx = snowflake.connector.aio.SnowflakeConnection(**CONNECTION_PARAMETERS_SSO) |
| 186 | + await cnx.connect() |
| 187 | + await cnx.close() |
0 commit comments