2222ACCOUNT = os .getenv ("SNOWFLAKE_TEST_WIF_ACCOUNT" )
2323HOST = os .getenv ("SNOWFLAKE_TEST_WIF_HOST" )
2424PROVIDER = os .getenv ("SNOWFLAKE_TEST_WIF_PROVIDER" )
25+ EXPECTED_USERNAME = os .getenv ("SNOWFLAKE_TEST_WIF_USERNAME" )
26+ IMPERSONATION_PATH = os .getenv ("SNOWFLAKE_TEST_WIF_IMPERSONATION_PATH" )
27+ EXPECTED_USERNAME_IMPERSONATION = os .getenv ("SNOWFLAKE_TEST_WIF_USERNAME_IMPERSONATION" )
2528
2629
2730@pytest .mark .wif
@@ -34,8 +37,8 @@ async def test_wif_defined_provider_async():
3437 "workload_identity_provider" : PROVIDER ,
3538 }
3639 assert await connect_and_execute_simple_query_async (
37- connection_params
38- ), "Failed to connect with using WIF - automatic provider detection "
40+ connection_params , EXPECTED_USERNAME
41+ ), f "Failed to connect with using WIF using provider { PROVIDER } "
3942
4043
4144@pytest .mark .wif
@@ -53,17 +56,47 @@ async def test_should_authenticate_using_oidc_async():
5356 }
5457
5558 assert await connect_and_execute_simple_query_async (
56- connection_params
59+ connection_params , expected_user = None
5760 ), "Failed to connect using WIF with OIDC provider"
5861
5962
60- async def connect_and_execute_simple_query_async (connection_params ) -> bool :
63+ @pytest .mark .wif
64+ @pytest .mark .aio
65+ @pytest .mark .skip ("Impersonation is still being developed" )
66+ async def test_should_authenticate_with_impersonation_async ():
67+ if not isinstance (IMPERSONATION_PATH , str ) or not IMPERSONATION_PATH :
68+ pytest .skip ("Skipping test - IMPERSONATION_PATH is not set" )
69+
70+ logger .debug (f"Using impersonation path: { IMPERSONATION_PATH } " )
71+ impersonation_path_list = IMPERSONATION_PATH .split ("," )
72+
73+ connection_params = {
74+ "host" : HOST ,
75+ "account" : ACCOUNT ,
76+ "authenticator" : "WORKLOAD_IDENTITY" ,
77+ "workload_identity_provider" : PROVIDER ,
78+ "workload_identity_impersonation_path" : impersonation_path_list ,
79+ }
80+
81+ assert await connect_and_execute_simple_query_async (
82+ connection_params , EXPECTED_USERNAME_IMPERSONATION
83+ ), f"Failed to connect using WIF with provider { PROVIDER } "
84+
85+
86+ async def connect_and_execute_simple_query_async (
87+ connection_params , expected_user = None
88+ ) -> bool :
6189 try :
6290 logger .info ("Trying to connect to Snowflake" )
6391 async with snowflake .connector .aio .connect (** connection_params ) as con :
64- result = await con .cursor ().execute ("select 1;" )
65- logger .debug (await result .fetchall ())
66- logger .info ("Successfully connected to Snowflake" )
92+ result = await con .cursor ().execute ("select current_user();" )
93+ (user ,) = await result .fetchone ()
94+ logger .debug (user )
95+ if expected_user :
96+ assert (
97+ expected_user == user
98+ ), f"Expected user '{ expected_user } ', got user '{ user } '"
99+ logger .info (f"Successfully connected to Snowflake as { user } " )
67100 return True
68101 except Exception as e :
69102 logger .error (e )
0 commit comments