Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-715504: MFA token cache support #988

Open
wants to merge 53 commits into
base: master
Choose a base branch
from

Conversation

sfc-gh-jmartinezramirez
Copy link
Collaborator

@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez commented Jul 4, 2024

Description

Added support for MFA token cache.

Checklist

  • Code compiles correctly
  • Code is formatted according to Coding Conventions
  • Created tests which fail without the change (if possible)
  • All tests passing (dotnet test)
  • Extended the README / documentation, if necessary
  • Provide JIRA issue id (if possible) or GitHub issue id in PR name

@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez force-pushed the SNOW-715504-MFA-Token-Cache branch 5 times, most recently from 6eba99f to 69d80c7 Compare July 5, 2024 21:54
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez changed the title Snow 715504 mfa token cache SNOW-715504: MFA token cache support Jul 8, 2024
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

Attention: Patch coverage is 85.40268% with 87 lines in your changes missing coverage. Please review.

Project coverage is 86.11%. Comparing base (a33bb45) to head (f5e1862).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...ager/Infrastructure/SFCredentialManagerFileImpl.cs 85.39% 21 Missing and 5 partials ⚠️
...astructure/SFCredentialManagerWindowsNativeImpl.cs 81.17% 14 Missing and 2 partials ⚠️
Snowflake.Data/Core/Session/SessionPool.cs 82.00% 3 Missing and 6 partials ⚠️
...e.Data/Client/SnowflakeCredentialManagerFactory.cs 86.88% 4 Missing and 4 partials ⚠️
Snowflake.Data/Core/Session/SFSession.cs 78.94% 6 Missing and 2 partials ⚠️
Snowflake.Data/Core/Tools/UnixOperations.cs 72.22% 4 Missing and 1 partial ⚠️
...e.Data/Core/Authenticator/MFACacheAuthenticator.cs 78.94% 3 Missing and 1 partial ⚠️
Snowflake.Data/Core/Tools/FileOperations.cs 55.55% 3 Missing and 1 partial ⚠️
Snowflake.Data/Core/Tools/StringUtils.cs 75.00% 1 Missing and 1 partial ⚠️
...Core/Authenticator/ExternalBrowserAuthenticator.cs 0.00% 1 Missing ⚠️
... and 4 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #988      +/-   ##
==========================================
- Coverage   86.32%   86.11%   -0.21%     
==========================================
  Files         121      129       +8     
  Lines       11845    12404     +559     
  Branches     1212     1273      +61     
==========================================
+ Hits        10225    10682     +457     
- Misses       1331     1406      +75     
- Partials      289      316      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez force-pushed the SNOW-715504-MFA-Token-Cache branch from a8dff4b to 67089fc Compare July 22, 2024 21:36
Snowflake.Data/Core/Authenticator/IAuthenticator.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Authenticator/IAuthenticator.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Authenticator/MFACacheAuthenticator.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez force-pushed the SNOW-715504-MFA-Token-Cache branch 4 times, most recently from 6af9d1a to e36f8ed Compare September 4, 2024 13:31
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez force-pushed the SNOW-715504-MFA-Token-Cache branch 2 times, most recently from e795b5f to 9316bbc Compare September 18, 2024 23:08
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez marked this pull request as ready for review October 4, 2024 02:42
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez requested a review from a team as a code owner October 4, 2024 02:42
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SFSession.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Tools/StringUtils.cs Outdated Show resolved Hide resolved
Snowflake.Data.Tests/UnitTests/SFSessionTest.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Tools/UnixOperations.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/SFError.cs Outdated Show resolved Hide resolved
Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs Outdated Show resolved Hide resolved
Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SFSessionProperty.cs Outdated Show resolved Hide resolved
@sfc-gh-jmartinezramirez sfc-gh-jmartinezramirez force-pushed the SNOW-715504-MFA-Token-Cache branch 3 times, most recently from 5d32b9c to be6f19d Compare October 19, 2024 03:51
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SFCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Core/Session/SessionPool.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data/Client/SnowflakeCredentialManagerFactory.cs Outdated Show resolved Hide resolved
Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs Outdated Show resolved Hide resolved
{
if (s_credentialManager == null)
{
s_credentialManager = s_defaultCredentialManager;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to check if s_credentialManager == null inside lock does lock mean possibly waiting for another thread setting this value? Also why are we checking it on Get instead of setting defaultCredentialManager as a default value of s_credentialManager?

Copy link
Collaborator

@sfc-gh-knozderko sfc-gh-knozderko Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to check if manager is null inside the lock is that even though it was null before the lock could stop the thread for a while so after waking up the situation can be different (another thread could initialise the manager).

Your second question is why not to initialise the s_credentialManager with the default value in the constructor? Maybe we could do so. The idea could be that we wanted to have lazy initialisation...
I changed it.

@@ -101,6 +101,24 @@ protected void Login()
/// <param name="data">The login request data to update.</param>
protected abstract void SetSpecializedAuthenticatorData(ref LoginRequestData data);

protected void SetSecondaryAuthenticationData(ref LoginRequestData data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we add a doc string to this method? Probably it's because I'm reading it in the morning but I understood secondary authentication data as some less important data instead of data related to the second authentication factor

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"less important data" - then it would be AuthtneticationSecondaryData not SecondaryAuthenticationData. I don't like commenting what a method is doing. I would prefer give it a better name if you could propose anything better.


internal KeyTokenDict ReadJsonFile()
{
var contentFile = _fileOperations.ReadAllText(_fileStorage.JsonCacheFilePath, ValidateFilePermissions);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make sure that the file exists first? I see that we're checking it before each call to this method but if we're returning new KeyTokenDict for the deserialization (corrupted file) then it'd make sense to return the same for non existing file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added handling the case if the file was deleted just after we checked it.

KeyTokenDict keyTokenPairs = _fileOperations.Exists(_fileStorage.JsonCacheFilePath) ? ReadJsonFile() : new KeyTokenDict();
keyTokenPairs[key] = token;
var credentials = new CredentialsFileContent { Tokens = keyTokenPairs };
string jsonString = JsonConvert.SerializeObject(credentials);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really really nit: JsonConvert.SerializeObject(credentials) could be passed directly to WriteToJsonFile to match line#149

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants