Skip to content

Commit

Permalink
Initial support for loading token from token_file_path for oauth auth…
Browse files Browse the repository at this point in the history
…entication
  • Loading branch information
sfc-gh-jmartinezramirez committed Jul 19, 2024
1 parent b2918d7 commit f0007af
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Snowflake.Data.Core
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Tomlyn;
using Tomlyn.Model;
Expand All @@ -16,12 +17,15 @@ public class SnowflakeTomlConnectionBuilder
{
private const string DefaultConnectionName = "default";
private const string DefaultSnowflakeFolder = ".snowflake";
private const string DefaultTokenPath = "/snowflake/session/token";

private Dictionary<string, string> TomlToNetPropertiesMapper = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
{
{ "DATABASE", "DB" }
};



private readonly FileOperations _fileOperations;
private readonly EnvironmentOperations _environmentOperations;

Expand Down Expand Up @@ -50,15 +54,42 @@ public string GetConnectionStringFromToml(string connectionName = null)
private string GetConnectionStringFromTomlTable(TomlTable connectionToml)
{
var connectionStringBuilder = new StringBuilder();
var tokenFilePathValue = string.Empty;
var isOauth = connectionToml.TryGetValue("authenticator", out var authenticator) && authenticator.ToString().Equals("oauth");
foreach (var property in connectionToml.Keys)
{
if (isOauth && property.Equals("token_file_path", StringComparison.InvariantCultureIgnoreCase))
{
tokenFilePathValue = (string)connectionToml[property];
continue;

Check warning on line 64 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L62-L64

Added lines #L62 - L64 were not covered by tests
}
var mappedProperty = TomlToNetPropertiesMapper.TryGetValue(property, out var mapped) ? mapped : property;
connectionStringBuilder.Append($"{mappedProperty}={(string)connectionToml[property]};");
}

if (!isOauth || connectionToml.ContainsKey("token"))
return connectionStringBuilder.ToString();

var token = LoadTokenFromFile(tokenFilePathValue);

Check warning on line 73 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L73

Added line #L73 was not covered by tests
if (!string.IsNullOrEmpty(token))
{
connectionStringBuilder.Append($"token={token};");
}

Check warning on line 77 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L75-L77

Added lines #L75 - L77 were not covered by tests
else
{

Check warning on line 79 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L79

Added line #L79 was not covered by tests
// log warning TODO
}

Check warning on line 81 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L81

Added line #L81 was not covered by tests


return connectionStringBuilder.ToString();

Check warning on line 84 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L84

Added line #L84 was not covered by tests
}

private string LoadTokenFromFile(string tokenFilePathValue)
{

Check warning on line 88 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L88

Added line #L88 was not covered by tests
var tokenFile = _fileOperations.Exists(tokenFilePathValue) ? tokenFilePathValue : DefaultTokenPath;
return _fileOperations.Exists(tokenFile) ? _fileOperations.ReadAllText(tokenFile) : null;
}

Check warning on line 91 in Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/SnowflakeTomlConnectionBuilder.cs#L91

Added line #L91 was not covered by tests

private TomlTable GetTomlTableFromConfig(string tomlPath, string connectionName)
{
TomlTable result = null;
Expand Down

0 comments on commit f0007af

Please sign in to comment.