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 22, 2024
1 parent dcea67d commit 67c5ea8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 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
8 changes: 7 additions & 1 deletion Snowflake.Data/Core/Tools/FileOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Snowflake.Data.Core.Tools
{
using System.Runtime.InteropServices;
using Mono.Unix;

internal class FileOperations
{
Expand All @@ -20,7 +21,12 @@ public virtual bool Exists(string path)

public virtual string ReadAllText(string path)
{
var contentFile = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? File.ReadAllText(path) : _unixOperations.ReadAllText(path);
return ReadAllText(path, FileAccessPermissions.OtherReadWriteExecute);
}

Check warning on line 25 in Snowflake.Data/Core/Tools/FileOperations.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Tools/FileOperations.cs#L23-L25

Added lines #L23 - L25 were not covered by tests

public virtual string ReadAllText(string path, FileAccessPermissions? forbiddenPermissions)
{

Check warning on line 28 in Snowflake.Data/Core/Tools/FileOperations.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Tools/FileOperations.cs#L28

Added line #L28 was not covered by tests
var contentFile = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? File.ReadAllText(path) : _unixOperations.ReadAllText(path, forbiddenPermissions);
return contentFile;
}

Check warning on line 31 in Snowflake.Data/Core/Tools/FileOperations.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Tools/FileOperations.cs#L30-L31

Added lines #L30 - L31 were not covered by tests
}
Expand Down
4 changes: 2 additions & 2 deletions Snowflake.Data/Core/Tools/UnixOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public virtual bool CheckFileHasAnyOfPermissions(string path, FileAccessPermissi
/// <returns>The content of the file as a string.</returns>
/// <exception cref="SecurityException">Thrown if the file is not owned by the effective user or group, or if it has forbidden permissions.</exception>

public string ReadAllText(string path, FileAccessPermissions forbiddenPermissions = FileAccessPermissions.OtherReadWriteExecute)
public string ReadAllText(string path, FileAccessPermissions? forbiddenPermissions = FileAccessPermissions.OtherReadWriteExecute)
{
var fileInfo = new UnixFileInfo(path: path);

Check warning on line 45 in Snowflake.Data/Core/Tools/UnixOperations.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Tools/UnixOperations.cs#L44-L45

Added lines #L44 - L45 were not covered by tests

Expand All @@ -50,7 +50,7 @@ public string ReadAllText(string path, FileAccessPermissions forbiddenPermission
throw new SecurityException("Attempting to read a file not owned by the effective user of the current process");

Check warning on line 50 in Snowflake.Data/Core/Tools/UnixOperations.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Tools/UnixOperations.cs#L50

Added line #L50 was not covered by tests
if (handle.OwnerGroup.GroupId != Syscall.getegid())
throw new SecurityException("Attempting to read a file not owned by the effective group of the current process");

Check warning on line 52 in Snowflake.Data/Core/Tools/UnixOperations.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Tools/UnixOperations.cs#L52

Added line #L52 was not covered by tests
if ((handle.FileAccessPermissions & forbiddenPermissions) != 0)
if (forbiddenPermissions.HasValue && (handle.FileAccessPermissions & forbiddenPermissions.Value) != 0)
throw new SecurityException("Attempting to read a file with too broad permissions assigned");
using (var streamReader = new StreamReader(handle, Encoding.Default))
{
Expand Down

0 comments on commit 67c5ea8

Please sign in to comment.