Skip to content

Commit

Permalink
Apply suggestion to SnowflakeCredentialManagerFactory and other addit…
Browse files Browse the repository at this point in the history
…ional comments
  • Loading branch information
sfc-gh-jmartinezramirez committed Oct 25, 2024
1 parent d84508b commit d755d02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2273,18 +2273,18 @@ 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();
using (SnowflakeDbConnection conn = new SnowflakeDbConnection())
{
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
Expand All @@ -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;";

Expand Down
34 changes: 16 additions & 18 deletions Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public class SnowflakeCredentialManagerFactory
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeCredentialManagerFactory>();

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)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
}

0 comments on commit d755d02

Please sign in to comment.