Skip to content

Commit

Permalink
Fix file overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Dec 9, 2024
1 parent 5a46335 commit 5eb2569
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public void SetUp()
_credentialManager = SFCredentialManagerFileImpl.Instance;
}

[TearDown]
public void CleanAll()
{
File.Delete(SFCredentialManagerFileImpl.Instance._jsonCacheFilePath);
}

[Test]
public void TestThatThrowsErrorWhenCacheFailToCreateCacheFile()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using System.Linq;
using System.Security;
using System.Threading;
using Newtonsoft.Json.Linq;
using KeyTokenDict = System.Collections.Generic.Dictionary<string, string>;

namespace Snowflake.Data.Core.CredentialManager.Infrastructure
Expand All @@ -36,7 +35,7 @@ internal class SFCredentialManagerFileImpl : ISnowflakeCredentialManager

private readonly string _jsonCacheDirectory;

private readonly string _jsonCacheFilePath;
internal readonly string _jsonCacheFilePath;

private readonly string _jsonCacheLockPath;

Expand Down Expand Up @@ -79,18 +78,21 @@ internal void WriteToJsonFile(string content)
{
_directoryOperations.CreateDirectory(_jsonCacheDirectory);
}
s_logger.Info($"Creating the json file for credential cache in {_jsonCacheFilePath}");
if (_fileOperations.Exists(_jsonCacheFilePath))
{
s_logger.Info($"The existing json file for credential cache in {_jsonCacheFilePath} will be overwritten");
}
var createFileResult = _unixOperations.CreateFileWithPermissions(_jsonCacheFilePath,
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR);
if (createFileResult == -1)
else
{
var errorMessage = "Failed to create the JSON token cache file";
s_logger.Error(errorMessage);
throw new Exception(errorMessage);
s_logger.Info($"Creating the json file for credential cache in {_jsonCacheFilePath}");
var createFileResult = _unixOperations.CreateFileWithPermissions(_jsonCacheFilePath,
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR);
if (createFileResult == -1)
{
var errorMessage = "Failed to create the JSON token cache file";
s_logger.Error(errorMessage);
throw new Exception(errorMessage);
}
}
_fileOperations.Write(_jsonCacheFilePath, content, ValidateFilePermissions);
}
Expand All @@ -100,9 +102,8 @@ internal KeyTokenDict ReadJsonFile()
var contentFile = _fileOperations.ReadAllText(_jsonCacheFilePath, ValidateFilePermissions);
try
{
JObject.Parse(contentFile);
var fileContent = JsonConvert.DeserializeObject<CredentialsFileContent>(contentFile);
return fileContent == null ? new KeyTokenDict() : fileContent.Tokens;
return (fileContent == null || fileContent.Tokens == null) ? new KeyTokenDict() : fileContent.Tokens;
}
catch (Exception)
{
Expand Down Expand Up @@ -159,7 +160,8 @@ public void RemoveCredentials(string key)
{
var keyTokenPairs = ReadJsonFile();
keyTokenPairs.Remove(key);
WriteToJsonFile(JsonConvert.SerializeObject(keyTokenPairs));
var credentials = new CredentialsFileContent { Tokens = keyTokenPairs };
WriteToJsonFile(JsonConvert.SerializeObject(credentials));
}
}
finally
Expand All @@ -184,8 +186,8 @@ public void SaveCredentials(string key, string token)
{
KeyTokenDict keyTokenPairs = _fileOperations.Exists(_jsonCacheFilePath) ? ReadJsonFile() : new KeyTokenDict();
keyTokenPairs[key] = token;

string jsonString = JsonConvert.SerializeObject(keyTokenPairs);
var credentials = new CredentialsFileContent { Tokens = keyTokenPairs };
string jsonString = JsonConvert.SerializeObject(credentials);
WriteToJsonFile(jsonString);
}
finally
Expand Down
2 changes: 1 addition & 1 deletion Snowflake.Data/Core/Tools/UnixOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void WriteAllText(string path, string content, Action<UnixStream> validat
{
var fileInfo = new UnixFileInfo(path: path);

using (var handle = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FilePermissions.S_IWUSR | FilePermissions.S_IRUSR))
using (var handle = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FilePermissions.S_IWUSR | FilePermissions.S_IRUSR))
{
validator?.Invoke(handle);
using (var streamWriter = new StreamWriter(handle, Encoding.UTF8))
Expand Down

0 comments on commit 5eb2569

Please sign in to comment.