diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index aaeeeb053..e6ffcd7c9 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -2273,10 +2273,10 @@ public void TestUseMultiplePoolsConnectionPoolByDefault() } [Test] - [Ignore("This test requires manual interaction and therefore cannot be run in CI")] + // [Ignore("This test requires manual interaction and therefore cannot be run in CI")] public void TestMFATokenCachingWithPasscodeFromConnectionString() { - // Use a connection with MFA enabled and set passcode property for mfa authentication. e.g. ConnectionString + ";authenticator=username_password_mfa;passcode=123456" + // Use a connection with MFA enabled and set passcode property for mfa authentication. e.g. ConnectionString + ";authenticator=username_password_mfa;passcode=(set proper passcode)" // ACCOUNT PARAMETER ALLOW_CLIENT_MFA_CACHING should be set to true in the account. // On Mac/Linux OS default credential manager is in memory so please uncomment following line to use file based credential manager // SnowflakeCredentialManagerFactory.UseFileCredentialManager(); @@ -2284,7 +2284,7 @@ public void TestMFATokenCachingWithPasscodeFromConnectionString() { conn.ConnectionString = ConnectionString - + ";authenticator=username_password_mfa;application=DuoTest;minPoolSize=0;"; + + ";authenticator=username_password_mfa;application=DuoTest;minPoolSize=0;passcode=(set proper passcode)"; // Authenticate to retrieve and store the token if doesn't exist or invalid @@ -2305,7 +2305,7 @@ public void TestMfaWithPasswordConnectionUsingPasscodeWithSecureString() // arrange using (SnowflakeDbConnection conn = new SnowflakeDbConnection()) { - conn.Passcode = SecureStringHelper.Encode("123456"); + conn.Passcode = SecureStringHelper.Encode("$(set proper passcode)"); // manual action: stop here in breakpoint to provide proper passcode by: conn.Passcode = SecureStringHelper.Encode("..."); conn.ConnectionString = ConnectionString + "minPoolSize=2;application=DuoTest;"; diff --git a/Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs b/Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs index 71b35c82c..f006ff607 100644 --- a/Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs +++ b/Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs @@ -15,10 +15,9 @@ public class SnowflakeCredentialManagerFactory private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); private static readonly object s_credentialManagerLock = new object(); + private static readonly ISnowflakeCredentialManager s_defaultCredentialManager = GetDefaultCredentialManager(); private static ISnowflakeCredentialManager s_credentialManager; - private static bool s_isDefaultCredentialManager = true; - private static ISnowflakeCredentialManager s_defaultCredentialManager; internal static string BuildCredentialKey(string host, string user, TokenType tokenType, string authenticator = null) { @@ -55,9 +54,12 @@ public static void SetCredentialManager(ISnowflakeCredentialManager customCreden "Credential manager cannot be null. If you want to use the default credential manager, please call the UseDefaultCredentialManager method."); } - if (customCredentialManager == s_credentialManager) return; + if (customCredentialManager == s_credentialManager) + { + s_logger.Info($"Credential manager is already set to: {customCredentialManager.GetType().Name}"); + return; + } - s_isDefaultCredentialManager = customCredentialManager == GetDefaultCredentialManager(); s_logger.Info($"Setting the credential manager: {customCredentialManager.GetType().Name}"); s_credentialManager = customCredentialManager; } @@ -71,27 +73,23 @@ public static ISnowflakeCredentialManager GetCredentialManager() { if (s_credentialManager == null) { - s_isDefaultCredentialManager = true; - s_credentialManager = GetDefaultCredentialManager(); + s_credentialManager = s_defaultCredentialManager; } } } - var typeCredentialText = s_isDefaultCredentialManager ? "default" : "custom"; - s_logger.Info($"Using {typeCredentialText} credential manager: {s_credentialManager?.GetType().Name}"); - return s_credentialManager; + + var credentialManager = s_credentialManager; + var typeCredentialText = credentialManager == s_defaultCredentialManager ? "default" : "custom"; + s_logger.Info($"Using {typeCredentialText} credential manager: {credentialManager?.GetType().Name}"); + return credentialManager; } private static ISnowflakeCredentialManager GetDefaultCredentialManager() { - if (s_defaultCredentialManager == null) - { - s_defaultCredentialManager = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? (ISnowflakeCredentialManager) - SFCredentialManagerWindowsNativeImpl.Instance - : SFCredentialManagerInMemoryImpl.Instance; - } - - return s_defaultCredentialManager; + return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + ? (ISnowflakeCredentialManager) + SFCredentialManagerWindowsNativeImpl.Instance + : SFCredentialManagerInMemoryImpl.Instance; } } }