|
9 | 9 | using NUnit.Framework; |
10 | 10 | using Snowflake.Data.Client; |
11 | 11 | using Snowflake.Data.Core; |
| 12 | +using Snowflake.Data.Core.CredentialManager; |
| 13 | +using Snowflake.Data.Core.CredentialManager.Infrastructure; |
12 | 14 | using Snowflake.Data.Core.Session; |
13 | 15 | using Snowflake.Data.Core.Tools; |
14 | 16 | using Snowflake.Data.Log; |
|
17 | 19 |
|
18 | 20 | namespace Snowflake.Data.Tests.IntegrationTests |
19 | 21 | { |
20 | | - |
21 | 22 | [TestFixture] |
22 | 23 | class SFConnectionIT : SFBaseTest |
23 | 24 | { |
@@ -1042,6 +1043,75 @@ public void TestSSOConnectionTimeoutAfter10s() |
1042 | 1043 | Assert.LessOrEqual(stopwatch.ElapsedMilliseconds, (waitSeconds + 5) * 1000); |
1043 | 1044 | } |
1044 | 1045 |
|
| 1046 | + [Test] |
| 1047 | + [Ignore("This test requires manual interaction and therefore cannot be run in CI")] |
| 1048 | + public void TestSSOConnectionWithTokenCaching() |
| 1049 | + { |
| 1050 | + /* |
| 1051 | + * This test checks that the connector successfully stores an SSO token and uses it for authentication if it exists |
| 1052 | + * 1. Login normally using external browser with CLIENT_STORE_TEMPORARY_CREDENTIAL enabled |
| 1053 | + * 2. Login again, this time without a browser, as the connector should be using the SSO token retrieved from step 1 |
| 1054 | + */ |
| 1055 | + |
| 1056 | + // Set the CLIENT_STORE_TEMPORARY_CREDENTIAL property to true to enable token caching |
| 1057 | + // The specified user should be configured for SSO |
| 1058 | + var externalBrowserConnectionString |
| 1059 | + = ConnectionStringWithoutAuth |
| 1060 | + + $";authenticator=externalbrowser;user={testConfig.user};CLIENT_STORE_TEMPORARY_CREDENTIAL=true;poolingEnabled=false"; |
| 1061 | + |
| 1062 | + using (IDbConnection conn = new SnowflakeDbConnection()) |
| 1063 | + { |
| 1064 | + conn.ConnectionString = externalBrowserConnectionString; |
| 1065 | + |
| 1066 | + // Authenticate to retrieve and store the token if doesn't exist or invalid |
| 1067 | + conn.Open(); |
| 1068 | + Assert.AreEqual(ConnectionState.Open, conn.State); |
| 1069 | + } |
| 1070 | + |
| 1071 | + using (IDbConnection conn = new SnowflakeDbConnection()) |
| 1072 | + { |
| 1073 | + conn.ConnectionString = externalBrowserConnectionString; |
| 1074 | + |
| 1075 | + // Authenticate using the SSO token (the connector will automatically use the token and a browser should not pop-up in this step) |
| 1076 | + conn.Open(); |
| 1077 | + Assert.AreEqual(ConnectionState.Open, conn.State); |
| 1078 | + } |
| 1079 | + } |
| 1080 | + |
| 1081 | + [Test] |
| 1082 | + [Ignore("This test requires manual interaction and therefore cannot be run in CI")] |
| 1083 | + public void TestSSOConnectionWithInvalidCachedToken() |
| 1084 | + { |
| 1085 | + /* |
| 1086 | + * This test checks that the connector will attempt to re-authenticate using external browser if the token retrieved from the cache is invalid |
| 1087 | + * 1. Create a credential manager and save credentials for the user with a wrong token |
| 1088 | + * 2. Open a connection which initially should try to use the token and then switch to external browser when the token fails |
| 1089 | + */ |
| 1090 | + |
| 1091 | + using (IDbConnection conn = new SnowflakeDbConnection()) |
| 1092 | + { |
| 1093 | + // Set the CLIENT_STORE_TEMPORARY_CREDENTIAL property to true to enable token caching |
| 1094 | + conn.ConnectionString |
| 1095 | + = ConnectionStringWithoutAuth |
| 1096 | + + $";authenticator=externalbrowser;user={testConfig.user};CLIENT_STORE_TEMPORARY_CREDENTIAL=true;"; |
| 1097 | + |
| 1098 | + // Create a credential manager and save a wrong token for the test user |
| 1099 | + var key = SnowflakeCredentialManagerFactory.GetSecureCredentialKey(testConfig.host, testConfig.user, TokenType.IdToken); |
| 1100 | + var credentialManager = SFCredentialManagerInMemoryImpl.Instance; |
| 1101 | + credentialManager.SaveCredentials(key, "wrongToken"); |
| 1102 | + |
| 1103 | + // Use the credential manager with the wrong token |
| 1104 | + SnowflakeCredentialManagerFactory.SetCredentialManager(credentialManager); |
| 1105 | + |
| 1106 | + // Open a connection which should switch to external browser after trying to connect using the wrong token |
| 1107 | + conn.Open(); |
| 1108 | + Assert.AreEqual(ConnectionState.Open, conn.State); |
| 1109 | + |
| 1110 | + // Switch back to the default credential manager |
| 1111 | + SnowflakeCredentialManagerFactory.UseDefaultCredentialManager(); |
| 1112 | + } |
| 1113 | + } |
| 1114 | + |
1045 | 1115 | [Test] |
1046 | 1116 | [Ignore("This test requires manual interaction and therefore cannot be run in CI")] |
1047 | 1117 | public void TestSSOConnectionWithWrongUser() |
@@ -2353,6 +2423,44 @@ public void TestOpenAsyncThrowExceptionWhenOperationIsCancelled() |
2353 | 2423 | } |
2354 | 2424 | } |
2355 | 2425 |
|
| 2426 | + [Test] |
| 2427 | + [Ignore("This test requires manual interaction and therefore cannot be run in CI")] |
| 2428 | + public void TestSSOConnectionWithTokenCachingAsync() |
| 2429 | + { |
| 2430 | + /* |
| 2431 | + * This test checks that the connector successfully stores an SSO token and uses it for authentication if it exists |
| 2432 | + * 1. Login normally using external browser with CLIENT_STORE_TEMPORARY_CREDENTIAL enabled |
| 2433 | + * 2. Login again, this time without a browser, as the connector should be using the SSO token retrieved from step 1 |
| 2434 | + */ |
| 2435 | + |
| 2436 | + // Set the CLIENT_STORE_TEMPORARY_CREDENTIAL property to true to enable token caching |
| 2437 | + // The specified user should be configured for SSO |
| 2438 | + var externalBrowserConnectionString |
| 2439 | + = ConnectionStringWithoutAuth |
| 2440 | + + $";authenticator=externalbrowser;user={testConfig.user};CLIENT_STORE_TEMPORARY_CREDENTIAL=true;poolingEnabled=false"; |
| 2441 | + |
| 2442 | + using (SnowflakeDbConnection conn = new SnowflakeDbConnection()) |
| 2443 | + { |
| 2444 | + conn.ConnectionString = externalBrowserConnectionString; |
| 2445 | + |
| 2446 | + // Authenticate to retrieve and store the token if doesn't exist or invalid |
| 2447 | + Task connectTask = conn.OpenAsync(CancellationToken.None); |
| 2448 | + connectTask.Wait(); |
| 2449 | + Assert.AreEqual(ConnectionState.Open, conn.State); |
| 2450 | + } |
| 2451 | + |
| 2452 | + using (SnowflakeDbConnection conn = new SnowflakeDbConnection()) |
| 2453 | + { |
| 2454 | + conn.ConnectionString = externalBrowserConnectionString; |
| 2455 | + |
| 2456 | + // Authenticate using the SSO token (the connector will automatically use the token and a browser should not pop-up in this step) |
| 2457 | + Task connectTask = conn.OpenAsync(CancellationToken.None); |
| 2458 | + connectTask.Wait(); |
| 2459 | + Assert.AreEqual(ConnectionState.Open, conn.State); |
| 2460 | + } |
| 2461 | + |
| 2462 | + } |
| 2463 | + |
2356 | 2464 | [Test] |
2357 | 2465 | public void TestCloseSessionWhenGarbageCollectorFinalizesConnection() |
2358 | 2466 | { |
|
0 commit comments