Skip to content

Commit

Permalink
Added mechanism to throw exception if file in token_file_path does no…
Browse files Browse the repository at this point in the history
…t exists.
  • Loading branch information
sfc-gh-jmartinezramirez committed Oct 30, 2024
1 parent b1ab2db commit d3d2c79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

using Mono.Unix;
using Snowflake.Data.Client;

namespace Snowflake.Data.Tests.UnitTests
{
Expand Down Expand Up @@ -309,7 +310,7 @@ public void TestConnectionWithOauthAuthenticatorTokenFromFile()
}

[Test]
public void TestConnectionWithOauthAuthenticatorFromDefaultIfTokenFilePathNotExists()
public void TestConnectionWithOauthAuthenticatorThrowsExceptionIfTokenFilePathNotExists()
{
// Arrange
var tokenFilePath = "/Users/testuser/token";
Expand Down Expand Up @@ -337,11 +338,9 @@ public void TestConnectionWithOauthAuthenticatorFromDefaultIfTokenFilePathNotExi

var reader = new TomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object);

// Act
var connectionString = reader.GetConnectionStringFromToml();

// Assert
Assert.AreEqual($"account=testaccountname;authenticator=oauth;token={defaultToken};", connectionString);
// Act and assert
var exception = Assert.Throws<SnowflakeDbException>(() => reader.GetConnectionStringFromToml());
Assert.IsTrue(exception.Message.StartsWith("Error: Invalid parameter value /Users/testuser/token for token_file_path"));
}

[Test]
Expand Down
16 changes: 15 additions & 1 deletion Snowflake.Data/Core/TomlConnectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,21 @@ private void AppendTokenFromFileIfNotGivenExplicitly(TomlTable connectionToml, b

private string LoadTokenFromFile(string tokenFilePathValue)
{
var tokenFile = !string.IsNullOrEmpty(tokenFilePathValue) && _fileOperations.Exists(tokenFilePathValue) ? tokenFilePathValue : DefaultTokenPath;
string tokenFile;
if(string.IsNullOrEmpty(tokenFilePathValue))
{
tokenFile = DefaultTokenPath;
}
else
{
if (!_fileOperations.Exists(tokenFilePathValue))
{
s_logger.Debug($"Specified file {tokenFilePathValue} does not exists.");
throw new SnowflakeDbException(SFError.INVALID_CONNECTION_PARAMETER_VALUE, tokenFilePathValue, "token_file_path");
}

tokenFile = tokenFilePathValue;
}
s_logger.Debug($"Read token from file path: {tokenFile}");
return _fileOperations.Exists(tokenFile) ? _fileOperations.ReadAllText(tokenFile, ValidateFilePermissions) : null;
}
Expand Down

0 comments on commit d3d2c79

Please sign in to comment.