Skip to content

Commit

Permalink
Move credential manager files to core folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Jun 6, 2024
1 parent 465da80 commit 94bce01
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
9 changes: 5 additions & 4 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
using Snowflake.Data.Tests.Mock;
using System.Runtime.InteropServices;
using System.Net.Http;
using Snowflake.Data.Core.CredentialManager;

[TestFixture]
class SFConnectionIT : SFBaseTest
Expand Down Expand Up @@ -1053,16 +1054,16 @@ public void TestSSOConnectionWithInvalidCachedToken()
= ConnectionStringWithoutAuth
+ ";authenticator=externalbrowser;[email protected];allow_sso_token_caching=true;";

var key = SnowflakeCredentialManagerFactory.BuildCredentialKey(testConfig.host, testConfig.user, TokenType.IdToken);
var credentialManager = SnowflakeCredentialManagerInMemoryImpl.Instance;
var key = SFCredentialManagerFactory.BuildCredentialKey(testConfig.host, testConfig.user, TokenType.IdToken);
var credentialManager = SFCredentialManagerInMemoryImpl.Instance;
credentialManager.SaveCredentials(key, "wrongToken");

SnowflakeCredentialManagerFactory.SetCredentialManager(credentialManager);
SFCredentialManagerFactory.SetCredentialManager(credentialManager);

conn.Open();
Assert.AreEqual(ConnectionState.Open, conn.State);

SnowflakeCredentialManagerFactory.UseDefaultCredentialManager();
SFCredentialManagerFactory.UseDefaultCredentialManager();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Snowflake.Data.Client;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Snowflake.Data.Core.CredentialManager;

namespace Snowflake.Data.Core.Authenticator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/

using Snowflake.Data.Core;

namespace Snowflake.Data.Client
namespace Snowflake.Data.Core.CredentialManager
{
internal enum TokenType
{
[StringAttr(value = "ID_TOKEN")]
IdToken
}

public interface ISnowflakeCredentialManager
public interface ISFCredentialManager
{
string GetCredentials(string key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/

using Snowflake.Data.Core;
using Snowflake.Data.Log;
using System.Runtime.InteropServices;

namespace Snowflake.Data.Client
namespace Snowflake.Data.Core.CredentialManager
{
internal class SnowflakeCredentialManagerFactory
internal class SFCredentialManagerFactory
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeCredentialManagerFactory>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFCredentialManagerFactory>();

private static ISnowflakeCredentialManager s_customCredentialManager = null;
private static ISFCredentialManager s_customCredentialManager = null;

internal static string BuildCredentialKey(string host, string user, TokenType tokenType)
{
Expand All @@ -25,18 +24,18 @@ public static void UseDefaultCredentialManager()
s_customCredentialManager = null;
}

public static void SetCredentialManager(ISnowflakeCredentialManager customCredentialManager)
public static void SetCredentialManager(ISFCredentialManager customCredentialManager)
{
s_logger.Info($"Setting the custom credential manager: {customCredentialManager.GetType().Name}");
s_customCredentialManager = customCredentialManager;
}

internal static ISnowflakeCredentialManager GetCredentialManager()
internal static ISFCredentialManager GetCredentialManager()
{
if (s_customCredentialManager == null)
{
var defaultCredentialManager = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? (ISnowflakeCredentialManager)
SnowflakeCredentialManagerWindowsNativeImpl.Instance : SnowflakeCredentialManagerInMemoryImpl.Instance;
var defaultCredentialManager = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? (ISFCredentialManager)
SFCredentialManagerWindowsNativeImpl.Instance : SFCredentialManagerInMemoryImpl.Instance;
s_logger.Info($"Using the default credential manager: {defaultCredentialManager.GetType().Name}");
return defaultCredentialManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
using System.Security.Principal;
using KeyToken = System.Collections.Generic.Dictionary<string, string>;

namespace Snowflake.Data.Client
namespace Snowflake.Data.Core.CredentialManager
{
public class SnowflakeCredentialManagerFileImpl : ISnowflakeCredentialManager
public class SFCredentialManagerFileImpl : ISFCredentialManager
{
internal const string CredentialCacheDirectoryEnvironmentName = "SF_TEMPORARY_CREDENTIAL_CACHE_DIR";

internal const string CredentialCacheFileName = "temporary_credential.json";

private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeCredentialManagerFileImpl>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFCredentialManagerFileImpl>();

private readonly string _jsonCacheDirectory;

Expand All @@ -36,9 +36,9 @@ public class SnowflakeCredentialManagerFileImpl : ISnowflakeCredentialManager

private readonly EnvironmentOperations _environmentOperations;

public static readonly SnowflakeCredentialManagerFileImpl Instance = new SnowflakeCredentialManagerFileImpl(FileOperations.Instance, DirectoryOperations.Instance, UnixOperations.Instance, EnvironmentOperations.Instance);
public static readonly SFCredentialManagerFileImpl Instance = new SFCredentialManagerFileImpl(FileOperations.Instance, DirectoryOperations.Instance, UnixOperations.Instance, EnvironmentOperations.Instance);

internal SnowflakeCredentialManagerFileImpl(FileOperations fileOperations, DirectoryOperations directoryOperations, UnixOperations unixOperations, EnvironmentOperations environmentOperations)
internal SFCredentialManagerFileImpl(FileOperations fileOperations, DirectoryOperations directoryOperations, UnixOperations unixOperations, EnvironmentOperations environmentOperations)
{
_fileOperations = fileOperations;
_directoryOperations = directoryOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using Snowflake.Data.Log;
using System.Collections.Generic;

namespace Snowflake.Data.Client
namespace Snowflake.Data.Core.CredentialManager
{
public class SnowflakeCredentialManagerInMemoryImpl : ISnowflakeCredentialManager
public class SFCredentialManagerInMemoryImpl : ISFCredentialManager
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeCredentialManagerInMemoryImpl>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFCredentialManagerInMemoryImpl>();

private Dictionary<string, string> s_credentials = new Dictionary<string, string>();

public static readonly SnowflakeCredentialManagerInMemoryImpl Instance = new SnowflakeCredentialManagerInMemoryImpl();
public static readonly SFCredentialManagerInMemoryImpl Instance = new SFCredentialManagerInMemoryImpl();

public string GetCredentials(string key)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
using System.Runtime.InteropServices;
using System.Text;

namespace Snowflake.Data.Client
namespace Snowflake.Data.Core.CredentialManager
{
public class SnowflakeCredentialManagerWindowsNativeImpl : ISnowflakeCredentialManager
public class SFCredentialManagerWindowsNativeImpl : ISFCredentialManager
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeCredentialManagerWindowsNativeImpl>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SFCredentialManagerWindowsNativeImpl>();

public static readonly SnowflakeCredentialManagerWindowsNativeImpl Instance = new SnowflakeCredentialManagerWindowsNativeImpl();
public static readonly SFCredentialManagerWindowsNativeImpl Instance = new SFCredentialManagerWindowsNativeImpl();

public string GetCredentials(string key)
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public void SaveCredentials(string key, string token)
credential.Type = 1; // Generic
credential.Persist = 2; // Local Machine
credential.CredentialBlobSize = (uint)(byteArray == null ? 0 : byteArray.Length);
credential.TargetName =key;
credential.TargetName = key;
credential.CredentialBlob = token;
credential.UserName = Environment.UserName;

Expand Down Expand Up @@ -115,7 +115,7 @@ protected override bool ReleaseHandle()
static extern bool CredRead(string target, uint type, int reservedFlag, out IntPtr credentialPtr);

[DllImport("Advapi32.dll", EntryPoint = "CredWriteW", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool CredWrite([In] ref Credential userCredential, [In] UInt32 flags);
static extern bool CredWrite([In] ref Credential userCredential, [In] uint flags);

[DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
static extern bool CredFree([In] IntPtr cred);
Expand Down
7 changes: 4 additions & 3 deletions Snowflake.Data/Core/Session/SFSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Net.Http;
using System.Text.RegularExpressions;
using Snowflake.Data.Configuration;
using Snowflake.Data.Core.CredentialManager;

namespace Snowflake.Data.Core
{
Expand Down Expand Up @@ -85,7 +86,7 @@ public class SFSession

internal String _queryTag;

private readonly ISnowflakeCredentialManager _credManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
private readonly ISFCredentialManager _credManager = SFCredentialManagerFactory.GetCredentialManager();

internal bool _allowSSOTokenCaching;

Expand All @@ -110,7 +111,7 @@ internal void ProcessLoginResponse(LoginResponse authnResponse)
if (_allowSSOTokenCaching && !string.IsNullOrEmpty(authnResponse.data.idToken))
{
_idToken = authnResponse.data.idToken;
var key = SnowflakeCredentialManagerFactory.BuildCredentialKey(properties[SFSessionProperty.HOST], properties[SFSessionProperty.USER], TokenType.IdToken);
var key = SFCredentialManagerFactory.BuildCredentialKey(properties[SFSessionProperty.HOST], properties[SFSessionProperty.USER], TokenType.IdToken);
_credManager.SaveCredentials(key, _idToken);
}
logger.Debug($"Session opened: {sessionId}");
Expand Down Expand Up @@ -199,7 +200,7 @@ internal SFSession(

if (_allowSSOTokenCaching)
{
var key = SnowflakeCredentialManagerFactory.BuildCredentialKey(properties[SFSessionProperty.HOST], properties[SFSessionProperty.USER], TokenType.IdToken);
var key = SFCredentialManagerFactory.BuildCredentialKey(properties[SFSessionProperty.HOST], properties[SFSessionProperty.USER], TokenType.IdToken);
_idToken = _credManager.GetCredentials(key);
}
}
Expand Down

0 comments on commit 94bce01

Please sign in to comment.